001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.rename;
011:
012: import java.util.Arrays;
013: import java.util.HashSet;
014: import java.util.Iterator;
015: import java.util.Set;
016:
017: import org.eclipse.text.edits.TextEdit;
018:
019: import org.eclipse.jface.text.IRegion;
020:
021: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
022: import org.eclipse.ltk.core.refactoring.RefactoringStatusContext;
023: import org.eclipse.ltk.core.refactoring.TextChange;
024:
025: import org.eclipse.jdt.core.compiler.IProblem;
026: import org.eclipse.jdt.core.dom.ASTNode;
027: import org.eclipse.jdt.core.dom.Block;
028: import org.eclipse.jdt.core.dom.CompilationUnit;
029: import org.eclipse.jdt.core.dom.MethodDeclaration;
030: import org.eclipse.jdt.core.dom.SimpleName;
031: import org.eclipse.jdt.core.dom.VariableDeclaration;
032:
033: import org.eclipse.jdt.internal.corext.SourceRange;
034: import org.eclipse.jdt.internal.corext.dom.ASTNodes;
035: import org.eclipse.jdt.internal.corext.dom.NodeFinder;
036: import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
037: import org.eclipse.jdt.internal.corext.refactoring.base.JavaStringStatusContext;
038: import org.eclipse.jdt.internal.corext.util.Messages;
039:
040: public class RefactoringAnalyzeUtil {
041:
042: private RefactoringAnalyzeUtil() {
043: //no instances
044: }
045:
046: public static IRegion[] getNewRanges(TextEdit[] edits,
047: TextChange change) {
048: IRegion[] result = new IRegion[edits.length];
049: for (int i = 0; i < edits.length; i++) {
050: result[i] = RefactoringAnalyzeUtil.getNewTextRange(
051: edits[i], change);
052: }
053: return result;
054: }
055:
056: public static RefactoringStatus reportProblemNodes(
057: String modifiedWorkingCopySource, SimpleName[] problemNodes) {
058: RefactoringStatus result = new RefactoringStatus();
059: for (int i = 0; i < problemNodes.length; i++) {
060: RefactoringStatusContext context = new JavaStringStatusContext(
061: modifiedWorkingCopySource, new SourceRange(
062: problemNodes[i]));
063: result
064: .addError(
065: Messages
066: .format(
067: RefactoringCoreMessages.RefactoringAnalyzeUtil_name_collision,
068: problemNodes[i]
069: .getIdentifier()),
070: context);
071: }
072: return result;
073: }
074:
075: public static MethodDeclaration getMethodDeclaration(TextEdit edit,
076: TextChange change, CompilationUnit cuNode) {
077: ASTNode decl = RefactoringAnalyzeUtil.findSimpleNameNode(
078: RefactoringAnalyzeUtil.getNewTextRange(edit, change),
079: cuNode);
080: return ((MethodDeclaration) ASTNodes.getParent(decl,
081: MethodDeclaration.class));
082: }
083:
084: public static Block getBlock(TextEdit edit, TextChange change,
085: CompilationUnit cuNode) {
086: ASTNode decl = RefactoringAnalyzeUtil.findSimpleNameNode(
087: RefactoringAnalyzeUtil.getNewTextRange(edit, change),
088: cuNode);
089: return ((Block) ASTNodes.getParent(decl, Block.class));
090: }
091:
092: public static IProblem[] getIntroducedCompileProblems(
093: CompilationUnit newCUNode, CompilationUnit oldCuNode) {
094: Set subResult = new HashSet();
095: Set oldProblems = getOldProblems(oldCuNode);
096: IProblem[] newProblems = ASTNodes.getProblems(newCUNode,
097: ASTNodes.INCLUDE_ALL_PARENTS, ASTNodes.PROBLEMS);
098: for (int i = 0; i < newProblems.length; i++) {
099: IProblem correspondingOld = findCorrespondingProblem(
100: oldProblems, newProblems[i]);
101: if (correspondingOld == null)
102: subResult.add(newProblems[i]);
103: }
104: return (IProblem[]) subResult.toArray(new IProblem[subResult
105: .size()]);
106: }
107:
108: public static IRegion getNewTextRange(TextEdit edit,
109: TextChange change) {
110: return change.getPreviewEdit(edit).getRegion();
111: }
112:
113: private static IProblem findCorrespondingProblem(Set oldProblems,
114: IProblem iProblem) {
115: for (Iterator iter = oldProblems.iterator(); iter.hasNext();) {
116: IProblem oldProblem = (IProblem) iter.next();
117: if (isCorresponding(oldProblem, iProblem))
118: return oldProblem;
119: }
120: return null;
121: }
122:
123: private static boolean isCorresponding(IProblem oldProblem,
124: IProblem iProblem) {
125: if (oldProblem.getID() != iProblem.getID())
126: return false;
127: if (!oldProblem.getMessage().equals(iProblem.getMessage()))
128: return false;
129: return true;
130: }
131:
132: private static SimpleName getSimpleName(ASTNode node) {
133: if (node instanceof SimpleName)
134: return (SimpleName) node;
135: if (node instanceof VariableDeclaration)
136: return ((VariableDeclaration) node).getName();
137: return null;
138: }
139:
140: private static SimpleName findSimpleNameNode(IRegion range,
141: CompilationUnit cuNode) {
142: ASTNode node = NodeFinder.perform(cuNode, range.getOffset(),
143: range.getLength());
144: return getSimpleName(node);
145: }
146:
147: private static Set getOldProblems(CompilationUnit oldCuNode) {
148: return new HashSet(Arrays.asList(ASTNodes.getProblems(
149: oldCuNode, ASTNodes.INCLUDE_ALL_PARENTS,
150: ASTNodes.PROBLEMS)));
151: }
152: }
|