Skip to content

Commit

Permalink
Add checks for private fields/methods during the annotation processor (
Browse files Browse the repository at this point in the history
…#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 <[email protected]>
  • Loading branch information
LeStegii and LeStegii authored Apr 18, 2024
1 parent 5536db7 commit 164b640
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ERROR_CODES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ExecutableElement> streamAllMethods(TypeElement componentClass, Class<? extends Annotation> 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<ExecutableElement> streamMethods(TypeElement componentClass, Class<? extends Annotation> annotation) {
Expand All @@ -362,8 +372,13 @@ private Stream<ExecutableElement> streamMethods(TypeElement componentClass, Clas
.map(element -> (ExecutableElement) element);
}

// This will throw an error if the fields are private
private Stream<VariableElement> streamAllFields(TypeElement componentClass, Class<? extends Annotation> 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<VariableElement> streamFields(TypeElement componentClass, Class<? extends Annotation> annotation) {
Expand Down
2 changes: 1 addition & 1 deletion ludo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
}

Expand Down

0 comments on commit 164b640

Please sign in to comment.