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

The following examples show how to use org.codehaus.groovy.ast.ClassNode#getNodeMetaData() . 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: AnnotationCollectorTransform.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static List<AnnotationNode> getMeta(ClassNode cn) {
    List<AnnotationNode> meta = cn.getNodeMetaData(AnnotationCollector.class);
    if (meta == null) {
        if (cn.isPrimaryClassNode()) {
            meta = getTargetListFromAnnotations(cn);
        } else {
            meta = getTargetListFromClass(cn);
        }
        cn.setNodeMetaData(AnnotationCollector.class, meta);
    }
    return meta;
}
 
Example 2
Source File: StaticCompilationVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void addDynamicOuterClassAccessorsCallback(final ClassNode outer) {
    if (outer != null) {
        if (!isStaticallyCompiled(outer) && outer.getNodeMetaData(DYNAMIC_OUTER_NODE_CALLBACK) == null) {
            outer.putNodeMetaData(DYNAMIC_OUTER_NODE_CALLBACK, (IPrimaryClassNodeOperation) (source, context, classNode) -> {
                if (classNode == outer) {
                    addPrivateBridgeMethods(classNode);
                    addPrivateFieldsAccessors(classNode);
                }
            });
        }
        // GROOVY-9328: apply to outer classes
        addDynamicOuterClassAccessorsCallback(outer.getOuterClass());
    }
}
 
Example 3
Source File: TraitASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void resolveHelperClassIfNecessary(final ClassNode helperClassNode) {
    if (helperClassNode == null) {
        return;
    }
    for (ClassNode cNode : unit.getAST().getClasses()) {
        ClassNode unresolvedHelperNode = cNode.getNodeMetaData(UNRESOLVED_HELPER_CLASS);
        if (unresolvedHelperNode != null
                && unresolvedHelperNode.getName().equals(helperClassNode.getName())) {
            unresolvedHelperNode.setRedirect(helperClassNode);
        }
    }
}
 
Example 4
Source File: Verifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void addFastPathHelperFieldsAndHelperMethod(ClassNode node, final String classInternalName, boolean knownSpecialCase) {
    if (node.getNodeMetaData(ClassNodeSkip.class) != null) return;
    FieldNode stMCB = checkFieldDoesNotExist(node, STATIC_METACLASS_BOOL);
    if (stMCB == null) {
        stMCB = node.addField(
                STATIC_METACLASS_BOOL,
                ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC | ACC_TRANSIENT,
                ClassHelper.boolean_TYPE, null);
        stMCB.setSynthetic(true);
    }
}
 
Example 5
Source File: StaticInvocationWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
private boolean tryPrivateMethod(final MethodNode target, final boolean implicitThis, final Expression receiver, final TupleExpression args, final ClassNode classNode) {
    ClassNode declaringClass = target.getDeclaringClass();
    if ((isPrivateBridgeMethodsCallAllowed(declaringClass, classNode) || isPrivateBridgeMethodsCallAllowed(classNode, declaringClass))
            && declaringClass.getNodeMetaData(StaticCompilationMetadataKeys.PRIVATE_BRIDGE_METHODS) != null
            && !declaringClass.equals(classNode)) {
        if (tryBridgeMethod(target, receiver, implicitThis, args, classNode)) {
            return true;
        } else {
            checkAndAddCannotCallPrivateMethodError(target, receiver, classNode, declaringClass);
        }
    }
    checkAndAddCannotCallPrivateMethodError(target, receiver, classNode, declaringClass);
    return false;
}
 
Example 6
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 7
Source File: StaticCompilationMopWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void createMopMethods() {
    ClassNode classNode = controller.getClassNode();
    LinkedList<MethodNode> requiredMopMethods = classNode.getNodeMetaData(StaticTypesMarker.SUPER_MOP_METHOD_REQUIRED);
    if (requiredMopMethods!=null) {
        generateMopCalls(requiredMopMethods, false);
    }
}
 
Example 8
Source File: StaticCompileTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(final ASTNode[] nodes, final SourceUnit source) {
    AnnotationNode annotationInformation = (AnnotationNode) nodes[0];
    AnnotatedNode node = (AnnotatedNode) nodes[1];
    StaticTypeCheckingVisitor visitor = null;
    Map<String,Expression> members = annotationInformation.getMembers();
    Expression extensions = members.get("extensions");
    if (node instanceof ClassNode) {
        ClassNode classNode = (ClassNode) node;
        visitor = newVisitor(source, classNode);
        visitor.setCompilationUnit(compilationUnit);
        addTypeCheckingExtensions(visitor, extensions);
        classNode.putNodeMetaData(WriterControllerFactory.class, factory);
        node.putNodeMetaData(STATIC_COMPILE_NODE, !visitor.isSkipMode(node));
        visitor.initialize();
        visitor.visitClass(classNode);
    } else if (node instanceof MethodNode) {
        MethodNode methodNode = (MethodNode) node;
        ClassNode declaringClass = methodNode.getDeclaringClass();
        visitor = newVisitor(source, declaringClass);
        visitor.setCompilationUnit(compilationUnit);
        addTypeCheckingExtensions(visitor, extensions);
        methodNode.putNodeMetaData(STATIC_COMPILE_NODE, !visitor.isSkipMode(node));
        if (declaringClass.getNodeMetaData(WriterControllerFactory.class) == null) {
            declaringClass.putNodeMetaData(WriterControllerFactory.class, factory);
        }
        visitor.setMethodsToBeVisited(Collections.singleton(methodNode));
        visitor.initialize();
        visitor.visitMethod(methodNode);
    } else {
        source.addError(new SyntaxException(STATIC_ERROR_PREFIX + "Unimplemented node type",
                node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()));
    }
    if (visitor != null) {
        visitor.performSecondPass();
    }
    StaticCompilationTransformer transformer = new StaticCompilationTransformer(source, visitor);
    if (node instanceof ClassNode) {
        transformer.visitClass((ClassNode) node);
    } else if (node instanceof MethodNode) {
        transformer.visitMethod((MethodNode) node);
    }
}
 
Example 9
Source File: OptimizingStatementWriter.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static void setNodeMeta(final TypeChooser chooser, final ClassNode classNode) {
    if (classNode.getNodeMetaData(ClassNodeSkip.class) != null) return;
    new OptVisitor(chooser).visitClass(classNode);
}
 
Example 10
Source File: StaticInvocationWriter.java    From groovy with Apache License 2.0 4 votes vote down vote up
/**
 * Attempts to make a direct method call on a bridge method, if it exists.
 */
protected boolean tryBridgeMethod(final MethodNode target, final Expression receiver, final boolean implicitThis, final TupleExpression args, final ClassNode thisClass) {
    ClassNode lookupClassNode;
    if (target.isProtected()) {
        lookupClassNode = controller.getClassNode();
        while (lookupClassNode != null && !lookupClassNode.isDerivedFrom(target.getDeclaringClass())) {
            lookupClassNode = lookupClassNode.getOuterClass();
        }
        if (lookupClassNode == null) {
            return false;
        }
    } else {
        lookupClassNode = target.getDeclaringClass().redirect();
    }
    Map<MethodNode, MethodNode> bridges = lookupClassNode.getNodeMetaData(StaticCompilationMetadataKeys.PRIVATE_BRIDGE_METHODS);
    MethodNode bridge = bridges == null ? null : bridges.get(target);
    if (bridge != null) {
        Expression fixedReceiver = receiver;
        if (implicitThis) {
            if (!controller.isInGeneratedFunction()) {
                fixedReceiver = propX(classX(lookupClassNode), "this");
            } else if (thisClass != null) {
                ClassNode current = thisClass.getOuterClass();
                fixedReceiver = varX("thisObject", current);
                // adjust for multiple levels of nesting if needed
                while (current.getOuterClass() != null && !lookupClassNode.equals(current)) {
                    FieldNode thisField = current.getField("this$0");
                    current = current.getOuterClass();
                    if (thisField != null) {
                        fixedReceiver = propX(fixedReceiver, "this$0");
                        fixedReceiver.setType(current);
                    }
                }
            }
        }
        ArgumentListExpression newArgs = args(target.isStatic() ? nullX() : fixedReceiver);
        for (Expression expression : args.getExpressions()) {
            newArgs.addExpression(expression);
        }
        return writeDirectMethodCall(bridge, implicitThis, fixedReceiver, newArgs);
    }
    return false;
}