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

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

Introduction

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

Prototype

public final int getLength() 

Source Link

Document

Returns the length in characters of the original source file indicating where the source fragment corresponding to this node ends.

Usage

From source file:br.uff.ic.gems.resources.ast.Visitor.java

@Override
public boolean visit(MethodInvocation node) {
    int beginLine = cu.getLineNumber(node.getStartPosition());
    int endLine = cu.getLineNumber(node.getStartPosition() + node.getLength());
    int beginColumn = cu.getColumnNumber(node.getStartPosition());
    int endColumn = cu.getColumnNumber(node.getStartPosition() + node.getLength());

    languageConstructs.add(/*from  ww  w .  j  a  v  a 2 s .  c  om*/
            new LanguageConstruct(node.getClass().getSimpleName(), beginLine, endLine, beginColumn, endColumn));

    return true;
}

From source file:br.uff.ic.mergeguider.javaparser.DepVisitor.java

@Override
public boolean visit(MethodInvocation node) {

    int elementLineBegin = cu.getLineNumber(node.getStartPosition());
    int elementLineEnd = cu.getLineNumber(node.getStartPosition() + node.getLength());
    int elementColumnBegin = cu.getColumnNumber(node.getStartPosition());
    int elementColumnEnd = cu.getColumnNumber(node.getStartPosition() + node.getLength());

    Location location = new Location(elementLineBegin, elementLineEnd, elementColumnBegin, elementColumnEnd);

    MyMethodInvocation myMethodInvocation = new MyMethodInvocation(node, location);

    if (!classLanguageConstructsList.isEmpty()) {
        classLanguageConstructsList.get(classLanguageConstructsList.size() - 1).getMethodInvocations()
                .add(myMethodInvocation);
    }/*from  ww w.  j  a  v  a  2s  .c  o m*/

    return true;
}

From source file:com.github.lbroudoux.dsl.eip.parser.camel.CamelJavaFileParser.java

License:Apache License

/** */
private void parseAndFillEndpoint(MethodInvocation invocation, Channel incomingChannel,
        List<Endpoint> endpoints) {
    //System.err.println("Parsing " + invocation.getName());
    Endpoint endpoint = null;/* w w w. ja  v a 2s  .c o  m*/

    if ("from".equals(invocation.getName().toString())) {
        // We may have some different stuffs here ! Check uri in order to guess...
        String uri = invocation.arguments().get(0).toString();
        if (uri.startsWith("\"direct:")) {
            // That's a multicast subroute definition, use it to retrieve previously created
            // channel and place it as the current incomingChannel.
            int invocationLine = routeCU.getLineNumber(invocation.getStartPosition() + invocation.getLength());
            String incomingChannelName = commentMap.get(invocationLine);
            incomingChannel = retrieveChannelByName(incomingChannelName, route.getOwnedChannels());
        } else {
            endpoint = EipFactory.eINSTANCE.createGateway();
        }
    } else if ("choice".equals(invocation.getName().toString())) {
        endpoint = EipFactory.eINSTANCE.createRouter();
    } else if ("filter".equals(invocation.getName().toString())) {
        endpoint = EipFactory.eINSTANCE.createFilter();
    } else if ("split".equals(invocation.getName().toString())) {
        // CompositeProcessor is implicit and should be created when split() appears.
        endpoint = EipFactory.eINSTANCE.createCompositeProcessor();
        endpoints.add(endpoint);
        // Intermediate channel should be connected and then reset cause first
        // contained endpoint does not have incming channel.
        incomingChannel.setToEndpoint(endpoint);
        incomingChannel = null;
        // We should "go down" and consider composite endpoints until end() appear.
        endpoints = ((CompositeProcessor) endpoint).getOwnedEndpoints();
        endpoint = EipFactory.eINSTANCE.createSplitter();
    } else if ("when".equals(invocation.getName().toString())) {
        // Parent should be a Router.
        Endpoint lastEndpoint = endpoints.get(endpoints.size() - 1);
        if (lastEndpoint instanceof Router) {
            ConditionalRoute cRoute = EipFactory.eINSTANCE.createConditionalRoute();
            ((Router) lastEndpoint).getOwnedRoutes().add(cRoute);
            // Inspect comment to get outgoing channel name.
            int invocationLine = routeCU.getLineNumber(invocation.getStartPosition() + invocation.getLength());
            String outgoingChannelName = commentMap.get(invocationLine);
            // Outgoing channel will became net endpoint incoming.
            incomingChannel = EipFactory.eINSTANCE.createChannel();
            incomingChannel.setName(outgoingChannelName);
            cRoute.setChannel(incomingChannel);
        }
    } else if ("otherwise".equals(invocation.getName().toString())) {
        // Everything should have been done at Router level...
    } else if ("end".equals(invocation.getName().toString())) {
        // We ended here a composite and should now "go up".
        Endpoint lastEndpoint = endpoints.get(endpoints.size() - 1);
        if (lastEndpoint.eContainer() instanceof CompositeProcessor) {
            endpoints = route.getOwnedEndpoints();
        }
    } else if ("resequence".equals(invocation.getName().toString())) {
        endpoint = EipFactory.eINSTANCE.createResequencer();
    } else if ("stream".equals(invocation.getName().toString())) {
        // Parent should be a Resequencer.
        Endpoint lastEndpoint = endpoints.get(endpoints.size() - 1);
        if (lastEndpoint instanceof Resequencer) {
            ((Resequencer) lastEndpoint).setStreamSequences(true);
        }
    } else if ("to".equals(invocation.getName().toString())) {
        // We may have a lot of stuffs here ! Check uri in order to guess...
        String uri = invocation.arguments().get(0).toString();
        if (uri.startsWith("\"xslt:")) {
            endpoint = EipFactory.eINSTANCE.createTransformer();
        } else if (uri.startsWith("\"switchyard:")) {
            endpoint = EipFactory.eINSTANCE.createServiceActivator();
        } else if (uri.startsWith("\"direct:")) {
            // That's a multicast channel to a sub-route...
            int invocationLine = routeCU.getLineNumber(invocation.getStartPosition() + invocation.getLength());
            String outgoingChannelName = commentMap.get(invocationLine);
            Channel multicast = retrieveChannelByName(outgoingChannelName, route.getOwnedChannels());
            if (multicast == null) {
                multicast = EipFactory.eINSTANCE.createChannel();
                multicast.setName(outgoingChannelName);
                route.getOwnedChannels().add(multicast);
            }

            Endpoint lastEndpoint = endpoints.get(endpoints.size() - 1);
            lastEndpoint.getToChannels().add(multicast);
        }
    } else {
        System.err.println("Got an unsupported: " + invocation.getName());
    }

    if (endpoint != null) {
        // Complete Endpoint with common attributes if any and store it.
        int invocationLine = routeCU.getLineNumber(invocation.getStartPosition() + invocation.getLength());
        String comment = commentMap.get(invocationLine);

        String endpointName = invocation.getName().toString() + "_" + endpoints.size();
        String outgoingChannelName = null;

        // Comment may have "<endpoint_name>|<outgoing_channel_name>" or just "<endpoint_name>" format.
        if (comment != null) {
            if (comment.contains("|")) {
                endpointName = comment.substring(0, comment.indexOf('|'));
                outgoingChannelName = comment.substring(comment.indexOf('|') + 1);
            } else {
                endpointName = comment.trim();
            }
        }
        endpoint.setName(endpointName);
        endpoints.add(endpoint);

        // Associate with incoming channel if any.
        if (incomingChannel != null) {
            incomingChannel.setToEndpoint(endpoint);
        }

        // We have created an endpoint so we need an outgoingChannel that
        // will become incoming one for next endpoint to create !
        incomingChannel = EipFactory.eINSTANCE.createChannel();
        if (outgoingChannelName != null) {
            incomingChannel.setName(outgoingChannelName);
        }
        incomingChannel.setFromEndpoint(endpoint);
        route.getOwnedChannels().add(incomingChannel);
    }

    if (!expressionStack.isEmpty()) {
        parseAndFillEndpoint(expressionStack.pollLast(), incomingChannel, endpoints);
    }
}

From source file:com.liferay.blade.eclipse.provider.JavaFileJDT.java

License:Open Source License

/**
 * find the method invocations for a particular method on a given type or expression
 *
 * @param typeHint the type hint to use when matching expressions
 * @param expressionValue    the expression only value (no type hint)
 * @param methodName     the method name
 * @return    search results/*from ww  w .j ava2 s .c o m*/
 */
@Override
@SuppressWarnings("unchecked")
public List<SearchResult> findMethodInvocations(final String typeHint, final String expressionValue,
        final String methodName, final String[] methodParamTypes) {
    final List<SearchResult> searchResults = new ArrayList<>();

    _ast.accept(new ASTVisitor() {

        @Override
        public boolean visit(MethodInvocation node) {
            final String methodNameValue = node.getName().toString();
            final Expression expression = node.getExpression();

            ITypeBinding type = null;

            if (expression != null) {
                type = expression.resolveTypeBinding();
            }

            if (((methodName.equals(methodNameValue)) || ("*".equals(methodName))) &&
            // if typeHint is not null it must match the type hint and ignore the expression
            // not strictly check the type and will check equals later
            ((typeHint != null && type != null && type.getName().endsWith(typeHint)) ||
            // with no typeHint then expressions can be used to match Static invocation
            (typeHint == null && expression != null && expression.toString().equals(expressionValue)))) {

                boolean argumentsMatch = false;

                if (methodParamTypes != null) {
                    Expression[] argExpressions = ((List<Expression>) node.arguments())
                            .toArray(new Expression[0]);

                    if (argExpressions.length == methodParamTypes.length) {
                        //args number matched
                        boolean possibleMatch = true;
                        // assume all types will match until we find otherwise
                        boolean typeMatched = true;
                        boolean typeUnresolved = false;

                        for (int i = 0; i < argExpressions.length; i++) {
                            Expression arg = argExpressions[i];
                            ITypeBinding argType = arg.resolveTypeBinding();

                            if (argType != null) {
                                //can resolve the type
                                if (argType.getName().equals(methodParamTypes[i])) {
                                    //type matched
                                    continue;
                                } else {
                                    //type unmatched
                                    possibleMatch = false;
                                    typeMatched = false;
                                    break;
                                }
                            } else {
                                possibleMatch = false;
                                //there are two cases :
                                //typeUnresolved : means that  all resolved type is matched and there is unsolved type , need to set fullMatch false
                                //typeUnmatched : means that some resolved type is unmatched , no need to add SearchResult

                                //do not add searchResults now , just record the state and continue
                                //because there maybe unmatched type later which will  break this case
                                typeUnresolved = true;
                            }
                        }

                        if (typeMatched && typeUnresolved) {
                            final int startOffset = expression.getStartPosition();
                            final int startLine = _ast.getLineNumber(startOffset);
                            final int endOffset = node.getStartPosition() + node.getLength();
                            final int endLine = _ast.getLineNumber(endOffset);
                            //can't resolve the type but  args number matched  ,  note that the last param is false
                            searchResults.add(createSearchResult(null, startOffset, endOffset, startLine,
                                    endLine, false));
                        }

                        if (possibleMatch) {
                            argumentsMatch = true;
                        }
                    }
                }
                //any method args types is OK without setting methodParamTypes
                else {
                    argumentsMatch = true;
                }

                if (argumentsMatch) {
                    final int startOffset = expression.getStartPosition();
                    final int startLine = _ast.getLineNumber(startOffset);
                    final int endOffset = node.getStartPosition() + node.getLength();
                    final int endLine = _ast.getLineNumber(endOffset);
                    boolean isFullMatch = true;

                    //endsWith but not equals
                    if (typeHint != null && type != null && type.getName().endsWith(typeHint)
                            && !type.getName().equals(typeHint)) {
                        isFullMatch = false;
                    }

                    searchResults.add(
                            createSearchResult(null, startOffset, endOffset, startLine, endLine, isFullMatch));
                }
            }

            return true;
        }
    });

    return searchResults;
}

From source file:edu.uci.ics.sourcerer.extractor.ast.ReferenceExtractorVisitor.java

License:Open Source License

/**
 * This method writes:/*from w  ww. j av  a2  s  .c  o  m*/
 *<ul>
 *  <li>Calls relation to <code>IRelationWriter</code>.</li>
 *</ul>
 */
@Override
public boolean visit(MethodInvocation node) {
    // Get the fqn
    String fqn = null;
    IMethodBinding binding = node.resolveMethodBinding();
    if (binding == null) {
        fqn = getFuzzyMethodFqn(node);
    } else {
        fqn = getMethodFqn(binding, false);
    }

    // Write the calls relation
    relationWriter.writeCalls(fqnStack.getFqn(), fqn,
            new Location(compilationUnitPath, node.getName().getStartPosition(),
                    node.getLength() - (node.getName().getStartPosition() - node.getStartPosition())));

    return true;
}

From source file:edu.uci.ics.sourcerer.tools.java.extractor.eclipse.ReferenceExtractorVisitor.java

License:Open Source License

/**
 * This method writes:/*from   www .  j a  va2 s . c om*/
 *<ul>
 *  <li>Calls relation to <code>IRelationWriter</code>.</li>
 *</ul>
 */
@Override
public boolean visit(MethodInvocation node) {
    // Get the fqn
    String fqn = null;
    IMethodBinding binding = node.resolveMethodBinding();
    if (binding == null) {
        fqn = createUnknownMethodFqn(node);
    } else {
        fqn = getMethodName(binding, false) + getMethodArgs(binding);
    }

    // Write the calls relation
    relationWriter.writeRelation(Relation.CALLS, fqnStack.getFqn(), fqn,
            new Location(fqnStack.find(EnclosingDeclaredType.class).getFqn(), compilationUnitPath,
                    node.getName().getStartPosition(),
                    node.getLength() - (node.getName().getStartPosition() - node.getStartPosition())));

    return true;
}

From source file:org.bundlemaker.core.ui.editor.sourceviewer.referencedetail.JdtAstVisitor.java

License:Open Source License

/**
 * {@inheritDoc}//from w w w . j a  va 2 s.  com
 */
@SuppressWarnings("unchecked")
@Override
public boolean visit(MethodInvocation node) {

    // access to static methods
    IMethodBinding methodBinding = node.resolveMethodBinding();

    //
    resolveMethodBinding(methodBinding, node.getStartPosition(), node.getLength());

    // resolve the associated expression
    resolveExpressionType(node.getExpression());

    // resolve the type arguments
    List<Expression> typeArguments = node.arguments();
    for (Expression exp : typeArguments) {
        resolveExpressionType(exp);
    }

    // visit the child nodes
    return true;
}

From source file:org.eclipse.ajdt.internal.ui.editor.quickfix.UnresolvedElementsSubProcessor.java

License:Open Source License

public static void getArrayAccessProposals(IInvocationContext context, IProblemLocation problem,
        Collection proposals) {//  w w w.j a  v a2 s .  co m

    CompilationUnit root = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(root);
    if (!(selectedNode instanceof MethodInvocation)) {
        return;
    }

    MethodInvocation decl = (MethodInvocation) selectedNode;
    SimpleName nameNode = decl.getName();
    String methodName = nameNode.getIdentifier();

    IBinding[] bindings = (new ScopeAnalyzer(root)).getDeclarationsInScope(nameNode, ScopeAnalyzer.METHODS);
    for (int i = 0; i < bindings.length; i++) {
        String currName = bindings[i].getName();
        if (NameMatcher.isSimilarName(methodName, currName)) {
            String label = Messages.format(
                    CorrectionMessages.UnresolvedElementsSubProcessor_arraychangetomethod_description,
                    currName);
            proposals.add(new RenameNodeCorrectionProposal(label, context.getCompilationUnit(),
                    nameNode.getStartPosition(), nameNode.getLength(), currName, 6));
        }
    }
    // always suggest 'length'
    String lengthId = "length"; //$NON-NLS-1$
    String label = CorrectionMessages.UnresolvedElementsSubProcessor_arraychangetolength_description;
    int offset = nameNode.getStartPosition();
    int length = decl.getStartPosition() + decl.getLength() - offset;
    proposals.add(
            new RenameNodeCorrectionProposal(label, context.getCompilationUnit(), offset, length, lengthId, 7));
}

From source file:org.gw4e.eclipse.facade.JDTManager.java

License:Open Source License

public static Set<String> findGeneratorFactoryParseInvocation(IProject project, IType itype)
        throws JavaModelException {
    Set<String> ret = new HashSet<String>();
    ICompilationUnit cu = itype.getCompilationUnit();
    CompilationUnit ast = parse(cu);/* w  w  w. j a v a 2 s  .c  om*/

    ast.accept(new ASTVisitor() {
        public boolean visit(MethodInvocation node) {
            SimpleName simpleName = node.getName();
            IBinding bding = simpleName.resolveBinding();
            if (bding instanceof IMethodBinding) {
                IMethodBinding imb = (IMethodBinding) bding;
                if ("parse".equalsIgnoreCase(imb.getName())) {
                    ITypeBinding[] arguments = imb.getParameterTypes();
                    if (arguments.length == 1) {
                        if (String.class.getName().equals(arguments[0].getQualifiedName())
                                && PathGenerator.class.getName()
                                        .equals(imb.getReturnType().getQualifiedName())) {
                            int start = node.getStartPosition();
                            int end = node.getLength();
                            try {
                                String code = cu.getSource().substring(start, start + end);
                                // System.out.println(code);
                            } catch (JavaModelException e) {
                                ResourceManager.logException(e);
                            }
                            List args = node.arguments();
                            Expression argumentExpression = (Expression) args.get(0);
                            ITypeBinding typeBinding = argumentExpression.resolveTypeBinding();
                            if (typeBinding != null) {
                                if (argumentExpression instanceof StringLiteral) {
                                    StringLiteral sl = (StringLiteral) argumentExpression;
                                    String lv = sl.getLiteralValue();
                                    ret.add(lv);
                                }
                            }
                        }
                    }
                }
            }

            return true;
        }
    });
    return ret;
}

From source file:org.springframework.ide.vscode.boot.java.requestmapping.WebfluxMethodFinder.java

License:Open Source License

@Override
public boolean visit(MethodInvocation node) {
    boolean visitChildren = true;

    if (node != this.root) {
        IMethodBinding methodBinding = node.resolveMethodBinding();

        try {/*from  w ww  . ja v  a  2 s .  c  o m*/
            if (WebfluxUtils.REQUEST_PREDICATES_TYPE
                    .equals(methodBinding.getDeclaringClass().getBinaryName())) {
                String name = methodBinding.getName();
                if (name != null && WebfluxUtils.REQUEST_PREDICATE_HTTPMETHOD_METHODS.contains(name)) {
                    Range range = doc.toRange(node.getStartPosition(), node.getLength());
                    methods.add(new WebfluxRouteElement(name, range));
                } else if (name != null && WebfluxUtils.REQUEST_PREDICATE_METHOD_METHOD.equals(name)) {
                    QualifiedName qualifiedName = WebfluxUtils.extractQualifiedNameArgument(node);
                    if (qualifiedName.getName() != null) {
                        Range range = doc.toRange(qualifiedName.getStartPosition(), qualifiedName.getLength());
                        methods.add(new WebfluxRouteElement(qualifiedName.getName().toString(), range));
                    }
                }
            }
        } catch (BadLocationException e) {
            // ignore
        }

        if (WebfluxUtils.isRouteMethodInvocation(methodBinding)) {
            visitChildren = false;
        }
    }
    return visitChildren;
}