Skip to content

Commit d49a70b

Browse files
authored
Support Kotlin 2 KSP 2 (#11742)
1 parent e335d5e commit d49a70b

File tree

834 files changed

+48109
-159
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

834 files changed

+48109
-159
lines changed

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ if (System.getenv("SONAR_TOKEN") != null) {
2727
"**/GroovyClassWriterOutputVisitor.java",
2828
"**/MutableHttpRequestWrapper.java",
2929
"**/tck/**",
30-
"**/test/support/**"
30+
"**/test/support/**",
31+
"**/micronaut/annotation/processing/test/**"
3132
]
3233
sonarqube {
34+
skipProject = project.name.contains("test")
3335
properties {
3436
property "sonar.exclusions", coverageExcludes.join(",")
3537
}

core-processor/src/main/java/io/micronaut/aop/writer/AopProxyWriter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,13 @@ public class AopProxyWriter implements ProxyingBeanDefinitionVisitor, ClassOutpu
246246

247247
private final OriginatingElements originatingElements;
248248

249-
private final ClassDef.ClassDefBuilder proxyBuilder;
249+
private ClassDef.ClassDefBuilder proxyBuilder;
250250
private final FieldDef interceptorsField;
251251
private final FieldDef proxyMethodsField;
252252
private FieldDef targetField;
253253

254+
private byte[] output;
255+
254256
/**
255257
* <p>Constructs a new {@link AopProxyWriter} for the given parent {@link BeanDefinitionWriter} and starting interceptors types.</p>
256258
*
@@ -827,6 +829,9 @@ public void visitBeanDefinitionEnd() {
827829
}
828830

829831
proxyBeanDefinitionWriter.visitBeanDefinitionEnd();
832+
833+
output = ByteCodeWriterUtils.writeByteCode(proxyBuilder.build(), visitorContext);
834+
proxyBuilder = null;
830835
}
831836

832837
private void generateProxyTarget(ClassTypeDef targetType) {
@@ -1127,7 +1132,7 @@ public Map<String, ClassElement> getTypeArgumentMap() {
11271132
public void accept(ClassWriterOutputVisitor visitor) throws IOException {
11281133
proxyBeanDefinitionWriter.accept(visitor);
11291134
try (OutputStream out = visitor.visitClass(proxyFullName, getOriginatingElements())) {
1130-
out.write(ByteCodeWriterUtils.writeByteCode(proxyBuilder.build(), visitorContext));
1135+
out.write(output);
11311136
}
11321137
}
11331138

core-processor/src/main/java/io/micronaut/expressions/EvaluatedExpressionWriter.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@
4343
import java.lang.reflect.Method;
4444
import java.util.ArrayList;
4545
import java.util.Arrays;
46-
import java.util.HashSet;
4746
import java.util.List;
48-
import java.util.Set;
4947

5048
/**
5149
* Writer for compile-time expressions.
@@ -59,12 +57,12 @@ public final class EvaluatedExpressionWriter implements ClassOutputWriter {
5957
private static final Method DO_EVALUATE_METHOD
6058
= ReflectionUtils.getRequiredMethod(AbstractEvaluatedExpression.class, "doEvaluate", ExpressionEvaluationContext.class);
6159

62-
private static final Set<String> WRITTEN_CLASSES = new HashSet<>();
63-
6460
private final ExpressionWithContext expressionMetadata;
6561
private final VisitorContext visitorContext;
6662
private final Element originatingElement;
6763

64+
private byte[] output;
65+
6866
public EvaluatedExpressionWriter(ExpressionWithContext expressionMetadata,
6967
VisitorContext visitorContext,
7068
Element originatingElement) {
@@ -73,22 +71,24 @@ public EvaluatedExpressionWriter(ExpressionWithContext expressionMetadata,
7371
this.originatingElement = originatingElement;
7472
}
7573

74+
/**
75+
* Finish generating the expression class.
76+
*/
77+
public void finish() {
78+
String expressionClassName = expressionMetadata.expressionClassName();
79+
ClassDef objectDef = generateClassDef(expressionClassName);
80+
output = ByteCodeWriterUtils.writeByteCode(
81+
objectDef,
82+
visitorContext
83+
);
84+
}
85+
7686
@Override
7787
public void accept(ClassWriterOutputVisitor outputVisitor) throws IOException {
78-
String expressionClassName = expressionMetadata.expressionClassName();
79-
if (WRITTEN_CLASSES.contains(expressionClassName)) {
80-
return;
81-
}
82-
try (OutputStream outputStream = outputVisitor.visitClass(expressionClassName, originatingElement)) {
83-
ClassDef objectDef = generateClassDef(expressionClassName);
84-
outputStream.write(
85-
ByteCodeWriterUtils.writeByteCode(
86-
objectDef,
87-
visitorContext
88-
)
89-
);
90-
WRITTEN_CLASSES.add(expressionClassName);
88+
try (OutputStream outputStream = outputVisitor.visitClass(expressionMetadata.expressionClassName(), originatingElement)) {
89+
outputStream.write(output);
9190
}
91+
output = null;
9292
}
9393

9494
private ClassDef generateClassDef(String expressionClassName) {

core-processor/src/main/java/io/micronaut/inject/beans/visitor/BeanIntrospectionWriter.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ final class BeanIntrospectionWriter implements OriginatingElements, ClassOutputW
193193
private final Map<String, FieldDef> annotationIndexFields = new HashMap<>(2);
194194
private final ClassTypeDef beanType;
195195
private final ClassElement beanClassElement;
196-
private boolean executed = false;
197196
private MethodElement constructor;
198197
private MethodElement defaultConstructor;
199198

@@ -209,6 +208,8 @@ final class BeanIntrospectionWriter implements OriginatingElements, ClassOutputW
209208
private CopyConstructorDispatchTarget copyConstructorDispatchTarget;
210209
private VisitorContext visitorContext;
211210

211+
private byte[] output;
212+
212213
/**
213214
* Default constructor.
214215
*
@@ -406,15 +407,23 @@ void indexProperty(String annotationName, String property, @Nullable String valu
406407
indexByAnnotations.computeIfAbsent(annotationName, (a) -> new LinkedHashSet<>()).add(property);
407408
}
408409

410+
/**
411+
* Finish writing the introspection.
412+
*/
413+
public void finish() {
414+
// Generate the bytecode in the round it's being invoked
415+
output = generateIntrospectionClass();
416+
evaluatedExpressionProcessor.finish();
417+
}
418+
409419
@Override
410420
public void accept(ClassWriterOutputVisitor classWriterOutputVisitor) throws IOException {
411-
if (!executed) {
412-
413-
// Run only once
414-
executed = true;
415-
416-
// First write the introspection for the annotation metadata can be populated with defaults that reference will contain
417-
writeIntrospectionClass(classWriterOutputVisitor);
421+
if (output != null) {
422+
classWriterOutputVisitor.visitServiceDescriptor(BeanIntrospectionReference.class, introspectionName, beanClassElement);
423+
try (OutputStream outputStream = classWriterOutputVisitor.visitClass(introspectionName, getOriginatingElements())) {
424+
outputStream.write(output);
425+
}
426+
output = null;
418427
this.evaluatedExpressionProcessor.writeEvaluatedExpressions(classWriterOutputVisitor);
419428
}
420429
}
@@ -538,7 +547,7 @@ private boolean hasAssociatedConstructorArgument(String name, TypedElement typed
538547
return false;
539548
}
540549

541-
private void writeIntrospectionClass(ClassWriterOutputVisitor classWriterOutputVisitor) throws IOException {
550+
private byte[] generateIntrospectionClass() {
542551
boolean isEnum = beanClassElement.isEnum();
543552

544553
Map<String, MethodDef> loadTypeMethods = new LinkedHashMap<>();
@@ -550,8 +559,6 @@ private void writeIntrospectionClass(ClassWriterOutputVisitor classWriterOutputV
550559
ClassDef.ClassDefBuilder classDefBuilder = ClassDef.builder(introspectionName).synthetic().addModifiers(Modifier.FINAL, Modifier.PUBLIC);
551560
classDefBuilder.superclass(isEnum ? ClassTypeDef.of(AbstractEnumBeanIntrospectionAndReference.class) : ClassTypeDef.of(AbstractInitializableBeanIntrospectionAndReference.class));
552561

553-
classWriterOutputVisitor.visitServiceDescriptor(BeanIntrospectionReference.class, introspectionName, beanClassElement);
554-
555562
classDefBuilder.addAnnotation(AnnotationDef.builder(Generated.class).addMember("service", introspectionName).build());
556563
// init expressions at build time
557564
evaluatedExpressionProcessor.registerExpressionForBuildTimeInit(classDefBuilder);
@@ -831,9 +838,7 @@ private void writeIntrospectionClass(ClassWriterOutputVisitor classWriterOutputV
831838

832839
loadTypeMethods.values().forEach(classDefBuilder::addMethod);
833840

834-
try (OutputStream outputStream = classWriterOutputVisitor.visitClass(introspectionName, getOriginatingElements())) {
835-
outputStream.write(ByteCodeWriterUtils.writeByteCode(classDefBuilder.build(), visitorContext));
836-
}
841+
return ByteCodeWriterUtils.writeByteCode(classDefBuilder.build(), visitorContext);
837842
}
838843

839844
private MethodDef getBooleanMethod(Method method, boolean state) {

core-processor/src/main/java/io/micronaut/inject/beans/visitor/IntrospectedTypeElementVisitor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ private void handleBuilder(
403403
);
404404
builderType.getEnclosedElements(builderMethodQuery)
405405
.forEach(builderWriter::visitBeanMethod);
406+
builderWriter.finish();
406407
writers.put(builderWriter.getBeanType().getName(), builderWriter);
407408
} else {
408409
context.fail("No build method found in builder: " + builderType.getName(), classToBuild);
@@ -480,6 +481,8 @@ private void processElement(boolean metadata,
480481
writers.put(writer.getBeanType().getName(), writer);
481482

482483
addExecutableMethods(ce, writer, beanProperties);
484+
485+
writer.finish();
483486
}
484487

485488
private AnnotationMetadata mergeAnnotations(AnnotationMetadata annotationMetadata) {

core-processor/src/main/java/io/micronaut/inject/writer/BeanDefinitionWriter.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,6 @@ public final class BeanDefinitionWriter implements ClassOutputWriter, BeanDefini
616616
private final List<String> beanTypeInnerClasses;
617617
private final EvaluatedExpressionProcessor evaluatedExpressionProcessor;
618618

619-
private boolean beanFinalized = false;
620619
private ClassTypeDef superType = TYPE_ABSTRACT_BEAN_DEFINITION_AND_REFERENCE;
621620
private boolean superBeanDefinition = false;
622621
private boolean isSuperFactory = false;
@@ -646,7 +645,7 @@ public final class BeanDefinitionWriter implements ClassOutputWriter, BeanDefini
646645

647646
private final OriginatingElements originatingElements;
648647

649-
private final ClassDef.ClassDefBuilder classDefBuilder;
648+
private ClassDef.ClassDefBuilder classDefBuilder;
650649

651650
private BuildMethodDefinition buildMethodDefinition;
652651
private final List<InjectMethodCommand> injectCommands = new ArrayList<>();
@@ -655,6 +654,8 @@ public final class BeanDefinitionWriter implements ClassOutputWriter, BeanDefini
655654

656655
private final Function<String, ExpressionDef> loadClassValueExpressionFn;
657656

657+
private Map<String, byte[]> output;
658+
658659
/**
659660
* Creates a bean definition writer.
660661
*
@@ -1255,7 +1256,17 @@ public void visitBeanDefinitionEnd() {
12551256

12561257
loadTypeMethods.values().forEach(classDefBuilder::addMethod);
12571258

1258-
this.beanFinalized = true;
1259+
output = new LinkedHashMap<>();
1260+
// Generate the bytecode in the round it's being invoked
1261+
generateFiles(classDefBuilder.build());
1262+
evaluatedExpressionProcessor.finish();
1263+
}
1264+
1265+
private void generateFiles(ObjectDef objectDef) {
1266+
output.put(objectDef.getName(), ByteCodeWriterUtils.writeByteCode(objectDef, visitorContext));
1267+
for (ObjectDef innerType : objectDef.getInnerTypes()) {
1268+
generateFiles(innerType);
1269+
}
12591270
}
12601271

12611272
private MethodDef getGetInterceptedType(TypeDef interceptedType) {
@@ -2387,7 +2398,7 @@ private boolean isSingleton(String scope) {
23872398
* @return The bytes of the class
23882399
*/
23892400
public byte[] toByteArray() {
2390-
if (!beanFinalized) {
2401+
if (output == null) {
23912402
throw new IllegalStateException("Bean definition not finalized. Call visitBeanDefinitionEnd() first.");
23922403
}
23932404
return ByteCodeWriterUtils.writeByteCode(classDefBuilder.build(), visitorContext);
@@ -2403,7 +2414,11 @@ public void accept(ClassWriterOutputVisitor visitor) throws IOException {
24032414
beanDefinitionName,
24042415
getOriginatingElement()
24052416
);
2406-
write(visitor, classDefBuilder.build());
2417+
for (Map.Entry<String, byte[]> e1 : output.entrySet()) {
2418+
try (OutputStream out = visitor.visitClass(e1.getKey(), getOriginatingElements())) {
2419+
out.write(e1.getValue());
2420+
}
2421+
}
24072422
try {
24082423
if (executableMethodsDefinitionWriter != null) {
24092424
executableMethodsDefinitionWriter.accept(visitor);
@@ -2419,15 +2434,6 @@ public void accept(ClassWriterOutputVisitor visitor) throws IOException {
24192434
evaluatedExpressionProcessor.writeEvaluatedExpressions(visitor);
24202435
}
24212436

2422-
private void write(ClassWriterOutputVisitor visitor, ObjectDef objectDef) throws IOException {
2423-
try (OutputStream out = visitor.visitClass(objectDef.getName(), getOriginatingElements())) {
2424-
out.write(ByteCodeWriterUtils.writeByteCode(objectDef, visitorContext));
2425-
}
2426-
for (ObjectDef innerType : objectDef.getInnerTypes()) {
2427-
write(visitor, innerType);
2428-
}
2429-
}
2430-
24312437
@Override
24322438
public void visitSetterValue(
24332439
TypedElement declaringType,

core-processor/src/main/java/io/micronaut/inject/writer/EvaluatedExpressionProcessor.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,24 @@
3939
import java.io.IOException;
4040
import java.util.ArrayList;
4141
import java.util.Collection;
42+
import java.util.HashSet;
43+
import java.util.List;
44+
import java.util.Set;
4245

4346
/**
4447
* Internal utility class for writing annotation metadata with evaluated expressions.
4548
*/
4649
@Internal
4750
public final class EvaluatedExpressionProcessor {
51+
private static final Set<String> WRITTEN_CLASSES = new HashSet<>();
52+
4853
private final Collection<ExpressionWithContext> evaluatedExpressions = new ArrayList<>(2);
4954
private final DefaultExpressionCompilationContextFactory expressionCompilationContextFactory;
5055
private final VisitorContext visitorContext;
5156
private final Element originatingElement;
5257

58+
private List<EvaluatedExpressionWriter> writers;
59+
5360
/**
5461
* Default constructor.
5562
* @param visitorContext The visitor context
@@ -118,16 +125,27 @@ public Collection<ExpressionWithContext> getEvaluatedExpressions() {
118125
return evaluatedExpressions;
119126
}
120127

121-
public void writeEvaluatedExpressions(ClassWriterOutputVisitor visitor) throws IOException {
122-
for (ExpressionWithContext expressionMetadata: getEvaluatedExpressions()) {
123-
EvaluatedExpressionWriter expressionWriter = new EvaluatedExpressionWriter(
124-
expressionMetadata,
125-
visitorContext,
126-
originatingElement
127-
);
128+
public void finish() {
129+
Collection<ExpressionWithContext> expressions = getEvaluatedExpressions();
130+
writers = new ArrayList<>(expressions.size());
131+
for (ExpressionWithContext expression : expressions) {
132+
if (WRITTEN_CLASSES.add(expression.expressionClassName())) {
133+
EvaluatedExpressionWriter writer = new EvaluatedExpressionWriter(
134+
expression,
135+
visitorContext,
136+
originatingElement
137+
);
138+
writer.finish();
139+
writers.add(writer);
140+
}
141+
}
142+
}
128143

129-
expressionWriter.accept(visitor);
144+
public void writeEvaluatedExpressions(ClassWriterOutputVisitor visitor) throws IOException {
145+
for (EvaluatedExpressionWriter writer : writers) {
146+
writer.accept(visitor);
130147
}
148+
writers = null;
131149
}
132150

133151
public boolean hasEvaluatedExpressions() {

core-processor/src/main/java/io/micronaut/inject/writer/ExecutableMethodsDefinitionWriter.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ public class ExecutableMethodsDefinitionWriter implements ClassOutputWriter {
102102
private final Set<String> methodNames = new HashSet<>();
103103
private final AnnotationMetadata annotationMetadataWithDefaults;
104104
private final EvaluatedExpressionProcessor evaluatedExpressionProcessor;
105-
private ClassDef.ClassDefBuilder classDefBuilder;
106105

107106
private final OriginatingElements originatingElements;
108107
private final VisitorContext visitorContext;
109108

109+
private byte[] output;
110+
110111
public ExecutableMethodsDefinitionWriter(EvaluatedExpressionProcessor evaluatedExpressionProcessor,
111112
AnnotationMetadata annotationMetadataWithDefaults,
112113
String beanDefinitionClassName,
@@ -228,8 +229,9 @@ public int visitExecutableMethod(TypedElement declaringType,
228229
@Override
229230
public void accept(ClassWriterOutputVisitor classWriterOutputVisitor) throws IOException {
230231
try (OutputStream outputStream = classWriterOutputVisitor.visitClass(className, originatingElements.getOriginatingElements())) {
231-
outputStream.write(ByteCodeWriterUtils.writeByteCode(classDefBuilder.build(), visitorContext));
232+
outputStream.write(output);
232233
}
234+
output = null;
233235
}
234236

235237
/**
@@ -242,7 +244,7 @@ public final void visitDefinitionEnd() {
242244

243245
Function<String, ExpressionDef> loadClassValueExpressionFn = AnnotationMetadataGenUtils.createLoadClassValueExpressionFn(thisType, loadTypeMethods);
244246

245-
classDefBuilder = ClassDef.builder(className)
247+
ClassDef.ClassDefBuilder classDefBuilder = ClassDef.builder(className)
246248
.synthetic()
247249
.addAnnotation(Generated.class)
248250
.superclass(ClassTypeDef.of(AbstractExecutableMethodsDefinition.class));
@@ -327,6 +329,8 @@ public final void visitDefinitionEnd() {
327329
classDefBuilder.addMethod(buildGetMethod());
328330
}
329331
loadTypeMethods.values().forEach(classDefBuilder::addMethod);
332+
333+
output = ByteCodeWriterUtils.writeByteCode(classDefBuilder.build(), visitorContext);
330334
}
331335

332336
private MethodDef buildGetMethod() {

0 commit comments

Comments
 (0)