List of usage examples for org.eclipse.jdt.core.dom MethodInvocation getStartPosition
public final int getStartPosition()
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 w w w .jav a 2s . com*/ 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 w w w . j a va2 s.co m*/ return true; }
From source file:ca.uvic.chisel.javasketch.internal.ast.ASTMessageFinder.java
License:Open Source License
@Override public boolean visit(MethodInvocation node) { if (!(message instanceof ICall)) return false; if (containsMessage(node)) { ICall call = (ICall) message;/*ww w .java2s . c o m*/ IMethodBinding binding = node.resolveMethodBinding(); if (binding != null) { binding = binding.getMethodDeclaration(); if (binding != null) { IJavaElement element = binding.getJavaElement(); if (element instanceof IMethod) { try { IMethod jm = (IMethod) element; //get the target method. ITraceClassMethod am = call.getTarget().getActivation().getMethod(); String types[] = Signature.getParameterTypes(am.getSignature()); IMethod testMethod = jm.getDeclaringType().getMethod(am.getName(), types); if (jm.isSimilar(testMethod)) { this.node = node; try { if (document.getLineOfOffset(node.getStartPosition()) != (call.codeLine() - 1)) //look for a better match. return true; } catch (BadLocationException e) { } return false; } } catch (NullPointerException e) { return false; } } } } return true; } return false; }
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;/*ww w . ja v a 2 s . 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.google.gdt.eclipse.designer.gxt.model.widgets.UntypedEventsRootProcessor.java
License:Open Source License
private void contributeContextMenu(IMenuManager manager, final ComponentInfo component) { Map<String, MenuManagerEx> eventManagers = Maps.newTreeMap(); for (final EventDescription description : getEventDescriptions(component)) { // prepare MenuManager for event MenuManagerEx eventManager;// w w w.jav a 2 s .c o m { String event = description.getEvent(); eventManager = eventManagers.get(event); if (eventManager == null) { eventManager = new MenuManagerEx(event); eventManager.setImage(EventsPropertyUtils.LISTENER_INTERFACE_IMAGE); eventManagers.put(event, eventManager); manager.appendToGroup(IContextMenuConstants.GROUP_EVENTS2, eventManager); } } // add specific name Action action = new Action() { @Override public void runWithEvent(Event event) { if ((event.stateMask & SWT.CTRL) != 0) { removeListener(component, description); } else { openListener(component, description); } } }; { String text = description.getName(); MethodInvocation invocation = getInvocation(component, description); if (invocation != null) { int line = 1 + component.getEditor().getLineNumber(invocation.getStartPosition()); text += "\tline " + line; } action.setText(text); } action.setImageDescriptor(EventsPropertyUtils.LISTENER_METHOD_IMAGE_DESCRIPTOR); eventManager.add(action); } }
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; }/*from w ww .j a va2 s . c om*/ } return AstEvaluationEngine.UNKNOWN; }
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/* www.ja va2s. c om*/ */ @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:com.motorolamobility.preflighting.core.internal.utils.ProjectUtils.java
License:Apache License
private static void analizeBody(final CompilationUnit javaCompilationUnit, final Method method, Block body) { body.accept(new ASTVisitor() { @Override//from w ww . ja v a 2s . co m public boolean visit(VariableDeclarationFragment node) { String varName = node.getName().getIdentifier(); ITypeBinding typeBinding = node.resolveBinding().getType(); String typeQualifiedName = typeBinding.getQualifiedName(); int modifiers = typeBinding.getModifiers(); boolean isFinal = isFinal(modifiers); boolean isStatic = isStatic(modifiers); String value = null; Expression initializer = node.getInitializer(); if (initializer != null) { value = initializer.toString(); } int lineNumber = javaCompilationUnit.getLineNumber(node.getStartPosition()); Variable variable = new Variable(); variable.setName(varName); variable.setType(typeQualifiedName); variable.setFinal(isFinal); variable.setStatic(isStatic); variable.setValue(value); variable.setLineNumber(lineNumber); method.addVariable(variable); return super.visit(node); } @Override public boolean visit(MethodInvocation node) { // Fill invoked method model. MethodInvocation invoked = node; IMethodBinding methodBinding = invoked.resolveMethodBinding(); if (methodBinding != null) { IMethodBinding methodDeclaration = methodBinding.getMethodDeclaration(); ITypeBinding declaringClass = methodDeclaration.getDeclaringClass(); String declaringClassName = ""; if (declaringClass != null) { declaringClassName = declaringClass.getQualifiedName(); } String methodSimpleName = methodBinding.getName(); int lineNumber = javaCompilationUnit.getLineNumber(invoked.getStartPosition()); String returnType = methodBinding.getReturnType().getQualifiedName(); int methodModifiers = methodBinding.getModifiers(); boolean isVirtual = isMethodVirtual(methodModifiers); String sourceFileFullPath = ((File) javaCompilationUnit.getProperty(JAVA_FILE_PROPERTY)) .getAbsolutePath(); // Retrieve parameter types and look for R constants used // within method arguments List arguments = invoked.arguments(); List<String> parameterTypes = new ArrayList<String>(arguments.size()); List<String> parameterNames = new ArrayList<String>(arguments.size()); for (Object argument : arguments) { Expression argumentExpression = (Expression) argument; ITypeBinding typeBinding = argumentExpression.resolveTypeBinding(); String parameterType = ""; String parameterName = ""; if (typeBinding != null) { parameterType = typeBinding.getName(); parameterName = argumentExpression.toString(); } else { continue; } parameterTypes.add(parameterType); parameterNames.add(parameterName); if (argumentExpression instanceof QualifiedName) /* * Can * be a * constant * access */ { QualifiedName qualifiedName = (QualifiedName) argumentExpression; String fullQualifiedName = qualifiedName.getQualifier().getFullyQualifiedName(); if (fullQualifiedName.startsWith("R.")) /* * Accessing * a R * constant */ { Constant constant = new Constant(); constant.setSourceFileFullPath(sourceFileFullPath); constant.setLine(lineNumber); constant.setType(parameterType); Object constantExpressionValue = qualifiedName.resolveConstantExpressionValue(); if (constantExpressionValue != null) { String constantValueHex = constantExpressionValue.toString(); if (constantExpressionValue instanceof Integer) { Integer integerValue = (Integer) constantExpressionValue; constantValueHex = Integer.toHexString(integerValue); } constant.setValue(constantValueHex); method.getInstructions().add(constant); } } } } // Get the name of the object who owns the method being // called. Expression expression = invoked.getExpression(); String objectName = null; if ((expression != null) && (expression instanceof SimpleName)) { SimpleName simpleName = (SimpleName) expression; objectName = simpleName.getIdentifier(); } // Get the variable, if any, that received the method // returned value ASTNode parent = invoked.getParent(); String assignedVariable = null; if (parent instanceof VariableDeclarationFragment) { VariableDeclarationFragment variableDeclarationFragment = (VariableDeclarationFragment) parent; assignedVariable = variableDeclarationFragment.getName().getIdentifier(); } else if (parent instanceof Assignment) { Assignment assignment = (Assignment) parent; Expression leftHandSide = assignment.getLeftHandSide(); if (leftHandSide instanceof SimpleName) { SimpleName name = (SimpleName) leftHandSide; assignedVariable = name.getIdentifier(); } } // Fill Invoke object and add to the method model. Invoke invoke = new Invoke(); invoke.setLine(lineNumber); invoke.setMethodName(methodSimpleName); invoke.setObjectName(objectName); invoke.setType(getMethodTypeString(isVirtual)); invoke.setReturnType(returnType); invoke.setClassCalled(declaringClassName); invoke.setParameterTypes(parameterTypes); invoke.setParameterNames(parameterNames); invoke.setSourceFileFullPath(sourceFileFullPath); invoke.setAssignedVariable(assignedVariable); method.getInstructions().add(invoke); } return super.visit(node); } @Override public boolean visit(VariableDeclarationExpression node) { return super.visit(node); } @Override public boolean visit(Assignment node) { Expression lhs = node.getLeftHandSide(); String name = ""; if (lhs instanceof SimpleName) { SimpleName simpleName = (SimpleName) lhs; name = simpleName.getIdentifier(); } ITypeBinding typeBinding = lhs.resolveTypeBinding(); String type = typeBinding.getName(); // method.addAssigment(assignment); // TODO Auto-generated method stub return super.visit(node); } }); }
From source file:com.motorolamobility.preflighting.samplechecker.findviewbyid.implementation.FindViewByIdVisitor.java
License:Apache License
/** * Create the App Validator issues found for the checker. * //from w w w . j a va 2 s.c o m * @param invoked method where the problem occurs * @return data containing the issue */ private ValidationResultData createResult(MethodInvocation invoked) { ValidationResultData resultData = new ValidationResultData(); //set the condition related to the problem resultData.setConditionID(id); resultData.setMarkerType(markerType); //set the lines where the problem occurred ArrayList<Integer> lines = new ArrayList<Integer>(); int issuedLine = compilationUnit.getLineNumber(invoked.getStartPosition()); if (issuedLine != -1) { lines.add(issuedLine); } //set the source file associated with the issue File javaFile = (File) compilationUnit.getProperty(CheckerUtils.JAVA_FILE_PROPERTY); resultData.addFileToIssueLines(javaFile, lines); //set description, quick fix, and severity level resultData.setIssueDescription(Messages.FindViewByIdInsideLoopCondition_IssueDescription); resultData.setQuickFixSuggestion(Messages.FindViewByIdInsideLoopCondition_QuickFixSuggestion); resultData.setSeverity(severityLevel); //set the URL with the help associated with developer page regarding this checker resultData.setInfoURL( "http://developer.motorola.com/docstools/library/motodev-app-validator/#unnecessaryFindViewById-unnecessaryFindViewByIdInsideLoops"); return resultData; }
From source file:com.motorolamobility.preflighting.samplechecker.findviewbyid.quickfix.FindViewByIdMarkerResolution.java
License:Apache License
private MethodInvocation getMethodInvocation(final CompilationUnit compUnit, final int lineNumber, MethodInvocation invokedMethod) { final MethodInvocation[] tempMethodInvocation = new MethodInvocation[1]; compUnit.accept(new ASTVisitor() { @Override/*from ww w. ja v a 2s.c om*/ public boolean visit(MethodInvocation node) { if (compUnit.getLineNumber(node.getStartPosition()) == lineNumber) { tempMethodInvocation[0] = node; } return super.visit(node); }; }); if (tempMethodInvocation[0] != null) { invokedMethod = tempMethodInvocation[0]; } return invokedMethod; }