Java Code Examples for org.codehaus.groovy.ast.MethodNode#putNodeMetaData()

The following examples show how to use org.codehaus.groovy.ast.MethodNode#putNodeMetaData() . 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: AbstractTypeCheckingExtension.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Used to instruct the type checker that the call is a dynamic method call.
 * Calling this method automatically sets the handled flag to true.
 * @param call the method call which is a dynamic method call
 * @param returnType the expected return type of the dynamic call
 * @return a virtual method node with the same name as the expected call
 */
public MethodNode makeDynamic(MethodCall call, ClassNode returnType) {
    TypeCheckingContext.EnclosingClosure enclosingClosure = context.getEnclosingClosure();
    MethodNode enclosingMethod = context.getEnclosingMethod();
    ((ASTNode)call).putNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION, returnType);
    if (enclosingClosure!=null) {
        enclosingClosure.getClosureExpression().putNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION, Boolean.TRUE);
    } else {
        enclosingMethod.putNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION, Boolean.TRUE);
    }
    setHandled(true);
    if (debug) {
        LOG.info("Turning "+call.getText()+" into a dynamic method call returning "+returnType.toString(false));
    }
    return new MethodNode(call.getMethodAsString(), 0, returnType, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, EmptyStatement.INSTANCE);
}
 
Example 2
Source File: StaticTypesClosureWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
protected ClassNode createClosureClass(final ClosureExpression expression, final int mods) {
    ClassNode closureClass = super.createClosureClass(expression, mods);
    List<MethodNode> methods = closureClass.getDeclaredMethods("call");
    List<MethodNode> doCall = closureClass.getMethods("doCall");
    if (doCall.size() != 1) {
        throw new GroovyBugError("Expected to find one (1) doCall method on generated closure, but found " + doCall.size());
    }
    MethodNode doCallMethod = doCall.get(0);
    if (methods.isEmpty() && doCallMethod.getParameters().length == 1) {
        createDirectCallMethod(closureClass, doCallMethod);
    }
    MethodTargetCompletionVisitor visitor = new MethodTargetCompletionVisitor(doCallMethod);
    Object dynamic = expression.getNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION);
    if (dynamic != null) {
        doCallMethod.putNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION, dynamic);
    }
    for (MethodNode method : methods) {
        visitor.visitMethod(method);
    }
    closureClass.putNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE, Boolean.TRUE);
    return closureClass;
}
 
Example 3
Source File: StaticTypesLambdaWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
private MethodNode addSyntheticLambdaMethodNode(final LambdaExpression expression, final ClassNode lambdaClass, final MethodNode abstractMethod) {
    Parameter[] parametersWithExactType = createParametersWithExactType(expression);
    Parameter[] localVariableParameters = getLambdaSharedVariables(expression);
    removeInitialValues(localVariableParameters);

    MethodNode doCallMethod = lambdaClass.addMethod(
            "doCall",
            ACC_PUBLIC,
            abstractMethod.getReturnType(),
            Arrays.copyOf(parametersWithExactType, parametersWithExactType.length),
            ClassNode.EMPTY_ARRAY,
            expression.getCode()
    );
    doCallMethod.putNodeMetaData(ORIGINAL_PARAMETERS_WITH_EXACT_TYPE, parametersWithExactType);
    expression.putNodeMetaData(LAMBDA_SHARED_VARIABLES, localVariableParameters);
    doCallMethod.setSourcePosition(expression);

    return doCallMethod;
}
 
Example 4
Source File: StaticCompilationVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethod(final MethodNode node) {
    if (isSkipMode(node)) {
        node.putNodeMetaData(STATIC_COMPILE_NODE, Boolean.FALSE);
    }
    super.visitMethod(node);
    checkForConstructorWithCSButClassWithout(node);
    if (isStaticallyCompiled(node)) {
        ClassNode declaringClass = node.getDeclaringClass();
        addDynamicOuterClassAccessorsCallback(declaringClass);
    }
}
 
Example 5
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);
    }
}