下面列出了org.eclipse.jdt.core.dom.MethodDeclaration#getName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
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);
}
@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);
}
/**
* 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);
}
}
}
}
}
}
}
}
}
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));
}
}
}
}
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));
}
}
}
}
protected SimpleName rename(ASTNode node, SimpleName newName) {
MethodDeclaration method = (MethodDeclaration) node;
SimpleName oldName = method.getName();
method.setName(newName);
return oldName;
}