Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sourcegen bytecode generator to generate internal metadata #11407

Merged
merged 14 commits into from
Jan 9, 2025
Prev Previous commit
CR
dstepanov committed Jan 9, 2025
commit 566ccbfe55df17bf5875952488b12c029ddec5bc
Original file line number Diff line number Diff line change
@@ -294,7 +294,7 @@ public AopProxyWriter(BeanDefinitionWriter parent,
proxyBeanDefinitionWriter.setRequiresMethodProcessing(parent.requiresMethodProcessing());
proxyBeanDefinitionWriter.setInterceptedType(targetClassFullName);

proxyBuilder = ClassDef.builder(proxyFullName);
proxyBuilder = ClassDef.builder(proxyFullName).synthetic();

interceptorsField = FieldDef.builder(FIELD_INTERCEPTORS, Interceptor[][].class)
.addModifiers(Modifier.PRIVATE, Modifier.FINAL)
@@ -402,7 +402,7 @@ public AopProxyWriter(String packageName,
proxyBeanDefinitionWriter.setInterceptedType(targetClassFullName);
}

proxyBuilder = ClassDef.builder(proxyFullName);
proxyBuilder = ClassDef.builder(proxyFullName).synthetic();

interceptorsField = FieldDef.builder(FIELD_INTERCEPTORS, Interceptor[][].class)
.addModifiers(Modifier.PRIVATE, Modifier.FINAL)
Original file line number Diff line number Diff line change
@@ -268,10 +268,7 @@ public static FieldDef createAnnotationMetadataFieldAndInitialize(AnnotationMeta
if (annotationMetadata.isEmpty()) {
initializer = AnnotationMetadataGenUtils.EMPTY_METADATA;
} else if (annotationMetadata instanceof MutableAnnotationMetadata mutableAnnotationMetadata) {
initializer = AnnotationMetadataGenUtils.instantiateNewMetadata(
mutableAnnotationMetadata,
loadClassValueExpressionFn
);
initializer = AnnotationMetadataGenUtils.instantiateNewMetadata(mutableAnnotationMetadata, loadClassValueExpressionFn);
} else if (annotationMetadata instanceof AnnotationMetadataHierarchy annotationMetadataHierarchy) {
initializer = AnnotationMetadataGenUtils.instantiateNewMetadataHierarchy(annotationMetadataHierarchy, loadClassValueExpressionFn);
} else {
@@ -351,7 +348,7 @@ private static void addAnnotationDefaults(List<StatementDef> statements,
final Map<String, Map<CharSequence, Object>> annotationDefaultValues = annotationMetadata.annotationDefaultValues;

if (CollectionUtils.isNotEmpty(annotationDefaultValues)) {
writeAnnotationDefaultsInternal(statements, annotationDefaultValues, new HashSet<>(), loadClassValueExpressionFn);
addAnnotationDefaultsInternal(statements, annotationDefaultValues, new HashSet<>(), loadClassValueExpressionFn);
}
if (annotationMetadata.annotationRepeatableContainer != null && !annotationMetadata.annotationRepeatableContainer.isEmpty()) {
Map<String, String> annotationRepeatableContainer = new LinkedHashMap<>(annotationMetadata.annotationRepeatableContainer);
@@ -368,10 +365,10 @@ private static void addAnnotationDefaults(List<StatementDef> statements,
}
}

private static void writeAnnotationDefaultsInternal(List<StatementDef> statements,
Map<String, Map<CharSequence, Object>> annotationDefaultValues,
Set<String> writtenAnnotations,
Function<String, ExpressionDef> loadClassValueExpressionFn) {
private static void addAnnotationDefaultsInternal(List<StatementDef> statements,
Map<String, Map<CharSequence, Object>> annotationDefaultValues,
Set<String> writtenAnnotations,
Function<String, ExpressionDef> loadClassValueExpressionFn) {
for (Map.Entry<String, Map<CharSequence, Object>> entry : annotationDefaultValues.entrySet()) {
addAnnotationDefaultsInternal(statements,
writtenAnnotations,
@@ -390,7 +387,8 @@ private static void addAnnotationDefaultsInternal(List<StatementDef> statements,
final boolean typeOnly = CollectionUtils.isEmpty(annotationValues);

// skip already registered
if (typeOnly && AnnotationMetadataSupport.getRegisteredAnnotationType(annotationName).isPresent() || AnnotationMetadataSupport.getCoreAnnotationDefaults().containsKey(annotationName)) {
if (typeOnly && AnnotationMetadataSupport.getRegisteredAnnotationType(annotationName).isPresent()
|| AnnotationMetadataSupport.getCoreAnnotationDefaults().containsKey(annotationName)) {
return;
}

Original file line number Diff line number Diff line change
@@ -64,6 +64,7 @@ public static byte[] write(String className, AnnotationMetadata annotationMetada
AnnotationMetadataGenUtils.addAnnotationDefaults(statements, annotationMetadata, loadClassValueExpressionFn);

ClassDef.ClassDefBuilder classDefBuilder = ClassDef.builder(type.getName())
.synthetic()
.addSuperinterface(TypeDef.of(AnnotationMetadataProvider.class))
.addField(annotationMetadataField)
.addStaticInitializer(StatementDef.multi(statements))
Original file line number Diff line number Diff line change
@@ -540,7 +540,7 @@ private void writeIntrospectionClass(ClassWriterOutputVisitor classWriterOutputV

Function<String, ExpressionDef> loadClassValueExpressionFn = AnnotationMetadataGenUtils.createLoadClassValueExpressionFn(introspectionTypeDef, loadTypeMethods);

ClassDef.ClassDefBuilder classDefBuilder = ClassDef.builder(introspectionName).addModifiers(Modifier.FINAL, Modifier.PUBLIC);
ClassDef.ClassDefBuilder classDefBuilder = ClassDef.builder(introspectionName).synthetic().addModifiers(Modifier.FINAL, Modifier.PUBLIC);
classDefBuilder.superclass(isEnum ? ClassTypeDef.of(AbstractEnumBeanIntrospectionAndReference.class) : ClassTypeDef.of(AbstractInitializableBeanIntrospectionAndReference.class));

classWriterOutputVisitor.visitServiceDescriptor(BeanIntrospectionReference.class, introspectionName, beanClassElement);
Original file line number Diff line number Diff line change
@@ -92,7 +92,7 @@ public void accept(ClassWriterOutputVisitor classWriterOutputVisitor) throws IOE
private byte[] generateClassBytes() {
ClassTypeDef targetType = ClassTypeDef.of(configurationClassName);

ClassDef.ClassDefBuilder configurationClassBuilder = ClassDef.builder(configurationClassName)
ClassDef.ClassDefBuilder configurationClassBuilder = ClassDef.builder(configurationClassName).synthetic()
.superclass(ClassTypeDef.of(AbstractBeanConfiguration.class))
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addAnnotation(AnnotationDef.builder(Generated.class).addMember("service", BeanConfiguration.class.getName()).build());
Original file line number Diff line number Diff line change
@@ -802,6 +802,7 @@ public BeanDefinitionWriter(Element beanProducingElement,
}

classDefBuilder = ClassDef.builder(beanDefinitionName)
.synthetic()
.addModifiers(Modifier.PUBLIC)
.addAnnotation(AnnotationDef.builder(Generated.class).addMember("service", BeanDefinitionReference.class.getName()).build())
.superclass(TypeDef.parameterized(superType, argumentType));
@@ -1812,6 +1813,7 @@ private StatementDef withGetFactoryBean(List<VariableDef.MethodParameter> parame
private ClassTypeDef createConstructorInterceptor(ConstructorBuildMethodDefinition constructorBuildMethodDefinition) {
String interceptedConstructorWriterName = "ConstructorInterceptor";
ClassDef.ClassDefBuilder innerClassBuilder = ClassDef.builder(interceptedConstructorWriterName)
.synthetic()
.addModifiers(Modifier.FINAL)
.superclass(ClassTypeDef.of(AbstractBeanDefinitionBeanConstructor.class))
.addAnnotation(Generated.class);
@@ -1845,6 +1847,7 @@ private ClassTypeDef createFactoryInterceptor(FactoryBuildMethodDefinition facto
String interceptedConstructorWriterName = "ConstructorInterceptor";

ClassDef.ClassDefBuilder innerClassBuilder = ClassDef.builder(interceptedConstructorWriterName)
.synthetic()
.addModifiers(Modifier.FINAL)
.superclass(ClassTypeDef.of(AbstractBeanDefinitionBeanConstructor.class))
.addAnnotation(Generated.class);
@@ -3623,9 +3626,9 @@ private void applyDefaultNamedToParameters(List<ParameterElement> argumentTypes)
private ClassTypeDef createExecutableMethodInterceptor(MethodDef interceptMethod, String name) {
// if there is method interception in place we need to construct an inner executable method class that invokes the "initialize"
// method and apply interception
String interceptedConstructorWriterName = name;

ClassDef.ClassDefBuilder innerClassBuilder = ClassDef.builder(interceptedConstructorWriterName)
ClassDef.ClassDefBuilder innerClassBuilder = ClassDef.builder(name)
.synthetic()
.addModifiers(Modifier.FINAL)
.superclass(ClassTypeDef.of(AbstractExecutableMethod.class))
.addAnnotation(Generated.class);
@@ -3692,7 +3695,7 @@ private ClassTypeDef createExecutableMethodInterceptor(MethodDef interceptMethod

classDefBuilder.addInnerType(innerClassBuilder.build());

return ClassTypeDef.of(beanDefinitionName + "$" + interceptedConstructorWriterName);
return ClassTypeDef.of(beanDefinitionName + "$" + name);
}

private StatementDef interceptAndReturn(VariableDef.This aThis,
Original file line number Diff line number Diff line change
@@ -242,6 +242,7 @@ public final void visitDefinitionEnd() {
Function<String, ExpressionDef> loadClassValueExpressionFn = AnnotationMetadataGenUtils.createLoadClassValueExpressionFn(thisType, loadTypeMethods);

classDefBuilder = ClassDef.builder(className)
.synthetic()
.addAnnotation(Generated.class)
.superclass(ClassTypeDef.of(AbstractExecutableMethodsDefinition.class));

Original file line number Diff line number Diff line change
@@ -70,6 +70,7 @@ public void accept(ClassWriterOutputVisitor classWriterOutputVisitor) throws IOE
ClassTypeDef thisType = ClassTypeDef.of(className);
try (OutputStream outputStream = classWriterOutputVisitor.visitClass(className, originatingElement)) {
ClassDef.ClassDefBuilder classDefBuilder = ClassDef.builder(className)
.synthetic()
.addModifiers(Modifier.PUBLIC)
.addAnnotation(AnnotationDef.builder(Generated.class).addMember("service", GraalReflectionConfigurer.class.getName()).build())
.addSuperinterface(ClassTypeDef.of(GraalReflectionConfigurer.class))
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ tomlj="1.1.1"
vertx = "4.5.11"
wiremock = "2.33.2"
mimepull = "1.10.0"
sourcegen-bytecode-generator = "1.5.4"
sourcegen-bytecode-generator = "1.5.5"

#
# Versions which start with managed- are managed by Micronaut in the sense

Unchanged files with check annotations Beta

assertUnauthorized(json, MediaType.APPLICATION_JSON_TYPE);
json = new PasswordChangeForm("sherlock", "elementary", csrfToken);
assertOk(json, MediaType.APPLICATION_JSON_TYPE);

Check failure on line 53 in http-server-tck/src/main/java/io/micronaut/http/server/tck/tests/forms/FormUrlEncodedBodyInRequestFilterTest.java

GitHub Actions / Java CI / Test Report (21)

FormUrlEncodedBodyInRequestFilterTest.bodyParsingInFilter()

org.opentest4j.AssertionFailedError: Unexpected exception thrown: io.micronaut.http.client.exceptions.ReadTimeoutException: Read Timeout
Raw output
org.opentest4j.AssertionFailedError: Unexpected exception thrown: io.micronaut.http.client.exceptions.ReadTimeoutException: Read Timeout
	at app//org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:152)
	at app//org.junit.jupiter.api.AssertDoesNotThrow.createAssertionFailedError(AssertDoesNotThrow.java:84)
	at app//org.junit.jupiter.api.AssertDoesNotThrow.assertDoesNotThrow(AssertDoesNotThrow.java:75)
	at app//org.junit.jupiter.api.AssertDoesNotThrow.assertDoesNotThrow(AssertDoesNotThrow.java:58)
	at app//org.junit.jupiter.api.Assertions.assertDoesNotThrow(Assertions.java:3259)
	at app//io.micronaut.http.tck.AssertionUtils.assertDoesNotThrow(AssertionUtils.java:117)
	at app//io.micronaut.http.server.tck.tests.forms.FormUrlEncodedBodyInRequestFilterTest.lambda$assertOk$0(FormUrlEncodedBodyInRequestFilterTest.java:64)
	at app//io.micronaut.http.tck.TestScenario.run(TestScenario.java:118)
	at app//io.micronaut.http.tck.TestScenario$Builder.run(TestScenario.java:201)
	at app//io.micronaut.http.server.tck.tests.forms.FormUrlEncodedBodyInRequestFilterTest.assertOk(FormUrlEncodedBodyInRequestFilterTest.java:67)
	at app//io.micronaut.http.server.tck.tests.forms.FormUrlEncodedBodyInRequestFilterTest.bodyParsingInFilter(FormUrlEncodedBodyInRequestFilterTest.java:53)
	at java.base@21.0.5/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base@21.0.5/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base@21.0.5/java.util.ArrayList.forEach(ArrayList.java:1596)
Caused by: io.micronaut.http.client.exceptions.ReadTimeoutException: Read Timeout
	at app//io.micronaut.http.client.exceptions.ReadTimeoutException.<clinit>(ReadTimeoutException.java:26)
	at app//io.micronaut.http.client.netty.DefaultHttpClient.handleResponseError(DefaultHttpClient.java:2029)
	at app//io.micronaut.http.client.netty.DefaultHttpClient$6.fail(DefaultHttpClient.java:1702)
	at app//io.micronaut.http.client.netty.Http1ResponseHandler$BeforeResponse.exceptionCaught(Http1ResponseHandler.java:147)
	at app//io.micronaut.http.client.netty.Http1ResponseHandler.exceptionCaught(Http1ResponseHandler.java:88)
	at app//io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:346)
	at app//io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:325)
	at app//io.netty.channel.AbstractChannelHandlerContext.fireExceptionCaught(AbstractChannelHandlerContext.java:317)
	at app//io.netty.channel.DefaultChannelPipeline$HeadContext.exceptionCaught(DefaultChannelPipeline.java:1324)
	at app//io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:346)
	at app//io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:325)
	at app//io.netty.channel.DefaultChannelPipeline.fireExceptionCaught(DefaultChannelPipeline.java:856)
	at app//io.netty.handler.codec.http2.Http2MultiplexHandler$2.visit(Http2MultiplexHandler.java:321)
	at app//io.netty.handler.codec.http2.Http2FrameCodec$1.visit(Http2FrameCodec.java:201)
	at app//io.netty.handler.codec.http2.DefaultHttp2Connection$ActiveStreams.forEachActiveStream(DefaultHttp2Connection.java:1005)
	at app//io.netty.handler.codec.http2.DefaultHttp2Connection.forEachActiveStream(DefaultHttp2Connection.java:207)
	at app//io.netty.handler.codec.http2.Http2FrameCodec.forEachActiveStream(Http2FrameCodec.java:197)
	at app//io.netty.handler.codec.http2.Http2ChannelDuplexHandler.forEachActiveStream(Http2ChannelDuplexHandler.java:81)
	at app//io.netty.handler.codec.http2.Http2MultiplexHandler.fireExceptionCaughtForActiveStream(Http2MultiplexHandler.java:316)
	at app//io.netty.handler.codec.http2.Http2MultiplexHandler.exceptionCaught(Http2MultiplexHandler.java:305)
	at app//io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:346)
	at app//io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:325)
	at app//io.netty.channel.AbstractChannelHandlerContext.fireExceptionCaught(AbstractChannelHandlerContext.java:317)
	at app//io.netty.channel.ChannelInboundHandlerAdapter.exceptionCaught(ChannelInboundHandlerAdapter.java:143)
	at app//io.netty.handler.codec.http2.Http2ConnectionHandler.exceptionCaught(Http2ConnectionHandler.java:580)
	at app//io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:346)
	at app//io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:325)
	at app//io.netty.channel.AbstractChannelHandlerContext.fireExceptionCaught(AbstractChannelHandlerContext.java:317)
	at app//io.netty.handler.ssl.SslHandler.exceptionCaught(SslHandler.java:1210)
	at app//io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:346)
	at app//io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:325)
	at app//io.netty.channel.AbstractChannelHandlerContext.fireExceptionCaught(AbstractChannelHandlerContext.java:317)
	at app//io.netty.channel.DefaultChannelPipeline$HeadContext.exceptionCaught(DefaultChannelPipeline.java:1324)
	at app//io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:346)
	at app//io.netty.channel.AbstractChannelHandlerContext.invokeExceptionCaught(AbstractChannelHandlerContext.java:325)
	at app//io.netty.channel.DefaultChannelPipeline.fireExceptionCaught(DefaultChannelPipeline.java:856)
	at app//io.micronaut.http.client.netty.ConnectionManager$Pool$Http2ConnectionHolder.fireReadTimeout(ConnectionManager.java:1614)
	at app//io.micronaut.http.client.netty.ConnectionManager$Pool$ConnectionHolder$1.readTimedOut(ConnectionManager.java:1316)
	at app//io.netty.handler.timeout.ReadTimeoutHandler.channelIdle(ReadTimeoutHandler.java:90)
	at app//io.netty.handler.timeout.IdleStateHandler$ReaderIdleTimeoutTask.run(IdleStateHandler.java:525)
	at app//io.netty.handler.timeout.IdleStateHandler$AbstractIdleTask.run(IdleStateHandler.java:497)
	at app//io.netty.util.concurrent.PromiseTask.runTask(PromiseTask.java:98)
	at app//io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:156)
	at app//io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:173)
	at app//io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:166)
	at app//io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
	at app//io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:569)
	at app//io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
	at app//io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
	at app//io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.base@21.0.5/java.lang.Thread.run(Thread.java:1583)
body = "username=sherlock&password=elementary";
assertUnauthorized(body, MediaType.APPLICATION_FORM_URLENCODED_TYPE);