PageRenderTime 62ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.jdt.junit.source_3.7.0.v20110505-0800/org/eclipse/jdt/internal/junit/ui/JUnitQuickFixProcessor.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 348 lines | 263 code | 47 blank | 38 comment | 50 complexity | c1fc7ecb9ad75c8bd394694ebcac4065 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. * Sebastian Davids <sdavids@gmx.de> - bug 48696
  11. *******************************************************************************/
  12. package org.eclipse.jdt.internal.junit.ui;
  13. import java.lang.reflect.InvocationTargetException;
  14. import java.util.ArrayList;
  15. import java.util.HashSet;
  16. import org.eclipse.swt.graphics.Image;
  17. import org.eclipse.swt.graphics.Point;
  18. import org.eclipse.core.runtime.CoreException;
  19. import org.eclipse.core.runtime.IProgressMonitor;
  20. import org.eclipse.core.runtime.NullProgressMonitor;
  21. import org.eclipse.core.runtime.OperationCanceledException;
  22. import org.eclipse.core.resources.IFile;
  23. import org.eclipse.text.edits.MalformedTreeException;
  24. import org.eclipse.text.edits.TextEdit;
  25. import org.eclipse.jface.operation.IRunnableWithProgress;
  26. import org.eclipse.jface.text.BadLocationException;
  27. import org.eclipse.jface.text.IDocument;
  28. import org.eclipse.jface.text.contentassist.IContextInformation;
  29. import org.eclipse.ui.PlatformUI;
  30. import org.eclipse.ltk.core.refactoring.Change;
  31. import org.eclipse.ltk.core.refactoring.CompositeChange;
  32. import org.eclipse.ltk.core.refactoring.PerformChangeOperation;
  33. import org.eclipse.ltk.core.refactoring.RefactoringCore;
  34. import org.eclipse.ltk.core.refactoring.TextFileChange;
  35. import org.eclipse.jdt.core.ICompilationUnit;
  36. import org.eclipse.jdt.core.IJavaProject;
  37. import org.eclipse.jdt.core.JavaModelException;
  38. import org.eclipse.jdt.core.compiler.IProblem;
  39. import org.eclipse.jdt.core.dom.ASTNode;
  40. import org.eclipse.jdt.core.dom.BodyDeclaration;
  41. import org.eclipse.jdt.core.dom.CompilationUnit;
  42. import org.eclipse.jdt.core.dom.IAnnotationBinding;
  43. import org.eclipse.jdt.core.dom.IMethodBinding;
  44. import org.eclipse.jdt.core.dom.ITypeBinding;
  45. import org.eclipse.jdt.core.dom.MarkerAnnotation;
  46. import org.eclipse.jdt.core.dom.MethodDeclaration;
  47. import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
  48. import org.eclipse.jdt.internal.junit.BasicElementLabels;
  49. import org.eclipse.jdt.internal.junit.JUnitCorePlugin;
  50. import org.eclipse.jdt.internal.junit.Messages;
  51. import org.eclipse.jdt.internal.junit.util.ExceptionHandler;
  52. import org.eclipse.jdt.internal.junit.util.JUnitStubUtility;
  53. import org.eclipse.jdt.ui.CodeStyleConfiguration;
  54. import org.eclipse.jdt.ui.ISharedImages;
  55. import org.eclipse.jdt.ui.JavaUI;
  56. import org.eclipse.jdt.ui.text.java.ClasspathFixProcessor;
  57. import org.eclipse.jdt.ui.text.java.IInvocationContext;
  58. import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
  59. import org.eclipse.jdt.ui.text.java.IProblemLocation;
  60. import org.eclipse.jdt.ui.text.java.IQuickFixProcessor;
  61. import org.eclipse.jdt.ui.text.java.ClasspathFixProcessor.ClasspathFixProposal;
  62. public class JUnitQuickFixProcessor implements IQuickFixProcessor {
  63. private static final HashSet<String> ASSERT_METHOD_NAMES= new HashSet<String>();
  64. public JUnitQuickFixProcessor() {
  65. ASSERT_METHOD_NAMES.add("fail"); //$NON-NLS-1$
  66. ASSERT_METHOD_NAMES.add("assertTrue"); //$NON-NLS-1$
  67. ASSERT_METHOD_NAMES.add("assertFalse"); //$NON-NLS-1$
  68. ASSERT_METHOD_NAMES.add("assertEquals"); //$NON-NLS-1$
  69. ASSERT_METHOD_NAMES.add("assertNotNull"); //$NON-NLS-1$
  70. ASSERT_METHOD_NAMES.add("assertNull"); //$NON-NLS-1$
  71. ASSERT_METHOD_NAMES.add("assertSame"); //$NON-NLS-1$
  72. ASSERT_METHOD_NAMES.add("assertNotSame"); //$NON-NLS-1$
  73. ASSERT_METHOD_NAMES.add("failNotEquals"); //$NON-NLS-1$
  74. ASSERT_METHOD_NAMES.add("failSame"); //$NON-NLS-1$
  75. ASSERT_METHOD_NAMES.add("failNotSame"); //$NON-NLS-1$
  76. }
  77. /* (non-Javadoc)
  78. * @see org.eclipse.jdt.ui.text.java.IQuickFixProcessor#hasCorrections(org.eclipse.jdt.core.ICompilationUnit, int)
  79. */
  80. public boolean hasCorrections(ICompilationUnit unit, int problemId) {
  81. return problemId == IProblem.UndefinedType || problemId == IProblem.UndefinedMethod;
  82. }
  83. /* (non-Javadoc)
  84. * @see org.eclipse.jdt.ui.text.java.IQuickFixProcessor#getCorrections(org.eclipse.jdt.ui.text.java.IInvocationContext, org.eclipse.jdt.ui.text.java.IProblemLocation[])
  85. */
  86. public IJavaCompletionProposal[] getCorrections(final IInvocationContext context, IProblemLocation[] locations) {
  87. ArrayList<IJavaCompletionProposal> res= null;
  88. for (IProblemLocation problem : locations) {
  89. int id= problem.getProblemId();
  90. if (IProblem.UndefinedType == id) {
  91. res= getAddJUnitToBuildPathProposals(context, problem, res);
  92. } else if (id == IProblem.UndefinedMethod) {
  93. res= getAddAssertImportProposals(context, problem, res);
  94. }
  95. }
  96. if (res == null || res.isEmpty()) {
  97. return null;
  98. }
  99. return res.toArray(new IJavaCompletionProposal[res.size()]);
  100. }
  101. private ArrayList<IJavaCompletionProposal> getAddAssertImportProposals(IInvocationContext context, IProblemLocation problem, ArrayList<IJavaCompletionProposal> proposals) {
  102. String[] args= problem.getProblemArguments();
  103. if (args.length > 1) {
  104. String methodName= args[1];
  105. if (ASSERT_METHOD_NAMES.contains(methodName) && isInsideJUnit4Test(context)) {
  106. if (proposals == null) {
  107. proposals= new ArrayList<IJavaCompletionProposal>();
  108. }
  109. proposals.add(new AddAssertProposal(context.getASTRoot(), methodName, 9));
  110. proposals.add(new AddAssertProposal(context.getASTRoot(), "*", 10)); //$NON-NLS-1$
  111. }
  112. }
  113. return proposals;
  114. }
  115. private ArrayList<IJavaCompletionProposal> getAddJUnitToBuildPathProposals(IInvocationContext context, IProblemLocation location, ArrayList<IJavaCompletionProposal> proposals) {
  116. try {
  117. ICompilationUnit unit= context.getCompilationUnit();
  118. String qualifiedName= null;
  119. String s= unit.getBuffer().getText(location.getOffset(), location.getLength());
  120. if (s.equals("TestCase") || s.equals("TestSuite")) { //$NON-NLS-1$ //$NON-NLS-2$
  121. qualifiedName= "junit.framework." + s; //$NON-NLS-1$
  122. } else if (s.equals("RunWith")) { //$NON-NLS-1$
  123. qualifiedName= "org.junit.runner.RunWith"; //$NON-NLS-1$
  124. } else if (s.equals("Test")) { //$NON-NLS-1$
  125. ASTNode node= location.getCoveredNode(context.getASTRoot());
  126. if (node != null && node.getLocationInParent() == MarkerAnnotation.TYPE_NAME_PROPERTY) {
  127. qualifiedName= "org.junit.Test"; //$NON-NLS-1$
  128. } else {
  129. qualifiedName= "junit.framework.Test"; //$NON-NLS-1$
  130. }
  131. }
  132. if (qualifiedName != null) {
  133. IJavaProject javaProject= unit.getJavaProject();
  134. if (javaProject.findType(qualifiedName) != null) {
  135. return proposals;
  136. }
  137. ClasspathFixProposal[] fixProposals= ClasspathFixProcessor.getContributedFixImportProposals(javaProject, qualifiedName, null);
  138. for (ClasspathFixProposal fixProposal : fixProposals) {
  139. if (proposals == null)
  140. proposals= new ArrayList<IJavaCompletionProposal>();
  141. proposals.add(new JUnitClasspathFixCorrectionProposal(javaProject, fixProposal, getImportRewrite(context.getASTRoot(), qualifiedName)));
  142. }
  143. }
  144. } catch (JavaModelException e) {
  145. JUnitPlugin.log(e.getStatus());
  146. }
  147. return proposals;
  148. }
  149. private ImportRewrite getImportRewrite(CompilationUnit astRoot, String typeToImport) {
  150. if (typeToImport != null) {
  151. ImportRewrite importRewrite= CodeStyleConfiguration.createImportRewrite(astRoot, true);
  152. importRewrite.addImport(typeToImport);
  153. return importRewrite;
  154. }
  155. return null;
  156. }
  157. private boolean isInsideJUnit4Test(IInvocationContext context) {
  158. if (!JUnitStubUtility.is50OrHigher(context.getCompilationUnit().getJavaProject())) {
  159. return false;
  160. }
  161. ASTNode node= context.getCoveringNode();
  162. while (node != null && !(node instanceof BodyDeclaration)) {
  163. node= node.getParent();
  164. }
  165. if (node instanceof MethodDeclaration) {
  166. IMethodBinding binding= ((MethodDeclaration) node).resolveBinding();
  167. if (binding != null) {
  168. IAnnotationBinding[] annotations= binding.getAnnotations();
  169. for (IAnnotationBinding annotation : annotations) {
  170. final ITypeBinding annotationType= annotation.getAnnotationType();
  171. if (annotationType != null && JUnitCorePlugin.JUNIT4_ANNOTATION_NAME.equals(annotationType.getQualifiedName()))
  172. return true;
  173. }
  174. }
  175. }
  176. return false;
  177. }
  178. private static class JUnitClasspathFixCorrectionProposal implements IJavaCompletionProposal {
  179. private final ClasspathFixProposal fClasspathFixProposal;
  180. private final ImportRewrite fImportRewrite;
  181. private final IJavaProject fJavaProject;
  182. public JUnitClasspathFixCorrectionProposal(IJavaProject javaProject, ClasspathFixProposal cpfix, ImportRewrite rewrite) {
  183. fJavaProject= javaProject;
  184. fClasspathFixProposal= cpfix;
  185. fImportRewrite= rewrite;
  186. }
  187. protected Change createChange() throws CoreException {
  188. Change change= fClasspathFixProposal.createChange(null);
  189. if (fImportRewrite != null) {
  190. TextFileChange cuChange= new TextFileChange("Add import", (IFile) fImportRewrite.getCompilationUnit().getResource()); //$NON-NLS-1$
  191. cuChange.setEdit(fImportRewrite.rewriteImports(null));
  192. CompositeChange composite= new CompositeChange(getDisplayString());
  193. composite.add(change);
  194. composite.add(cuChange);
  195. return composite;
  196. }
  197. return change;
  198. }
  199. public void apply(IDocument document) {
  200. try {
  201. PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(false, true, new IRunnableWithProgress() {
  202. public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
  203. try {
  204. Change change= createChange();
  205. change.initializeValidationData(new NullProgressMonitor());
  206. PerformChangeOperation op= new PerformChangeOperation(change);
  207. op.setUndoManager(RefactoringCore.getUndoManager(), getDisplayString());
  208. op.setSchedulingRule(fJavaProject.getProject().getWorkspace().getRoot());
  209. op.run(monitor);
  210. } catch (CoreException e) {
  211. throw new InvocationTargetException(e);
  212. } catch (OperationCanceledException e) {
  213. throw new InterruptedException();
  214. }
  215. }
  216. });
  217. } catch (InvocationTargetException e) {
  218. ExceptionHandler.handle(e, JUnitPlugin.getActiveWorkbenchShell(), JUnitMessages.JUnitQuickFixProcessor_apply_problem_title, JUnitMessages.JUnitQuickFixProcessor_apply_problem_description);
  219. } catch (InterruptedException e) {
  220. }
  221. }
  222. public String getAdditionalProposalInfo() {
  223. return fClasspathFixProposal.getAdditionalProposalInfo();
  224. }
  225. public int getRelevance() {
  226. return fClasspathFixProposal.getRelevance();
  227. }
  228. public IContextInformation getContextInformation() {
  229. return null;
  230. }
  231. public String getDisplayString() {
  232. return fClasspathFixProposal.getDisplayString();
  233. }
  234. public Image getImage() {
  235. return fClasspathFixProposal.getImage();
  236. }
  237. public Point getSelection(IDocument document) {
  238. return null;
  239. }
  240. }
  241. private static class AddAssertProposal implements IJavaCompletionProposal {
  242. private final CompilationUnit fAstRoot;
  243. private final String fMethodName;
  244. private final int fRelevance;
  245. public AddAssertProposal(CompilationUnit astRoot, String methodName, int relevance) {
  246. fAstRoot= astRoot;
  247. fMethodName= methodName;
  248. fRelevance= relevance;
  249. }
  250. /* (non-Javadoc)
  251. * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposal#getRelevance()
  252. */
  253. public int getRelevance() {
  254. return fRelevance;
  255. }
  256. /* (non-Javadoc)
  257. * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument)
  258. */
  259. public void apply(IDocument document) {
  260. try {
  261. ImportRewrite rewrite= CodeStyleConfiguration.createImportRewrite(fAstRoot, true);
  262. rewrite.addStaticImport("org.junit.Assert", fMethodName, true); //$NON-NLS-1$
  263. TextEdit edit= rewrite.rewriteImports(null);
  264. edit.apply(document);
  265. } catch (MalformedTreeException e) {
  266. } catch (CoreException e) {
  267. } catch (BadLocationException e) {
  268. }
  269. }
  270. /* (non-Javadoc)
  271. * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo()
  272. */
  273. public String getAdditionalProposalInfo() {
  274. return Messages.format(JUnitMessages.JUnitQuickFixProcessor_add_assert_info, BasicElementLabels.getJavaElementName("org.junit.Assert." + fMethodName)); //$NON-NLS-1$
  275. }
  276. /* (non-Javadoc)
  277. * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getContextInformation()
  278. */
  279. public IContextInformation getContextInformation() {
  280. return null;
  281. }
  282. /* (non-Javadoc)
  283. * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
  284. */
  285. public String getDisplayString() {
  286. return Messages.format(JUnitMessages.JUnitQuickFixProcessor_add_assert_description, BasicElementLabels.getJavaElementName("org.junit.Assert." + fMethodName)); //$NON-NLS-1$
  287. }
  288. /* (non-Javadoc)
  289. * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getImage()
  290. */
  291. public Image getImage() {
  292. return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_IMPDECL);
  293. }
  294. /* (non-Javadoc)
  295. * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
  296. */
  297. public Point getSelection(IDocument document) {
  298. return null;
  299. }
  300. }
  301. }