Example usage for org.eclipse.jdt.core.dom MethodInvocation toString

List of usage examples for org.eclipse.jdt.core.dom MethodInvocation toString

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom MethodInvocation toString.

Prototype

@Override
public final String toString() 

Source Link

Document

Returns a string representation of this node suitable for debugging purposes only.

Usage

From source file:com.dnw.depmap.ast.MethodInvocationVisitor.java

License:Open Source License

/**
 * The method will be called when a <code>MethodInvocation</code> node is met.
 * // w  ww . j  av  a2s.  c  o m
 * @author manbaum
 * @since Sep 29, 2014
 * @param node the <code>MethodInvocation</code> node.
 * @param context the visiting context.
 * @see com.dnw.plugin.ast.IVisitor#visit(org.eclipse.jdt.core.dom.ASTNode,
 *      com.dnw.plugin.ast.VisitContext)
 */
@Override
public void visit(MethodInvocation node, VisitContext context) {
    Activator.console.println(" -- Visit MethodInvocation: " + node.toString());
    // find the containing method of this method invocation.
    // TODO: what if a method invocation not happens in a method? i.e. during class initialization.
    ASTNode p = node.getParent();
    while (p != null) {
        if (p instanceof MethodDeclaration) {
            break;
        } else {
            p = p.getParent();
        }
    }
    MethodDeclaration decl = (MethodDeclaration) p;
    Activator.console.println("  . Method is called from: " + make(decl));
    if (p == null)
        return;

    IMethodBinding from = decl.resolveBinding();
    Activator.console.println(AstUtil.infoOf(context, decl, from));
    IMethodBinding to = node.resolveMethodBinding();
    Activator.console.println(AstUtil.infoOf(context, node, to));
    // call DAO to generate the method node and its related relationships.
    Activator.neo().createInvocation(from, to, args(node.arguments()));
}

From source file:com.google.gdt.eclipse.designer.model.widgets.panels.grid.FlexTableInfo.java

License:Open Source License

/**
 * @return <code>true</code> if this {@link FlexTableInfo} already "fixed".
 *//*  w w w . ja  va  2  s .  c  om*/
private boolean hasInvocation_fixRowSpan() {
    for (ASTNode relatedNode : getRelatedNodes()) {
        if (relatedNode.getLocationInParent() == MethodInvocation.ARGUMENTS_PROPERTY) {
            MethodInvocation invocation = (MethodInvocation) relatedNode.getParent();
            if (invocation.toString().contains("FlexTableHelper.fixRowSpan(")) {
                return true;
            }
        }
    }
    return false;
}

From source file:com.google.gdt.eclipse.designer.util.GwtInvocationEvaluatorInterceptor.java

License:Open Source License

@Override
public Object evaluate(EvaluationContext context, MethodInvocation invocation, IMethodBinding methodBinding,
        Class<?> clazz, Method method, Object[] argumentValues) {
    // Panel.add(Widget) throws exception, so make it fatal
    if (ReflectionUtils.getMethodSignature(method).equals("add(com.google.gwt.user.client.ui.Widget)")) {
        if (method.getDeclaringClass().getName().equals("com.google.gwt.user.client.ui.Panel")) {
            FatalDesignerException e = new FatalDesignerException(IExceptionConstants.PANEL_ADD_INVOCATION,
                    invocation.toString());
            e.setSourcePosition(invocation.getStartPosition());
            throw e;
        }/* w w  w . ja  v  a  2 s  .  c  o m*/
    }
    return AstEvaluationEngine.UNKNOWN;
}

From source file:com.windowtester.eclipse.ui.convert.WTConvertAPIContext.java

License:Open Source License

/**
 * Construct a new method invocation// ww  w  . j  a v  a2 s.c o  m
 * 
 * @param target the target expression or null if none
 * @param methodName the method name (not <code>null</code>, not empty)
 * @param arguments the argument expressions
 * @return the method invocation
 */
@SuppressWarnings("unchecked")
public MethodInvocation newMethodInvocation(Expression target, String methodName, Expression... arguments) {
    MethodInvocation invocation = getCompilationUnit().getAST().newMethodInvocation();
    int start = 0;
    if (target != null) {
        assertRoot(target);
        invocation.setExpression(target);
        start += target.toString().length() + 1; // target plus dot
    }
    invocation.setName(newSimpleName(methodName, start));
    start += methodName.length();
    for (int i = 0; i < arguments.length; i++) {
        Expression arg = arguments[i];
        assertRoot(arg);
        start += i == 0 ? 1 : 2; // opening parenthesis or comma and space
        adjustStartPositions(arg, 0, start);
        invocation.arguments().add(arg);
        start += arg.toString().length();
    }
    invocation.setSourceRange(0, invocation.toString().length());
    return invocation;
}

From source file:egovframework.mgt.fit.library.parser.visitor.MethodParsingVisitor.java

License:Apache License

/**
 *  ?  ? ? .//ww w.j a  v  a  2s . c  om
 * @return   
 */
private MethodInvoke processInvocation(MethodInvocation node) {
    method.addParsedInvocation(node.getStartPosition());
    MethodInvoke mi = new MethodInvoke();

    UnitList<Variable> args = new UnitList<Variable>();
    Project p = method.getProject();

    for (Object o : node.arguments()) {
        if (o instanceof SimpleName) {
            Variable targetField;
            String argName = ((SimpleName) o).getFullyQualifiedName();
            JavaClass checkType = method.getType();
            targetField = method.getLocalVariable(argName);
            while (true) {
                if (targetField == null) {
                    targetField = checkType.getField(argName);
                }
                if (targetField != null) {
                    args.addUnit(targetField);
                    break;
                }
                checkType = checkType.getSuperClass();
                if (!checkType.isProjectClass()) {
                    DebugLog.log("Failed!!!!");
                    break;
                }

            }

        } else if (o instanceof Expression) {
            Expression e = (Expression) o;
            switch (e.getNodeType()) {
            case ASTNode.BOOLEAN_LITERAL:
                args.addUnit(new Variable(p.BOOLEAN_CLASS, ObjectHelper.getUniqueKey("Literal"),
                        String.valueOf(((BooleanLiteral) e).booleanValue())));
                break;
            case ASTNode.CHARACTER_LITERAL:
                args.addUnit(new Variable(p.CHAR_CLASS, ObjectHelper.getUniqueKey("Literal"),
                        String.valueOf(((CharacterLiteral) e).charValue())));
                break;
            case ASTNode.FIELD_ACCESS:
                FieldAccess fa = (FieldAccess) e;
                break;
            case ASTNode.NULL_LITERAL:
                args.addUnit(Variable.getNullVariable(p.OBJECT_CLASS, ObjectHelper.getUniqueKey("Literal")));
                break;
            case ASTNode.NUMBER_LITERAL:
                args.addUnit(parseNumberLiteral(((NumberLiteral) e).getToken(), p));
                break;
            case ASTNode.STRING_LITERAL:
                args.addUnit(new Variable(p.STRING_CLASS, ObjectHelper.getUniqueKey("Literal"),
                        String.valueOf(((StringLiteral) e).getLiteralValue())));
                break;
            case ASTNode.SUPER_FIELD_ACCESS:

                break;
            case ASTNode.THIS_EXPRESSION:

                break;
            case ASTNode.MEMBER_REF:

                break;
            case ASTNode.METHOD_REF:

                break;
            case ASTNode.TYPE_LITERAL:
                args.addUnit(new Variable(p.TYPE_CLASS, ObjectHelper.getUniqueKey("Literal"),
                        String.valueOf(((TypeLiteral) e))));
                break;
            case ASTNode.PREFIX_EXPRESSION:
                PrefixExpression pe = (PrefixExpression) e;
                if (pe.getOperator().equals(PrefixExpression.Operator.PLUS)) {
                    args.addUnit(parseNumberLiteral(pe.getOperand().toString(), p));
                } else if (pe.getOperator().equals(PrefixExpression.Operator.MINUS)) {
                    args.addUnit(parseNumberLiteral("-", pe.getOperand().toString(), p));
                }
            default:
                break;
            }
        }
    }

    mi.setFromMethod(method);
    mi.setArguments(args.clone());
    String methodKey = StringHelper.getMethodKeyString(node.getName().getFullyQualifiedName(), args);
    Expression e = node.getExpression();
    if (e == null) { // ??   method  super ?? method ?
                     //   (?   Super Method Invocation?
                     // visit)

        JavaClass checkType = method.getType();

        if (node.toString().equals("getCategory(vo,searchVO)")) {
            System.out.println("!");
        }

        while (true) {
            if (checkType.isProjectClass()) {
                for (Method m : checkType.getMethods()) {
                    if (StringHelper.getMethodKeyString(m).equals(methodKey)) {
                        mi.setToMethod(m);
                        break;
                    }
                }

            } else {
                Method toM = new Method(p);
                toM.setType(checkType);
                toM.setName(node.getName().getFullyQualifiedName());
                toM.setReturnType(p.UNKNOWN_CLASS);
                for (Variable v : args) {
                    Variable tempParam = new Variable();
                    tempParam.setName(v.getName());
                    tempParam.setType(v.getType());
                    toM.addParameter(tempParam);
                }
                mi.setToMethod(toM);
                break;
            }
            if (mi.getToMethod() != null) {
                break;
            }
            checkType = checkType.getSuperClass();
        }

    } else { //  ??   
        switch (e.getNodeType()) {
        case ASTNode.SIMPLE_NAME:
            Variable targetField;
            String argName = ((SimpleName) e).getFullyQualifiedName();
            JavaClass checkType = method.getType();
            targetField = method.getLocalVariable(argName);
            JavaClass callType = null;
            while (true) {
                if (targetField == null) {
                    targetField = checkType.getField(argName);
                }
                if (targetField != null) {
                    callType = targetField.getType();
                    break;
                }
                checkType = checkType.getSuperClass();
                if (!checkType.isProjectClass()) {
                    if ((callType = p.resolveClass(ParsingHelper.getFullNameWithSimpleName(argName,
                            checkType.getPackage(), checkType.getImports()))) != null) {
                        break;
                    }

                    break;
                }
            }
            if (callType != null) {
                if (callType.isProjectClass()) {
                    for (Method m : callType.getMethods()) {
                        if (StringHelper.getMethodKeyString(m).equals(methodKey)) {
                            mi.setToMethod(m);
                            break;
                        }
                    }
                } else {
                    Method toM = new Method(p);
                    toM.setType(callType);
                    toM.setName(node.getName().getFullyQualifiedName());
                    toM.setReturnType(p.UNKNOWN_CLASS);
                    for (Variable v : args) {
                        Variable tempParam = new Variable();
                        tempParam.setName(v.getName());
                        tempParam.setType(v.getType());
                        toM.addParameter(tempParam);
                    }

                    mi.setToMethod(toM);
                }

            }

            break;
        case ASTNode.METHOD_INVOCATION:
            MethodInvoke mik = processInvocation((MethodInvocation) e);
            if (mik.getToMethod() == null) {
                break;
            }
            JavaClass jc = mik.getToMethod().getReturnType();
            if (jc == null || jc == p.UNKNOWN_CLASS) {
                break;
            }
            for (Method m : jc.getMethods()) {
                if (StringHelper.getMethodKeyString(m).equals(methodKey)) {
                    mi.setToMethod(m);
                    break;
                }
            }

            break;
        case ASTNode.STRING_LITERAL:
            Method toM = new Method(p);
            toM.setType(p.STRING_CLASS);
            toM.setName(node.getName().getFullyQualifiedName());
            toM.setReturnType(p.UNKNOWN_CLASS);
            for (Variable v : args) {
                Variable tempParam = new Variable();
                tempParam.setName(v.getName());
                tempParam.setType(v.getType());
                toM.addParameter(tempParam);
            }

            mi.setToMethod(toM);
            break;
        case ASTNode.SUPER_METHOD_INVOCATION:
            toM = new Method(p);
            toM.setType(method.getType().getSuperClass());
            toM.setName(node.getName().getFullyQualifiedName());
            toM.setReturnType(p.UNKNOWN_CLASS);
            for (Variable v : args) {
                Variable tempParam = new Variable();
                tempParam.setName(v.getName());
                tempParam.setType(v.getType());
                toM.addParameter(tempParam);
            }
            mi.setToMethod(toM);
            break;
        case ASTNode.PARENTHESIZED_EXPRESSION:
            break;
        case ASTNode.ARRAY_ACCESS:
            break;
        case ASTNode.FIELD_ACCESS:
            break;
        case ASTNode.THIS_EXPRESSION:
            break;
        case ASTNode.QUALIFIED_NAME:
            break;
        case ASTNode.CLASS_INSTANCE_CREATION:
            ClassInstanceCreation cic = (ClassInstanceCreation) e;

            JavaClass createdType = p.resolveClass(ParsingHelper.getFullNameWithSimpleName(
                    cic.getType().toString(), method.getType().getPackage(), method.getType().getImports()));
            for (Method m : createdType.getMethods()) {
                if (StringHelper.getMethodKeyString(m).equals(methodKey)) {
                    mi.setToMethod(m);
                    break;
                }
            }
            break;
        default:
        }
        // System.out.println("--->"+node.getExpression().toString());
    }

    if (mi.getFromMethod() != null && mi.getToMethod() != null) {
        p.addInvocation(mi);
    } else {
        //System.out.println("Fail....TT");
    }

    return mi;
    // System.out.println("invoke : " + node.toString());
}

From source file:org.eclipse.babel.tapiji.tools.java.util.ASTutils.java

License:Open Source License

public static String createResourceReference(String bundleId, String key, Locale locale, IResource resource,
        int typePos, String accessorName, AST ast, ASTRewrite astRewrite, CompilationUnit cu) {

    final StringLiteralFinder literalFinder = new StringLiteralFinder(typePos);
    cu.accept(literalFinder);/*from  w  ww  . ja v a  2 s .c om*/
    final StringLiteral literal = literalFinder.getStringLiteral();

    final MethodInvocation methodInvocation = referenceResource(ast, accessorName, key, locale);

    if (literal != null) {
        astRewrite.replace(literal, methodInvocation, null);
        return null;
    } else {
        String exp = methodInvocation.toString();

        // remove semicolon and line break at the end of this expression
        // statement
        if (exp.endsWith(";\n")) {
            exp = exp.substring(0, exp.length() - 2);
        }

        return exp;
    }
}

From source file:org.evosuite.junit.TestExtractingVisitor.java

License:Open Source License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override/*from w  w  w. j  a  v  a  2s.co  m*/
public void endVisit(MethodInvocation methodInvocation) {
    // TODO If in constructor, treat calls to this() and super().
    String methodName = methodInvocation.getName().toString();
    if (methodName.equals("fail") || methodName.startsWith("assert")) {
        logger.warn("We are ignoring fail and assert statements for now.");
        for (Expression expression : (List<Expression>) methodInvocation.arguments()) {
            if ((expression instanceof MethodInvocation) || (expression instanceof ClassInstanceCreation)) {
                assert !nestedCallResults.isEmpty();
                nestedCallResults.pop();
            }
        }
        return;
    }
    IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
    if (methodBinding == null) {
        throw new RuntimeException("JDT was unable to resolve method for " + methodInvocation);
    }
    List<?> paramTypes = Arrays.asList(methodBinding.getParameterTypes());
    List<VariableReference> params = convertParams(methodInvocation.arguments(), paramTypes);
    Method method = retrieveMethod(methodInvocation, methodBinding, params);
    /*
    Class<?> declaringClass = method.getDeclaringClass();
    if (false) {
       // TODO Methods can be declared in an order such that the called
       // method is not yet read
       if (testCase.getClassName().equals(declaringClass.getName())
         || testCase.isDescendantOf(declaringClass)) {
    MethodDef methodDef = testCase.getMethod(method.getName());
    VariableReference retVal = retrieveResultReference(methodInvocation);
    retVal.setOriginalCode(methodInvocation.toString());
    testCase.convertMethod(methodDef, params, retVal);
    return;
       }
    }
    */
    VariableReference callee = null;
    if (!Modifier.isStatic(method.getModifiers())) {
        callee = retrieveCalleeReference(methodInvocation);
    }
    MethodStatement methodStatement = null;
    ASTNode parent = methodInvocation.getParent();
    if (parent instanceof ExpressionStatement) {
        VariableReference retVal = new ValidVariableReference(testCase.getReference(), method.getReturnType());
        retVal.setOriginalCode(methodInvocation.toString());
        methodStatement = new ValidMethodStatement(testCase.getReference(), method, callee, retVal, params);
        testCase.addStatement(methodStatement);
        return;
    }
    VariableReference retVal = retrieveResultReference(methodInvocation);
    retVal.setOriginalCode(methodInvocation.toString());
    methodStatement = new ValidMethodStatement(testCase.getReference(), method, callee, retVal, params);
    if (!(parent instanceof Block)) {
        nestedCallResults.push(retVal);
    }
    testCase.addStatement(methodStatement);
}

From source file:org.evosuite.junit.TestExtractingVisitor.java

License:Open Source License

private VariableReference retrieveResultReference(MethodInvocation methodInvocation) {
    VariableReference result = calleeResultMap.get(methodInvocation.toString());
    if (result != null) {
        return result;
    }//w  w  w.j  av a 2  s.c o  m
    ASTNode parent = methodInvocation.getParent();
    if (parent instanceof VariableDeclarationFragment) {
        return retrieveVariableReference(parent, null);
    }
    if (parent instanceof Assignment) {
        Assignment assignment = (Assignment) parent;
        return retrieveVariableReference(assignment.getLeftHandSide(), null);
    }
    IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
    ITypeBinding returnType = methodBinding.getReturnType();
    Class<?> resultClass = null;
    try {
        resultClass = retrieveTypeClass(returnType);
    } catch (Exception exc) {
        String localClass = methodBinding.getDeclaringClass().getQualifiedName() + "." + returnType.getName();
        resultClass = loadClass(localClass);
    }
    result = retrieveVariableReference(returnType, resultClass);
    calleeResultMap.put(methodInvocation.toString(), result);
    return result;
}

From source file:org.jboss.windup.decorator.java.JavaASTVariableResolvingVisitor.java

License:Open Source License

public boolean visit(MethodInvocation node) {
    if (!StringUtils.contains(node.toString(), ".")) {
        // it must be a local method. ignore.
        return true;
    }/* w  w w. j  a  v  a2s .c  o  m*/

    String nodeName = StringUtils.removeStart(node.toString(), "this.");

    List arguements = node.arguments();
    List<String> resolvedParams = methodParameterGuesser(arguements);

    String objRef = StringUtils.substringBefore(nodeName, "." + node.getName().toString());

    if (nameInstance.containsKey(objRef)) {
        objRef = nameInstance.get(objRef);
    }

    if (classNameToFullyQualified.containsKey(objRef)) {
        objRef = classNameToFullyQualified.get(objRef);
    }
    String resolvedMethodCall = objRef + "." + node.getName().toString() + "(";

    for (int i = 0, j = resolvedParams.size(); i < j; i++) {
        resolvedMethodCall += resolvedParams.get(i);

        if (i < j - 1) {
            resolvedMethodCall += ", ";
        }
    }
    resolvedMethodCall = resolvedMethodCall + ")";
    LOG.trace("Resolved Method call: " + resolvedMethodCall);
    // Here is the call to blacklist
    processInterest(resolvedMethodCall, cu.getLineNumber(node.getStartPosition()), "Usage of",
            SourceType.METHOD);

    return true;
}

From source file:org.jboss.windup.rules.apps.java.scan.ast.VariableResolvingASTVisitor.java

License:Open Source License

/***
 * Takes the MethodInvocation, and attempts to resolve the types of objects passed into the method invocation.
 *///w  w  w.java  2 s  . com
public boolean visit(MethodInvocation node) {
    if (!StringUtils.contains(node.toString(), ".")) {
        // it must be a local method. ignore.
        return true;
    }

    String nodeName = StringUtils.removeStart(node.toString(), "this.");

    List<?> arguments = node.arguments();
    List<String> resolvedParams = methodParameterGuesser(arguments);

    String objRef = StringUtils.substringBefore(nodeName, "." + node.getName().toString());

    if (nameInstance.containsKey(objRef)) {
        objRef = nameInstance.get(objRef);
    }

    objRef = resolveClassname(objRef);

    MethodType methodCall = new MethodType(objRef, node.getName().toString(), resolvedParams);
    processMethod(methodCall, cu.getLineNumber(node.getName().getStartPosition()),
            cu.getColumnNumber(node.getName().getStartPosition()), node.getName().getLength());

    return super.visit(node);
}