org.codehaus.groovy.ast.expr.StaticMethodCallExpression Java Examples

The following examples show how to use org.codehaus.groovy.ast.expr.StaticMethodCallExpression. 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: PathFinderVisitor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean isInSource(ASTNode node) {
    if (node instanceof AnnotatedNode) {
        if (((AnnotatedNode) node).hasNoRealSourcePosition()) {
            return false;
        }
    }

    // FIXME probably http://jira.codehaus.org/browse/GROOVY-3263
    if (node instanceof StaticMethodCallExpression && node.getLineNumber() == -1
            && node.getLastLineNumber() == -1 && node.getColumnNumber() == -1
            && node.getLastColumnNumber() == -1) {

        StaticMethodCallExpression methodCall = (StaticMethodCallExpression) node;
        if ("initMetaClass".equals(methodCall.getMethod())) { // NOI18N
            Expression args = methodCall.getArguments();
            if (args instanceof VariableExpression) {
                VariableExpression var = (VariableExpression) args;
                if ("this".equals(var.getName())) { // NOI18N
                    return false;
                }
            }
        }
    }
    return true;
}
 
Example #2
Source File: TypeInferenceVisitor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void visitBinaryExpression(BinaryExpression expression) {
    if (!leafReached) {
        // have a look at assignment and try to get type from its right side
        Expression leftExpression = expression.getLeftExpression();
        if (leftExpression instanceof VariableExpression) {
            if (expression.getOperation().isA(Types.EQUAL) && sameVariableName(leaf, leftExpression)) {
                Expression rightExpression = expression.getRightExpression();
                if (rightExpression instanceof ConstantExpression
                        && !rightExpression.getText().equals("null")) { // NOI18N
                    guessedType = ((ConstantExpression) rightExpression).getType();
                } else if (rightExpression instanceof ConstructorCallExpression) {
                    guessedType = ClassHelper.make(((ConstructorCallExpression) rightExpression).getType().getName());
                } else if (rightExpression instanceof MethodCallExpression) {
                    guessedType = MethodInference.findCallerType(rightExpression, path, doc, cursorOffset);
                } else if (rightExpression instanceof StaticMethodCallExpression) {
                    guessedType = MethodInference.findCallerType(rightExpression, path, doc, cursorOffset);
                }
            }
        }
    }
    super.visitBinaryExpression(expression);
}
 
Example #3
Source File: TypeInferenceVisitor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private ClassNode deriveExpressonType(Expression expression) {
    ClassNode derivedExpressionType = null;
    if (expression instanceof ConstantExpression
            && !expression.getText().equals("null")) { // NOI18N
        derivedExpressionType = ((ConstantExpression) expression).getType();
    } else if (expression instanceof ConstructorCallExpression) {
        derivedExpressionType = ((ConstructorCallExpression) expression).getType();
    } else if (expression instanceof MethodCallExpression) {
        int newOffset = ASTUtils.getOffset(doc, expression.getLineNumber(), expression.getColumnNumber());
        AstPath newPath = new AstPath(path.root(), newOffset, doc);
        derivedExpressionType = MethodInference.findCallerType(expression, newPath, doc, newOffset);
    } else if (expression instanceof StaticMethodCallExpression) {
        derivedExpressionType = MethodInference.findCallerType(expression, path, doc, cursorOffset);
    } else if (expression instanceof ListExpression) {
        derivedExpressionType = ((ListExpression) expression).getType();
    } else if (expression instanceof MapExpression) {
        derivedExpressionType = ((MapExpression) expression).getType();
    } else if (expression instanceof RangeExpression) {
        // this should work, but the type is Object - nut sure why
        // guessedType = ((RangeExpression)initialExpression).getType();
        derivedExpressionType = ClassHelper.makeWithoutCaching(Range.class, true);                
    } 
    return derivedExpressionType;
}
 
Example #4
Source File: TypeCheckingExtension.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Given a method call, first checks that it's a static method call, and if it is, returns the
 * class node for the receiver. For example, with the following code:
 * <code></code>Person.findAll { ... }</code>, it would return the class node for <i>Person</i>.
 * If it's not a static method call, returns null.
 * @param call a method call
 * @return null if it's not a static method call, or the class node for the receiver instead.
 */
public ClassNode extractStaticReceiver(MethodCall call) {
    if (call instanceof StaticMethodCallExpression) {
        return ((StaticMethodCallExpression) call).getOwnerType();
    } else if (call instanceof MethodCallExpression) {
        Expression objectExpr = ((MethodCallExpression) call).getObjectExpression();
        if (objectExpr instanceof ClassExpression && ClassHelper.CLASS_Type.equals(objectExpr.getType())) {
            GenericsType[] genericsTypes = objectExpr.getType().getGenericsTypes();
            if (genericsTypes!=null && genericsTypes.length==1) {
                return genericsTypes[0].getType();
            }
        }
        if (objectExpr instanceof ClassExpression) {
            return objectExpr.getType();
        }
    }
    return null;
}
 
Example #5
Source File: StaticImportVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
protected Expression transformBinaryExpression(BinaryExpression be) {
    int type = be.getOperation().getType();
    boolean oldInLeftExpression;
    Expression right = transform(be.getRightExpression());
    be.setRightExpression(right);
    Expression left;
    if (type == Types.EQUAL && be.getLeftExpression() instanceof VariableExpression) {
        oldInLeftExpression = inLeftExpression;
        inLeftExpression = true;
        left = transform(be.getLeftExpression());
        inLeftExpression = oldInLeftExpression;
        if (left instanceof StaticMethodCallExpression) {
            StaticMethodCallExpression smce = (StaticMethodCallExpression) left;
            StaticMethodCallExpression result = new StaticMethodCallExpression(smce.getOwnerType(), smce.getMethod(), right);
            setSourcePosition(result, be);
            return result;
        }
    } else {
        left = transform(be.getLeftExpression());
    }
    be.setLeftExpression(left);
    return be;
}
 
Example #6
Source File: ASTNodeVisitor.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
public void visitStaticMethodCallExpression(StaticMethodCallExpression node) {
	pushASTNode(node);
	try {
		super.visitStaticMethodCallExpression(node);
	} finally {
		popASTNode();
	}
}
 
Example #7
Source File: TraitComposer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static Statement createSuperFallback(MethodNode forwarderMethod, ClassNode returnType) {
    ArgumentListExpression args = new ArgumentListExpression();
    Parameter[] forwarderMethodParameters = forwarderMethod.getParameters();
    for (final Parameter forwarderMethodParameter : forwarderMethodParameters) {
        args.addExpression(new VariableExpression(forwarderMethodParameter));
    }
    BinaryExpression instanceOfExpr = new BinaryExpression(new VariableExpression("this"), Token.newSymbol(Types.KEYWORD_INSTANCEOF, -1, -1), new ClassExpression(Traits.GENERATED_PROXY_CLASSNODE));
    MethodCallExpression superCall = new MethodCallExpression(
            new VariableExpression("super"),
            forwarderMethod.getName(),
            args
    );
    superCall.setImplicitThis(false);
    CastExpression proxyReceiver = new CastExpression(Traits.GENERATED_PROXY_CLASSNODE, new VariableExpression("this"));
    MethodCallExpression getProxy = new MethodCallExpression(proxyReceiver, "getProxyTarget", ArgumentListExpression.EMPTY_ARGUMENTS);
    getProxy.setImplicitThis(true);
    StaticMethodCallExpression proxyCall = new StaticMethodCallExpression(
            ClassHelper.make(InvokerHelper.class),
            "invokeMethod",
            new ArgumentListExpression(getProxy, new ConstantExpression(forwarderMethod.getName()), new ArrayExpression(ClassHelper.OBJECT_TYPE, args.getExpressions()))
    );
    IfStatement stmt = new IfStatement(
            new BooleanExpression(instanceOfExpr),
            new ExpressionStatement(new CastExpression(returnType,proxyCall)),
            new ExpressionStatement(superCall)
    );
    return stmt;
}
 
Example #8
Source File: StaticCompilationTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public Expression transform(Expression expr) {
    if (expr instanceof StaticMethodCallExpression) {
        return staticMethodCallExpressionTransformer.transformStaticMethodCallExpression((StaticMethodCallExpression) expr);
    }
    if (expr instanceof BinaryExpression) {
        return binaryExpressionTransformer.transformBinaryExpression((BinaryExpression)expr);
    }
    if (expr instanceof MethodCallExpression) {
        return methodCallExpressionTransformer.transformMethodCallExpression((MethodCallExpression) expr);
    }
    if (expr instanceof ClosureExpression) {
        return closureExpressionTransformer.transformClosureExpression((ClosureExpression) expr);
    }
    if (expr instanceof ConstructorCallExpression) {
        return constructorCallTransformer.transformConstructorCall((ConstructorCallExpression) expr);
    }
    if (expr instanceof BooleanExpression) {
        return booleanExpressionTransformer.transformBooleanExpression((BooleanExpression)expr);
    }
    if (expr instanceof VariableExpression) {
        return variableExpressionTransformer.transformVariableExpression((VariableExpression)expr);
    }
    if (expr instanceof RangeExpression) {
        return rangeExpressionTransformer.transformRangeExpression(((RangeExpression)expr));
    }
    if (expr instanceof ListExpression) {
        return listExpressionTransformer.transformListExpression((ListExpression) expr);
    }
    if (expr instanceof CastExpression) {
        return castExpressionTransformer.transformCastExpression(((CastExpression)expr));
    }
    return super.transform(expr);
}
 
Example #9
Source File: StaticMethodCallExpressionTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
Expression transformStaticMethodCallExpression(final StaticMethodCallExpression orig) {
    MethodNode target = (MethodNode) orig.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
    if (target != null) {
        MethodCallExpression call = new MethodCallExpression(
                new ClassExpression(orig.getOwnerType()),
                orig.getMethod(),
                orig.getArguments()
        );
        call.setMethodTarget(target);
        call.setSourcePosition(orig);
        call.copyNodeMetaData(orig);
        return transformer.transform(call);
    }
    return transformer.superTransform(orig);
}
 
Example #10
Source File: TypeCheckingContext.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Pushes a method call into the method call stack.
 *
 * @param call the call expression to be pushed, either a {@link MethodCallExpression} or a {@link StaticMethodCallExpression}
 */
public void pushEnclosingMethodCall(final Expression call) {
    if (call instanceof MethodCallExpression || call instanceof StaticMethodCallExpression) {
        enclosingMethodCalls.addFirst(call);
    } else {
        throw new IllegalArgumentException("Expression must be a method call or a static method call");
    }
}
 
Example #11
Source File: SecureASTCustomizer.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitStaticMethodCallExpression(final StaticMethodCallExpression call) {
    assertExpressionAuthorized(call);
    final String typeName = call.getOwnerType().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 + "]");
    }
    call.getArguments().visit(this);
}
 
Example #12
Source File: MethodInference.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to infer correct {@link ClassNode} representing type of the caller for
 * the given expression. Typically the given parameter is instance of {@link MethodCallExpression}
 * and in that case the return type of the method call is returned.<br/><br/>
 * 
 * The method also handles method chain and in such case the return type of the
 * last method call should be return.
 * 
 * @param expression
 * @return class type of the caller if found, {@code null} otherwise
 */
@CheckForNull
public static ClassNode findCallerType(@NonNull ASTNode expression, @NonNull AstPath path, BaseDocument baseDocument, int offset) {
    // In case if the method call is chained with another method call
    // For example: someInteger.toString().^
    if (expression instanceof MethodCallExpression) {
        MethodCallExpression methodCall = (MethodCallExpression) expression;

        ClassNode callerType = findCallerType(methodCall.getObjectExpression(), path, baseDocument, offset);
        if (callerType != null) {
            return findReturnTypeFor(callerType, methodCall.getMethodAsString(), methodCall.getArguments(), path, false, baseDocument, offset);
        }
    }

    // In case if the method call is directly on a variable
    if (expression instanceof VariableExpression) {
        int newOffset = ASTUtils.getOffset(baseDocument, expression.getLineNumber(), expression.getColumnNumber());
        AstPath newPath = new AstPath(path.root(), newOffset, baseDocument);
        TypeInferenceVisitor tiv = new TypeInferenceVisitor(((ModuleNode)path.root()).getContext(), newPath, baseDocument, newOffset);
        tiv.collect();
        return tiv.getGuessedType();

        }
    if (expression instanceof ConstantExpression) {
        return ((ConstantExpression) expression).getType();
    }
    if (expression instanceof ClassExpression) {
        return ClassHelper.make(((ClassExpression) expression).getType().getName());
    }

    if (expression instanceof StaticMethodCallExpression) {
        StaticMethodCallExpression staticMethodCall = (StaticMethodCallExpression) expression;

        return findReturnTypeFor(staticMethodCall.getOwnerType(), staticMethodCall.getMethod(), staticMethodCall.getArguments(), path, true, baseDocument, offset);
    }
    return null;
}
 
Example #13
Source File: GeneralUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static StaticMethodCallExpression callX(final ClassNode receiver, final String methodName) {
    return callX(receiver, methodName, MethodCallExpression.NO_ARGUMENTS);
}
 
Example #14
Source File: GeneralUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static StaticMethodCallExpression callX(final ClassNode receiver, final String methodName, final Expression args) {
    return new StaticMethodCallExpression(receiver, methodName, args);
}
 
Example #15
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitStaticMethodCallExpression(final StaticMethodCallExpression call) {
    onLineNumber(call, "visitStaticMethodCallExpression: \"" + call.getMethod() + "\":");
    controller.getInvocationWriter().writeInvokeStaticMethod(call);
    controller.getAssertionWriter().record(call);
}
 
Example #16
Source File: OptimizingStatementWriter.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitStaticMethodCallExpression(final StaticMethodCallExpression expression) {
    if (expression.getNodeMetaData(StatementMeta.class) != null) return;
    super.visitStaticMethodCallExpression(expression);
    setMethodTarget(expression, expression.getMethod(), expression.getArguments(), true);
}
 
Example #17
Source File: InvocationWriter.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void writeInvokeStaticMethod(final StaticMethodCallExpression call) {
    Expression receiver = new ClassExpression(call.getOwnerType());
    Expression messageName = new ConstantExpression(call.getMethod());
    makeCall(call, receiver, messageName, call.getArguments(), InvocationWriter.invokeStaticMethod, false, false, false);
}
 
Example #18
Source File: CodeVisitorSupport.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitStaticMethodCallExpression(StaticMethodCallExpression call) {
    call.getArguments().visit(this);
}
 
Example #19
Source File: TransformingCodeVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitStaticMethodCallExpression(final StaticMethodCallExpression call) {
    super.visitStaticMethodCallExpression(call);
    trn.visitStaticMethodCallExpression(call);
}
 
Example #20
Source File: StaticImportVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static StaticMethodCallExpression newStaticMethodCallX(ClassNode type, String name, Expression args) {
    return new StaticMethodCallExpression(type.getPlainNodeReference(), name, args);
}
 
Example #21
Source File: ContextualClassCodeVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitStaticMethodCallExpression(final StaticMethodCallExpression call) {
    pushContext(call);
    super.visitStaticMethodCallExpression(call);
    popContext();
}
 
Example #22
Source File: ASTFinder.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitStaticMethodCallExpression(final StaticMethodCallExpression call) {
    super.visitStaticMethodCallExpression(call);
    tryFind(StaticMethodCallExpression.class, call);
}
 
Example #23
Source File: PathFinderVisitor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void visitStaticMethodCallExpression(StaticMethodCallExpression node) {
    if (isInside(node, line, column)) {
        super.visitStaticMethodCallExpression(node);
    }
}
 
Example #24
Source File: DriverCompilationCustomizer.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
@Override
public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
   LOGGER.trace("Customize [phase: {} {}, classNode: {}]", source.getPhase(), source.getPhaseDescription(), classNode);
   if(classNode.getField("_HASH") == null) {
      String hash = hash(source);
      if(hash != null) {
         classNode.addField(
               "_HASH", 
               Modifier.PUBLIC | Modifier.FINAL, 
               new ClassNode(String.class), 
               new ConstantExpression(hash)
         );
      }
   }
   
   ClassNode groovyCapabilityDefinition = new ClassNode(GroovyCapabilityDefinition.class);
   for(CapabilityDefinition definition: capabilityRegistry.listCapabilityDefinitions()) {
      if(classNode.getProperty(definition.getCapabilityName()) != null) {
         continue;
      }
      
      if(!isDeviceCapability(definition)) {
         continue;
      }
      
      String fieldName = definition.getNamespace();
      FieldNode field = classNode.addField(
            fieldName,
            Modifier.PRIVATE | Modifier.FINAL, 
            groovyCapabilityDefinition,
            new StaticMethodCallExpression(
                  new ClassNode(GroovyCapabilityDefinitionFactory.class),
                  "create",
                  new TupleExpression(
                        new ConstantExpression(definition.getCapabilityName()),
                        VariableExpression.THIS_EXPRESSION
                  )
            )
      );
      
      
      classNode.addProperty(
            definition.getCapabilityName(),
            Modifier.PUBLIC | Modifier.FINAL,
            groovyCapabilityDefinition,
            new FieldExpression(field),
            new ReturnStatement(new FieldExpression(field)),
            null
       );
   }
}
 
Example #25
Source File: GroovyCodeVisitor.java    From groovy with Apache License 2.0 votes vote down vote up
void visitStaticMethodCallExpression(StaticMethodCallExpression expression);