Java Code Examples for org.codehaus.groovy.ast.ClassNode#addSyntheticMethod()

The following examples show how to use org.codehaus.groovy.ast.ClassNode#addSyntheticMethod() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StaticTypesLambdaWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void addDeserializeLambdaMethod() {
    ClassNode enclosingClass = controller.getClassNode();
    Parameter[] parameters = createDeserializeLambdaMethodParams();
    if (enclosingClass.hasMethod("$deserializeLambda$", parameters)) {
        return;
    }

    Statement code = block(
            declS(localVarX("enclosingClass", OBJECT_TYPE), classX(enclosingClass)),
            ((BlockStatement) new AstStringCompiler().compile(
                    "return enclosingClass" +
                            ".getDeclaredMethod(\"\\$deserializeLambda_${serializedLambda.getImplClass().replace('/', '$')}\\$\", serializedLambda.getClass())" +
                            ".invoke(null, serializedLambda)"
            ).get(0)).getStatements().get(0)
    );

    enclosingClass.addSyntheticMethod(
            "$deserializeLambda$",
            ACC_PRIVATE | ACC_STATIC,
            OBJECT_TYPE,
            parameters,
            ClassNode.EMPTY_ARRAY,
            code);
}
 
Example 2
Source File: Verifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to add a new method to a ClassNode.  Depending on the shouldBeSynthetic flag the
 * call will either be made to ClassNode.addSyntheticMethod() or ClassNode.addMethod(). If a non-synthetic method
 * is to be added the ACC_SYNTHETIC modifier is removed if it has been accidentally supplied.
 */
protected MethodNode addMethod(ClassNode node, boolean shouldBeSynthetic, String name, int modifiers, ClassNode returnType, Parameter[] parameters,
                               ClassNode[] exceptions, Statement code) {
    if (shouldBeSynthetic) {
        return node.addSyntheticMethod(name, modifiers, returnType, parameters, exceptions, code);
    } else {
        return node.addMethod(name, modifiers & ~ACC_SYNTHETIC, returnType, parameters, exceptions, code);
    }
}
 
Example 3
Source File: StaticTypesLambdaWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void addDeserializeLambdaMethodForEachLambdaExpression(final LambdaExpression expression, final ClassNode lambdaClass) {
    ClassNode enclosingClass = controller.getClassNode();
    Statement code = block(
            new BytecodeSequence(new BytecodeInstruction() {
                @Override
                public void visit(final MethodVisitor mv) {
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitInsn(ICONST_0);
                    mv.visitMethodInsn(
                            INVOKEVIRTUAL,
                            "java/lang/invoke/SerializedLambda",
                            "getCapturedArg",
                            "(I)Ljava/lang/Object;",
                            false);
                    mv.visitTypeInsn(CHECKCAST, BytecodeHelper.getClassInternalName(lambdaClass));
                    OperandStack operandStack = controller.getOperandStack();
                    operandStack.push(lambdaClass);
                }
            }),
            returnS(expression)
    );

    enclosingClass.addSyntheticMethod(
            createDeserializeLambdaMethodName(lambdaClass),
            ACC_PUBLIC | ACC_STATIC,
            OBJECT_TYPE,
            createDeserializeLambdaMethodParams(),
            ClassNode.EMPTY_ARRAY,
            code);
}
 
Example 4
Source File: Verifier.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void addStaticMetaClassField(final ClassNode node, final String classInternalName) {
    String _staticClassInfoFieldName = "$staticClassInfo";
    while (node.getDeclaredField(_staticClassInfoFieldName) != null)
        _staticClassInfoFieldName = _staticClassInfoFieldName + "$";
    final String staticMetaClassFieldName = _staticClassInfoFieldName;

    FieldNode staticMetaClassField = node.addField(staticMetaClassFieldName, ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, ClassHelper.make(ClassInfo.class, false), null);
    staticMetaClassField.setSynthetic(true);

    node.addSyntheticMethod(
            "$getStaticMetaClass",
            ACC_PROTECTED,
            ClassHelper.make(MetaClass.class),
            Parameter.EMPTY_ARRAY,
            ClassNode.EMPTY_ARRAY,
            new BytecodeSequence(new BytecodeInstruction() {
                @Override
                public void visit(MethodVisitor mv) {
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
                    if (BytecodeHelper.isClassLiteralPossible(node) || BytecodeHelper.isSameCompilationUnit(classNode, node)) {
                        BytecodeHelper.visitClassLiteral(mv, node);
                    } else {
                        mv.visitMethodInsn(INVOKESTATIC, classInternalName, "$get$$class$" + classInternalName.replace('/', '$'), "()Ljava/lang/Class;", false);
                    }
                    Label l1 = new Label();
                    mv.visitJumpInsn(IF_ACMPEQ, l1);

                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/ScriptBytecodeAdapter", "initMetaClass", "(Ljava/lang/Object;)Lgroovy/lang/MetaClass;", false);
                    mv.visitInsn(ARETURN);

                    mv.visitLabel(l1);

                    mv.visitFieldInsn(GETSTATIC, classInternalName, staticMetaClassFieldName, "Lorg/codehaus/groovy/reflection/ClassInfo;");
                    mv.visitVarInsn(ASTORE, 1);
                    mv.visitVarInsn(ALOAD, 1);
                    Label l0 = new Label();
                    mv.visitJumpInsn(IFNONNULL, l0);

                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
                    mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/reflection/ClassInfo", "getClassInfo", "(Ljava/lang/Class;)Lorg/codehaus/groovy/reflection/ClassInfo;", false);
                    mv.visitInsn(DUP);
                    mv.visitVarInsn(ASTORE, 1);
                    mv.visitFieldInsn(PUTSTATIC, classInternalName, staticMetaClassFieldName, "Lorg/codehaus/groovy/reflection/ClassInfo;");

                    mv.visitLabel(l0);

                    mv.visitVarInsn(ALOAD, 1);
                    mv.visitMethodInsn(INVOKEVIRTUAL, "org/codehaus/groovy/reflection/ClassInfo", "getMetaClass", "()Lgroovy/lang/MetaClass;", false);
                    mv.visitInsn(ARETURN);
                }
            })
    );
}