Skip to content

Commit 3138507

Browse files
committed
#13 - @reflectable on interface throws NPE in annotation processor
1 parent db3a706 commit 3138507

12 files changed

Lines changed: 375 additions & 391 deletions

File tree

LICENSE_SHORT

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright [2020] FormKiQ Inc. Licensed under the Apache License, Version 2.0 (the "License"); you
3+
* may not use this file except in compliance with the License. You may obtain a copy of the License
4+
* at
5+
*
6+
* <p>http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
9+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
10+
* express or implied. See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/

build.gradle

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ compileJava {
2727
}
2828

2929
spotless {
30-
java {
31-
googleJavaFormat()
32-
}
30+
java {
31+
eclipse().configFile project.rootProject.file("spotless.eclipseformat.xml")
32+
licenseHeaderFile project.rootProject.file("LICENSE_SHORT")
33+
}
3334
}
3435

3536
spotbugs {
@@ -45,11 +46,11 @@ spotbugsMain {
4546
}
4647

4748
checkstyle {
48-
toolVersion '8.29'
49-
configFile file("config/checkstyle/checkstyle.xml")
50-
configProperties = [project_loc: "${projectDir}"]
51-
ignoreFailures = false
52-
maxWarnings = 0
49+
toolVersion = '10.12.1'
50+
configFile file("config/checkstyle/checkstyle.xml")
51+
configProperties = [project_loc: "${projectDir}"]
52+
maxWarnings = 0
53+
maxErrors = 0
5354
}
5455

5556
repositories {

config/checkstyle/checkstyle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
<message key="ws.illegalFollow" value="GenericWhitespace ''{0}'' should followed by whitespace."/>
131131
</module>
132132
<module name="Indentation">
133-
<property name="arrayInitIndent" value="2"/>
133+
<property name="arrayInitIndent" value="4"/>
134134
<property name="basicOffset" value="2"/>
135135
<property name="caseIndent" value="2"/>
136136
<property name="lineWrappingIndentation" value="2" />

config/checkstyle/import-control.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<allow pkg="java.nio.file" />
1717
<allow pkg="java.util" />
1818
<allow pkg="java.lang.reflect" />
19+
<allow pkg="java.nio.charset" />
1920

2021
<allow pkg="javax.annotation.processing" />
2122
<allow pkg="javax.lang.model" />

spotless.eclipseformat.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_colon_in_labeled_statement" value="insert"/>
154154
<setting id="org.eclipse.jdt.core.formatter.align_type_members_on_columns" value="false"/>
155155
<setting id="org.eclipse.jdt.core.formatter.alignment_for_assignment" value="16"/>
156-
<setting id="org.eclipse.jdt.core.formatter.alignment_for_MODULE_statements" value="16"/>
156+
<setting id="org.eclipse.jdt.core.formatter.alignment_for_module_statements" value="16"/>
157157
<setting id="org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_type_header" value="true"/>
158158
<setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_declaration" value="do not insert"/>
159159
<setting id="org.eclipse.jdt.core.formatter.comment.align_tags_names_descriptions" value="false"/>

src/main/java/com/formkiq/graalvm/processors/GraalvmReflectAnnontationProcessor.java

Lines changed: 28 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright [2020] FormKiQ Inc. Licensed under the Apache License, Version 2.0 (the "License"); you
33
* may not use this file except in compliance with the License. You may obtain a copy of the License
44
* at
5-
*
5+
*
66
* <p>http://www.apache.org/licenses/LICENSE-2.0
77
*
88
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
@@ -59,13 +59,11 @@
5959

6060
/** Processes {@link Reflectable} {@link ReflectableImport} Annotations. */
6161
@AutoService(Processor.class)
62-
@SupportedAnnotationTypes({
63-
"com.formkiq.graalvm.annotations.Reflectable",
64-
"com.formkiq.graalvm.annotations.ReflectableImport",
65-
"com.formkiq.graalvm.annotations.ReflectableClasses",
66-
"com.formkiq.graalvm.annotations.ReflectableClass",
67-
"com.formkiq.graalvm.annotations.ReflectableClass.ReflectableClasses"
68-
})
62+
@SupportedAnnotationTypes({"com.formkiq.graalvm.annotations.Reflectable",
63+
"com.formkiq.graalvm.annotations.ReflectableImport",
64+
"com.formkiq.graalvm.annotations.ReflectableClasses",
65+
"com.formkiq.graalvm.annotations.ReflectableClass",
66+
"com.formkiq.graalvm.annotations.ReflectableClass.ReflectableClasses"})
6967
@SupportedSourceVersion(SourceVersion.RELEASE_11)
7068
public class GraalvmReflectAnnontationProcessor extends AbstractProcessor {
7169

@@ -106,8 +104,8 @@ private List<String> findClasses(final Element element, final String key) {
106104
Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues =
107105
annotationMirror.getElementValues();
108106

109-
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
110-
elementValues.entrySet()) {
107+
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : elementValues
108+
.entrySet()) {
111109

112110
String simpleNameKey = entry.getKey().getSimpleName().toString();
113111
Object value = entry.getValue().getValue();
@@ -146,10 +144,8 @@ private Class<?> forName(final String name) throws ClassNotFoundException {
146144

147145
int lastDotIndex = name.lastIndexOf(PACKAGE_SEPARATOR);
148146
if (lastDotIndex != -1) {
149-
String innerClassName =
150-
name.substring(0, lastDotIndex)
151-
+ INNER_CLASS_SEPARATOR
152-
+ name.substring(lastDotIndex + 1);
147+
String innerClassName = name.substring(0, lastDotIndex) + INNER_CLASS_SEPARATOR
148+
+ name.substring(lastDotIndex + 1);
153149
return forName(innerClassName);
154150
}
155151

@@ -159,22 +155,16 @@ private Class<?> forName(final String name) throws ClassNotFoundException {
159155

160156
String generateReflectConfigPath(final Set<String> keys) {
161157

162-
Set<String> strings =
163-
keys.stream()
164-
.map(this::removePartsContainingDotFollowedByCapital)
165-
.filter(m -> m != null && m.length() > 1)
166-
.collect(Collectors.toSet());
158+
Set<String> strings = keys.stream().map(this::removePartsContainingDotFollowedByCapital)
159+
.filter(m -> m != null && m.length() > 1).collect(Collectors.toSet());
167160

168161
if (strings.isEmpty()) {
169162
strings.add("default");
170163
}
171164
final int shortestLength = strings.stream().mapToInt(String::length).min().getAsInt();
172165

173166
List<String> list =
174-
strings.stream()
175-
.filter(s -> s.length() == shortestLength)
176-
.sorted()
177-
.toList();
167+
strings.stream().filter(s -> s.length() == shortestLength).sorted().toList();
178168

179169
return list.get(0);
180170
}
@@ -194,9 +184,7 @@ private String getClassNameByType(final Element element) {
194184
case METHOD:
195185
className = ((TypeElement) element.getEnclosingElement()).getQualifiedName().toString();
196186
break;
197-
case ENUM:
198-
case RECORD:
199-
case CLASS:
187+
default:
200188
TypeElement te = (TypeElement) element;
201189

202190
List<String> simpleNames = new ArrayList<>();
@@ -216,9 +204,6 @@ private String getClassNameByType(final Element element) {
216204
className = e.substring(0, pos) + String.join("$", simpleNames);
217205
}
218206
}
219-
220-
break;
221-
default:
222207
break;
223208
}
224209

@@ -249,8 +234,8 @@ private Reflect getReflect(final String className) {
249234
}
250235

251236
@Override
252-
public boolean process(
253-
final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
237+
public boolean process(final Set<? extends TypeElement> annotations,
238+
final RoundEnvironment roundEnv) {
254239

255240
if (roundEnv.processingOver()) {
256241

@@ -276,8 +261,7 @@ public boolean process(
276261
private Reflect processClass(final Reflect reflect, final Reflectable reflectable) {
277262

278263
LOGGER.log(LOGLEVEL, "processClass " + reflect.name());
279-
reflect
280-
.allDeclaredConstructors(reflectable.allDeclaredConstructors())
264+
reflect.allDeclaredConstructors(reflectable.allDeclaredConstructors())
281265
.allDeclaredFields(reflectable.allDeclaredFields())
282266
.allDeclaredMethods(reflectable.allDeclaredMethods())
283267
.allPublicConstructors(reflectable.allPublicConstructors())
@@ -297,8 +281,7 @@ private Reflect processClass(final Reflect reflect, final Reflectable reflectabl
297281
private Reflect processClass(final Reflect reflect, final ReflectableClass reflectable) {
298282

299283
LOGGER.log(LOGLEVEL, "processClass " + reflect.name());
300-
reflect
301-
.allDeclaredConstructors(reflectable.allDeclaredConstructors())
284+
reflect.allDeclaredConstructors(reflectable.allDeclaredConstructors())
302285
.allDeclaredFields(reflectable.allDeclaredFields())
303286
.allDeclaredMethods(reflectable.allDeclaredMethods())
304287
.allPublicConstructors(reflectable.allPublicConstructors())
@@ -334,10 +317,8 @@ private void processImportedClass(final String clazz) {
334317
for (Method method : forName.getMethods()) {
335318
Reflectable reflection = method.getAnnotation(Reflectable.class);
336319
if (reflection != null) {
337-
List<String> parameterTypes =
338-
Arrays.stream(method.getParameters())
339-
.map(p -> p.getParameterizedType().getTypeName())
340-
.collect(Collectors.toList());
320+
List<String> parameterTypes = Arrays.stream(method.getParameters())
321+
.map(p -> p.getParameterizedType().getTypeName()).collect(Collectors.toList());
341322

342323
LOGGER.log(LOGLEVEL, "adding Method " + method.getName() + " to " + clazz);
343324
reflect.addMethod(method.getName(), parameterTypes);
@@ -425,22 +406,15 @@ private void processingReflectable(final RoundEnvironment roundEnv) {
425406
case METHOD:
426407
String methodName = element.getSimpleName().toString();
427408

428-
List<String> parameterTypes =
429-
((ExecutableElement) element)
430-
.getParameters().stream()
431-
.map(param -> param.asType().toString())
432-
.collect(Collectors.toList());
409+
List<String> parameterTypes = ((ExecutableElement) element).getParameters().stream()
410+
.map(param -> param.asType().toString()).collect(Collectors.toList());
433411

434412
LOGGER.log(LOGLEVEL, "adding Method " + methodName + " to " + className);
435413
reflect.addMethod(methodName, parameterTypes);
436414
break;
437-
case ENUM:
438-
case RECORD:
439-
case CLASS:
415+
default:
440416
reflect = processClass(reflect, reflectable);
441417
break;
442-
default:
443-
throw new RuntimeException("Unsupported kind of element: " + element.getKind());
444418
}
445419
}
446420
}
@@ -520,9 +494,8 @@ private void processReflectableClasses(final RoundEnvironment roundEnv) {
520494
}
521495
}
522496

523-
Set<? extends Element> reflectableClasses =
524-
roundEnv.getElementsAnnotatedWithAny(
525-
Set.of(ReflectableClass.class, ReflectableClass.ReflectableClasses.class));
497+
Set<? extends Element> reflectableClasses = roundEnv.getElementsAnnotatedWithAny(
498+
Set.of(ReflectableClass.class, ReflectableClass.ReflectableClasses.class));
526499

527500
for (Element element : reflectableClasses) {
528501
String className = getClassNameByType(element);
@@ -536,8 +509,7 @@ private void processReflectableClasses(final RoundEnvironment roundEnv) {
536509
}
537510

538511
private String removePartsContainingDotFollowedByCapital(final String input) {
539-
return Arrays.stream(input.split("\\."))
540-
.filter(part -> !part.matches("\\p{Upper}.*"))
512+
return Arrays.stream(input.split("\\.")).filter(part -> !part.matches("\\p{Upper}.*"))
541513
.collect(Collectors.joining("."));
542514
}
543515

@@ -548,13 +520,8 @@ private void writeOutput() {
548520

549521
String name = generateReflectConfigPath(this.reflects.keySet());
550522

551-
FileObject file =
552-
this.processingEnv
553-
.getFiler()
554-
.createResource(
555-
StandardLocation.CLASS_OUTPUT,
556-
"",
557-
"META-INF/native-image/" + name + "/reflect-config.json");
523+
FileObject file = this.processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT,
524+
"", "META-INF/native-image/" + name + "/reflect-config.json");
558525

559526
List<Map<String, Object>> data =
560527
this.reflects.values().stream().map(Reflect::data).collect(Collectors.toList());

src/main/java/com/formkiq/graalvm/processors/Reflect.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright [2020] FormKiQ Inc. Licensed under the Apache License, Version 2.0 (the "License"); you
33
* may not use this file except in compliance with the License. You may obtain a copy of the License
44
* at
5-
*
5+
*
66
* <p>http://www.apache.org/licenses/LICENSE-2.0
77
*
88
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
@@ -34,8 +34,8 @@ public Reflect() {}
3434
* @param allowUnsafeAccess boolean
3535
*/
3636
@SuppressWarnings("unchecked")
37-
public void addField(
38-
final String fieldName, final boolean allowWrite, final boolean allowUnsafeAccess) {
37+
public void addField(final String fieldName, final boolean allowWrite,
38+
final boolean allowUnsafeAccess) {
3939

4040
if (!this.data.containsKey("methods")) {
4141
this.data.put("methods", new ArrayList<>());

0 commit comments

Comments
 (0)