[BAEL-5905] Spoon Library (#13853)
* [BAEL-4849] Article code * [BAEL-4968] Article code * [BAEL-4968] Article code * [BAEL-4968] Article code * [BAEL-4968] Remove extra comments * [BAEL-5258] Article Code * [BAEL-2765] PKCE Support for Secret Clients * [BAEL-5698] Article code * [BAEL-5698] Article code * [BAEL-5905] Initial code * [BAEL-5905] Article code --------- Co-authored-by: Philippe Sevestre <psevestre@gmail.com>
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user