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

The following examples show how to use org.codehaus.groovy.ast.expr.MethodCallExpression#getMethod() . 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: TraitReceiverTransformer.java    From groovy with Apache License 2.0 6 votes vote down vote up
private Expression transformMethodCallOnThis(final MethodCallExpression call) {
    Expression method = call.getMethod();
    Expression arguments = call.getArguments();
    if (method instanceof ConstantExpression) {
        String methodName = method.getText();
        List<MethodNode> methods = traitClass.getMethods(methodName);
        for (MethodNode methodNode : methods) {
            if (methodName.equals(methodNode.getName()) && methodNode.isPrivate()) {
                if (inClosure) {
                    return transformPrivateMethodCallOnThisInClosure(call, arguments, methodName);
                }
                return transformPrivateMethodCallOnThis(call, arguments, methodName);
            }
        }
    }

    if (inClosure) {
        return transformMethodCallOnThisInClosure(call);
    }

    return transformMethodCallOnThisFallBack(call, method, arguments);

}
 
Example 2
Source File: InvocationWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void writeInvokeMethod(MethodCallExpression call) {
    if (isClosureCall(call)) {
        // let's invoke the closure method
        invokeClosure(call.getArguments(), call.getMethodAsString());
    } else {
        if (isFunctionInterfaceCall(call)) {
            call = transformToRealMethodCall(call);
        }
        MethodCallerMultiAdapter adapter = invokeMethod;
        Expression objectExpression = call.getObjectExpression();
        if (AsmClassGenerator.isSuperExpression(objectExpression)) {
            adapter = invokeMethodOnSuper;
        } else if (AsmClassGenerator.isThisExpression(objectExpression)) {
            adapter = invokeMethodOnCurrent;
        }
        if (isStaticInvocation(call)) {
            adapter = invokeStaticMethod;
        }
        Expression messageName = new CastExpression(ClassHelper.STRING_TYPE, call.getMethod());
        makeCall(call, objectExpression, messageName, call.getArguments(), adapter, call.isSafe(), call.isSpreadSafe(), call.isImplicitThis());
    }
}
 
Example 3
Source File: PluginsAndBuildscriptTransformer.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private ScriptBlock detectScriptBlock(Statement statement) {
    if (!(statement instanceof ExpressionStatement)) {
        return null;
    }

    ExpressionStatement expressionStatement = (ExpressionStatement) statement;
    if (!(expressionStatement.getExpression() instanceof MethodCallExpression)) {
        return null;
    }

    MethodCallExpression methodCall = (MethodCallExpression) expressionStatement.getExpression();
    if (!AstUtils.targetIsThis(methodCall)) {
        return null;
    }

    if (!(methodCall.getMethod() instanceof ConstantExpression)) {
        return null;
    }

    String methodName = methodCall.getMethod().getText();

    if (methodName.equals(PLUGINS) || methodName.equals(classpathBlockName)) {
        if (!(methodCall.getArguments() instanceof ArgumentListExpression)) {
            return null;
        }

        ArgumentListExpression args = (ArgumentListExpression) methodCall.getArguments();
        if (args.getExpressions().size() == 1 && args.getExpression(0) instanceof ClosureExpression) {
            return new ScriptBlock(methodName, (ClosureExpression) args.getExpression(0));
        } else {
            return null;
        }
    } else {
        return null;
    }
}
 
Example 4
Source File: Methods.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean isSameMethod(ExecutableElement javaMethod, MethodCallExpression methodCall) {
    ConstantExpression methodName = (ConstantExpression) methodCall.getMethod();
    if (javaMethod.getSimpleName().contentEquals(methodName.getText())) {
        // not comparing parameter types for now, only their count
        // is it even possible to make some check for parameter types?
        if (getParameterCount(methodCall) == javaMethod.getParameters().size()) {
            return true;
        }
    }
    return false;
}
 
Example 5
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 6
Source File: GroovyClassFilterTransformer.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected Expression unwrapTransformedArguments(Expression transformedArgs, 
		Expression originalArgs)
{
	if (!(transformedArgs instanceof MethodCallExpression))
	{
		return null;
	}
	
	MethodCallExpression transformedArgsCall = (MethodCallExpression) transformedArgs;
	Expression transformedObject = transformedArgsCall.getObjectExpression();
	Expression transformedMethod = transformedArgsCall.getMethod();
	if (!(transformedObject instanceof ListExpression) 
			|| !(transformedMethod instanceof ConstantExpression)
			|| !("toArray".equals(transformedMethod.getText())))
	{
		return null;
	}
	
	List<Expression> transformedExpressions = ((ListExpression) transformedObject).getExpressions();
	if (originalArgs instanceof ArgumentListExpression)
	{
		List<Expression> originalExpressions = ((ArgumentListExpression) originalArgs).getExpressions();
		if (transformedExpressions.equals(originalExpressions))
		{
			return originalArgs;
		}
		
		return new ArgumentListExpression(transformedExpressions);
	}
	
	return null;
}
 
Example 7
Source File: AstBuilderTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Looks for method calls on the AstBuilder class called build that take
 * a Closure as parameter. This is all needed b/c build is overloaded.
 *
 * @param call the method call expression, may not be null
 */
@Override
protected boolean isBuildInvocation(MethodCallExpression call) {
    if (call == null) throw new IllegalArgumentException("Null: call");

    // is method name correct?
    if (call.getMethod() instanceof ConstantExpression && "buildFromCode".equals(((ConstantExpression) call.getMethod()).getValue())) {

        // is method object correct type?
        if (call.getObjectExpression() != null && call.getObjectExpression().getType() != null) {
            String name = call.getObjectExpression().getType().getName();
            if (name != null && !name.isEmpty() && factoryTargets.contains(name)) {

                // is one of the arguments a closure?
                if (call.getArguments() != null && call.getArguments() instanceof TupleExpression) {
                    if (((TupleExpression) call.getArguments()).getExpressions() != null) {
                        for (ASTNode node : ((TupleExpression) call.getArguments()).getExpressions()) {
                            if (node instanceof ClosureExpression) {
                                return true;
                            }
                        }
                    }
                }
            }
        }
    }
    return false;
}
 
Example 8
Source File: SecureASTCustomizer.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethodCallExpression(final MethodCallExpression call) {
    assertExpressionAuthorized(call);
    Expression receiver = call.getObjectExpression();
    final String typeName = receiver.getType().getName();
    if (allowedReceivers != null && !allowedReceivers.contains(typeName)) {
        throw new SecurityException("Method calls not allowed on [" + typeName + "]");
    } else if (disallowedReceivers != null && disallowedReceivers.contains(typeName)) {
        throw new SecurityException("Method calls not allowed on [" + typeName + "]");
    }
    receiver.visit(this);
    final Expression method = call.getMethod();
    checkConstantTypeIfNotMethodNameOrProperty(method);
    call.getArguments().visit(this);
}
 
Example 9
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 10
Source File: VariableScopeVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethodCallExpression(final MethodCallExpression expression) {
    if (expression.isImplicitThis() && expression.getMethod() instanceof ConstantExpression) {
        ConstantExpression methodNameConstant = (ConstantExpression) expression.getMethod();
        String methodName = methodNameConstant.getText();

        if (methodName == null) {
            throw new GroovyBugError("method name is null");
        }

        Variable variable = findVariableDeclaration(methodName);
        if (variable != null && !(variable instanceof DynamicVariable)) {
            checkVariableContextAccess(variable, expression);
        }

        if (variable instanceof VariableExpression || variable instanceof Parameter) {
            VariableExpression object = new VariableExpression(variable);
            object.setSourcePosition(methodNameConstant);
            expression.setObjectExpression(object);
            ConstantExpression method = new ConstantExpression("call");
            method.setSourcePosition(methodNameConstant); // important for GROOVY-4344
            expression.setImplicitThis(false);
            expression.setMethod(method);
        }
    }
    super.visitMethodCallExpression(expression);
}
 
Example 11
Source File: PluginsAndBuildscriptTransformer.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private ScriptBlock detectScriptBlock(Statement statement) {
    if (!(statement instanceof ExpressionStatement)) {
        return null;
    }

    ExpressionStatement expressionStatement = (ExpressionStatement) statement;
    if (!(expressionStatement.getExpression() instanceof MethodCallExpression)) {
        return null;
    }

    MethodCallExpression methodCall = (MethodCallExpression) expressionStatement.getExpression();
    if (!AstUtils.targetIsThis(methodCall)) {
        return null;
    }

    if (!(methodCall.getMethod() instanceof ConstantExpression)) {
        return null;
    }

    String methodName = methodCall.getMethod().getText();

    if (methodName.equals(PLUGINS) || methodName.equals(classpathBlockName)) {
        if (!(methodCall.getArguments() instanceof ArgumentListExpression)) {
            return null;
        }

        ArgumentListExpression args = (ArgumentListExpression) methodCall.getArguments();
        if (args.getExpressions().size() == 1 && args.getExpression(0) instanceof ClosureExpression) {
            return new ScriptBlock(methodName, (ClosureExpression) args.getExpression(0));
        } else {
            return null;
        }
    } else {
        return null;
    }
}
 
Example 12
Source File: AstUtils.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static boolean isMethodOnThis(MethodCallExpression call, String name) {
    boolean hasName = call.getMethod() instanceof ConstantExpression && call.getMethod().getText().equals(name);
    return hasName && targetIsThis(call);
}
 
Example 13
Source File: NewifyASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static boolean isNewMethodStyle(MethodCallExpression mce) {
    final Expression obj = mce.getObjectExpression();
    final Expression meth = mce.getMethod();
    return (obj instanceof ClassExpression && meth instanceof ConstantExpression
            && ((ConstantExpression) meth).getValue().equals("new"));
}
 
Example 14
Source File: AstUtils.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static boolean isMethodOnThis(MethodCallExpression call, String name) {
    boolean hasName = call.getMethod() instanceof ConstantExpression && call.getMethod().getText().equals(name);
    return hasName && targetIsThis(call);
}