From 164b6405b353682453c1c8f49f5a477a37952a7b Mon Sep 17 00:00:00 2001 From: Paul Mertens <50475262+LeStegii@users.noreply.github.com> Date: Thu, 18 Apr 2024 16:23:11 +0200 Subject: [PATCH] Add checks for private fields/methods during the annotation processor (#85) * feat: Add check for private fields and methods * build(ludo): Fix incorrect manifest main class * docs: Update ERROR_CODES.md * fix(processor): Fix incorrect type in output * docs: Remove unnecessary note in error code list --------- Co-authored-by: LeStegii --- ERROR_CODES.md | 2 +- .../java/org/fulib/fx/FxClassGenerator.java | 19 +++++++++++++++++-- ludo/build.gradle | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/ERROR_CODES.md b/ERROR_CODES.md index 8eac1674..843040b9 100644 --- a/ERROR_CODES.md +++ b/ERROR_CODES.md @@ -181,7 +181,7 @@ show(new MyComponent()); // Wrong, should not be able to show a controller that ### 1012: `Cannot access private * '*' in class '*' annotated with '*'.` - Runtime: ✅ -- Annotation Processor: ❌ (Caught by the compiler) +- Annotation Processor: ✅ This error is thrown when the framework tries to access a private field or method. diff --git a/annotation-processor/src/main/java/org/fulib/fx/FxClassGenerator.java b/annotation-processor/src/main/java/org/fulib/fx/FxClassGenerator.java index 23d6aeea..d0654169 100644 --- a/annotation-processor/src/main/java/org/fulib/fx/FxClassGenerator.java +++ b/annotation-processor/src/main/java/org/fulib/fx/FxClassGenerator.java @@ -17,13 +17,18 @@ import javax.lang.model.type.DeclaredType; import javax.lang.model.type.ExecutableType; import javax.lang.model.type.TypeMirror; +import javax.tools.Diagnostic; import javax.tools.JavaFileObject; import java.io.IOException; import java.io.PrintWriter; import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.util.*; import java.util.stream.Stream; +import static org.fulib.fx.util.FrameworkUtil.error; + public class FxClassGenerator { private static final String CLASS_SUFFIX = "_Fx"; private final ProcessingEnvironment processingEnv; @@ -350,8 +355,13 @@ private String stringLiteral(String value) { return processingEnv.getElementUtils().getConstantExpression(value); } + // This will throw an error if the methods are private private Stream streamAllMethods(TypeElement componentClass, Class annotation) { - return streamSuperClasses(componentClass).flatMap(e -> streamMethods(e, annotation)); + return streamSuperClasses(componentClass).flatMap(e -> streamMethods(e, annotation)).peek(field -> { + if (field.getModifiers().contains(Modifier.PRIVATE)) { + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, error(1012).formatted(Method.class.getSimpleName(), field.getSimpleName(), componentClass.getQualifiedName(), annotation.getSimpleName(), field)); + } + }); } private Stream streamMethods(TypeElement componentClass, Class annotation) { @@ -362,8 +372,13 @@ private Stream streamMethods(TypeElement componentClass, Clas .map(element -> (ExecutableElement) element); } + // This will throw an error if the fields are private private Stream streamAllFields(TypeElement componentClass, Class annotation) { - return streamSuperClasses(componentClass).flatMap(e -> streamFields(e, annotation)); + return streamSuperClasses(componentClass).flatMap(e -> streamFields(e, annotation)).peek(field -> { + if (field.getModifiers().contains(Modifier.PRIVATE)) { + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, error(1012).formatted(Field.class.getSimpleName(), field.getSimpleName(), componentClass.getQualifiedName(), annotation.getSimpleName(), field)); + } + }); } private Stream streamFields(TypeElement componentClass, Class annotation) { diff --git a/ludo/build.gradle b/ludo/build.gradle index a725c0de..46f592bb 100644 --- a/ludo/build.gradle +++ b/ludo/build.gradle @@ -52,7 +52,7 @@ jar { attributes( "Manifest-Version": 1.0, "Class-Path": ".", - "Main-Class": "io.github.sekassel.person.Main" + "Main-Class": "de.uniks.ludo.LudoMain" ) }