Search in sources :

Example 1 with CLOSURE_TYPE

use of org.codehaus.groovy.ast.ClassHelper.CLOSURE_TYPE in project groovy by apache.

the class StaticTypesLambdaWriter method newGroovyLambdaWrapperAndLoad.

private void newGroovyLambdaWrapperAndLoad(final ClassNode lambdaClass, final LambdaExpression expression, final boolean accessingInstanceMembers) {
    CompileStack compileStack = controller.getCompileStack();
    OperandStack operandStack = controller.getOperandStack();
    MethodVisitor mv = controller.getMethodVisitor();
    String lambdaClassInternalName = BytecodeHelper.getClassInternalName(lambdaClass);
    mv.visitTypeInsn(NEW, lambdaClassInternalName);
    mv.visitInsn(DUP);
    if (controller.isStaticMethod() || compileStack.isInSpecialConstructorCall() || !accessingInstanceMembers) {
        classX(controller.getThisType()).visit(controller.getAcg());
    } else {
        loadThis();
    }
    operandStack.dup();
    loadSharedVariables(expression);
    Optional<ConstructorNode> generatedConstructor = lambdaClass.getDeclaredConstructors().stream().filter(ctor -> Boolean.TRUE.equals(ctor.getNodeMetaData(IS_GENERATED_CONSTRUCTOR))).findFirst();
    if (!generatedConstructor.isPresent()) {
        throw new GroovyBugError("Failed to find the generated constructor");
    }
    Parameter[] lambdaClassConstructorParameters = generatedConstructor.get().getParameters();
    mv.visitMethodInsn(INVOKESPECIAL, lambdaClassInternalName, "<init>", BytecodeHelper.getMethodDescriptor(VOID_TYPE, lambdaClassConstructorParameters), lambdaClass.isInterface());
    operandStack.replace(CLOSURE_TYPE, lambdaClassConstructorParameters.length);
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) MethodVisitor(org.objectweb.asm.MethodVisitor) WriterController(org.codehaus.groovy.classgen.asm.WriterController) LambdaWriter(org.codehaus.groovy.classgen.asm.LambdaWriter) GeneralUtils.classX(org.codehaus.groovy.ast.tools.GeneralUtils.classX) ClassHelper.findSAM(org.codehaus.groovy.ast.ClassHelper.findSAM) H_INVOKEVIRTUAL(org.objectweb.asm.Opcodes.H_INVOKEVIRTUAL) NEW(org.objectweb.asm.Opcodes.NEW) BytecodeHelper(org.codehaus.groovy.classgen.asm.BytecodeHelper) Map(java.util.Map) GeneralUtils.block(org.codehaus.groovy.ast.tools.GeneralUtils.block) VOID_TYPE(org.codehaus.groovy.ast.ClassHelper.VOID_TYPE) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) AstStringCompiler(org.codehaus.groovy.ast.builder.AstStringCompiler) INVOKEVIRTUAL(org.objectweb.asm.Opcodes.INVOKEVIRTUAL) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) SERIALIZABLE_TYPE(org.codehaus.groovy.ast.ClassHelper.SERIALIZABLE_TYPE) GroovyBugError(org.codehaus.groovy.GroovyBugError) DUP(org.objectweb.asm.Opcodes.DUP) List(java.util.List) CompileStack(org.codehaus.groovy.classgen.asm.CompileStack) OBJECT_TYPE(org.codehaus.groovy.ast.ClassHelper.OBJECT_TYPE) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ACC_PUBLIC(org.objectweb.asm.Opcodes.ACC_PUBLIC) Optional(java.util.Optional) ClassHelper.long_TYPE(org.codehaus.groovy.ast.ClassHelper.long_TYPE) Statement(org.codehaus.groovy.ast.stmt.Statement) GeneralUtils.constX(org.codehaus.groovy.ast.tools.GeneralUtils.constX) CodeVisitorSupport(org.codehaus.groovy.ast.CodeVisitorSupport) LambdaExpression(org.codehaus.groovy.ast.expr.LambdaExpression) ACC_FINAL(org.objectweb.asm.Opcodes.ACC_FINAL) ACC_PRIVATE(org.objectweb.asm.Opcodes.ACC_PRIVATE) HashMap(java.util.HashMap) GeneralUtils.declS(org.codehaus.groovy.ast.tools.GeneralUtils.declS) ACC_STATIC(org.objectweb.asm.Opcodes.ACC_STATIC) ClosureUtils.getParametersSafe(org.codehaus.groovy.ast.tools.ClosureUtils.getParametersSafe) GENERATED_LAMBDA_TYPE(org.codehaus.groovy.ast.ClassHelper.GENERATED_LAMBDA_TYPE) MethodNode(org.codehaus.groovy.ast.MethodNode) GeneralUtils.returnS(org.codehaus.groovy.ast.tools.GeneralUtils.returnS) CLOSURE_TYPE(org.codehaus.groovy.ast.ClassHelper.CLOSURE_TYPE) Expression(org.codehaus.groovy.ast.expr.Expression) SERIALIZEDLAMBDA_TYPE(org.codehaus.groovy.ast.ClassHelper.SERIALIZEDLAMBDA_TYPE) LinkedList(java.util.LinkedList) CHECKCAST(org.objectweb.asm.Opcodes.CHECKCAST) Parameter(org.codehaus.groovy.ast.Parameter) BytecodeInstruction(org.codehaus.groovy.classgen.BytecodeInstruction) ICONST_0(org.objectweb.asm.Opcodes.ICONST_0) ClassNode(org.codehaus.groovy.ast.ClassNode) OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) GeneralUtils.cloneParams(org.codehaus.groovy.ast.tools.GeneralUtils.cloneParams) GeneralUtils.localVarX(org.codehaus.groovy.ast.tools.GeneralUtils.localVarX) StaticCompilationMetadataKeys(org.codehaus.groovy.transform.sc.StaticCompilationMetadataKeys) StaticTypesMarker(org.codehaus.groovy.transform.stc.StaticTypesMarker) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) INVOKESPECIAL(org.objectweb.asm.Opcodes.INVOKESPECIAL) BytecodeSequence(org.codehaus.groovy.classgen.BytecodeSequence) WriterControllerFactory(org.codehaus.groovy.classgen.asm.WriterControllerFactory) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) ALOAD(org.objectweb.asm.Opcodes.ALOAD) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) GroovyBugError(org.codehaus.groovy.GroovyBugError) Parameter(org.codehaus.groovy.ast.Parameter) CompileStack(org.codehaus.groovy.classgen.asm.CompileStack) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 GroovyBugError (org.codehaus.groovy.GroovyBugError)1 CLOSURE_TYPE (org.codehaus.groovy.ast.ClassHelper.CLOSURE_TYPE)1 GENERATED_LAMBDA_TYPE (org.codehaus.groovy.ast.ClassHelper.GENERATED_LAMBDA_TYPE)1 OBJECT_TYPE (org.codehaus.groovy.ast.ClassHelper.OBJECT_TYPE)1 SERIALIZABLE_TYPE (org.codehaus.groovy.ast.ClassHelper.SERIALIZABLE_TYPE)1 SERIALIZEDLAMBDA_TYPE (org.codehaus.groovy.ast.ClassHelper.SERIALIZEDLAMBDA_TYPE)1 VOID_TYPE (org.codehaus.groovy.ast.ClassHelper.VOID_TYPE)1 ClassHelper.findSAM (org.codehaus.groovy.ast.ClassHelper.findSAM)1 ClassHelper.long_TYPE (org.codehaus.groovy.ast.ClassHelper.long_TYPE)1 ClassNode (org.codehaus.groovy.ast.ClassNode)1 CodeVisitorSupport (org.codehaus.groovy.ast.CodeVisitorSupport)1 ConstructorNode (org.codehaus.groovy.ast.ConstructorNode)1 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)1 MethodNode (org.codehaus.groovy.ast.MethodNode)1 Parameter (org.codehaus.groovy.ast.Parameter)1