Skip to content

Commit 6623b22

Browse files
authored
#11 - Incorrect reflect-config.json for nested inner classes (#12)
1 parent a791678 commit 6623b22

File tree

4 files changed

+167
-11
lines changed

4 files changed

+167
-11
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ plugins {
99
}
1010

1111
group 'com.formkiq'
12-
version '1.4.1'
12+
version '1.4.2'
1313

1414
dependencies {
1515
annotationProcessor group: 'com.google.auto.service', name: 'auto-service', version: '1.0.1'

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ String generateReflectConfigPath(final Set<String> keys) {
184184
* @param element {@link Element}
185185
* @return {@link String}s
186186
*/
187-
private String getClassName(final Element element) {
187+
private String getClassNameByType(final Element element) {
188188
String className = null;
189189

190190
switch (element.getKind()) {
@@ -197,10 +197,22 @@ private String getClassName(final Element element) {
197197
case CLASS:
198198
TypeElement te = (TypeElement) element;
199199

200-
if (te.getEnclosingElement().getKind().equals(ElementKind.CLASS)) {
201-
className = te.getEnclosingElement().toString() + "$" + te.getSimpleName();
202-
} else {
203-
className = te.getQualifiedName().toString();
200+
List<String> simpleNames = new ArrayList<>();
201+
Element result = element;
202+
while (result != null && ElementKind.CLASS.equals(result.getKind())) {
203+
simpleNames.add(0, result.getSimpleName().toString());
204+
result = result.getEnclosingElement();
205+
}
206+
207+
className = te.getQualifiedName().toString();
208+
209+
if (!simpleNames.isEmpty()) {
210+
String e = element.getEnclosingElement().toString();
211+
212+
int pos = e.indexOf(simpleNames.get(0));
213+
if (pos > 0) {
214+
className = e.substring(0, pos) + simpleNames.stream().collect(Collectors.joining("$"));
215+
}
204216
}
205217

206218
break;
@@ -393,7 +405,7 @@ private void processingReflectable(final RoundEnvironment roundEnv) {
393405

394406
for (Element element : roundEnv.getElementsAnnotatedWith(Reflectable.class)) {
395407

396-
String className = getClassName(element);
408+
String className = getClassNameByType(element);
397409
LOGGER.log(LOGLEVEL, "processing 'Reflectable' annotation on class " + className);
398410

399411
Reflectable[] reflectables = element.getAnnotationsByType(Reflectable.class);
@@ -492,7 +504,7 @@ private void processReflectableClasses(final RoundEnvironment roundEnv) {
492504

493505
for (Element element : roundEnv.getElementsAnnotatedWith(ReflectableClasses.class)) {
494506

495-
String className = getClassName(element);
507+
String className = getClassNameByType(element);
496508
LOGGER.log(LOGLEVEL, "processing 'ReflectableClasses' annotation on class " + className);
497509

498510
ReflectableClasses[] reflectables = element.getAnnotationsByType(ReflectableClasses.class);
@@ -511,7 +523,7 @@ private void processReflectableClasses(final RoundEnvironment roundEnv) {
511523
Set.of(ReflectableClass.class, ReflectableClass.ReflectableClasses.class));
512524

513525
for (Element element : reflectableClasses) {
514-
String className = getClassName(element);
526+
String className = getClassNameByType(element);
515527
LOGGER.log(LOGLEVEL, "processing 'ReflectableClasses' annotation on class " + className);
516528

517529
ReflectableClass[] reflectables = element.getAnnotationsByType(ReflectableClass.class);

src/test/java/com/formkiq/graalvm/processors/GraalvmReflectAnnontationProcessorTest.java

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,12 @@ public void testClassAnnotation() throws IOException {
387387
}
388388

389389
/**
390-
* testInnerClassAnnotation.
390+
* testInnerClassAnnotation 01.
391391
*
392392
* @throws IOException IOException
393393
*/
394394
@Test
395-
public void testInnerClassAnnotation() throws IOException {
395+
public void testInnerClassAnnotation01() throws IOException {
396396
Compilation compilation =
397397
javac()
398398
.withProcessors(new GraalvmReflectAnnontationProcessor())
@@ -433,6 +433,82 @@ public void testInnerClassAnnotation() throws IOException {
433433
assertNull(map.get(i++).get("methods"));
434434
}
435435

436+
/**
437+
* testInnerClassAnnotation 02.
438+
*
439+
* @throws IOException IOException
440+
*/
441+
@Test
442+
public void testInnerClassAnnotation02() throws IOException {
443+
Compilation compilation =
444+
javac()
445+
.withProcessors(new GraalvmReflectAnnontationProcessor())
446+
.compile(
447+
JavaFileObjects.forSourceString(
448+
"Test",
449+
"package test;\n"
450+
+ "import com.formkiq.graalvm.annotations.Reflectable;\n"
451+
+ "\n"
452+
+ "@Reflectable\n"
453+
+ "final class Test6 {\n"
454+
+ "@Reflectable\n"
455+
+ " public static final class Data0 {"
456+
+ "@Reflectable\n"
457+
+ " public static final class Data1 {"
458+
+ "}\n"
459+
+ "}\n"
460+
+ "}\n"));
461+
462+
final int expected = 3;
463+
List<Map<String, Object>> map = getReflectConf(compilation, "test");
464+
assertEquals(expected, map.size());
465+
466+
int i = 0;
467+
assertEquals("test.Test6", map.get(i++).get("name"));
468+
assertEquals("test.Test6$Data0", map.get(i++).get("name"));
469+
assertEquals("test.Test6$Data0$Data1", map.get(i++).get("name"));
470+
}
471+
472+
/**
473+
* testInnerClassAnnotation 03.
474+
*
475+
* @throws IOException IOException
476+
*/
477+
@Test
478+
public void testInnerClassAnnotation03() throws IOException {
479+
Compilation compilation =
480+
javac()
481+
.withProcessors(new GraalvmReflectAnnontationProcessor())
482+
.compile(
483+
JavaFileObjects.forSourceString(
484+
"Test",
485+
"package test;\n"
486+
+ "import com.formkiq.graalvm.annotations.Reflectable;\n"
487+
+ "\n"
488+
+ "@Reflectable\n"
489+
+ "final class Test6 {\n"
490+
+ "@Reflectable\n"
491+
+ " public static final class Data0 {"
492+
+ "@Reflectable\n"
493+
+ " public static final class Data1 {"
494+
+ "@Reflectable\n"
495+
+ " public static final class Data2 {"
496+
+ "}\n"
497+
+ "}\n"
498+
+ "}\n"
499+
+ "}\n"));
500+
501+
final int expected = 4;
502+
List<Map<String, Object>> map = getReflectConf(compilation, "test");
503+
assertEquals(expected, map.size());
504+
505+
int i = 0;
506+
assertEquals("test.Test6", map.get(i++).get("name"));
507+
assertEquals("test.Test6$Data0", map.get(i++).get("name"));
508+
assertEquals("test.Test6$Data0$Data1", map.get(i++).get("name"));
509+
assertEquals("test.Test6$Data0$Data1$Data2", map.get(i++).get("name"));
510+
}
511+
436512
@SuppressWarnings({"unchecked", "resource"})
437513
private List<Map<String, Object>> getReflectConf(
438514
final Compilation compilation, final String filename)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.formkiq.graalvm.processors;
2+
3+
import com.formkiq.graalvm.annotations.Reflectable;
4+
5+
/** Test6 class. */
6+
@Reflectable
7+
public class Test6 {
8+
9+
/** Test Inner Class. */
10+
@Reflectable
11+
public static class Test6Inner {
12+
13+
/** Test Nested Innter Class. */
14+
@Reflectable
15+
public static class Test6NestedInner {
16+
/** Foo. */
17+
private String foo;
18+
19+
/** Bar. */
20+
private String bar;
21+
22+
/**
23+
* Get Foo.
24+
*
25+
* @return {@link String}
26+
*/
27+
public String getFoo() {
28+
return this.foo;
29+
}
30+
31+
/**
32+
* Set Foo.
33+
*
34+
* @param val {@link String}
35+
*/
36+
public void setFoo(final String val) {
37+
this.foo = val;
38+
}
39+
40+
/**
41+
* Get Bar.
42+
*
43+
* @return {@link String}
44+
*/
45+
public String getBar() {
46+
return this.bar;
47+
}
48+
49+
/**
50+
* Set Bar.
51+
*
52+
* @param val {@link String}
53+
*/
54+
public void setBar(final String val) {
55+
this.bar = val;
56+
}
57+
58+
/**
59+
* Dummy Method.
60+
*
61+
* @param foobar {@link String}
62+
*/
63+
public void foo(final String foobar) {
64+
// empty
65+
}
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)