KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > rename > RefactoringAnalyzeUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.rename;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.text.edits.TextEdit;
19
20 import org.eclipse.jface.text.IRegion;
21
22 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
23 import org.eclipse.ltk.core.refactoring.RefactoringStatusContext;
24 import org.eclipse.ltk.core.refactoring.TextChange;
25
26 import org.eclipse.jdt.core.compiler.IProblem;
27 import org.eclipse.jdt.core.dom.ASTNode;
28 import org.eclipse.jdt.core.dom.Block;
29 import org.eclipse.jdt.core.dom.CompilationUnit;
30 import org.eclipse.jdt.core.dom.MethodDeclaration;
31 import org.eclipse.jdt.core.dom.SimpleName;
32 import org.eclipse.jdt.core.dom.VariableDeclaration;
33
34 import org.eclipse.jdt.internal.corext.SourceRange;
35 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
36 import org.eclipse.jdt.internal.corext.dom.NodeFinder;
37 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
38 import org.eclipse.jdt.internal.corext.refactoring.base.JavaStringStatusContext;
39 import org.eclipse.jdt.internal.corext.util.Messages;
40
41 public class RefactoringAnalyzeUtil {
42     
43     private RefactoringAnalyzeUtil() {
44         //no instances
45
}
46     
47     public static IRegion[] getNewRanges(TextEdit[] edits, TextChange change){
48         IRegion[] result= new IRegion[edits.length];
49         for (int i= 0; i < edits.length; i++) {
50             result[i]= RefactoringAnalyzeUtil.getNewTextRange(edits[i], change);
51         }
52         return result;
53     }
54
55     public static RefactoringStatus reportProblemNodes(String JavaDoc modifiedWorkingCopySource, SimpleName[] problemNodes) {
56         RefactoringStatus result= new RefactoringStatus();
57         for (int i= 0; i < problemNodes.length; i++) {
58             RefactoringStatusContext context= new JavaStringStatusContext(modifiedWorkingCopySource, new SourceRange(problemNodes[i]));
59             result.addError(Messages.format(RefactoringCoreMessages.RefactoringAnalyzeUtil_name_collision, problemNodes[i].getIdentifier()), context);
60         }
61         return result;
62     }
63     
64
65     public static MethodDeclaration getMethodDeclaration(TextEdit edit, TextChange change, CompilationUnit cuNode){
66         ASTNode decl= RefactoringAnalyzeUtil.findSimpleNameNode(RefactoringAnalyzeUtil.getNewTextRange(edit, change), cuNode);
67         return ((MethodDeclaration)ASTNodes.getParent(decl, MethodDeclaration.class));
68     }
69
70     public static Block getBlock(TextEdit edit, TextChange change, CompilationUnit cuNode){
71         ASTNode decl= RefactoringAnalyzeUtil.findSimpleNameNode(RefactoringAnalyzeUtil.getNewTextRange(edit, change), cuNode);
72         return ((Block)ASTNodes.getParent(decl, Block.class));
73     }
74     
75     public static IProblem[] getIntroducedCompileProblems(CompilationUnit newCUNode, CompilationUnit oldCuNode) {
76         Set JavaDoc subResult= new HashSet JavaDoc();
77         Set JavaDoc oldProblems= getOldProblems(oldCuNode);
78         IProblem[] newProblems= ASTNodes.getProblems(newCUNode, ASTNodes.INCLUDE_ALL_PARENTS, ASTNodes.PROBLEMS);
79         for (int i= 0; i < newProblems.length; i++) {
80             IProblem correspondingOld= findCorrespondingProblem(oldProblems, newProblems[i]);
81             if (correspondingOld == null)
82                 subResult.add(newProblems[i]);
83         }
84         return (IProblem[]) subResult.toArray(new IProblem[subResult.size()]);
85     }
86     
87     public static IRegion getNewTextRange(TextEdit edit, TextChange change){
88         return change.getPreviewEdit(edit).getRegion();
89     }
90     
91     private static IProblem findCorrespondingProblem(Set JavaDoc oldProblems, IProblem iProblem) {
92         for (Iterator JavaDoc iter= oldProblems.iterator(); iter.hasNext();) {
93             IProblem oldProblem= (IProblem) iter.next();
94             if (isCorresponding(oldProblem, iProblem))
95                 return oldProblem;
96         }
97         return null;
98     }
99     
100     private static boolean isCorresponding(IProblem oldProblem, IProblem iProblem) {
101         if (oldProblem.getID() != iProblem.getID())
102             return false;
103         if (! oldProblem.getMessage().equals(iProblem.getMessage()))
104             return false;
105         return true;
106     }
107
108     private static SimpleName getSimpleName(ASTNode node){
109         if (node instanceof SimpleName)
110             return (SimpleName)node;
111         if (node instanceof VariableDeclaration)
112             return ((VariableDeclaration)node).getName();
113         return null;
114     }
115
116     private static SimpleName findSimpleNameNode(IRegion range, CompilationUnit cuNode) {
117         ASTNode node= NodeFinder.perform(cuNode, range.getOffset(), range.getLength());
118         return getSimpleName(node);
119     }
120
121     private static Set JavaDoc getOldProblems(CompilationUnit oldCuNode) {
122         return new HashSet JavaDoc(Arrays.asList(ASTNodes.getProblems(oldCuNode, ASTNodes.INCLUDE_ALL_PARENTS, ASTNodes.PROBLEMS)));
123     }
124 }
125
Popular Tags