Java Code Examples for org.codehaus.groovy.ast.expr.MethodCallExpression#setSpreadSafe()

The following examples show how to use org.codehaus.groovy.ast.expr.MethodCallExpression#setSpreadSafe() . 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: AstBuilderTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean handleTargetMethodCallExpression(MethodCallExpression call) {
    ClosureExpression closureExpression = getClosureArgument(call);
    List<Expression> otherArgs = getNonClosureArguments(call);
    String source = convertClosureToSource(closureExpression);

    // parameter order is build(CompilePhase, boolean, String)
    otherArgs.add(new ConstantExpression(source));
    call.setArguments(new ArgumentListExpression(otherArgs));
    call.setMethod(new ConstantExpression("buildFromBlock"));
    call.setSpreadSafe(false);
    call.setSafe(false);
    call.setImplicitThis(false);
    
    return false;
}
 
Example 2
Source File: MacroCallTransformingVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to call given macroMethod
 * @param call MethodCallExpression before the transformation
 * @param macroMethod a macro method candidate
 * @param macroArguments arguments to pass to
 * @return true if call succeeded and current call was transformed
 */
private boolean tryMacroMethod(MethodCallExpression call, ExtensionMethodNode macroMethod, Object[] macroArguments) {
    Expression result = (Expression) InvokerHelper.invokeStaticMethod(
        macroMethod.getExtensionMethodNode().getDeclaringClass().getTypeClass(),
        macroMethod.getName(),
        macroArguments
    );

    if (result == null) {
        // Allow macro methods to return null as an indicator that they didn't match a method call
        return false;
    }

    call.setObjectExpression(MACRO_STUB_INSTANCE);
    call.setMethod(new ConstantExpression(MACRO_STUB_METHOD_NAME));

    // TODO check that we reset everything here
    call.setSpreadSafe(false);
    call.setSafe(false);
    call.setImplicitThis(false);
    call.setArguments(result);
    call.setGenericsTypes(GenericsType.EMPTY_ARRAY);

    return true;
}
 
Example 3
Source File: MethodCallExpressionTransformer.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static MethodCallExpression transformToMopSuperCall(final ClassNode superCallReceiver, final MethodCallExpression expr) {
    MethodNode mn = expr.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
    String mopName = MopWriter.getMopMethodName(mn, false);
    MethodNode direct = new MethodNode(
            mopName,
            ACC_PUBLIC | ACC_SYNTHETIC,
            mn.getReturnType(),
            mn.getParameters(),
            mn.getExceptions(),
            EmptyStatement.INSTANCE
    );
    direct.setDeclaringClass(superCallReceiver);
    MethodCallExpression result = new MethodCallExpression(
            new VariableExpression("this"),
            mopName,
            expr.getArguments()
    );
    result.setImplicitThis(true);
    result.setSpreadSafe(false);
    result.setSafe(false);
    result.setSourcePosition(expr);
    result.setMethodTarget(direct);
    return result;
}
 
Example 4
Source File: GroovyClassFilterTransformer.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected Expression transformMethodCall(MethodCallExpression originalCall, 
		Expression transformedObject, Expression transformedMethod, Expression transformedArgs)
{
	Expression originalObject = originalCall.getObjectExpression();
	if (allowed(originalObject.getType()))
	{
		Expression originalMethod = originalCall.getMethod();
		Expression originalArgs = originalCall.getArguments();
		Expression unwrappedArgs = unwrapTransformedArguments(transformedArgs, originalArgs);
		if (unwrappedArgs != null)
		{
			if (transformedObject.equals(originalObject) 
					&& transformedMethod.equals(originalMethod)
					&& unwrappedArgs.equals(originalArgs))
			{
				if (log.isDebugEnabled())
				{
					log.debug("allowed method call " + originalCall);
				}
				return originalCall;
			}
			
			MethodCallExpression transformedCall = new MethodCallExpression(
					transformedObject, transformedMethod, unwrappedArgs);
			transformedCall.setSafe(originalCall.isSafe());
			transformedCall.setSpreadSafe(originalCall.isSpreadSafe());
			if (log.isDebugEnabled())
			{
				log.debug("transformed method call " + transformedCall);
			}
			return transformedCall;
		}
	}
	
	return super.transformMethodCall(originalCall, 
			transformedObject, transformedMethod, transformedArgs);
}
 
Example 5
Source File: MarkupBuilderCodeTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression tryTransformInclude(final MethodCallExpression exp) {
    Expression arguments = exp.getArguments();
    if (arguments instanceof TupleExpression) {
        List<Expression> expressions = ((TupleExpression) arguments).getExpressions();
        if (expressions.size() == 1 && expressions.get(0) instanceof MapExpression) {
            MapExpression map = (MapExpression) expressions.get(0);
            List<MapEntryExpression> entries = map.getMapEntryExpressions();
            if (entries.size() == 1) {
                MapEntryExpression mapEntry = entries.get(0);
                Expression keyExpression = mapEntry.getKeyExpression();
                try {
                    IncludeType includeType = IncludeType.valueOf(keyExpression.getText().toLowerCase());
                    MethodCallExpression call = new MethodCallExpression(
                            exp.getObjectExpression(),
                            includeType.getMethodName(),
                            new ArgumentListExpression(
                                    mapEntry.getValueExpression()
                            )
                    );
                    call.setImplicitThis(true);
                    call.setSafe(exp.isSafe());
                    call.setSpreadSafe(exp.isSpreadSafe());
                    call.setSourcePosition(exp);
                    return call;
                } catch (IllegalArgumentException e) {
                    // not a valid import type, do not modify the code
                }
            }

        }
    }
    return super.transform(exp);
}
 
Example 6
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 7
Source File: ResolveVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected Expression transformMethodCallExpression(final MethodCallExpression mce) {
    Expression args = transform(mce.getArguments());
    Expression method = transform(mce.getMethod());
    Expression object = transform(mce.getObjectExpression());

    resolveGenericsTypes(mce.getGenericsTypes());

    MethodCallExpression ret = new MethodCallExpression(object, method, args);
    ret.setGenericsTypes(mce.getGenericsTypes());
    ret.setMethodTarget(mce.getMethodTarget());
    ret.setImplicitThis(mce.isImplicitThis());
    ret.setSpreadSafe(mce.isSpreadSafe());
    ret.setSafe(mce.isSafe());
    return ret;
}
 
Example 8
Source File: SuperCallTraitTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformMethodCallExpression(final MethodCallExpression exp) {
    if (isTraitSuperPropertyExpression(exp.getObjectExpression())) {
        Expression objectExpression = exp.getObjectExpression();
        ClassNode traitReceiver = ((PropertyExpression) objectExpression).getObjectExpression().getType();

        if (traitReceiver != null) {
            // (SomeTrait.super).foo() --> SomeTrait$Helper.foo(this)
            ClassExpression receiver = new ClassExpression(
                    getHelper(traitReceiver)
            );
            ArgumentListExpression newArgs = new ArgumentListExpression();
            Expression arguments = exp.getArguments();
            newArgs.addExpression(new VariableExpression("this"));
            if (arguments instanceof TupleExpression) {
                List<Expression> expressions = ((TupleExpression) arguments).getExpressions();
                for (Expression expression : expressions) {
                    newArgs.addExpression(transform(expression));
                }
            } else {
                newArgs.addExpression(transform(arguments));
            }
            MethodCallExpression result = new MethodCallExpression(
                    receiver,
                    transform(exp.getMethod()),
                    newArgs
            );
            result.setImplicitThis(false);
            result.setSpreadSafe(exp.isSpreadSafe());
            result.setSafe(exp.isSafe());
            result.setSourcePosition(exp);
            return result;
        }
    }
    return super.transform(exp);
}
 
Example 9
Source File: TraitReceiverTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformSuperMethodCall(final MethodCallExpression call) {
    String method = call.getMethodAsString();
    if (method == null) {
        throwSuperError(call);
    }

    Expression arguments = transform(call.getArguments());
    ArgumentListExpression superCallArgs = new ArgumentListExpression();
    if (arguments instanceof ArgumentListExpression) {
        ArgumentListExpression list = (ArgumentListExpression) arguments;
        for (Expression expression : list) {
            superCallArgs.addExpression(expression);
        }
    } else {
        superCallArgs.addExpression(arguments);
    }
    MethodCallExpression transformed = new MethodCallExpression(
            weaved,
            Traits.getSuperTraitMethodName(traitClass, method),
            superCallArgs
    );
    transformed.setSourcePosition(call);
    transformed.setSafe(call.isSafe());
    transformed.setSpreadSafe(call.isSpreadSafe());
    transformed.setImplicitThis(false);
    return transformed;
}
 
Example 10
Source File: TraitReceiverTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformMethodCallOnThisFallBack(final MethodCallExpression call,
                                                     final Expression method, final Expression arguments) {
    MethodCallExpression transformed = new MethodCallExpression(
            weaved,
            method,
            transform(arguments)
    );
    transformed.setSourcePosition(call);
    transformed.setSafe(call.isSafe());
    transformed.setSpreadSafe(call.isSpreadSafe());
    transformed.setImplicitThis(false);
    return transformed;
}
 
Example 11
Source File: TraitReceiverTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformMethodCallOnThisInClosure(final MethodCallExpression call) {
    MethodCallExpression transformed = new MethodCallExpression(
            (Expression) call.getReceiver(),
            call.getMethod(),
            transform(call.getArguments())
    );
    transformed.setSourcePosition(call);
    transformed.setSafe(call.isSafe());
    transformed.setSpreadSafe(call.isSpreadSafe());
    transformed.setImplicitThis(call.isImplicitThis());
    return transformed;
}
 
Example 12
Source File: TraitReceiverTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformPrivateMethodCallOnThis(final MethodCallExpression call,
                                                    final Expression arguments, final String methodName) {
    ArgumentListExpression newArgs = createArgumentList(arguments);
    MethodCallExpression transformed = new MethodCallExpression(
            new VariableExpression("this"),
            methodName,
            newArgs
    );
    transformed.setSourcePosition(call);
    transformed.setSafe(call.isSafe());
    transformed.setSpreadSafe(call.isSpreadSafe());
    transformed.setImplicitThis(true);
    return transformed;
}
 
Example 13
Source File: TraitReceiverTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformPrivateMethodCallOnThisInClosure(final MethodCallExpression call,
                                                             final Expression arguments, final String methodName) {
    ArgumentListExpression newArgs = createArgumentList(arguments);
    MethodCallExpression transformed = new MethodCallExpression(
            new ClassExpression(traitHelperClass),
            methodName,
            newArgs
    );
    transformed.setSourcePosition(call);
    transformed.setSafe(call.isSafe());
    transformed.setSpreadSafe(call.isSpreadSafe());
    transformed.setImplicitThis(true);
    return transformed;
}
 
Example 14
Source File: StaticTypesBinaryExpressionMultiTypeDispatcher.java    From groovy with Apache License 2.0 5 votes vote down vote up
private boolean makeSetPrivateFieldWithBridgeMethod(final Expression receiver, final ClassNode receiverType, final String fieldName, final Expression arguments, final boolean safe, final boolean spreadSafe, final boolean implicitThis) {
    FieldNode field = receiverType.getField(fieldName);
    ClassNode outerClass = receiverType.getOuterClass();
    if (field == null && implicitThis && outerClass != null && !receiverType.isStaticClass()) {
        Expression pexp;
        if (controller.isInGeneratedFunction()) {
            MethodCallExpression mce = callThisX("getThisObject");
            mce.setImplicitThis(true);
            mce.setMethodTarget(CLOSURE_GETTHISOBJECT_METHOD);
            mce.putNodeMetaData(INFERRED_TYPE, controller.getOutermostClass());
            pexp = castX(controller.getOutermostClass(), mce);
        } else {
            pexp = propX(classX(outerClass), "this");
            ((PropertyExpression) pexp).setImplicitThis(true);
        }
        pexp.putNodeMetaData(INFERRED_TYPE, outerClass);
        pexp.setSourcePosition(receiver);
        return makeSetPrivateFieldWithBridgeMethod(pexp, outerClass, fieldName, arguments, safe, spreadSafe, true);
    }
    ClassNode classNode = controller.getClassNode();
    if (field != null && field.isPrivate() && !receiverType.equals(classNode)
            && (StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(receiverType, classNode)
                || StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(classNode,receiverType))) {
        Map<String, MethodNode> mutators = receiverType.redirect().getNodeMetaData(StaticCompilationMetadataKeys.PRIVATE_FIELDS_MUTATORS);
        if (mutators != null) {
            MethodNode methodNode = mutators.get(fieldName);
            if (methodNode != null) {
                MethodCallExpression call = callX(receiver, methodNode.getName(), args(field.isStatic() ? nullX() : receiver, arguments));
                call.setImplicitThis(implicitThis);
                call.setMethodTarget(methodNode);
                call.setSafe(safe);
                call.setSpreadSafe(spreadSafe);
                call.visit(controller.getAcg());
                return true;
            }
        }
    }
    return false;
}
 
Example 15
Source File: MarkupBuilderCodeTransformer.java    From groovy with Apache License 2.0 4 votes vote down vote up
private Expression transformMethodCall(final MethodCallExpression exp) {
    String name = exp.getMethodAsString();
    if (exp.isImplicitThis() && "include".equals(name)) {
        return tryTransformInclude(exp);
    } else if (exp.isImplicitThis() && name.startsWith(":")) {
        List<Expression> args;
        if (exp.getArguments() instanceof ArgumentListExpression) {
            args = ((ArgumentListExpression) exp.getArguments()).getExpressions();
        } else {
            args = Collections.singletonList(exp.getArguments());
        }
        Expression newArguments = transform(new ArgumentListExpression(new ConstantExpression(name.substring(1)), new ArrayExpression(ClassHelper.OBJECT_TYPE, args)));
        MethodCallExpression call = new MethodCallExpression(
                new VariableExpression("this"),
                "methodMissing",
                newArguments
        );
        call.setImplicitThis(true);
        call.setSafe(exp.isSafe());
        call.setSpreadSafe(exp.isSpreadSafe());
        call.setSourcePosition(exp);
        return call;
    } else if (name!=null && name.startsWith("$")) {
        MethodCallExpression reformatted = new MethodCallExpression(
                exp.getObjectExpression(),
                name.substring(1),
                exp.getArguments()
        );
        reformatted.setImplicitThis(exp.isImplicitThis());
        reformatted.setSafe(exp.isSafe());
        reformatted.setSpreadSafe(exp.isSpreadSafe());
        reformatted.setSourcePosition(exp);
        // wrap in a stringOf { ... } closure call
        ClosureExpression clos = new ClosureExpression(Parameter.EMPTY_ARRAY, new ExpressionStatement(reformatted));
        clos.setVariableScope(new VariableScope());
        MethodCallExpression stringOf = new MethodCallExpression(new VariableExpression("this"),
                "stringOf",
                clos);
        stringOf.setImplicitThis(true);
        stringOf.setSourcePosition(reformatted);
        return stringOf;
    }
    return super.transform(exp);
}