|
Lines 1-5
Link Here
|
| 1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2016 IBM Corporation and others. |
2 |
* Copyright (c) 2000, 2017 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
|
Lines 25-36
Link Here
|
| 25 |
|
25 |
|
| 26 |
import org.eclipse.core.resources.IFile; |
26 |
import org.eclipse.core.resources.IFile; |
| 27 |
|
27 |
|
| 28 |
import org.eclipse.text.edits.MalformedTreeException; |
|
|
| 29 |
import org.eclipse.text.edits.TextEdit; |
| 30 |
|
| 31 |
import org.eclipse.jface.operation.IRunnableWithProgress; |
28 |
import org.eclipse.jface.operation.IRunnableWithProgress; |
| 32 |
|
29 |
|
| 33 |
import org.eclipse.jface.text.BadLocationException; |
|
|
| 34 |
import org.eclipse.jface.text.IDocument; |
30 |
import org.eclipse.jface.text.IDocument; |
| 35 |
import org.eclipse.jface.text.contentassist.IContextInformation; |
31 |
import org.eclipse.jface.text.contentassist.IContextInformation; |
| 36 |
|
32 |
|
|
Lines 47-70
Link Here
|
| 47 |
import org.eclipse.jdt.core.JavaModelException; |
43 |
import org.eclipse.jdt.core.JavaModelException; |
| 48 |
import org.eclipse.jdt.core.compiler.IProblem; |
44 |
import org.eclipse.jdt.core.compiler.IProblem; |
| 49 |
import org.eclipse.jdt.core.dom.ASTNode; |
45 |
import org.eclipse.jdt.core.dom.ASTNode; |
| 50 |
import org.eclipse.jdt.core.dom.BodyDeclaration; |
|
|
| 51 |
import org.eclipse.jdt.core.dom.CompilationUnit; |
46 |
import org.eclipse.jdt.core.dom.CompilationUnit; |
| 52 |
import org.eclipse.jdt.core.dom.IAnnotationBinding; |
|
|
| 53 |
import org.eclipse.jdt.core.dom.IMethodBinding; |
| 54 |
import org.eclipse.jdt.core.dom.ITypeBinding; |
| 55 |
import org.eclipse.jdt.core.dom.MarkerAnnotation; |
47 |
import org.eclipse.jdt.core.dom.MarkerAnnotation; |
| 56 |
import org.eclipse.jdt.core.dom.MethodDeclaration; |
|
|
| 57 |
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; |
48 |
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; |
| 58 |
|
49 |
|
| 59 |
import org.eclipse.jdt.internal.junit.BasicElementLabels; |
|
|
| 60 |
import org.eclipse.jdt.internal.junit.JUnitCorePlugin; |
| 61 |
import org.eclipse.jdt.internal.junit.Messages; |
| 62 |
import org.eclipse.jdt.internal.junit.util.ExceptionHandler; |
50 |
import org.eclipse.jdt.internal.junit.util.ExceptionHandler; |
| 63 |
import org.eclipse.jdt.internal.junit.util.JUnitStubUtility; |
|
|
| 64 |
|
51 |
|
| 65 |
import org.eclipse.jdt.ui.CodeStyleConfiguration; |
52 |
import org.eclipse.jdt.ui.CodeStyleConfiguration; |
| 66 |
import org.eclipse.jdt.ui.ISharedImages; |
|
|
| 67 |
import org.eclipse.jdt.ui.JavaUI; |
| 68 |
import org.eclipse.jdt.ui.text.java.ClasspathFixProcessor; |
53 |
import org.eclipse.jdt.ui.text.java.ClasspathFixProcessor; |
| 69 |
import org.eclipse.jdt.ui.text.java.ClasspathFixProcessor.ClasspathFixProposal; |
54 |
import org.eclipse.jdt.ui.text.java.ClasspathFixProcessor.ClasspathFixProposal; |
| 70 |
import org.eclipse.jdt.ui.text.java.IInvocationContext; |
55 |
import org.eclipse.jdt.ui.text.java.IInvocationContext; |
|
Lines 92-130
Link Here
|
| 92 |
|
77 |
|
| 93 |
@Override |
78 |
@Override |
| 94 |
public boolean hasCorrections(ICompilationUnit unit, int problemId) { |
79 |
public boolean hasCorrections(ICompilationUnit unit, int problemId) { |
| 95 |
return problemId == IProblem.UndefinedType || problemId == IProblem.UndefinedMethod; |
80 |
return problemId == IProblem.UndefinedType; |
| 96 |
} |
81 |
} |
| 97 |
|
82 |
|
| 98 |
@Override |
83 |
@Override |
| 99 |
public IJavaCompletionProposal[] getCorrections(final IInvocationContext context, IProblemLocation[] locations) { |
84 |
public IJavaCompletionProposal[] getCorrections(final IInvocationContext context, IProblemLocation[] locations) { |
| 100 |
ArrayList<IJavaCompletionProposal> res= null; |
85 |
ArrayList<IJavaCompletionProposal> res= null; |
| 101 |
for (IProblemLocation problem : locations) { |
86 |
for (IProblemLocation problem : locations) { |
| 102 |
int id= problem.getProblemId(); |
87 |
res= getAddJUnitToBuildPathProposals(context, problem, res); |
| 103 |
if (IProblem.UndefinedType == id) { |
|
|
| 104 |
res= getAddJUnitToBuildPathProposals(context, problem, res); |
| 105 |
} else if (id == IProblem.UndefinedMethod) { |
| 106 |
res= getAddAssertImportProposals(context, problem, res); |
| 107 |
} |
| 108 |
} |
88 |
} |
| 109 |
if (res == null || res.isEmpty()) { |
89 |
if (res == null || res.isEmpty()) { |
| 110 |
return null; |
90 |
return null; |
| 111 |
} |
91 |
} |
| 112 |
return res.toArray(new IJavaCompletionProposal[res.size()]); |
92 |
return res.toArray(new IJavaCompletionProposal[res.size()]); |
| 113 |
} |
|
|
| 114 |
|
| 115 |
private ArrayList<IJavaCompletionProposal> getAddAssertImportProposals(IInvocationContext context, IProblemLocation problem, ArrayList<IJavaCompletionProposal> proposals) { |
| 116 |
String[] args= problem.getProblemArguments(); |
| 117 |
if (args.length > 1) { |
| 118 |
String methodName= args[1]; |
| 119 |
if (ASSERT_METHOD_NAMES.contains(methodName) && isInsideJUnit4Test(context)) { |
| 120 |
if (proposals == null) { |
| 121 |
proposals= new ArrayList<>(); |
| 122 |
} |
| 123 |
proposals.add(new AddAssertProposal(context.getASTRoot(), methodName, 9)); |
| 124 |
proposals.add(new AddAssertProposal(context.getASTRoot(), "*", 10)); //$NON-NLS-1$ |
| 125 |
} |
| 126 |
} |
| 127 |
return proposals; |
| 128 |
} |
93 |
} |
| 129 |
|
94 |
|
| 130 |
private ArrayList<IJavaCompletionProposal> getAddJUnitToBuildPathProposals(IInvocationContext context, IProblemLocation location, ArrayList<IJavaCompletionProposal> proposals) { |
95 |
private ArrayList<IJavaCompletionProposal> getAddJUnitToBuildPathProposals(IInvocationContext context, IProblemLocation location, ArrayList<IJavaCompletionProposal> proposals) { |
|
Lines 189-217
Link Here
|
| 189 |
return importRewrite; |
154 |
return importRewrite; |
| 190 |
} |
155 |
} |
| 191 |
return null; |
156 |
return null; |
| 192 |
} |
|
|
| 193 |
|
| 194 |
private boolean isInsideJUnit4Test(IInvocationContext context) { |
| 195 |
if (!JUnitStubUtility.is50OrHigher(context.getCompilationUnit().getJavaProject())) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
ASTNode node= context.getCoveringNode(); |
| 200 |
while (node != null && !(node instanceof BodyDeclaration)) { |
| 201 |
node= node.getParent(); |
| 202 |
} |
| 203 |
if (node instanceof MethodDeclaration) { |
| 204 |
IMethodBinding binding= ((MethodDeclaration) node).resolveBinding(); |
| 205 |
if (binding != null) { |
| 206 |
IAnnotationBinding[] annotations= binding.getAnnotations(); |
| 207 |
for (IAnnotationBinding annotation : annotations) { |
| 208 |
final ITypeBinding annotationType= annotation.getAnnotationType(); |
| 209 |
if (annotationType != null && JUnitCorePlugin.JUNIT4_ANNOTATION_NAME.equals(annotationType.getQualifiedName())) |
| 210 |
return true; |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
return false; |
| 215 |
} |
157 |
} |
| 216 |
|
158 |
|
| 217 |
private static class JUnitClasspathFixCorrectionProposal implements IJavaCompletionProposal { |
159 |
private static class JUnitClasspathFixCorrectionProposal implements IJavaCompletionProposal { |
|
Lines 290-351
Link Here
|
| 290 |
@Override |
232 |
@Override |
| 291 |
public Image getImage() { |
233 |
public Image getImage() { |
| 292 |
return fClasspathFixProposal.getImage(); |
234 |
return fClasspathFixProposal.getImage(); |
| 293 |
} |
|
|
| 294 |
|
| 295 |
@Override |
| 296 |
public Point getSelection(IDocument document) { |
| 297 |
return null; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
private static class AddAssertProposal implements IJavaCompletionProposal { |
| 302 |
|
| 303 |
private final CompilationUnit fAstRoot; |
| 304 |
private final String fMethodName; |
| 305 |
private final int fRelevance; |
| 306 |
|
| 307 |
public AddAssertProposal(CompilationUnit astRoot, String methodName, int relevance) { |
| 308 |
fAstRoot= astRoot; |
| 309 |
fMethodName= methodName; |
| 310 |
fRelevance= relevance; |
| 311 |
} |
| 312 |
|
| 313 |
@Override |
| 314 |
public int getRelevance() { |
| 315 |
return fRelevance; |
| 316 |
} |
| 317 |
|
| 318 |
@Override |
| 319 |
public void apply(IDocument document) { |
| 320 |
try { |
| 321 |
ImportRewrite rewrite= CodeStyleConfiguration.createImportRewrite(fAstRoot, true); |
| 322 |
rewrite.addStaticImport("org.junit.Assert", fMethodName, true); //$NON-NLS-1$ |
| 323 |
TextEdit edit= rewrite.rewriteImports(null); |
| 324 |
edit.apply(document); |
| 325 |
} catch (MalformedTreeException e) { |
| 326 |
} catch (CoreException e) { |
| 327 |
} catch (BadLocationException e) { |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
@Override |
| 332 |
public String getAdditionalProposalInfo() { |
| 333 |
return Messages.format(JUnitMessages.JUnitQuickFixProcessor_add_assert_info, BasicElementLabels.getJavaElementName("org.junit.Assert." + fMethodName)); //$NON-NLS-1$ |
| 334 |
} |
| 335 |
|
| 336 |
@Override |
| 337 |
public IContextInformation getContextInformation() { |
| 338 |
return null; |
| 339 |
} |
| 340 |
|
| 341 |
@Override |
| 342 |
public String getDisplayString() { |
| 343 |
return Messages.format(JUnitMessages.JUnitQuickFixProcessor_add_assert_description, BasicElementLabels.getJavaElementName("org.junit.Assert." + fMethodName)); //$NON-NLS-1$ |
| 344 |
} |
| 345 |
|
| 346 |
@Override |
| 347 |
public Image getImage() { |
| 348 |
return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_IMPDECL); |
| 349 |
} |
235 |
} |
| 350 |
|
236 |
|
| 351 |
@Override |
237 |
@Override |