List of usage examples for org.eclipse.jdt.core.dom MethodInvocation setSourceRange
public final void setSourceRange(int startPosition, int length)
From source file:com.windowtester.eclipse.ui.convert.WTConvertAPIContext.java
License:Open Source License
/** * Construct a new method invocation// w ww.ja v a 2s.c om * * @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:org.eclipse.objectteams.otdt.internal.ui.text.correction.MappingProposalSubProcessor.java
License:Open Source License
/** * Add proposals to create method for unresolved method spec. * @param selectedNode the unresolved method spec * @param enclosingType the enclosing role * @param context invocation context as needed to find covering nodes * @param problem the problem that triggered the assist * @param proposals list of proposals to which to add the new proposal * @throws CoreException if some compilation unit could not be found *//*from w w w . j a v a 2 s. c o m*/ @SuppressWarnings({ "rawtypes", "unchecked" }) public static void addUnresolvedMethodSpecProposals(ASTNode selectedNode, TypeDeclaration enclosingType, IInvocationContext context, final IProblemLocation problem, Collection proposals) throws CoreException { // Note: this completion proposal needs to create new ast nodes in order to // provide a MethodInvocation serving as a template for the proposal. // While these nodes point to the existing enclosingType as their parent, // the "real" ast has no reference to the faked nodes. AST ast = selectedNode.getAST(); while (selectedNode.getNodeType() != ASTNode.METHOD_SPEC) { selectedNode = selectedNode.getParent(); if (selectedNode == null) return; } MethodSpec methodSpec = (MethodSpec) selectedNode; // construct a faked problem location: // a new method, with unidirectional linkage to its 'parent': MethodDeclaration fakeMethod = ast.newFakedMethodDeclaration(enclosingType); fakeMethod.setName(ast.newSimpleName(FAKED_METHOD)); { ASTNode mapping = methodSpec.getParent(); fakeMethod.setSourceRange(mapping.getStartPosition(), mapping.getLength()); } // a new method invocation: MethodInvocation invoc = ast.newMethodInvocation(); invoc.setSourceRange(methodSpec.getStartPosition(), methodSpec.getLength()); // receiver: StructuralPropertyDescriptor locationInParent = methodSpec.getLocationInParent(); if (locationInParent == CalloutMappingDeclaration.BASE_MAPPING_ELEMENT_PROPERTY || locationInParent == CallinMappingDeclaration.BASE_MAPPING_ELEMENTS_PROPERTY) { if (enclosingType.getNodeType() == ASTNode.ROLE_TYPE_DECLARATION) { // create a receiver of the baseclass type: Type baseType = ((RoleTypeDeclaration) enclosingType).getBaseClassType(); invoc.setExpression(ast.newResolvedVariableName(new String(IOTConstants._OT_BASE_ARG), baseType)); } } else { // using an explicit this-typed receiver avoids proposal to define method in the enclosing team: invoc.setExpression(ast.newResolvedVariableName(FAKETHIS, enclosingType)); } // final for the IProblemLocation-adaptor below: final SimpleName selector = (SimpleName) ASTNode.copySubtree(ast, methodSpec.getName()); selector.setSourceRange(methodSpec.getName().getStartPosition(), methodSpec.getName().getLength()); invoc.setName(selector); // set args/parameters for both: for (Object elem : methodSpec.parameters()) { SingleVariableDeclaration param = (SingleVariableDeclaration) elem; fakeMethod.parameters().add(ASTNode.copySubtree(ast, param)); invoc.arguments().add(ast.newSimpleName(param.getName().getIdentifier())); } // assemble and add to type: fakeMethod.setBody(ast.newBlock()); fakeMethod.getBody().statements().add(ast.newExpressionStatement(invoc)); // wrap the problem: IProblemLocation newProblem = new IProblemLocation() { public ASTNode getCoveredNode(CompilationUnit astRoot) { return selector; } public ASTNode getCoveringNode(CompilationUnit astRoot) { return selector; } public int getLength() { return selector.getLength(); } public String getMarkerType() { return problem.getMarkerType(); } public int getOffset() { return selector.getStartPosition(); } public String[] getProblemArguments() { return problem.getProblemArguments(); } public int getProblemId() { return problem.getProblemId(); } public boolean isError() { return true; } }; UnresolvedElementsSubProcessor.getMethodProposals(context, newProblem, problem.getProblemId() == IProblem.DifferentParamInCallinMethodSpec, proposals); // parameters may have been set to Object, because types could not be resolved. // adjust these from the original method spec now: for (Object proposal : proposals) { if (proposal instanceof NewMethodCorrectionProposal) { NewMethodCorrectionProposal methodProposal = (NewMethodCorrectionProposal) proposal; OTQuickFixes.instance().registerNewMethodCorrectionProposal(methodSpec, methodProposal); methodProposal.setDisplayName(updateDisplayName(methodSpec, methodProposal.getDisplayString())); } } }