|
| 1 | +/* |
| 2 | + * Copyright 2017-2024 original authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.micronaut.sourcegen.bytecode.expression; |
| 17 | + |
| 18 | +import io.micronaut.sourcegen.bytecode.MethodContext; |
| 19 | +import io.micronaut.sourcegen.bytecode.TypeUtils; |
| 20 | +import io.micronaut.sourcegen.model.ExpressionDef; |
| 21 | +import io.micronaut.sourcegen.model.ExpressionDef.Lambda; |
| 22 | +import io.micronaut.sourcegen.model.MethodDef; |
| 23 | +import io.micronaut.sourcegen.model.ParameterDef; |
| 24 | +import io.micronaut.sourcegen.model.StatementDef; |
| 25 | +import io.micronaut.sourcegen.model.VariableDef; |
| 26 | +import org.objectweb.asm.Handle; |
| 27 | +import org.objectweb.asm.Opcodes; |
| 28 | +import org.objectweb.asm.Type; |
| 29 | +import org.objectweb.asm.commons.GeneratorAdapter; |
| 30 | + |
| 31 | +import javax.lang.model.element.Modifier; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.LinkedHashSet; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Set; |
| 36 | + |
| 37 | +final class LambdaExpressionWriter extends AbstractStatementAwareExpressionWriter { |
| 38 | + |
| 39 | + public static final String EXCEPTION_VAR_NAME = "exception"; |
| 40 | + public static final String THIS_VAR_NAME = "this"; |
| 41 | + public static final String SUPER_VAR_NAME = "super"; |
| 42 | + |
| 43 | + private static final String METAFACTORY_OWNER = "java/lang/invoke/LambdaMetafactory"; |
| 44 | + private static final String METAFACTORY_METHOD = "metafactory"; |
| 45 | + private static final String METAFACTORY_DESCRIPTOR = |
| 46 | + "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;" + |
| 47 | + "Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;" + |
| 48 | + "Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"; |
| 49 | + |
| 50 | + private final Lambda lambda; |
| 51 | + |
| 52 | + public LambdaExpressionWriter(Lambda lambda) { |
| 53 | + this.lambda = lambda; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void write(GeneratorAdapter generatorAdapter, MethodContext context) { |
| 58 | + List<VariableDef> capturedVariables = captureVariables(lambda.implementation()); |
| 59 | + MethodDef implementationMethodDef = createLambdaMethodDef(context, lambda, capturedVariables); |
| 60 | + context.lambdaMethods().add(implementationMethodDef); |
| 61 | + |
| 62 | + // The captured variables are the parameters to the called bootstrap method |
| 63 | + for (VariableDef variable : capturedVariables) { |
| 64 | + new VariableExpressionWriter(variable).write(generatorAdapter, context); |
| 65 | + } |
| 66 | + |
| 67 | + String descriptor = TypeUtils.getType(context.objectDef().getName()).getDescriptor(); |
| 68 | + if (descriptor.endsWith(";")) { |
| 69 | + descriptor = descriptor.substring(0, descriptor.length() - 1); |
| 70 | + } |
| 71 | + if (descriptor.startsWith("L")) { |
| 72 | + descriptor = descriptor.substring(1); |
| 73 | + } |
| 74 | + Handle lambdaMethodHandle = new Handle( |
| 75 | + Opcodes.H_INVOKESTATIC, |
| 76 | + descriptor, |
| 77 | + implementationMethodDef.getName(), |
| 78 | + TypeUtils.getMethodDescriptor(context.objectDef(), implementationMethodDef), |
| 79 | + false |
| 80 | + ); |
| 81 | + Handle bootstrapMethodHandle = new Handle( |
| 82 | + Opcodes.H_INVOKESTATIC, |
| 83 | + METAFACTORY_OWNER, |
| 84 | + METAFACTORY_METHOD, |
| 85 | + METAFACTORY_DESCRIPTOR, |
| 86 | + false |
| 87 | + ); |
| 88 | + |
| 89 | + generatorAdapter.visitInvokeDynamicInsn( |
| 90 | + lambda.implementation().getName(), |
| 91 | + createDynamicInvocationDescriptor(capturedVariables, context), |
| 92 | + bootstrapMethodHandle, |
| 93 | + Type.getType(TypeUtils.getMethodDescriptor(context.objectDef(), lambda.target())), |
| 94 | + lambdaMethodHandle, |
| 95 | + Type.getType(TypeUtils.getMethodDescriptor(context.objectDef(), lambda.implementation())) |
| 96 | + ); |
| 97 | + popValueIfNeeded(generatorAdapter, lambda.type()); |
| 98 | + } |
| 99 | + |
| 100 | + private String createDynamicInvocationDescriptor(List<VariableDef> capturedVariables, MethodContext context) { |
| 101 | + StringBuilder dynamicDescriptor = new StringBuilder("("); |
| 102 | + for (VariableDef variable : capturedVariables) { |
| 103 | + dynamicDescriptor.append(TypeUtils.getType(variable.type(), context.objectDef())); |
| 104 | + } |
| 105 | + dynamicDescriptor.append(")"); |
| 106 | + dynamicDescriptor.append(TypeUtils.getType(lambda.type()).getDescriptor()); |
| 107 | + return dynamicDescriptor.toString(); |
| 108 | + } |
| 109 | + |
| 110 | + private MethodDef createLambdaMethodDef(MethodContext context, Lambda lambda, List<VariableDef> capturedVariables) { |
| 111 | + MethodDef original = lambda.implementation(); |
| 112 | + List<ParameterDef> parameters = new ArrayList<>(); |
| 113 | + |
| 114 | + // The captured variables are parameters |
| 115 | + for (VariableDef variable : capturedVariables) { |
| 116 | + if (variable instanceof VariableDef.Local local) { |
| 117 | + parameters.add(ParameterDef.builder(local.name(), local.type()).build()); |
| 118 | + } else if (variable instanceof VariableDef.MethodParameter parameter) { |
| 119 | + parameters.add(ParameterDef.builder(parameter.name(), parameter.type()).build()); |
| 120 | + } else if (variable instanceof VariableDef.Field field) { |
| 121 | + parameters.add(ParameterDef.builder(field.name(), field.type()).build()); |
| 122 | + } else if (variable instanceof VariableDef.This thisVar) { |
| 123 | + parameters.add(ParameterDef.builder(THIS_VAR_NAME, thisVar.type()).build()); |
| 124 | + } else if (variable instanceof VariableDef.Super superVar) { |
| 125 | + parameters.add(ParameterDef.builder(SUPER_VAR_NAME, superVar.type()).build()); |
| 126 | + } else if (variable instanceof VariableDef.ExceptionVar exception) { |
| 127 | + parameters.add(ParameterDef.builder(EXCEPTION_VAR_NAME, exception.type()).build()); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + parameters.addAll(original.getParameters()); |
| 132 | + return MethodDef.builder("lambda$" + context.methodDef().getName() + "$" + |
| 133 | + context.lambdaMethods().size()) |
| 134 | + .addModifiers(Modifier.PRIVATE, Modifier.STATIC) |
| 135 | + .addParameters(parameters) |
| 136 | + .returns(original.getReturnType()) |
| 137 | + .addStatements(original.getStatements()) |
| 138 | + .build(); |
| 139 | + } |
| 140 | + |
| 141 | + private List<VariableDef> captureVariables(MethodDef method) { |
| 142 | + Set<String> variables = new LinkedHashSet<>( |
| 143 | + method.getParameters().stream().map(v -> v.getName()).toList() |
| 144 | + ); |
| 145 | + List<VariableDef> capturedVariables = new ArrayList<>(); |
| 146 | + for (StatementDef statement : method.getStatements()) { |
| 147 | + captureVariables(statement, variables, capturedVariables); |
| 148 | + } |
| 149 | + return capturedVariables; |
| 150 | + } |
| 151 | + |
| 152 | + private void captureVariables(StatementDef statement, Set<String> variables, List<VariableDef> capturedVariables) { |
| 153 | + statement.nestedExpressionsStream() |
| 154 | + .forEach(expressionDef -> captureVariables(expressionDef, variables, capturedVariables)); |
| 155 | + } |
| 156 | + |
| 157 | + private void captureVariables(ExpressionDef expression, Set<String> variables, List<VariableDef> capturedVariables) { |
| 158 | + if (expression instanceof VariableDef variable) { |
| 159 | + if (variable instanceof VariableDef.Local local) { |
| 160 | + if (!variables.contains(local.name())) { |
| 161 | + capturedVariables.add(local); |
| 162 | + variables.add(local.name()); |
| 163 | + } |
| 164 | + } else if (variable instanceof VariableDef.MethodParameter parameter) { |
| 165 | + if (!variables.contains(parameter.name())) { |
| 166 | + capturedVariables.add(parameter); |
| 167 | + variables.add(parameter.name()); |
| 168 | + } |
| 169 | + } else if (variable instanceof VariableDef.Field field) { |
| 170 | + captureVariables(field.instance(), variables, capturedVariables); |
| 171 | + } else if (variable instanceof VariableDef.This) { |
| 172 | + if (!variables.contains(THIS_VAR_NAME)) { |
| 173 | + capturedVariables.add(variable); |
| 174 | + variables.add(THIS_VAR_NAME); |
| 175 | + } |
| 176 | + } else if (variable instanceof VariableDef.Super) { |
| 177 | + if (!variables.contains(SUPER_VAR_NAME)) { |
| 178 | + capturedVariables.add(variable); |
| 179 | + variables.add(SUPER_VAR_NAME); |
| 180 | + } |
| 181 | + } else if (variable instanceof VariableDef.ExceptionVar) { |
| 182 | + if (!variables.contains(EXCEPTION_VAR_NAME)) { |
| 183 | + capturedVariables.add(variable); |
| 184 | + variables.add(EXCEPTION_VAR_NAME); |
| 185 | + } |
| 186 | + } |
| 187 | + } else { |
| 188 | + expression.nestedExpressionsStream() |
| 189 | + .forEach(expressionDef -> captureVariables(expressionDef, variables, capturedVariables)); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | +} |
0 commit comments