Skip to content

Commit fc45d54

Browse files
Fixes #11838 for clearing mutated annotation metadata (#11842)
* Create another test for mutated annotation metadata * Another test case for postponed to next round visiting * fix caching bug by always including declaring element --------- Co-authored-by: Graeme Rocher <[email protected]>
1 parent cc2786e commit fc45d54

File tree

5 files changed

+78
-6
lines changed

5 files changed

+78
-6
lines changed

inject-java/src/main/java/io/micronaut/annotation/processing/visitor/AbstractJavaElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ public boolean equals(Object o) {
448448
if (this == o) {
449449
return true;
450450
}
451-
if (o == null) {
451+
if (!(o instanceof io.micronaut.inject.ast.Element)) {
452452
return false;
453453
}
454454
io.micronaut.inject.ast.Element that = (io.micronaut.inject.ast.Element) o;

inject-java/src/main/java/io/micronaut/annotation/processing/visitor/JavaElementFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,14 @@ private void failIfPostponeIsNeeded(TypedElement member, ExecutableElement execu
225225
TypeMirror returnType = executableElement.getReturnType();
226226
TypeKind returnKind = returnType.getKind();
227227
if (returnKind == TypeKind.ERROR) {
228-
throw new PostponeToNextRoundException(executableElement, member.getName() + " " + executableElement);
228+
throw new PostponeToNextRoundException(member, member.getName() + " " + executableElement);
229229
}
230230
}
231231

232232
private void failIfPostponeIsNeeded(TypedElement member, VariableElement variableElement) {
233233
TypeMirror type = variableElement.asType();
234234
if (type.getKind() == TypeKind.ERROR) {
235-
throw new PostponeToNextRoundException(variableElement, member.getName() + " " + variableElement);
235+
throw new PostponeToNextRoundException(member, member.getName() + " " + variableElement);
236236
}
237237
}
238238

inject-java/src/test/groovy/io/micronaut/visitors/CollectingVisitor.groovy

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.micronaut.visitors
22

33
import io.micronaut.core.annotation.Introspected
4+
import io.micronaut.http.annotation.Controller
45
import io.micronaut.http.annotation.Get
56
import io.micronaut.inject.ast.ClassElement
67
import io.micronaut.inject.ast.MethodElement
@@ -15,6 +16,13 @@ class CollectingVisitor implements TypeElementVisitor<Object, Object> {
1516
static int numMethodVisited = 0
1617
static boolean hasIntrospected
1718
static String getPath
19+
static String controllerPath
20+
21+
@Override
22+
void start(VisitorContext visitorContext) {
23+
numVisited = 0
24+
numMethodVisited = 0
25+
}
1826

1927
@Override
2028
void visitClass(ClassElement element, VisitorContext context) {
@@ -26,6 +34,10 @@ class CollectingVisitor implements TypeElementVisitor<Object, Object> {
2634
throw new ElementPostponedToNextRoundException(element)
2735
}
2836

37+
controllerPath = element.stringValue(Controller.class).orElse(null)
38+
if ("<error>".equalsIgnoreCase(controllerPath)) {
39+
throw new ElementPostponedToNextRoundException(element)
40+
}
2941
++numVisited
3042
hasIntrospected = element.hasStereotype(Introspected)
3143
}
@@ -37,6 +49,8 @@ class CollectingVisitor implements TypeElementVisitor<Object, Object> {
3749
}
3850

3951
++numMethodVisited
40-
getPath = element.stringValue(Get).orElse(null)
52+
53+
def path = element.stringValue(Get).orElse(null)
54+
getPath = path
4155
}
4256
}

inject-java/src/test/groovy/io/micronaut/visitors/GeneratorVisitor.groovy

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,23 @@ import io.micronaut.http.annotation.Get;
1717
@Introspected
1818
public interface Parent {
1919
20+
String BASE_PATH = "/hello";
21+
2022
@Get("/get")
21-
String hello();
23+
TestModel hello();
24+
25+
}
26+
"""
27+
28+
private static final @Language("java") String SOURCE_MODEL = """
29+
package example;
30+
31+
import io.micronaut.core.annotation.Introspected;
32+
33+
@Introspected
34+
public record TestModel(
35+
String greeting
36+
) {
2237
2338
}
2439
"""
@@ -34,6 +49,15 @@ public interface Parent {
3449
throw new RuntimeException(e)
3550
}
3651
})
52+
53+
context.visitGeneratedSourceFile("example", "TestModel", element)
54+
.ifPresent(generatedFile -> {
55+
try {
56+
generatedFile.write(writer -> writer.write(SOURCE_MODEL))
57+
} catch (IOException e) {
58+
throw new RuntimeException(e)
59+
}
60+
})
3761
}
3862

3963
}

inject-java/src/test/groovy/io/micronaut/visitors/PostponedVisitorsSpec.groovy

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,49 @@ class MyBean implements GeneratedInterface {
128128
package example;
129129
130130
import jakarta.inject.Singleton;
131+
import io.micronaut.http.annotation.Controller;
131132
132133
@io.micronaut.visitors.GeneratorTrigger
133134
class Trigger {}
134135
135136
// Parent is generated, we want to retrieve inherited annotations correctly
136137
@Singleton
138+
@Controller(Parent.BASE_PATH)
137139
class Child implements Parent {
138140
139141
@Override
142+
public TestModel hello() {
143+
return new TestModel("Hola!");
144+
}
145+
146+
}
147+
''')
148+
then:
149+
CollectingVisitor.numVisited == 1
150+
CollectingVisitor.numMethodVisited == 1
151+
CollectingVisitor.getPath == "/get"
152+
CollectingVisitor.hasIntrospected
153+
CollectingVisitor.controllerPath == "/hello"
154+
}
155+
156+
void "test information collecting visitor not through parent"() {
157+
when:
158+
buildClassLoader('example.Trigger', '''
159+
package example;
160+
161+
import jakarta.inject.Singleton;
162+
import io.micronaut.http.annotation.Controller;
163+
import io.micronaut.http.annotation.Get;
164+
165+
@io.micronaut.visitors.GeneratorTrigger
166+
class Trigger {}
167+
168+
// Parent is generated, we want to retrieve the value correctly
169+
@Singleton
170+
@Controller(Parent.BASE_PATH)
171+
class Child {
172+
173+
@Get("/get")
140174
public String hello() {
141175
return "Hola!";
142176
}
@@ -147,7 +181,7 @@ class Child implements Parent {
147181
CollectingVisitor.numVisited == 1
148182
CollectingVisitor.numMethodVisited == 1
149183
CollectingVisitor.getPath == "/get"
150-
CollectingVisitor.hasIntrospected
184+
CollectingVisitor.controllerPath == "/hello"
151185
}
152186

153187
}

0 commit comments

Comments
 (0)