Lines 1-6
Link Here
|
1 |
package org.eclipse.jdt.internal.ui.text.correction; |
1 |
package org.eclipse.jdt.internal.ui.text.correction; |
2 |
|
2 |
|
3 |
import java.util.ArrayList; |
3 |
import java.util.ArrayList; |
|
|
4 |
import java.util.Iterator; |
4 |
import java.util.List; |
5 |
import java.util.List; |
5 |
|
6 |
|
6 |
import org.eclipse.core.runtime.CoreException; |
7 |
import org.eclipse.core.runtime.CoreException; |
Lines 22-33
Link Here
|
22 |
import org.eclipse.jdt.core.dom.BodyDeclaration; |
23 |
import org.eclipse.jdt.core.dom.BodyDeclaration; |
23 |
import org.eclipse.jdt.core.dom.CompilationUnit; |
24 |
import org.eclipse.jdt.core.dom.CompilationUnit; |
24 |
import org.eclipse.jdt.core.dom.Expression; |
25 |
import org.eclipse.jdt.core.dom.Expression; |
|
|
26 |
import org.eclipse.jdt.core.dom.FieldAccess; |
25 |
import org.eclipse.jdt.core.dom.FieldDeclaration; |
27 |
import org.eclipse.jdt.core.dom.FieldDeclaration; |
26 |
import org.eclipse.jdt.core.dom.ITypeBinding; |
28 |
import org.eclipse.jdt.core.dom.ITypeBinding; |
27 |
import org.eclipse.jdt.core.dom.MethodDeclaration; |
29 |
import org.eclipse.jdt.core.dom.MethodDeclaration; |
|
|
30 |
import org.eclipse.jdt.core.dom.MethodInvocation; |
31 |
import org.eclipse.jdt.core.dom.Name; |
32 |
import org.eclipse.jdt.core.dom.QualifiedName; |
28 |
import org.eclipse.jdt.core.dom.ReturnStatement; |
33 |
import org.eclipse.jdt.core.dom.ReturnStatement; |
29 |
import org.eclipse.jdt.core.dom.SimpleName; |
34 |
import org.eclipse.jdt.core.dom.SimpleName; |
30 |
import org.eclipse.jdt.core.dom.Statement; |
35 |
import org.eclipse.jdt.core.dom.Statement; |
|
|
36 |
import org.eclipse.jdt.core.dom.ThisExpression; |
31 |
import org.eclipse.jdt.core.dom.Type; |
37 |
import org.eclipse.jdt.core.dom.Type; |
32 |
import org.eclipse.jdt.core.dom.VariableDeclarationFragment; |
38 |
import org.eclipse.jdt.core.dom.VariableDeclarationFragment; |
33 |
import org.eclipse.jdt.core.dom.VariableDeclarationStatement; |
39 |
import org.eclipse.jdt.core.dom.VariableDeclarationStatement; |
Lines 35-41
Link Here
|
35 |
import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; |
41 |
import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; |
36 |
import org.eclipse.jdt.internal.corext.codemanipulation.ImportEdit; |
42 |
import org.eclipse.jdt.internal.corext.codemanipulation.ImportEdit; |
37 |
import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; |
43 |
import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; |
|
|
44 |
import org.eclipse.jdt.internal.corext.dom.ASTNodes; |
38 |
import org.eclipse.jdt.internal.corext.dom.ASTRewriteAnalyzer; |
45 |
import org.eclipse.jdt.internal.corext.dom.ASTRewriteAnalyzer; |
|
|
46 |
import org.eclipse.jdt.internal.corext.dom.Bindings; |
39 |
import org.eclipse.jdt.internal.corext.dom.GenericVisitor; |
47 |
import org.eclipse.jdt.internal.corext.dom.GenericVisitor; |
40 |
import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange; |
48 |
import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange; |
41 |
import org.eclipse.jdt.internal.corext.refactoring.nls.NLSRefactoring; |
49 |
import org.eclipse.jdt.internal.corext.refactoring.nls.NLSRefactoring; |
Lines 88-97
Link Here
|
88 |
String label= CorrectionMessages.getFormattedString("LocalCorrectionsSubProcessor.addcast.description", castDestType); //$NON-NLS-1$ |
96 |
String label= CorrectionMessages.getFormattedString("LocalCorrectionsSubProcessor.addcast.description", castDestType); //$NON-NLS-1$ |
89 |
InsertCorrectionProposal proposal= new InsertCorrectionProposal(label, cu, pos, cast, 1); |
97 |
InsertCorrectionProposal proposal= new InsertCorrectionProposal(label, cu, pos, cast, 1); |
90 |
|
98 |
|
91 |
CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(); |
99 |
addImportToProposal(cu, castDestType, proposal); |
92 |
ImportEdit edit= new ImportEdit(cu, settings); |
|
|
93 |
edit.addImport(castDestType); |
94 |
proposal.getCompilationUnitChange().addTextEdit("import", edit); //$NON-NLS-1$ |
95 |
|
100 |
|
96 |
proposals.add(proposal); |
101 |
proposals.add(proposal); |
97 |
|
102 |
|
Lines 116-124
Link Here
|
116 |
|
121 |
|
117 |
label= CorrectionMessages.getFormattedString("LocalCorrectionsSubProcessor.addcast_var.description", simpleCastType); //$NON-NLS-1$ |
122 |
label= CorrectionMessages.getFormattedString("LocalCorrectionsSubProcessor.addcast_var.description", simpleCastType); //$NON-NLS-1$ |
118 |
ReplaceCorrectionProposal varProposal= new ReplaceCorrectionProposal(label, cu, type.getStartPosition(), type.getLength(), simpleCastType, 1); |
123 |
ReplaceCorrectionProposal varProposal= new ReplaceCorrectionProposal(label, cu, type.getStartPosition(), type.getLength(), simpleCastType, 1); |
119 |
edit= new ImportEdit(cu, settings); |
124 |
|
120 |
edit.addImport(castType); |
125 |
addImportToProposal(cu, castType, varProposal); |
121 |
varProposal.getCompilationUnitChange().addTextEdit("import", edit); //$NON-NLS-1$ |
|
|
122 |
|
126 |
|
123 |
proposals.add(varProposal); |
127 |
proposals.add(varProposal); |
124 |
} |
128 |
} |
Lines 164-173
Link Here
|
164 |
Image image= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_EXCEPTION); |
168 |
Image image= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_EXCEPTION); |
165 |
ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, astRoot, 0, image); |
169 |
ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, cu, astRoot, 0, image); |
166 |
|
170 |
|
167 |
ImportEdit edit= new ImportEdit(cu, JavaPreferencesSettings.getCodeGenerationSettings()); |
171 |
addImportToProposal(cu, uncaughtName, proposal); |
168 |
edit.addImport(uncaughtName); |
172 |
|
169 |
proposal.getCompilationUnitChange().addTextEdit("import", edit); //$NON-NLS-1$ |
|
|
170 |
|
171 |
proposals.add(proposal); |
173 |
proposals.add(proposal); |
172 |
} |
174 |
} |
173 |
} |
175 |
} |
Lines 256-265
Link Here
|
256 |
String label= CorrectionMessages.getFormattedString("LocalCorrectionsSubProcessor.missingreturntype.description", type.getName()); //$NON-NLS-1$ |
258 |
String label= CorrectionMessages.getFormattedString("LocalCorrectionsSubProcessor.missingreturntype.description", type.getName()); //$NON-NLS-1$ |
257 |
InsertCorrectionProposal proposal= new InsertCorrectionProposal(label, cu, pos, str, 1); |
259 |
InsertCorrectionProposal proposal= new InsertCorrectionProposal(label, cu, pos, str, 1); |
258 |
|
260 |
|
259 |
CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(); |
261 |
addImportToProposal(problemPos.getCompilationUnit(), type, proposal); |
260 |
ImportEdit edit= new ImportEdit(problemPos.getCompilationUnit(), settings); |
|
|
261 |
edit.addImport(type.getName()); |
262 |
proposal.getCompilationUnitChange().addTextEdit("import", edit); //$NON-NLS-1$ |
263 |
|
262 |
|
264 |
proposals.add(proposal); |
263 |
proposals.add(proposal); |
265 |
} |
264 |
} |
Lines 334-339
Link Here
|
334 |
proposals.add(proposal); |
333 |
proposals.add(proposal); |
335 |
} |
334 |
} |
336 |
|
335 |
|
|
|
336 |
/** |
337 |
* A static field or method is accessed using a non-static reference. E.g. |
338 |
* <pre> |
339 |
* File f = new File(); |
340 |
* f.pathSeparator; |
341 |
* </pre> |
342 |
* This correction changes <code>f</code> above to <code>File</code>. |
343 |
* |
344 |
* @param problemPos |
345 |
* @param proposals |
346 |
*/ |
347 |
public static void addAccessToStaticProposals(ProblemPosition problemPos, List proposals) throws CoreException { |
348 |
ICompilationUnit cu= problemPos.getCompilationUnit(); |
349 |
|
350 |
CompilationUnit astRoot= AST.parseCompilationUnit(cu, true); |
351 |
ASTNode selectedNode= ASTResolving.findSelectedNode(astRoot, problemPos.getOffset(), problemPos.getLength()); |
352 |
|
353 |
if (selectedNode != null) { |
354 |
Name qualifier= getQualifier(selectedNode); |
355 |
|
356 |
if (qualifier != null) { |
357 |
ITypeBinding type= qualifier.resolveTypeBinding(); |
358 |
|
359 |
String label= CorrectionMessages.getString("LocalCorrectionsSubProcessor.changeaccesstostatic.description"); |
360 |
ReplaceCorrectionProposal proposal= new ReplaceCorrectionProposal(label, cu, qualifier.getStartPosition(), qualifier.getLength(), type.getName(), 0); |
361 |
|
362 |
addImportToProposal(cu, type, proposal); |
363 |
|
364 |
proposals.add(proposal); |
365 |
} |
366 |
} |
367 |
} |
337 |
|
368 |
|
|
|
369 |
private static void addImportToProposal(ICompilationUnit cu, ITypeBinding type, CUCorrectionProposal proposal) throws CoreException { |
370 |
addImportToProposal(cu, Bindings.getFullyQualifiedImportName(type), proposal); |
371 |
} |
372 |
|
373 |
private static void addImportToProposal(ICompilationUnit cu, String typeName, CUCorrectionProposal proposal) throws CoreException { |
374 |
ImportEdit edit= new ImportEdit(cu, JavaPreferencesSettings.getCodeGenerationSettings()); |
375 |
edit.addImport(typeName); |
376 |
proposal.getCompilationUnitChange().addTextEdit("import", edit); //$NON-NLS-1$ |
377 |
} |
378 |
|
379 |
private static Name getQualifier(ASTNode node) { |
380 |
if (node instanceof QualifiedName) { |
381 |
QualifiedName qn= (QualifiedName) node; |
382 |
return qn.getQualifier(); |
383 |
} else if (node instanceof MethodInvocation) { |
384 |
MethodInvocation mi= (MethodInvocation) node; |
385 |
if (mi.getExpression() instanceof Name) { |
386 |
return (Name) mi.getExpression(); |
387 |
} |
388 |
} |
389 |
return null; |
390 |
} |
338 |
|
391 |
|
339 |
} |
392 |
} |