BAEL-5905 move article

This commit is contained in:
Loredana Crusoveanu
2023-05-19 20:40:38 +03:00
parent fcd357036f
commit 8cb142eb02
13 changed files with 32 additions and 8 deletions
@@ -0,0 +1,15 @@
package com.baeldung.spoon;
import spoon.processing.AbstractProcessor;
import spoon.reflect.code.CtComment;
import spoon.reflect.code.CtComment.CommentType;
import spoon.reflect.declaration.CtClass;
public class AddCopyrightProcessor extends AbstractProcessor<CtClass<?>> {
@Override
public void process(CtClass<?> clazz) {
CtComment comment = getFactory().createComment("Copyright(c) 2023 etc", CommentType.JAVADOC);
clazz.addComment(comment);
}
}
@@ -0,0 +1,31 @@
package com.baeldung.spoon;
import spoon.Launcher;
import spoon.SpoonAPI;
import spoon.reflect.CtModel;
import spoon.reflect.code.CtComment;
import spoon.reflect.code.CtComment.CommentType;
import spoon.reflect.declaration.CtClass;
public class AddCopyrightTransformer {
public void addCopyright(String source) {
SpoonAPI spoon = new Launcher();
spoon.addInputResource(source);
spoon.getEnvironment().setLevel("DEBUG");
CtModel model = spoon.buildModel();
model.filterChildren((el) -> el instanceof CtClass<?>)
.forEach((CtClass<?> cl) -> {
CtComment comment = cl.getFactory()
.createComment("Copyright(c) 2023 etc", CommentType.JAVADOC);
cl.addComment(comment);
});
spoon.setSourceOutputDirectory("./target");
spoon.prettyprint();
}
}
@@ -0,0 +1,111 @@
package com.baeldung.spoon;
import spoon.Launcher;
import spoon.SpoonAPI;
import spoon.reflect.CtModel;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.support.sniper.SniperJavaPrettyPrinter;
/**
* Loads a given class and creates a summary o
*/
public class ClassReporter {
public MethodSummary generateMethodSummaryReport(String source) {
SpoonAPI spoon = new Launcher();
spoon.addInputResource(source);
CtModel model = spoon.buildModel();
MethodSummary report = new MethodSummary();
model.filterChildren((el) -> el instanceof CtClass<?>)
.forEach((CtClass<?> clazz) -> processMethods(report,clazz));
return report;
}
private void processMethods(MethodSummary report, CtClass<?> ctClass) {
ctClass.filterChildren((c) -> c instanceof CtMethod<?> )
.forEach((CtMethod<?> m) -> {
if (m.isPublic()) {
report.addPublicMethod();
}
else if ( m.isPrivate()) {
report.addPrivateMethod();
}
else if ( m.isProtected()) {
report.addProtectedMethod();
}
else {
report.addPackagePrivateMethod();
}
});
}
public static class MethodSummary {
private int totalMethodCount;
private int publicMethodCount;
private int protectedMethodCount;
private int packagePrivateMethodCount;
private int privateMethodCount;
void addPublicMethod() {
totalMethodCount++;
publicMethodCount++;
}
void addPrivateMethod() {
totalMethodCount++;
privateMethodCount++;
}
void addProtectedMethod() {
totalMethodCount++;
protectedMethodCount++;
}
void addPackagePrivateMethod() {
totalMethodCount++;
packagePrivateMethodCount++;
}
/**
* @return the totalMethodCount
*/
public int getTotalMethodCount() {
return totalMethodCount;
}
/**
* @return the publicMethodCount
*/
public int getPublicMethodCount() {
return publicMethodCount;
}
/**
* @return the protectedMethodCount
*/
public int getProtectedMethodCount() {
return protectedMethodCount;
}
/**
* @return the privateMethodCount
*/
public int getPrivateMethodCount() {
return privateMethodCount;
}
/**
* @return the privateMethodCount
*/
public int getPackagePrivateMethodCount() {
return packagePrivateMethodCount;
}
}
}