Java Code Examples for org.eclipse.jdt.core.dom.MethodDeclaration#getName()

The following examples show how to use org.eclipse.jdt.core.dom.MethodDeclaration#getName() . 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: MethodFinder.java    From lapse-plus with GNU General Public License v3.0 6 votes vote down vote up
public IMethod convertMethodDecl2IMethod(MethodDeclaration methodDecl){
	SimpleName methodName = methodDecl.getName();
	//cu.accept(visitor);
	
	try {
		ICompilationUnit iCompilationUnit = JavaCore.createCompilationUnitFrom((IFile) resource);
		
		int startPos = methodDecl.getStartPosition();
		IJavaElement element = iCompilationUnit.getElementAt(startPos);
		if(element instanceof IMethod) {
			return (IMethod) element;
		}
		return null;
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
	return null;
}
 
Example 2
Source File: MethodDiff.java    From apidiff with MIT License 5 votes vote down vote up
/**
 * Returning method full name. [access modifier + return + name + (parameters list)]
 * @param methodVersion
 * @return
 */
private String getFullNameMethod(MethodDeclaration methodVersion){
	String nameMethod = "";
	if(methodVersion != null){
		String modifiersMethod = (methodVersion.modifiers() != null) ? (StringUtils.join(methodVersion.modifiers(), " ") + " ") : " ";
		String returnMethod = (methodVersion.getReturnType2() != null) ? (methodVersion.getReturnType2().toString() + " ") : "";
		String parametersMethod = (methodVersion.parameters() != null) ? StringUtils.join(methodVersion.parameters(), ", ") : " ";
		nameMethod = modifiersMethod + returnMethod + methodVersion.getName() + "(" + parametersMethod + ")";
	}
	return nameMethod;
}
 
Example 3
Source File: MethodDiff.java    From apidiff with MIT License 5 votes vote down vote up
/**
 * Returning method name. Example: [name(parameters list)]
 * @param methodVersion
 * @return
 */
private String getSimpleNameMethod(MethodDeclaration methodVersion){
	String nameMethod = "";
	if(methodVersion != null){
		String parametersMethod = (methodVersion.parameters() != null) ? StringUtils.join(methodVersion.parameters(), ", ") : " ";
		nameMethod = methodVersion.getName() + "(" + parametersMethod + ")";
	}
	return nameMethod;
}
 
Example 4
Source File: MethodDiff.java    From apidiff with MIT License 5 votes vote down vote up
/**
 * Returning type + method. Example: org.felines.Tiger#setAge(int)
 * @param method
 * @param type
 * @return
 */
private String getFullNameMethodAndPath(final MethodDeclaration method, final TypeDeclaration type){
	String nameMethod = "";
	if(method != null){
		nameMethod = UtilTools.getPath(type) + "#" + method.getName() + "(" + this.getSignatureMethod(method) + ")";
	}
	return nameMethod;
}
 
Example 5
Source File: MethodInvocationResolver.java    From KodeBeagle with Apache License 2.0 5 votes vote down vote up
private MethodDecl getMethodDecl(MethodDeclaration node) {
    String qualifiedTypeName = currentPackage + "."
            + Joiner.on(".").skipNulls().join(typesInFile);
    SimpleName nameNode = node.getName();
    String methodName = nameNode.toString();
    String returnType = "";
    if (node.getReturnType2() != null) {
        returnType = getNameOfType(node.getReturnType2());
    }

    Map<String, String> params = new HashMap<>();
    for (Object p : node.parameters()) {
        if (p instanceof SingleVariableDeclaration) {

            SingleVariableDeclaration svd = (SingleVariableDeclaration) p;
            String varName = svd.getName().toString();
            Type type = svd.getType();
            String typeName = getNameOfType(type);

            params.put(varName, typeName);
        } else {
            System.err.println("Unxepected AST node type for param - " + p);
        }

    }
    return new MethodDecl(methodName, qualifiedTypeName, returnType, nameNode
            .getStartPosition(), params);
}
 
Example 6
Source File: ImplementOccurrencesFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(MethodDeclaration node) {
	IMethodBinding binding= node.resolveBinding();
	if (binding != null && !Modifier.isStatic(binding.getModifiers())) {
		IMethodBinding method= Bindings.findOverriddenMethodInHierarchy(fSelectedType, binding);
		if (method != null) {
			SimpleName name= node.getName();
			fResult.add(new OccurrenceLocation(name.getStartPosition(), name.getLength(), 0, fDescription));
		}
	}
	return super.visit(node);
}
 
Example 7
Source File: CopyResourceElementsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Renames the main type in <code>cu</code>.
 */
private void updateTypeName(ICompilationUnit cu, CompilationUnit astCU, String oldName, String newName, ASTRewrite rewriter) throws JavaModelException {
	if (newName != null) {
		String oldTypeName= Util.getNameWithoutJavaLikeExtension(oldName);
		String newTypeName= Util.getNameWithoutJavaLikeExtension(newName);
		AST ast = astCU.getAST();
		// update main type name
		IType[] types = cu.getTypes();
		for (int i = 0, max = types.length; i < max; i++) {
			IType currentType = types[i];
			if (currentType.getElementName().equals(oldTypeName)) {
				AbstractTypeDeclaration typeNode = (AbstractTypeDeclaration) ((JavaElement) currentType).findNode(astCU);
				if (typeNode != null) {
					// rename type
					rewriter.replace(typeNode.getName(), ast.newSimpleName(newTypeName), null);
					// rename constructors
					Iterator bodyDeclarations = typeNode.bodyDeclarations().iterator();
					while (bodyDeclarations.hasNext()) {
						Object bodyDeclaration = bodyDeclarations.next();
						if (bodyDeclaration instanceof MethodDeclaration) {
							MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
							if (methodDeclaration.isConstructor()) {
								SimpleName methodName = methodDeclaration.getName();
								if (methodName.getIdentifier().equals(oldTypeName)) {
									rewriter.replace(methodName, ast.newSimpleName(newTypeName), null);
								}
							}
						}
					}
				}
			}
		}
	}
}
 
Example 8
Source File: ReturnTypeSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();

	CompilationUnit astRoot= context.getASTRoot();
	ASTNode selectedNode= problem.getCoveringNode(astRoot);
	if (selectedNode == null) {
		return;
	}
	BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(selectedNode);
	if (decl instanceof MethodDeclaration) {
		MethodDeclaration methodDeclaration= (MethodDeclaration) decl;

		ReturnStatementCollector eval= new ReturnStatementCollector();
		decl.accept(eval);

		AST ast= astRoot.getAST();

		ITypeBinding typeBinding= eval.getTypeBinding(decl.getAST());
		typeBinding= Bindings.normalizeTypeBinding(typeBinding);
		if (typeBinding == null) {
			typeBinding= ast.resolveWellKnownType("void"); //$NON-NLS-1$
		}
		if (typeBinding.isWildcardType()) {
			typeBinding= ASTResolving.normalizeWildcardType(typeBinding, true, ast);
		}

		ASTRewrite rewrite= ASTRewrite.create(ast);

		String label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_missingreturntype_description, BindingLabelProviderCore.getBindingLabel(typeBinding, BindingLabelProviderCore.DEFAULT_TEXTFLAGS));
		LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, CodeActionKind.QuickFix, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE);

		ImportRewrite imports= proposal.createImportRewrite(astRoot);
		ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(decl, imports);
		Type type= imports.addImport(typeBinding, ast, importRewriteContext, TypeLocation.RETURN_TYPE);

		rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
		rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);

		Javadoc javadoc= methodDeclaration.getJavadoc();
		if (javadoc != null && typeBinding != null) {
			TagElement newTag= ast.newTagElement();
			newTag.setTagName(TagElement.TAG_RETURN);
			TextElement commentStart= ast.newTextElement();
			newTag.fragments().add(commentStart);

			JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
			proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start"); //$NON-NLS-1$
		}

		String key= "return_type"; //$NON-NLS-1$
		proposal.addLinkedPosition(rewrite.track(type), true, key);
		if (typeBinding != null) {
			ITypeBinding[] bindings= ASTResolving.getRelaxingTypes(ast, typeBinding);
			for (int i= 0; i < bindings.length; i++) {
				proposal.addLinkedPositionProposal(key, bindings[i]);
			}
		}

		proposals.add(proposal);

		// change to constructor
		ASTNode parentType= ASTResolving.findParentType(decl);
		if (parentType instanceof AbstractTypeDeclaration) {
			boolean isInterface= parentType instanceof TypeDeclaration && ((TypeDeclaration) parentType).isInterface();
			if (!isInterface) {
				String constructorName= ((AbstractTypeDeclaration) parentType).getName().getIdentifier();
				ASTNode nameNode= methodDeclaration.getName();
				label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_wrongconstructorname_description, BasicElementLabels.getJavaElementName(constructorName));
				proposals.add(new ReplaceCorrectionProposal(label, cu, nameNode.getStartPosition(), nameNode.getLength(), constructorName, IProposalRelevance.CHANGE_TO_CONSTRUCTOR));
			}
		}
	}
}
 
Example 9
Source File: ReturnTypeSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	ICompilationUnit cu= context.getCompilationUnit();

	CompilationUnit astRoot= context.getASTRoot();
	ASTNode selectedNode= problem.getCoveringNode(astRoot);
	if (selectedNode == null) {
		return;
	}
	BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(selectedNode);
	if (decl instanceof MethodDeclaration) {
		MethodDeclaration methodDeclaration= (MethodDeclaration) decl;

		ReturnStatementCollector eval= new ReturnStatementCollector();
		decl.accept(eval);

		AST ast= astRoot.getAST();

		ITypeBinding typeBinding= eval.getTypeBinding(decl.getAST());
		typeBinding= Bindings.normalizeTypeBinding(typeBinding);
		if (typeBinding == null) {
			typeBinding= ast.resolveWellKnownType("void"); //$NON-NLS-1$
		}
		if (typeBinding.isWildcardType()) {
			typeBinding= ASTResolving.normalizeWildcardType(typeBinding, true, ast);
		}

		ASTRewrite rewrite= ASTRewrite.create(ast);

		String label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_missingreturntype_description, BindingLabelProvider.getBindingLabel(typeBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS));
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
		LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE, image);

		ImportRewrite imports= proposal.createImportRewrite(astRoot);
		ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(decl, imports);
		Type type= imports.addImport(typeBinding, ast, importRewriteContext);

		rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
		rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);

		Javadoc javadoc= methodDeclaration.getJavadoc();
		if (javadoc != null && typeBinding != null) {
			TagElement newTag= ast.newTagElement();
			newTag.setTagName(TagElement.TAG_RETURN);
			TextElement commentStart= ast.newTextElement();
			newTag.fragments().add(commentStart);

			JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
			proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start"); //$NON-NLS-1$
		}

		String key= "return_type"; //$NON-NLS-1$
		proposal.addLinkedPosition(rewrite.track(type), true, key);
		if (typeBinding != null) {
			ITypeBinding[] bindings= ASTResolving.getRelaxingTypes(ast, typeBinding);
			for (int i= 0; i < bindings.length; i++) {
				proposal.addLinkedPositionProposal(key, bindings[i]);
			}
		}

		proposals.add(proposal);

		// change to constructor
		ASTNode parentType= ASTResolving.findParentType(decl);
		if (parentType instanceof AbstractTypeDeclaration) {
			boolean isInterface= parentType instanceof TypeDeclaration && ((TypeDeclaration) parentType).isInterface();
			if (!isInterface) {
				String constructorName= ((AbstractTypeDeclaration) parentType).getName().getIdentifier();
				ASTNode nameNode= methodDeclaration.getName();
				label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_wrongconstructorname_description, BasicElementLabels.getJavaElementName(constructorName));
				proposals.add(new ReplaceCorrectionProposal(label, cu, nameNode.getStartPosition(), nameNode.getLength(), constructorName, IProposalRelevance.CHANGE_TO_CONSTRUCTOR));
			}
		}
	}
}
 
Example 10
Source File: CreateMethodOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected SimpleName rename(ASTNode node, SimpleName newName) {
	MethodDeclaration method = (MethodDeclaration) node;
	SimpleName oldName = method.getName();
	method.setName(newName);
	return oldName;
}