PageRenderTime 34ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/plugins/plugin-java/che-plugin-java-ext-jdt/org-eclipse-jdt-ui/src/main/java/org/eclipse/jdt/internal/ui/fix/UnusedCodeCleanUp.java

https://gitlab.com/unofficial-mirrors/eclipse-che
Java | 227 lines | 165 code | 26 blank | 36 comment | 71 complexity | e8b7c05953156872077ee6b43dfa3ff1 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2011 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.fix;
  12. import org.eclipse.core.runtime.CoreException;
  13. import org.eclipse.jdt.core.ICompilationUnit;
  14. import org.eclipse.jdt.core.JavaCore;
  15. import org.eclipse.jdt.core.compiler.IProblem;
  16. import org.eclipse.jdt.core.dom.CompilationUnit;
  17. import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
  18. import org.eclipse.jdt.internal.corext.fix.UnusedCodeFix;
  19. import org.eclipse.jdt.ui.cleanup.CleanUpRequirements;
  20. import org.eclipse.jdt.ui.cleanup.ICleanUpFix;
  21. import org.eclipse.jdt.ui.text.java.IProblemLocation;
  22. import java.util.ArrayList;
  23. import java.util.Hashtable;
  24. import java.util.List;
  25. import java.util.Map;
  26. /**
  27. * Create fixes which can remove unused code
  28. * @see org.eclipse.jdt.internal.corext.fix.UnusedCodeFix
  29. *
  30. */
  31. public class UnusedCodeCleanUp extends AbstractMultiFix {
  32. public UnusedCodeCleanUp(Map<String, String> options) {
  33. super(options);
  34. }
  35. public UnusedCodeCleanUp() {
  36. super();
  37. }
  38. /**
  39. * {@inheritDoc}
  40. */
  41. @Override
  42. public CleanUpRequirements getRequirements() {
  43. boolean requireAST= requireAST();
  44. Map<String, String> requiredOptions= requireAST ? getRequiredOptions() : null;
  45. return new CleanUpRequirements(requireAST, false, false, requiredOptions);
  46. }
  47. private boolean requireAST() {
  48. boolean removeUnuseMembers= isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS);
  49. return removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS) ||
  50. removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS) ||
  51. removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS) ||
  52. removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES) ||
  53. isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES) ||
  54. isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS) && !isEnabled(CleanUpConstants.ORGANIZE_IMPORTS);
  55. }
  56. /**
  57. * {@inheritDoc}
  58. */
  59. @Override
  60. protected ICleanUpFix createFix(CompilationUnit compilationUnit) throws CoreException {
  61. boolean removeUnuseMembers= isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS);
  62. return UnusedCodeFix.createCleanUp(compilationUnit,
  63. removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS),
  64. removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS),
  65. removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS),
  66. removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES),
  67. isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES),
  68. isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS) &&
  69. !isEnabled(CleanUpConstants.ORGANIZE_IMPORTS),
  70. false);
  71. }
  72. /**
  73. * {@inheritDoc}
  74. */
  75. @Override
  76. protected ICleanUpFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException {
  77. boolean removeMembers= isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS);
  78. return UnusedCodeFix.createCleanUp(compilationUnit, problems,
  79. removeMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS),
  80. removeMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS),
  81. removeMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS),
  82. removeMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES),
  83. isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES),
  84. isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS) &&
  85. !isEnabled(CleanUpConstants.ORGANIZE_IMPORTS),
  86. false);
  87. }
  88. public Map<String, String> getRequiredOptions() {
  89. Map<String, String> result= new Hashtable<String, String>();
  90. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS) && !isEnabled(CleanUpConstants.ORGANIZE_IMPORTS))
  91. result.put(JavaCore.COMPILER_PB_UNUSED_IMPORT, JavaCore.WARNING);
  92. boolean removeMembers= isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS);
  93. if (removeMembers && (
  94. isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS) ||
  95. isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS) ||
  96. isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS) ||
  97. isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES)))
  98. result.put(JavaCore.COMPILER_PB_UNUSED_PRIVATE_MEMBER, JavaCore.WARNING);
  99. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES))
  100. result.put(JavaCore.COMPILER_PB_UNUSED_LOCAL, JavaCore.WARNING);
  101. return result;
  102. }
  103. /**
  104. * {@inheritDoc}
  105. */
  106. @Override
  107. public String[] getStepDescriptions() {
  108. List<String> result= new ArrayList<String>();
  109. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS))
  110. result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedImport_description);
  111. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS))
  112. result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedMethod_description);
  113. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS))
  114. result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedConstructor_description);
  115. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES))
  116. result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedType_description);
  117. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS))
  118. result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedField_description);
  119. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES))
  120. result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedVariable_description);
  121. return result.toArray(new String[result.size()]);
  122. }
  123. /**
  124. * {@inheritDoc}
  125. */
  126. @Override
  127. public String getPreview() {
  128. StringBuffer buf= new StringBuffer();
  129. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS)) {
  130. } else {
  131. buf.append("import pack.Bar;\n"); //$NON-NLS-1$
  132. }
  133. buf.append("class Example {\n"); //$NON-NLS-1$
  134. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES)) {
  135. } else {
  136. buf.append(" private class Sub {}\n"); //$NON-NLS-1$
  137. }
  138. buf.append(" public Example(boolean b) {}\n"); //$NON-NLS-1$
  139. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS)) {
  140. } else {
  141. buf.append(" private Example() {}\n"); //$NON-NLS-1$
  142. }
  143. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS)) {
  144. } else {
  145. buf.append(" private int fField;\n"); //$NON-NLS-1$
  146. }
  147. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS)) {
  148. } else {
  149. buf.append(" private void foo() {}\n"); //$NON-NLS-1$
  150. }
  151. buf.append(" public void bar() {\n"); //$NON-NLS-1$
  152. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES)) {
  153. } else {
  154. buf.append(" int i= 10;\n"); //$NON-NLS-1$
  155. }
  156. buf.append(" }\n"); //$NON-NLS-1$
  157. buf.append("}\n"); //$NON-NLS-1$
  158. return buf.toString();
  159. }
  160. /**
  161. * {@inheritDoc}
  162. */
  163. public boolean canFix(ICompilationUnit compilationUnit, IProblemLocation problem) {
  164. if (UnusedCodeFix.isUnusedImport(problem))
  165. return isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS);
  166. if (UnusedCodeFix.isUnusedMember(problem))
  167. return isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS) ||
  168. isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS) ||
  169. isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES) ||
  170. isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS) ||
  171. isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES);
  172. return false;
  173. }
  174. /**
  175. * {@inheritDoc}
  176. */
  177. @Override
  178. public int computeNumberOfFixes(CompilationUnit compilationUnit) {
  179. int result= 0;
  180. IProblem[] problems= compilationUnit.getProblems();
  181. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS) && !isEnabled(CleanUpConstants.ORGANIZE_IMPORTS)) {
  182. for (int i=0;i<problems.length;i++) {
  183. int id= problems[i].getID();
  184. if (id == IProblem.UnusedImport || id == IProblem.DuplicateImport || id == IProblem.ConflictingImport ||
  185. id == IProblem.CannotImportPackage || id == IProblem.ImportNotFound)
  186. result++;
  187. }
  188. }
  189. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS))
  190. result+= getNumberOfProblems(problems, IProblem.UnusedPrivateMethod);
  191. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS))
  192. result+= getNumberOfProblems(problems, IProblem.UnusedPrivateConstructor);
  193. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES))
  194. result+= getNumberOfProblems(problems, IProblem.UnusedPrivateType);
  195. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS))
  196. result+= getNumberOfProblems(problems, IProblem.UnusedPrivateField);
  197. if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES))
  198. result+= getNumberOfProblems(problems, IProblem.LocalVariableIsNeverUsed);
  199. return result;
  200. }
  201. }