Java Code Examples for org.codehaus.groovy.ast.expr.MethodCallExpression#getObjectExpression()
The following examples show how to use
org.codehaus.groovy.ast.expr.MethodCallExpression#getObjectExpression() .
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: InvocationWriter.java From groovy with Apache License 2.0 | 6 votes |
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 2
Source File: FindMethodUsagesVisitor.java From netbeans with Apache License 2.0 | 6 votes |
@Override public void visitMethodCallExpression(MethodCallExpression methodCall) { Expression expression = methodCall.getObjectExpression(); if (expression instanceof VariableExpression) { VariableExpression variableExpression = ((VariableExpression) expression); Variable variable = variableExpression.getAccessedVariable(); if (variable != null) { if (variable.isDynamicTyped()) { addDynamicVarUsages(methodCall, variable); } else { addStaticVarUsages(methodCall, variable); } } else { addThisUsages(methodCall); } } else if (expression instanceof ConstructorCallExpression) { addConstructorUsages(methodCall, (ConstructorCallExpression) expression); } super.visitMethodCallExpression(methodCall); }
Example 3
Source File: MethodCallExpressionTransformer.java From groovy with Apache License 2.0 | 6 votes |
/** * Identifies a method call expression on {@link DefaultGroovyMethods#is(Object, Object)} and if recognized, transforms it into a {@link CompareIdentityExpression}. * @param call a method call to be transformed * @return null if the method call is not DGM#is, or {@link CompareIdentityExpression} */ private static Expression tryTransformIsToCompareIdentity(MethodCallExpression call) { if (call.isSafe()) return null; MethodNode methodTarget = call.getMethodTarget(); if (methodTarget instanceof ExtensionMethodNode && "is".equals(methodTarget.getName()) && methodTarget.getParameters().length==1) { methodTarget = ((ExtensionMethodNode) methodTarget).getExtensionMethodNode(); ClassNode owner = methodTarget.getDeclaringClass(); if (DGM_CLASSNODE.equals(owner)) { Expression args = call.getArguments(); if (args instanceof ArgumentListExpression) { ArgumentListExpression arguments = (ArgumentListExpression) args; List<Expression> exprs = arguments.getExpressions(); if (exprs.size() == 1) { CompareIdentityExpression cid = new CompareIdentityExpression(call.getObjectExpression(), exprs.get(0)); cid.setSourcePosition(call); return cid; } } } } return null; }
Example 4
Source File: GroovyGradleParser.java From size-analyzer with Apache License 2.0 | 5 votes |
@Override public void visitMethodCallExpression(MethodCallExpression expression) { // get initial parent, parentParent values. String parent = methodCallStack.size() < 1 ? "" : Iterables.getLast(methodCallStack).getMethodAsString(); String parentParent = getParentParent(); methodCallStack.add(expression); if (expression.getArguments() instanceof ArgumentListExpression) { Expression objectExpression = expression.getObjectExpression(); String newParent = getValidParentString(objectExpression); if (newParent != null) { parentParent = parent; parent = newParent; if (objectExpression instanceof PropertyExpression) { String newParentParent = getValidParentString(((PropertyExpression) objectExpression).getObjectExpression()); if (newParentParent != null) { parentParent = newParentParent; } } } checkDslProperty(expression, parent, parentParent); } super.visitMethodCallExpression(expression); methodCallStack.remove(methodCallStack.size() - 1); }
Example 5
Source File: InvocationWriter.java From groovy with Apache License 2.0 | 5 votes |
private static boolean isFunctionInterfaceCall(final MethodCallExpression call) { if ("call".equals(call.getMethodAsString())) { Expression objectExpression = call.getObjectExpression(); if (!AsmClassGenerator.isThisExpression(objectExpression)) { return ClassHelper.isFunctionalInterface(objectExpression.getType()); } } return false; }
Example 6
Source File: GroovyClassFilterTransformer.java From jasperreports with GNU Lesser General Public License v3.0 | 5 votes |
@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 7
Source File: GroovyClassFilterTransformer.java From jasperreports with GNU Lesser General Public License v3.0 | 5 votes |
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 8
Source File: MarkupBuilderCodeTransformer.java From groovy with Apache License 2.0 | 5 votes |
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 9
Source File: AstBuilderTransformation.java From groovy with Apache License 2.0 | 5 votes |
/** * 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 10
Source File: SecureASTCustomizer.java From groovy with Apache License 2.0 | 5 votes |
@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 11
Source File: SuperCallTraitTransformer.java From groovy with Apache License 2.0 | 5 votes |
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 12
Source File: AstUtils.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 4 votes |
public static boolean targetIsThis(MethodCallExpression call) { Expression target = call.getObjectExpression(); return target instanceof VariableExpression && target.getText().equals("this"); }
Example 13
Source File: NewifyASTTransformation.java From groovy with Apache License 2.0 | 4 votes |
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: NewifyASTTransformation.java From groovy with Apache License 2.0 | 4 votes |
private boolean isNewifyCandidate(MethodCallExpression mce) { return (auto && isNewMethodStyle(mce)) || (mce.isImplicitThis() && mce.getObjectExpression() instanceof VariableExpression && ((VariableExpression) mce.getObjectExpression()).isThisExpression()); }
Example 15
Source File: MarkupBuilderCodeTransformer.java From groovy with Apache License 2.0 | 4 votes |
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); }
Example 16
Source File: GroovyDeclarationFinder.java From netbeans with Apache License 2.0 | 4 votes |
private IndexedMethod findBestMethodMatchHelper(String name, Set<IndexedMethod> methods, BaseDocument doc, int astOffset, int lexOffset, AstPath path, ASTNode callNode, GroovyIndex index) { Set<IndexedMethod> candidates = new HashSet<IndexedMethod>(); if(path == null) { return null; } ASTNode parent = path.leafParent(); if (callNode instanceof ConstantExpression && parent instanceof MethodCallExpression) { String fqn = null; MethodCallExpression methodCall = (MethodCallExpression) parent; Expression objectExpression = methodCall.getObjectExpression(); if (objectExpression instanceof VariableExpression) { VariableExpression variable = (VariableExpression) objectExpression; if ("this".equals(variable.getName())) { // NOI18N fqn = ASTUtils.getFqnName(path); } else { fqn = variable.getType().getName(); } } if (fqn != null) { for (IndexedMethod method : methods) { if (fqn.equals(method.getIn())) { candidates.add(method); } } } } if (candidates.size() == 1) { return candidates.iterator().next(); } else if (!candidates.isEmpty()) { methods = candidates; } return null; }
Example 17
Source File: PathFinderVisitor.java From netbeans with Apache License 2.0 | 4 votes |
private void fixNode(ASTNode node) { // FIXME http://jira.codehaus.org/browse/GROOVY-3263 if (node instanceof MethodCallExpression && !((MethodCallExpression) node).isImplicitThis()) { MethodCallExpression call = (MethodCallExpression) node; if (call.getObjectExpression() == VariableExpression.THIS_EXPRESSION || call.getObjectExpression() == VariableExpression.SUPER_EXPRESSION) { // this is not bulletproof but fix most of the problems VariableExpression var = new VariableExpression( call.getObjectExpression() == VariableExpression.THIS_EXPRESSION ? "this" : "super", // NOI18N call.getObjectExpression().getType()); // NOI18N var.setLineNumber(call.getLineNumber()); var.setColumnNumber(call.getColumnNumber()); var.setLastLineNumber(call.getMethod().getLineNumber()); var.setLastColumnNumber(call.getMethod().getColumnNumber()); call.setObjectExpression(var); } // FIXME http://jira.codehaus.org/browse/GROOVY-3472 } else if (node instanceof MethodNode || node instanceof ClosureExpression) { Statement code = null; if (node instanceof MethodNode) { code = ((MethodNode) node).getCode(); } else { code = ((ClosureExpression) node).getCode(); } if (code != null && code instanceof BlockStatement && ((code.getLineNumber() < 0 && code.getColumnNumber() < 0) || (code.getLastLineNumber() < 0 && code.getLastColumnNumber() < 0))) { BlockStatement block = (BlockStatement) code; List statements = block.getStatements(); if (statements != null && !statements.isEmpty()) { if (code.getLineNumber() < 0 && code.getColumnNumber() < 0) { Statement first = (Statement) statements.get(0); code.setLineNumber(first.getLineNumber()); code.setColumnNumber(first.getColumnNumber()); } if (code.getLastLineNumber() < 0 && code.getLastColumnNumber() < 0) { // maybe not accurate code.setLastLineNumber(node.getLastLineNumber()); int lastColumn = node.getLastColumnNumber(); if (lastColumn > 0) { lastColumn--; } code.setLastColumnNumber(lastColumn); } } } } }
Example 18
Source File: AstUtils.java From pushfish-android with BSD 2-Clause "Simplified" License | 4 votes |
public static boolean targetIsThis(MethodCallExpression call) { Expression target = call.getObjectExpression(); return target instanceof VariableExpression && target.getText().equals("this"); }