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

Open
wants to merge 9 commits into
base: 4.8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core-processor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {
exclude(group = "com.google.guava", module = "guava")
}
implementation(projects.micronautCoreReactive)
api(libs.sourcegen.bytecode.generator)

compileOnly(libs.managed.kotlin.stdlib.jdk8)
}
1,698 changes: 761 additions & 937 deletions core-processor/src/main/java/io/micronaut/aop/writer/AopProxyWriter.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micronaut.expressions;

import io.micronaut.context.expressions.AbstractEvaluatedExpression;
import io.micronaut.core.annotation.Generated;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.expressions.ExpressionEvaluationContext;
import io.micronaut.expressions.context.ExpressionWithContext;
Expand All @@ -26,10 +27,13 @@
import io.micronaut.expressions.parser.exception.ExpressionCompilationException;
import io.micronaut.expressions.parser.exception.ExpressionParsingException;
import io.micronaut.inject.ast.Element;
import io.micronaut.inject.processing.JavaModelUtils;
import io.micronaut.inject.visitor.VisitorContext;
import io.micronaut.inject.writer.AbstractClassFileWriter;
import io.micronaut.inject.writer.ClassOutputWriter;
import io.micronaut.inject.writer.ClassWriterOutputVisitor;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.GeneratorAdapter;
import org.objectweb.asm.commons.Method;
Expand All @@ -39,6 +43,8 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.objectweb.asm.ClassWriter.COMPUTE_FRAMES;
import static org.objectweb.asm.ClassWriter.COMPUTE_MAXS;
Expand All @@ -50,7 +56,13 @@
* @since 4.0.0
*/
@Internal
public final class EvaluatedExpressionWriter extends AbstractClassFileWriter {
public final class EvaluatedExpressionWriter implements ClassOutputWriter, Opcodes {

private static final String CONSTRUCTOR_NAME = "<init>";
private static final Pattern ARRAY_PATTERN = Pattern.compile("(\\[])+$");

private static final Type TYPE_GENERATED = Type.getType(Generated.class);

private static final Method EVALUATED_EXPRESSIONS_CONSTRUCTOR =
new Method(CONSTRUCTOR_NAME, getConstructorDescriptor(Object.class));

Expand All @@ -77,8 +89,7 @@ public void accept(ClassWriterOutputVisitor outputVisitor) throws IOException {
if (WRITTEN_CLASSES.contains(expressionClassName)) {
return;
}
try (OutputStream outputStream = outputVisitor.visitClass(expressionClassName,
getOriginatingElements())) {
try (OutputStream outputStream = outputVisitor.visitClass(expressionClassName, originatingElement)) {
ClassWriter classWriter = generateClassBytes(expressionClassName);
outputStream.write(classWriter.toByteArray());
WRITTEN_CLASSES.add(expressionClassName);
Expand Down Expand Up @@ -146,4 +157,96 @@ private void failCompilation(Throwable ex, Object initialAnnotationValue) {

visitorContext.fail(message, originatingElement);
}

private static void pushBoxPrimitiveIfNecessary(Type fieldType, GeneratorAdapter injectMethodVisitor) {
if (JavaModelUtils.isPrimitive(fieldType)) {
injectMethodVisitor.valueOf(fieldType);
}
}

private void startPublicClass(ClassVisitor classWriter, String className, Type superType) {
classWriter.visit(V17, ACC_PUBLIC | ACC_SYNTHETIC, className, null, superType.getInternalName(), null);
classWriter.visitAnnotation(TYPE_GENERATED.getDescriptor(), false);
}

private GeneratorAdapter startConstructor(ClassVisitor classWriter, Class<?>... argumentTypes) {
String descriptor = getConstructorDescriptor(argumentTypes);
return new GeneratorAdapter(classWriter.visitMethod(ACC_PUBLIC, CONSTRUCTOR_NAME, descriptor, null, null), ACC_PUBLIC, CONSTRUCTOR_NAME, descriptor);
}

private GeneratorAdapter startProtectedMethod(ClassWriter writer, String methodName, String returnType, String... argumentTypes) {
return new GeneratorAdapter(writer.visitMethod(
ACC_PROTECTED,
methodName,
getMethodDescriptor(returnType, argumentTypes),
null,
null
), ACC_PROTECTED,
methodName,
getMethodDescriptor(returnType, argumentTypes));
}

private static String getTypeDescriptor(Class<?> type) {
return Type.getDescriptor(type);
}

private static String getTypeDescriptor(String className, String... genericTypes) {
if (JavaModelUtils.NAME_TO_TYPE_MAP.containsKey(className)) {
return JavaModelUtils.NAME_TO_TYPE_MAP.get(className);
} else {
String internalName = getInternalName(className);
StringBuilder start = new StringBuilder(40);
Matcher matcher = ARRAY_PATTERN.matcher(className);
if (matcher.find()) {
int dimensions = matcher.group(0).length() / 2;
for (int i = 0; i < dimensions; i++) {
start.append('[');
}
}
start.append('L').append(internalName);
if (genericTypes != null && genericTypes.length > 0) {
start.append('<');
for (String genericType : genericTypes) {
start.append(getTypeDescriptor(genericType));
}
start.append('>');
}
return start.append(';').toString();
}
}

private static String getMethodDescriptor(String returnType, String... argumentTypes) {
StringBuilder builder = new StringBuilder();
builder.append('(');

for (String argumentType : argumentTypes) {
builder.append(getTypeDescriptor(argumentType));
}

builder.append(')');

builder.append(getTypeDescriptor(returnType));
return builder.toString();
}

private static String getConstructorDescriptor(Class<?>... argumentTypes) {
StringBuilder builder = new StringBuilder();
builder.append('(');

for (Class<?> argumentType : argumentTypes) {
builder.append(getTypeDescriptor(argumentType));
}

return builder.append(")V").toString();
}

private static String getInternalName(String className) {
String newClassName = className.replace('.', '/');
Matcher matcher = ARRAY_PATTERN.matcher(newClassName);
if (matcher.find()) {
newClassName = matcher.replaceFirst("");
}
return newClassName;
}

}
Loading
Loading