PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/plug-ins/helios/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/ReturnTypeSubProcessor.java

https://github.com/vazexqi/CodingSpectator
Java | 337 lines | 271 code | 55 blank | 11 comment | 60 complexity | 0825ec2ed69bc016b040fb0d2f507271 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2010 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.eclipse.jdt.internal.ui.text.correction;
  12. import java.util.ArrayList;
  13. import java.util.Collection;
  14. import org.eclipse.swt.graphics.Image;
  15. import org.eclipse.jdt.core.ICompilationUnit;
  16. import org.eclipse.jdt.core.JavaModelException;
  17. import org.eclipse.jdt.core.dom.AST;
  18. import org.eclipse.jdt.core.dom.ASTNode;
  19. import org.eclipse.jdt.core.dom.ASTVisitor;
  20. import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
  21. import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration;
  22. import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
  23. import org.eclipse.jdt.core.dom.Block;
  24. import org.eclipse.jdt.core.dom.BodyDeclaration;
  25. import org.eclipse.jdt.core.dom.CompilationUnit;
  26. import org.eclipse.jdt.core.dom.EnumDeclaration;
  27. import org.eclipse.jdt.core.dom.Expression;
  28. import org.eclipse.jdt.core.dom.ITypeBinding;
  29. import org.eclipse.jdt.core.dom.Javadoc;
  30. import org.eclipse.jdt.core.dom.MethodDeclaration;
  31. import org.eclipse.jdt.core.dom.PrimitiveType;
  32. import org.eclipse.jdt.core.dom.ReturnStatement;
  33. import org.eclipse.jdt.core.dom.TagElement;
  34. import org.eclipse.jdt.core.dom.TextElement;
  35. import org.eclipse.jdt.core.dom.Type;
  36. import org.eclipse.jdt.core.dom.TypeDeclaration;
  37. import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
  38. import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
  39. import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext;
  40. import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext;
  41. import org.eclipse.jdt.internal.corext.dom.ASTNodes;
  42. import org.eclipse.jdt.internal.corext.dom.Bindings;
  43. import org.eclipse.jdt.internal.corext.util.Messages;
  44. import org.eclipse.jdt.ui.text.java.IInvocationContext;
  45. import org.eclipse.jdt.ui.text.java.IProblemLocation;
  46. import org.eclipse.jdt.internal.ui.JavaPluginImages;
  47. import org.eclipse.jdt.internal.ui.text.correction.proposals.ASTRewriteCorrectionProposal;
  48. import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal;
  49. import org.eclipse.jdt.internal.ui.text.correction.proposals.MissingReturnTypeCorrectionProposal;
  50. import org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal;
  51. import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
  52. import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider;
  53. public class ReturnTypeSubProcessor {
  54. private static class ReturnStatementCollector extends ASTVisitor {
  55. private ArrayList fResult= new ArrayList();
  56. public ITypeBinding getTypeBinding(AST ast) {
  57. boolean couldBeObject= false;
  58. for (int i= 0; i < fResult.size(); i++) {
  59. ReturnStatement node= (ReturnStatement) fResult.get(i);
  60. Expression expr= node.getExpression();
  61. if (expr != null) {
  62. ITypeBinding binding= Bindings.normalizeTypeBinding(expr.resolveTypeBinding());
  63. if (binding != null) {
  64. return binding;
  65. } else {
  66. couldBeObject= true;
  67. }
  68. } else {
  69. return ast.resolveWellKnownType("void"); //$NON-NLS-1$
  70. }
  71. }
  72. if (couldBeObject) {
  73. return ast.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
  74. }
  75. return ast.resolveWellKnownType("void"); //$NON-NLS-1$
  76. }
  77. public boolean visit(ReturnStatement node) {
  78. fResult.add(node);
  79. return false;
  80. }
  81. public boolean visit(AnonymousClassDeclaration node) {
  82. return false;
  83. }
  84. public boolean visit(TypeDeclaration node) {
  85. return false;
  86. }
  87. public boolean visit(EnumDeclaration node) {
  88. return false;
  89. }
  90. public boolean visit(AnnotationTypeDeclaration node) {
  91. return false;
  92. }
  93. }
  94. public static void addMethodWithConstrNameProposals(IInvocationContext context, IProblemLocation problem, Collection proposals) {
  95. ICompilationUnit cu= context.getCompilationUnit();
  96. ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
  97. if (selectedNode instanceof MethodDeclaration) {
  98. MethodDeclaration declaration= (MethodDeclaration) selectedNode;
  99. ASTRewrite rewrite= ASTRewrite.create(declaration.getAST());
  100. rewrite.set(declaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.TRUE, null);
  101. String label= CorrectionMessages.ReturnTypeSubProcessor_constrnamemethod_description;
  102. Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
  103. ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, 5, image);
  104. proposals.add(proposal);
  105. }
  106. }
  107. public static void addVoidMethodReturnsProposals(IInvocationContext context, IProblemLocation problem, Collection proposals) {
  108. ICompilationUnit cu= context.getCompilationUnit();
  109. CompilationUnit astRoot= context.getASTRoot();
  110. ASTNode selectedNode= problem.getCoveringNode(astRoot);
  111. if (selectedNode == null) {
  112. return;
  113. }
  114. BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(selectedNode);
  115. if (decl instanceof MethodDeclaration && selectedNode.getNodeType() == ASTNode.RETURN_STATEMENT) {
  116. ReturnStatement returnStatement= (ReturnStatement) selectedNode;
  117. Expression expr= returnStatement.getExpression();
  118. if (expr != null) {
  119. AST ast= astRoot.getAST();
  120. ITypeBinding binding= Bindings.normalizeTypeBinding(expr.resolveTypeBinding());
  121. if (binding == null) {
  122. binding= ast.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
  123. }
  124. if (binding.isWildcardType()) {
  125. binding= ASTResolving.normalizeWildcardType(binding, true, ast);
  126. }
  127. MethodDeclaration methodDeclaration= (MethodDeclaration) decl;
  128. ASTRewrite rewrite= ASTRewrite.create(ast);
  129. String label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_voidmethodreturns_description, BindingLabelProvider.getBindingLabel(binding, BindingLabelProvider.DEFAULT_TEXTFLAGS));
  130. Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
  131. LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, cu, rewrite, 6, image);
  132. ImportRewrite imports= proposal.createImportRewrite(astRoot);
  133. ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(methodDeclaration, imports);
  134. Type newReturnType= imports.addImport(binding, ast, importRewriteContext);
  135. if (methodDeclaration.isConstructor()) {
  136. rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);
  137. rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, newReturnType, null);
  138. } else {
  139. rewrite.replace(methodDeclaration.getReturnType2(), newReturnType, null);
  140. }
  141. String key= "return_type"; //$NON-NLS-1$
  142. proposal.addLinkedPosition(rewrite.track(newReturnType), true, key);
  143. ITypeBinding[] bindings= ASTResolving.getRelaxingTypes(ast, binding);
  144. for (int i= 0; i < bindings.length; i++) {
  145. proposal.addLinkedPositionProposal(key, bindings[i]);
  146. }
  147. Javadoc javadoc= methodDeclaration.getJavadoc();
  148. if (javadoc != null) {
  149. TagElement newTag= ast.newTagElement();
  150. newTag.setTagName(TagElement.TAG_RETURN);
  151. TextElement commentStart= ast.newTextElement();
  152. newTag.fragments().add(commentStart);
  153. JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
  154. proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start"); //$NON-NLS-1$
  155. }
  156. proposals.add(proposal);
  157. }
  158. ASTRewrite rewrite= ASTRewrite.create(decl.getAST());
  159. rewrite.remove(returnStatement.getExpression(), null);
  160. String label= CorrectionMessages.ReturnTypeSubProcessor_removereturn_description;
  161. Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
  162. ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, 5, image);
  163. proposals.add(proposal);
  164. }
  165. }
  166. public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection proposals) {
  167. ICompilationUnit cu= context.getCompilationUnit();
  168. CompilationUnit astRoot= context.getASTRoot();
  169. ASTNode selectedNode= problem.getCoveringNode(astRoot);
  170. if (selectedNode == null) {
  171. return;
  172. }
  173. BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(selectedNode);
  174. if (decl instanceof MethodDeclaration) {
  175. MethodDeclaration methodDeclaration= (MethodDeclaration) decl;
  176. ReturnStatementCollector eval= new ReturnStatementCollector();
  177. decl.accept(eval);
  178. AST ast= astRoot.getAST();
  179. ITypeBinding typeBinding= eval.getTypeBinding(decl.getAST());
  180. typeBinding= Bindings.normalizeTypeBinding(typeBinding);
  181. if (typeBinding == null) {
  182. typeBinding= ast.resolveWellKnownType("void"); //$NON-NLS-1$
  183. }
  184. if (typeBinding.isWildcardType()) {
  185. typeBinding= ASTResolving.normalizeWildcardType(typeBinding, true, ast);
  186. }
  187. ASTRewrite rewrite= ASTRewrite.create(ast);
  188. String label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_missingreturntype_description, BindingLabelProvider.getBindingLabel(typeBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS));
  189. Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
  190. LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, cu, rewrite, 6, image);
  191. ImportRewrite imports= proposal.createImportRewrite(astRoot);
  192. ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(decl, imports);
  193. Type type= imports.addImport(typeBinding, ast, importRewriteContext);
  194. rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
  195. rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);
  196. Javadoc javadoc= methodDeclaration.getJavadoc();
  197. if (javadoc != null && typeBinding != null) {
  198. TagElement newTag= ast.newTagElement();
  199. newTag.setTagName(TagElement.TAG_RETURN);
  200. TextElement commentStart= ast.newTextElement();
  201. newTag.fragments().add(commentStart);
  202. JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
  203. proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start"); //$NON-NLS-1$
  204. }
  205. String key= "return_type"; //$NON-NLS-1$
  206. proposal.addLinkedPosition(rewrite.track(type), true, key);
  207. if (typeBinding != null) {
  208. ITypeBinding[] bindings= ASTResolving.getRelaxingTypes(ast, typeBinding);
  209. for (int i= 0; i < bindings.length; i++) {
  210. proposal.addLinkedPositionProposal(key, bindings[i]);
  211. }
  212. }
  213. proposals.add(proposal);
  214. // change to constructor
  215. ASTNode parentType= ASTResolving.findParentType(decl);
  216. if (parentType instanceof AbstractTypeDeclaration) {
  217. boolean isInterface= parentType instanceof TypeDeclaration && ((TypeDeclaration) parentType).isInterface();
  218. if (!isInterface) {
  219. String constructorName= ((AbstractTypeDeclaration) parentType).getName().getIdentifier();
  220. ASTNode nameNode= methodDeclaration.getName();
  221. label= Messages.format(CorrectionMessages.ReturnTypeSubProcessor_wrongconstructorname_description, BasicElementLabels.getJavaElementName(constructorName));
  222. proposals.add(new ReplaceCorrectionProposal(label, cu, nameNode.getStartPosition(), nameNode.getLength(), constructorName, 5));
  223. }
  224. }
  225. }
  226. }
  227. public static void addMissingReturnStatementProposals(IInvocationContext context, IProblemLocation problem, Collection proposals) {
  228. ICompilationUnit cu= context.getCompilationUnit();
  229. ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
  230. if (selectedNode == null) {
  231. return;
  232. }
  233. BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(selectedNode);
  234. if (decl instanceof MethodDeclaration) {
  235. MethodDeclaration methodDecl= (MethodDeclaration) decl;
  236. Block block= methodDecl.getBody();
  237. if (block == null) {
  238. return;
  239. }
  240. ReturnStatement existingStatement= (selectedNode instanceof ReturnStatement) ? (ReturnStatement) selectedNode : null;
  241. proposals.add( new MissingReturnTypeCorrectionProposal(cu, methodDecl, existingStatement, 6));
  242. Type returnType= methodDecl.getReturnType2();
  243. if (returnType != null && !"void".equals(ASTNodes.asString(returnType))) { //$NON-NLS-1$
  244. AST ast= methodDecl.getAST();
  245. ASTRewrite rewrite= ASTRewrite.create(ast);
  246. rewrite.replace(returnType, ast.newPrimitiveType(PrimitiveType.VOID), null);
  247. Javadoc javadoc= methodDecl.getJavadoc();
  248. if (javadoc != null) {
  249. TagElement tagElement= JavadocTagsSubProcessor.findTag(javadoc, TagElement.TAG_RETURN, null);
  250. if (tagElement != null) {
  251. rewrite.remove(tagElement, null);
  252. }
  253. }
  254. String label= CorrectionMessages.ReturnTypeSubProcessor_changetovoid_description;
  255. Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
  256. ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, rewrite, 5, image);
  257. proposals.add(proposal);
  258. }
  259. }
  260. }
  261. public static void addMethodRetunsVoidProposals(IInvocationContext context, IProblemLocation problem, Collection proposals) throws JavaModelException {
  262. CompilationUnit astRoot= context.getASTRoot();
  263. ASTNode selectedNode= problem.getCoveringNode(astRoot);
  264. if (!(selectedNode instanceof ReturnStatement)) {
  265. return;
  266. }
  267. ReturnStatement returnStatement= (ReturnStatement) selectedNode;
  268. Expression expression= returnStatement.getExpression();
  269. if (expression == null) {
  270. return;
  271. }
  272. BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(selectedNode);
  273. if (decl instanceof MethodDeclaration) {
  274. MethodDeclaration methDecl= (MethodDeclaration) decl;
  275. Type retType= methDecl.getReturnType2();
  276. if (retType == null || retType.resolveBinding() == null) {
  277. return;
  278. }
  279. TypeMismatchSubProcessor.addChangeSenderTypeProposals(context, expression, retType.resolveBinding(), false, 4, proposals);
  280. }
  281. }
  282. }