]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
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.corext.refactoring.code; | |
12 | ||
13 | import java.util.HashMap; | |
14 | import java.util.Map; | |
15 | import java.util.StringTokenizer; | |
16 | ||
17 | import org.eclipse.core.runtime.Assert; | |
18 | import org.eclipse.core.runtime.CoreException; | |
19 | import org.eclipse.core.runtime.IProgressMonitor; | |
20 | import org.eclipse.core.runtime.SubProgressMonitor; | |
21 | ||
22 | import org.eclipse.text.edits.MalformedTreeException; | |
23 | import org.eclipse.text.edits.RangeMarker; | |
24 | import org.eclipse.text.edits.TextEdit; | |
25 | import org.eclipse.text.edits.TextEditGroup; | |
26 | ||
27 | import org.eclipse.jface.text.BadLocationException; | |
28 | import org.eclipse.jface.text.Document; | |
29 | import org.eclipse.jface.text.IDocument; | |
30 | import org.eclipse.jface.text.IRegion; | |
31 | import org.eclipse.jface.text.TextUtilities; | |
32 | ||
33 | import org.eclipse.ltk.core.refactoring.Change; | |
34 | import org.eclipse.ltk.core.refactoring.Refactoring; | |
35 | import org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor; | |
36 | import org.eclipse.ltk.core.refactoring.RefactoringDescriptor; | |
37 | import org.eclipse.ltk.core.refactoring.RefactoringStatus; | |
38 | import org.eclipse.ltk.core.refactoring.RefactoringStatusContext; | |
39 | ||
40 | import org.eclipse.jdt.core.ICompilationUnit; | |
41 | import org.eclipse.jdt.core.IJavaElement; | |
42 | import org.eclipse.jdt.core.IJavaProject; | |
43 | import org.eclipse.jdt.core.ISourceRange; | |
44 | import org.eclipse.jdt.core.JavaModelException; | |
45 | import org.eclipse.jdt.core.SourceRange; | |
46 | import org.eclipse.jdt.core.dom.AST; | |
47 | import org.eclipse.jdt.core.dom.ASTNode; | |
48 | import org.eclipse.jdt.core.dom.ArrayCreation; | |
49 | import org.eclipse.jdt.core.dom.ArrayInitializer; | |
50 | import org.eclipse.jdt.core.dom.ArrayType; | |
51 | import org.eclipse.jdt.core.dom.Assignment; | |
52 | import org.eclipse.jdt.core.dom.CastExpression; | |
53 | import org.eclipse.jdt.core.dom.CatchClause; | |
54 | import org.eclipse.jdt.core.dom.CompilationUnit; | |
55 | import org.eclipse.jdt.core.dom.Expression; | |
56 | import org.eclipse.jdt.core.dom.FieldDeclaration; | |
57 | import org.eclipse.jdt.core.dom.ForStatement; | |
58 | import org.eclipse.jdt.core.dom.IMethodBinding; | |
59 | import org.eclipse.jdt.core.dom.ITypeBinding; | |
60 | import org.eclipse.jdt.core.dom.IVariableBinding; | |
61 | import org.eclipse.jdt.core.dom.MethodDeclaration; | |
62 | import org.eclipse.jdt.core.dom.MethodInvocation; | |
63 | import org.eclipse.jdt.core.dom.Modifier; | |
64 | import org.eclipse.jdt.core.dom.ParenthesizedExpression; | |
65 | import org.eclipse.jdt.core.dom.SimpleName; | |
66 | import org.eclipse.jdt.core.dom.SingleVariableDeclaration; | |
67 | import org.eclipse.jdt.core.dom.TryStatement; | |
68 | import org.eclipse.jdt.core.dom.Type; | |
69 | import org.eclipse.jdt.core.dom.VariableDeclaration; | |
70 | import org.eclipse.jdt.core.dom.VariableDeclarationExpression; | |
71 | import org.eclipse.jdt.core.dom.VariableDeclarationFragment; | |
72 | import org.eclipse.jdt.core.dom.VariableDeclarationStatement; | |
73 | import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; | |
74 | import org.eclipse.jdt.core.dom.rewrite.ListRewrite; | |
75 | import org.eclipse.jdt.core.refactoring.CompilationUnitChange; | |
76 | import org.eclipse.jdt.core.refactoring.IJavaRefactorings; | |
77 | import org.eclipse.jdt.core.refactoring.descriptors.InlineLocalVariableDescriptor; | |
78 | ||
79 | import org.eclipse.jdt.internal.core.refactoring.descriptors.RefactoringSignatureDescriptorFactory; | |
80 | import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory; | |
81 | import org.eclipse.jdt.internal.corext.dom.ASTNodes; | |
82 | import org.eclipse.jdt.internal.corext.dom.NecessaryParenthesesChecker; | |
83 | import org.eclipse.jdt.internal.corext.refactoring.Checks; | |
84 | import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment; | |
85 | import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments; | |
86 | import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringDescriptorUtil; | |
87 | import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; | |
88 | import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext; | |
89 | import org.eclipse.jdt.internal.corext.refactoring.rename.TempDeclarationFinder; | |
90 | import org.eclipse.jdt.internal.corext.refactoring.rename.TempOccurrenceAnalyzer; | |
91 | import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; | |
92 | import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; | |
93 | import org.eclipse.jdt.internal.corext.refactoring.util.ResourceUtil; | |
94 | import org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer; | |
95 | import org.eclipse.jdt.internal.corext.util.Messages; | |
96 | import org.eclipse.jdt.internal.corext.util.Strings; | |
97 | ||
98 | import org.eclipse.jdt.ui.JavaElementLabels; | |
99 | ||
100 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
101 | import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels; | |
102 | import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider; | |
103 | ||
104 | public class InlineTempRefactoring extends Refactoring { | |
105 | ||
106 | private int fSelectionStart; | |
107 | private int fSelectionLength; | |
108 | private ICompilationUnit fCu; | |
109 | ||
110 | //the following fields are set after the construction | |
111 | private VariableDeclaration fVariableDeclaration; | |
112 | private SimpleName[] fReferences; | |
113 | private CompilationUnit fASTRoot; | |
114 | ||
115 | /** | |
116 | * Creates a new inline constant refactoring. | |
117 | * @param unit the compilation unit, or <code>null</code> if invoked by scripting | |
118 | * @param node compilation unit node, or <code>null</code> | |
119 | * @param selectionStart start | |
120 | * @param selectionLength length | |
121 | */ | |
122 | public InlineTempRefactoring(ICompilationUnit unit, CompilationUnit node, int selectionStart, int selectionLength) { | |
123 | Assert.isTrue(selectionStart >= 0); | |
124 | Assert.isTrue(selectionLength >= 0); | |
125 | fSelectionStart= selectionStart; | |
126 | fSelectionLength= selectionLength; | |
127 | fCu= unit; | |
128 | ||
129 | fASTRoot= node; | |
130 | fVariableDeclaration= null; | |
131 | } | |
132 | ||
133 | /** | |
134 | * Creates a new inline constant refactoring. | |
135 | * @param unit the compilation unit, or <code>null</code> if invoked by scripting | |
136 | * @param selectionStart start | |
137 | * @param selectionLength length | |
138 | */ | |
139 | public InlineTempRefactoring(ICompilationUnit unit, int selectionStart, int selectionLength) { | |
140 | this(unit, null, selectionStart, selectionLength); | |
141 | } | |
142 | ||
143 | public InlineTempRefactoring(VariableDeclaration decl) { | |
144 | fVariableDeclaration= decl; | |
145 | ASTNode astRoot= decl.getRoot(); | |
146 | Assert.isTrue(astRoot instanceof CompilationUnit); | |
147 | fASTRoot= (CompilationUnit) astRoot; | |
148 | Assert.isTrue(fASTRoot.getJavaElement() instanceof ICompilationUnit); | |
149 | ||
150 | fSelectionStart= decl.getStartPosition(); | |
151 | fSelectionLength= decl.getLength(); | |
152 | fCu= (ICompilationUnit) fASTRoot.getJavaElement(); | |
153 | } | |
154 | ||
155 | public InlineTempRefactoring(JavaRefactoringArguments arguments, RefactoringStatus status) { | |
156 | this(null, null, 0, 0); | |
157 | RefactoringStatus initializeStatus= initialize(arguments); | |
158 | status.merge(initializeStatus); | |
159 | } | |
160 | ||
161 | ||
162 | public RefactoringStatus checkIfTempSelected() { | |
163 | VariableDeclaration decl= getVariableDeclaration(); | |
164 | if (decl == null) { | |
165 | return CodeRefactoringUtil.checkMethodSyntaxErrors(fSelectionStart, fSelectionLength, getASTRoot(), RefactoringCoreMessages.InlineTempRefactoring_select_temp); | |
166 | } | |
167 | if (decl.getParent() instanceof FieldDeclaration) { | |
168 | return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTemRefactoring_error_message_fieldsCannotBeInlined); | |
169 | } | |
170 | return new RefactoringStatus(); | |
171 | } | |
172 | ||
173 | private CompilationUnit getASTRoot() { | |
174 | if (fASTRoot == null) { | |
175 | fASTRoot= RefactoringASTParser.parseWithASTProvider(fCu, true, null); | |
176 | } | |
177 | return fASTRoot; | |
178 | } | |
179 | ||
180 | public VariableDeclaration getVariableDeclaration() { | |
181 | if (fVariableDeclaration == null) { | |
182 | fVariableDeclaration= TempDeclarationFinder.findTempDeclaration(getASTRoot(), fSelectionStart, fSelectionLength); | |
183 | } | |
184 | return fVariableDeclaration; | |
185 | } | |
186 | ||
187 | /* | |
188 | * @see IRefactoring#getName() | |
189 | */ | |
190 | @Override | |
191 | public String getName() { | |
192 | return RefactoringCoreMessages.InlineTempRefactoring_name; | |
193 | } | |
194 | ||
195 | /* | |
196 | * @see Refactoring#checkActivation(IProgressMonitor) | |
197 | */ | |
198 | @Override | |
199 | public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException { | |
200 | try { | |
201 | pm.beginTask("", 1); //$NON-NLS-1$ | |
202 | ||
203 | RefactoringStatus result= Checks.validateModifiesFiles(ResourceUtil.getFiles(new ICompilationUnit[]{fCu}), getValidationContext()); | |
204 | if (result.hasFatalError()) | |
205 | return result; | |
206 | ||
207 | VariableDeclaration declaration= getVariableDeclaration(); | |
208 | ||
209 | result.merge(checkSelection(declaration)); | |
210 | if (result.hasFatalError()) | |
211 | return result; | |
212 | ||
213 | result.merge(checkInitializer(declaration)); | |
214 | return result; | |
215 | } finally { | |
216 | pm.done(); | |
217 | } | |
218 | } | |
219 | ||
220 | private RefactoringStatus checkInitializer(VariableDeclaration decl) { | |
221 | if (decl.getInitializer().getNodeType() == ASTNode.NULL_LITERAL) | |
222 | return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTemRefactoring_error_message_nulLiteralsCannotBeInlined); | |
223 | return null; | |
224 | } | |
225 | ||
226 | private RefactoringStatus checkSelection(VariableDeclaration decl) { | |
227 | ASTNode parent= decl.getParent(); | |
228 | if (parent instanceof MethodDeclaration) { | |
229 | return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_method_parameter); | |
230 | } | |
231 | ||
232 | if (parent instanceof CatchClause) { | |
233 | return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_exceptions_declared); | |
234 | } | |
235 | ||
236 | if (parent instanceof VariableDeclarationExpression && parent.getLocationInParent() == ForStatement.INITIALIZERS_PROPERTY) { | |
237 | return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_for_initializers); | |
238 | } | |
239 | ||
240 | if (parent instanceof VariableDeclarationExpression && parent.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) { | |
241 | return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_resource_in_try_with_resources); | |
242 | } | |
243 | ||
244 | if (decl.getInitializer() == null) { | |
245 | String message= Messages.format(RefactoringCoreMessages.InlineTempRefactoring_not_initialized, BasicElementLabels.getJavaElementName(decl.getName().getIdentifier())); | |
246 | return RefactoringStatus.createFatalErrorStatus(message); | |
247 | } | |
248 | ||
249 | return checkAssignments(decl); | |
250 | } | |
251 | ||
252 | private RefactoringStatus checkAssignments(VariableDeclaration decl) { | |
253 | TempAssignmentFinder assignmentFinder= new TempAssignmentFinder(decl); | |
254 | getASTRoot().accept(assignmentFinder); | |
255 | if (!assignmentFinder.hasAssignments()) | |
256 | return new RefactoringStatus(); | |
257 | ASTNode firstAssignment= assignmentFinder.getFirstAssignment(); | |
258 | int start= firstAssignment.getStartPosition(); | |
259 | int length= firstAssignment.getLength(); | |
260 | ISourceRange range= new SourceRange(start, length); | |
261 | RefactoringStatusContext context= JavaStatusContext.create(fCu, range); | |
262 | String message= Messages.format(RefactoringCoreMessages.InlineTempRefactoring_assigned_more_once, BasicElementLabels.getJavaElementName(decl.getName().getIdentifier())); | |
263 | return RefactoringStatus.createFatalErrorStatus(message, context); | |
264 | } | |
265 | ||
266 | /* | |
267 | * @see Refactoring#checkInput(IProgressMonitor) | |
268 | */ | |
269 | @Override | |
270 | public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException { | |
271 | try { | |
272 | pm.beginTask("", 1); //$NON-NLS-1$ | |
273 | return new RefactoringStatus(); | |
274 | } finally { | |
275 | pm.done(); | |
276 | } | |
277 | } | |
278 | ||
279 | //----- changes | |
280 | ||
281 | @Override | |
282 | public Change createChange(IProgressMonitor pm) throws CoreException { | |
283 | try { | |
284 | pm.beginTask(RefactoringCoreMessages.InlineTempRefactoring_preview, 2); | |
285 | final Map<String, String> arguments= new HashMap<String, String>(); | |
286 | String project= null; | |
287 | IJavaProject javaProject= fCu.getJavaProject(); | |
288 | if (javaProject != null) | |
289 | project= javaProject.getElementName(); | |
290 | ||
291 | final IVariableBinding binding= getVariableDeclaration().resolveBinding(); | |
292 | String text= null; | |
293 | final IMethodBinding method= binding.getDeclaringMethod(); | |
294 | if (method != null) | |
295 | text= BindingLabelProvider.getBindingLabel(method, JavaElementLabels.ALL_FULLY_QUALIFIED); | |
296 | else | |
297 | text= BasicElementLabels.getJavaElementName('{' + JavaElementLabels.ELLIPSIS_STRING + '}'); | |
298 | final String description= Messages.format(RefactoringCoreMessages.InlineTempRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(binding.getName())); | |
299 | final String header= Messages.format(RefactoringCoreMessages.InlineTempRefactoring_descriptor_description, new String[] { BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED), text}); | |
300 | final JDTRefactoringDescriptorComment comment= new JDTRefactoringDescriptorComment(project, this, header); | |
301 | comment.addSetting(Messages.format(RefactoringCoreMessages.InlineTempRefactoring_original_pattern, BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED))); | |
302 | final InlineLocalVariableDescriptor descriptor= RefactoringSignatureDescriptorFactory.createInlineLocalVariableDescriptor(project, description, comment.asString(), arguments, RefactoringDescriptor.NONE); | |
303 | arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCu)); | |
304 | arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, String.valueOf(fSelectionStart) + ' ' + String.valueOf(fSelectionLength)); | |
305 | ||
306 | CompilationUnitRewrite cuRewrite= new CompilationUnitRewrite(fCu, fASTRoot); | |
307 | ||
308 | inlineTemp(cuRewrite); | |
309 | removeTemp(cuRewrite); | |
310 | ||
311 | final CompilationUnitChange result= cuRewrite.createChange(RefactoringCoreMessages.InlineTempRefactoring_inline, false, new SubProgressMonitor(pm, 1)); | |
312 | result.setDescriptor(new RefactoringChangeDescriptor(descriptor)); | |
313 | return result; | |
314 | } finally { | |
315 | pm.done(); | |
316 | } | |
317 | } | |
318 | ||
319 | private void inlineTemp(CompilationUnitRewrite cuRewrite) throws JavaModelException { | |
320 | SimpleName[] references= getReferences(); | |
321 | ||
322 | TextEditGroup groupDesc= cuRewrite.createGroupDescription(RefactoringCoreMessages.InlineTempRefactoring_inline_edit_name); | |
323 | ASTRewrite rewrite= cuRewrite.getASTRewrite(); | |
324 | ||
325 | for (int i= 0; i < references.length; i++){ | |
326 | SimpleName curr= references[i]; | |
327 | ASTNode initializerCopy= getInitializerSource(cuRewrite, curr); | |
328 | rewrite.replace(curr, initializerCopy, groupDesc); | |
329 | } | |
330 | } | |
331 | ||
332 | private void removeTemp(CompilationUnitRewrite cuRewrite) { | |
333 | VariableDeclaration variableDeclaration= getVariableDeclaration(); | |
334 | TextEditGroup groupDesc= cuRewrite.createGroupDescription(RefactoringCoreMessages.InlineTempRefactoring_remove_edit_name); | |
335 | ASTNode parent= variableDeclaration.getParent(); | |
336 | ASTRewrite rewrite= cuRewrite.getASTRewrite(); | |
337 | TightSourceRangeComputer sourceRangeComputer= new TightSourceRangeComputer(); | |
338 | rewrite.setTargetSourceRangeComputer(sourceRangeComputer); | |
339 | if (parent instanceof VariableDeclarationStatement && ((VariableDeclarationStatement) parent).fragments().size() == 1) { | |
340 | sourceRangeComputer.addTightSourceNode(parent); | |
341 | rewrite.remove(parent, groupDesc); | |
342 | } else { | |
343 | sourceRangeComputer.addTightSourceNode(variableDeclaration); | |
344 | rewrite.remove(variableDeclaration, groupDesc); | |
345 | } | |
346 | } | |
347 | ||
348 | private Expression getInitializerSource(CompilationUnitRewrite rewrite, SimpleName reference) throws JavaModelException { | |
349 | Expression copy= getModifiedInitializerSource(rewrite, reference); | |
350 | if (NecessaryParenthesesChecker.needsParentheses(copy, reference.getParent(), reference.getLocationInParent())) { | |
351 | ParenthesizedExpression parentExpr= rewrite.getAST().newParenthesizedExpression(); | |
352 | parentExpr.setExpression(copy); | |
353 | return parentExpr; | |
354 | } | |
355 | return copy; | |
356 | } | |
357 | ||
358 | private Expression getModifiedInitializerSource(CompilationUnitRewrite rewrite, SimpleName reference) throws JavaModelException { | |
359 | VariableDeclaration varDecl= getVariableDeclaration(); | |
360 | Expression initializer= varDecl.getInitializer(); | |
361 | ||
362 | ASTNode referenceContext= reference.getParent(); | |
363 | if (Invocations.isResolvedTypeInferredFromExpectedType(initializer)) { | |
364 | if (! (referenceContext instanceof VariableDeclarationFragment | |
365 | || referenceContext instanceof SingleVariableDeclaration | |
366 | || referenceContext instanceof Assignment)) { | |
367 | ITypeBinding[] typeArguments= Invocations.getInferredTypeArguments(initializer); | |
368 | if (typeArguments != null) { | |
369 | String newSource= createParameterizedInvocation(initializer, typeArguments, rewrite); | |
370 | return (Expression) rewrite.getASTRewrite().createStringPlaceholder(newSource, initializer.getNodeType()); | |
371 | } | |
372 | } | |
373 | } | |
374 | ||
375 | Expression copy= (Expression) rewrite.getASTRewrite().createCopyTarget(initializer); | |
376 | ||
377 | AST ast= rewrite.getAST(); | |
378 | ITypeBinding explicitCast= ASTNodes.getExplicitCast(initializer, reference); | |
379 | if (explicitCast != null) { | |
380 | CastExpression cast= ast.newCastExpression(); | |
381 | if (NecessaryParenthesesChecker.needsParentheses(initializer, cast, CastExpression.EXPRESSION_PROPERTY)) { | |
382 | ParenthesizedExpression parenthesized= ast.newParenthesizedExpression(); | |
383 | parenthesized.setExpression(copy); | |
384 | copy= parenthesized; | |
385 | } | |
386 | cast.setExpression(copy); | |
387 | cast.setType(rewrite.getImportRewrite().addImport(explicitCast, ast)); | |
388 | copy= cast; | |
389 | ||
390 | } else if (initializer instanceof ArrayInitializer && ASTNodes.getDimensions(varDecl) > 0) { | |
391 | ArrayType newType= (ArrayType) ASTNodeFactory.newType(ast, varDecl); | |
392 | ||
393 | ArrayCreation newArrayCreation= ast.newArrayCreation(); | |
394 | newArrayCreation.setType(newType); | |
395 | newArrayCreation.setInitializer((ArrayInitializer) copy); | |
396 | return newArrayCreation; | |
397 | } | |
398 | return copy; | |
399 | } | |
400 | ||
401 | private String createParameterizedInvocation(Expression invocation, ITypeBinding[] typeArguments, CompilationUnitRewrite cuRewrite) throws JavaModelException { | |
402 | ASTRewrite rewrite= ASTRewrite.create(invocation.getAST()); | |
403 | ListRewrite typeArgsRewrite= Invocations.getInferredTypeArgumentsRewrite(rewrite, invocation); | |
404 | ||
405 | for (int i= 0; i < typeArguments.length; i++) { | |
406 | Type typeArgumentNode= cuRewrite.getImportRewrite().addImport(typeArguments[i], cuRewrite.getAST()); | |
407 | typeArgsRewrite.insertLast(typeArgumentNode, null); | |
408 | } | |
409 | ||
410 | if (invocation instanceof MethodInvocation) { | |
411 | MethodInvocation methodInvocation= (MethodInvocation) invocation; | |
412 | Expression expression= methodInvocation.getExpression(); | |
413 | if (expression == null) { | |
414 | IMethodBinding methodBinding= methodInvocation.resolveMethodBinding(); | |
415 | if (methodBinding != null && Modifier.isStatic(methodBinding.getModifiers())) { | |
416 | expression= cuRewrite.getAST().newName(cuRewrite.getImportRewrite().addImport(methodBinding.getDeclaringClass().getTypeDeclaration())); | |
417 | } else { | |
418 | expression= invocation.getAST().newThisExpression(); | |
419 | } | |
420 | rewrite.set(invocation, MethodInvocation.EXPRESSION_PROPERTY, expression, null); | |
421 | } | |
422 | } | |
423 | ||
424 | IDocument document= new Document(fCu.getBuffer().getContents()); | |
425 | final RangeMarker marker= new RangeMarker(invocation.getStartPosition(), invocation.getLength()); | |
426 | IJavaProject project= fCu.getJavaProject(); | |
427 | TextEdit[] rewriteEdits= rewrite.rewriteAST(document, project.getOptions(true)).removeChildren(); | |
428 | marker.addChildren(rewriteEdits); | |
429 | try { | |
430 | marker.apply(document, TextEdit.UPDATE_REGIONS); | |
431 | String rewrittenInitializer= document.get(marker.getOffset(), marker.getLength()); | |
432 | IRegion region= document.getLineInformation(document.getLineOfOffset(marker.getOffset())); | |
433 | int oldIndent= Strings.computeIndentUnits(document.get(region.getOffset(), region.getLength()), project); | |
434 | return Strings.changeIndent(rewrittenInitializer, oldIndent, project, "", TextUtilities.getDefaultLineDelimiter(document)); //$NON-NLS-1$ | |
435 | } catch (MalformedTreeException e) { | |
436 | JavaPlugin.log(e); | |
437 | } catch (BadLocationException e) { | |
438 | JavaPlugin.log(e); | |
439 | } | |
440 | //fallback: | |
441 | return fCu.getBuffer().getText(invocation.getStartPosition(), invocation.getLength()); | |
442 | } | |
443 | ||
444 | public SimpleName[] getReferences() { | |
445 | if (fReferences != null) | |
446 | return fReferences; | |
447 | TempOccurrenceAnalyzer analyzer= new TempOccurrenceAnalyzer(getVariableDeclaration(), false); | |
448 | analyzer.perform(); | |
449 | fReferences= analyzer.getReferenceNodes(); | |
450 | return fReferences; | |
451 | } | |
452 | ||
453 | private RefactoringStatus initialize(JavaRefactoringArguments arguments) { | |
454 | final String selection= arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION); | |
455 | if (selection != null) { | |
456 | int offset= -1; | |
457 | int length= -1; | |
458 | final StringTokenizer tokenizer= new StringTokenizer(selection); | |
459 | if (tokenizer.hasMoreTokens()) | |
460 | offset= Integer.valueOf(tokenizer.nextToken()).intValue(); | |
461 | if (tokenizer.hasMoreTokens()) | |
462 | length= Integer.valueOf(tokenizer.nextToken()).intValue(); | |
463 | if (offset >= 0 && length >= 0) { | |
464 | fSelectionStart= offset; | |
465 | fSelectionLength= length; | |
466 | } else | |
467 | return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { selection, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION})); | |
468 | } else | |
469 | return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION)); | |
470 | final String handle= arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT); | |
471 | if (handle != null) { | |
472 | final IJavaElement element= JavaRefactoringDescriptorUtil.handleToElement(arguments.getProject(), handle, false); | |
473 | if (element == null || !element.exists() || element.getElementType() != IJavaElement.COMPILATION_UNIT) | |
474 | return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INLINE_LOCAL_VARIABLE); | |
475 | else { | |
476 | fCu= (ICompilationUnit) element; | |
477 | if (checkIfTempSelected().hasFatalError()) | |
478 | return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INLINE_LOCAL_VARIABLE); | |
479 | } | |
480 | } else | |
481 | return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT)); | |
482 | return new RefactoringStatus(); | |
483 | } | |
484 | } |