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

The following examples show how to use org.codehaus.groovy.ast.expr.ConstructorCallExpression#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: MacroClassTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstructorCallExpression(final ConstructorCallExpression call) {
    ClassNode type = call.getType();
    if (type instanceof InnerClassNode) {
        if (((InnerClassNode) type).isAnonymous() &&
                MACROCLASS_TYPE.getNameWithoutPackage().equals(type.getSuperClass().getNameWithoutPackage())) {
            try {
                String source = convertInnerClassToSource(type);

                MethodCallExpression macroCall = callX(
                        propX(classX(ClassHelper.makeWithoutCaching(MacroBuilder.class, false)), "INSTANCE"),
                        MACRO_METHOD,
                        args(
                                constX(source),
                                MacroGroovyMethods.buildSubstitutions(sourceUnit, type),
                                classX(ClassHelper.make(ClassNode.class))
                        )
                );

                macroCall.setSpreadSafe(false);
                macroCall.setSafe(false);
                macroCall.setImplicitThis(false);
                call.putNodeMetaData(MacroTransformation.class, macroCall);
                List<ClassNode> classes = sourceUnit.getAST().getClasses();
                for (Iterator<ClassNode> iterator = classes.iterator(); iterator.hasNext(); ) {
                    final ClassNode aClass = iterator.next();
                    if (aClass == type || type == aClass.getOuterClass()) {
                        iterator.remove();
                    }
                }
            } catch (Exception e) {
                // FIXME
                e.printStackTrace();
            }
            return;
        }
    }
    super.visitConstructorCallExpression(call);

}
 
Example 2
Source File: RangeExpressionTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
public Expression transformRangeExpression(RangeExpression range) {
    final ClassNode inferred = range.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
    if (INTRANGE_TYPE.equals(inferred)) {
        ArgumentListExpression bounds = new ArgumentListExpression(new ConstantExpression(range.isInclusive(),true),range.getFrom(), range.getTo());
        ConstructorCallExpression cce = new ConstructorCallExpression(INTRANGE_TYPE, bounds);
        cce.setSourcePosition(range);
        cce.putNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET, INTRANGE_CTOR);
        cce.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, INTRANGE_TYPE);
        return transformer.transform(cce);
    }
    return transformer.superTransform(range);
}
 
Example 3
Source File: ListExpressionTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformRegularConstructor(final ListExpression expr, final MethodNode target) {
    // can be replaced with a direct constructor call
    List<Expression> transformedArgs = transformArguments(expr);
    ConstructorCallExpression cce = new ConstructorCallExpression(
            target.getDeclaringClass(),
            new ArgumentListExpression(transformedArgs)
    );
    cce.setSourcePosition(expr);
    cce.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, target);
    return cce;
}
 
Example 4
Source File: StaticCompilationVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstructorCallExpression(final ConstructorCallExpression call) {
    super.visitConstructorCallExpression(call);

    if (call.isUsingAnonymousInnerClass() && call.getType().getNodeMetaData(StaticTypeCheckingVisitor.class) != null) {
        ClassNode anonType = call.getType();
        anonType.putNodeMetaData(STATIC_COMPILE_NODE, anonType.getEnclosingMethod().getNodeMetaData(STATIC_COMPILE_NODE));
        anonType.putNodeMetaData(WriterControllerFactory.class, anonType.getOuterClass().getNodeMetaData(WriterControllerFactory.class));
    }

    MethodNode target = call.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
    if (target == null && call.getLineNumber() > 0) {
        addError("Target constructor for constructor call expression hasn't been set", call);
    } else if (target == null) {
        // try to find a target
        ArgumentListExpression argumentListExpression = InvocationWriter.makeArgumentList(call.getArguments());
        List<Expression> expressions = argumentListExpression.getExpressions();
        ClassNode[] args = new ClassNode[expressions.size()];
        for (int i = 0, n = args.length; i < n; i += 1) {
            args[i] = typeChooser.resolveType(expressions.get(i), classNode);
        }
        target = findMethodOrFail(call, call.isSuperCall() ? classNode.getSuperClass() : classNode, "<init>", args);
        call.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, target);
    }
    if (target != null) {
        memorizeInitialExpressions(target);
    }
}