Java Code Examples for org.codehaus.groovy.ast.ClassHelper#isGeneratedFunction()

The following examples show how to use org.codehaus.groovy.ast.ClassHelper#isGeneratedFunction() . 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: InvocationWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void makeCall(final Expression origin, final Expression receiver, final Expression message, final Expression arguments, final MethodCallerMultiAdapter adapter, boolean safe, final boolean spreadSafe, boolean implicitThis) {
    ClassNode sender = controller.getClassNode();
    if (AsmClassGenerator.isSuperExpression(receiver) || (AsmClassGenerator.isThisExpression(receiver) && !implicitThis)) {
        while (ClassHelper.isGeneratedFunction(sender)) {
            sender = sender.getOuterClass();
        }
        if (AsmClassGenerator.isSuperExpression(receiver)) {
            sender = sender.getSuperClass(); // GROOVY-4035
            implicitThis = false; // prevent recursion
            safe = false; // GROOVY-6045
        }
    }

    makeCall(origin, new ClassExpression(sender), receiver, message, arguments, adapter, safe, spreadSafe, implicitThis);
}
 
Example 2
Source File: StaticTypesWriterController.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void updateStaticCompileFlag(final MethodNode mn) {
    ClassNode classNode = getClassNode();
    AnnotatedNode node = mn;
    boolean implementsGeneratedClosureOrGeneratedLambdaInterface = ClassHelper.isGeneratedFunction(classNode);
    if (implementsGeneratedClosureOrGeneratedLambdaInterface) {
        node = classNode.getOuterClass();
    }

    boolean isStaticCompileNode = classNode.getNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE) != null;
    isInStaticallyCheckedMethod =
            mn != null && (StaticCompilationVisitor.isStaticallyCompiled(node)
                            || implementsGeneratedClosureOrGeneratedLambdaInterface && isStaticCompileNode);
}
 
Example 3
Source File: MopWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void createMopMethods() {
    ClassNode classNode = controller.getClassNode();
    if (ClassHelper.isGeneratedFunction(classNode)) {
        return;
    }
    Set<MopKey> currentClassSignatures = classNode.getMethods().stream()
            .map(mn -> new MopKey(mn.getName(), mn.getParameters())).collect(Collectors.toSet());
    visitMopMethodList(classNode.getMethods(), true, Collections.emptySet(), Collections.emptyList());
    visitMopMethodList(classNode.getSuperClass().getAllDeclaredMethods(), false, currentClassSignatures, controller.getSuperMethodNames());
}
 
Example 4
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void visitAttributeOrProperty(final PropertyExpression pexp, final MethodCallerMultiAdapter adapter) {
    ClassNode classNode = controller.getClassNode();
    String propertyName = pexp.getPropertyAsString();
    Expression objectExpression = pexp.getObjectExpression();

    if (objectExpression instanceof ClassExpression && "this".equals(propertyName)) {
        // we have something like A.B.this, and need to make it
        // into this.this$0.this$0, where this.this$0 returns
        // A.B and this.this$0.this$0 return A.
        ClassNode type = objectExpression.getType();
        if (controller.getCompileStack().isInSpecialConstructorCall() && type.equals(classNode.getOuterClass())) {
            // Outer.this in a special constructor call
            ConstructorNode ctor = controller.getConstructorNode();
            Expression receiver = !classNode.isStaticClass() ? new VariableExpression(ctor.getParameters()[0]) : new ClassExpression(type);
            receiver.setSourcePosition(pexp);
            receiver.visit(this);
            return;
        }

        MethodVisitor mv = controller.getMethodVisitor();
        mv.visitVarInsn(ALOAD, 0);
        ClassNode iterType = classNode;
        while (!iterType.equals(type)) {
            String ownerName = BytecodeHelper.getClassInternalName(iterType);
            if (iterType.getOuterClass() == null) break;
            FieldNode thisField = iterType.getField("this$0");
            iterType = iterType.getOuterClass();
            if (thisField == null) {
                // closure within inner class
                while (ClassHelper.isGeneratedFunction(iterType)) {
                    // GROOVY-8881: cater for closures within closures - getThisObject is already outer class of all closures
                    iterType = iterType.getOuterClass();
                }
                mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ClassHelper.CLOSURE_TYPE), "getThisObject", "()Ljava/lang/Object;", false);
                mv.visitTypeInsn(CHECKCAST, BytecodeHelper.getClassInternalName(iterType));
            } else {
                ClassNode thisFieldType = thisField.getType();
                if (ClassHelper.CLOSURE_TYPE.equals(thisFieldType)) {
                    mv.visitFieldInsn(GETFIELD, ownerName, "this$0", BytecodeHelper.getTypeDescription(ClassHelper.CLOSURE_TYPE));
                    mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ClassHelper.CLOSURE_TYPE), "getThisObject", "()Ljava/lang/Object;", false);
                    mv.visitTypeInsn(CHECKCAST, BytecodeHelper.getClassInternalName(iterType));
                } else {
                    String typeName = BytecodeHelper.getTypeDescription(iterType);
                    mv.visitFieldInsn(GETFIELD, ownerName, "this$0", typeName);
                }
            }
        }
        controller.getOperandStack().push(type);
        return;
    }

    if (propertyName != null) {
        // TODO: spread safe should be handled inside
        if (adapter == getProperty && !pexp.isSpreadSafe()) {
            controller.getCallSiteWriter().makeGetPropertySite(objectExpression, propertyName, pexp.isSafe(), pexp.isImplicitThis());
        } else if (adapter == getGroovyObjectProperty && !pexp.isSpreadSafe()) {
            controller.getCallSiteWriter().makeGroovyObjectGetPropertySite(objectExpression, propertyName, pexp.isSafe(), pexp.isImplicitThis());
        } else {
            controller.getCallSiteWriter().fallbackAttributeOrPropertySite(pexp, objectExpression, propertyName, adapter);
        }
    } else {
        controller.getCallSiteWriter().fallbackAttributeOrPropertySite(pexp, objectExpression, null, adapter);
    }
}
 
Example 5
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitVariableExpression(final VariableExpression expression) {
    final String variableName = expression.getName();

    if (expression.isThisExpression()) {
        // "this" in static context is Class instance
        if (controller.isStaticMethod() || controller.getCompileStack().isInSpecialConstructorCall()
                || (!controller.getCompileStack().isImplicitThis() && controller.isStaticContext())) {
            ClassNode thisType = controller.getClassNode();
            if (controller.isInGeneratedFunction()) {
                do { thisType = thisType.getOuterClass();
                } while (ClassHelper.isGeneratedFunction(thisType));
            }
            classX(thisType).visit(this);
        } else {
            loadThis(expression);
        }
        return;
    }

    if (expression.isSuperExpression()) {
        // "super" in static context is Class instance
        if (controller.isStaticMethod()) {
            ClassNode superType = controller.getClassNode().getSuperClass();
            classX(superType).visit(this);
        } else {
            loadThis(expression);
        }
        return;
    }

    BytecodeVariable variable = controller.getCompileStack().getVariable(variableName, false);
    if (variable != null) {
        controller.getOperandStack().loadOrStoreVariable(variable, expression.isUseReferenceDirectly());
    } else if (passingParams && controller.isInScriptBody()) {
        MethodVisitor mv = controller.getMethodVisitor();
        mv.visitTypeInsn(NEW, "org/codehaus/groovy/runtime/ScriptReference");
        mv.visitInsn(DUP);
        loadThisOrOwner();
        mv.visitLdcInsn(variableName);
        mv.visitMethodInsn(INVOKESPECIAL, "org/codehaus/groovy/runtime/ScriptReference", "<init>", "(Lgroovy/lang/Script;Ljava/lang/String;)V", false);
    } else {
        PropertyExpression pexp = thisPropX(true, variableName);
        pexp.getObjectExpression().setSourcePosition(expression);
        pexp.getProperty().setSourcePosition(expression);
        pexp.visit(this);
    }

    if (!controller.getCompileStack().isLHS()) {
        controller.getAssertionWriter().record(expression);
    }
}