]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/core refactoring/org/eclipse/jdt/internal/corext/refactoring/SearchResultGroup.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / core refactoring / org / eclipse / jdt / internal / corext / refactoring / SearchResultGroup.java
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;
12
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.Comparator;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.eclipse.core.runtime.Assert;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.core.runtime.SubProgressMonitor;
26
27 import org.eclipse.core.resources.IResource;
28
29 import org.eclipse.text.edits.TextEditGroup;
30
31 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
32
33 import org.eclipse.jdt.core.ICompilationUnit;
34 import org.eclipse.jdt.core.IImportDeclaration;
35 import org.eclipse.jdt.core.IJavaElement;
36 import org.eclipse.jdt.core.IJavaProject;
37 import org.eclipse.jdt.core.IMethod;
38 import org.eclipse.jdt.core.JavaCore;
39 import org.eclipse.jdt.core.JavaModelException;
40 import org.eclipse.jdt.core.dom.ASTNode;
41 import org.eclipse.jdt.core.dom.ClassInstanceCreation;
42 import org.eclipse.jdt.core.dom.CompilationUnit;
43 import org.eclipse.jdt.core.dom.MethodRef;
44 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
45 import org.eclipse.jdt.core.refactoring.CompilationUnitChange;
46 import org.eclipse.jdt.core.search.SearchMatch;
47
48 import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceFactoryRefactoring;
49 import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceIndirectionRefactoring;
50 import org.eclipse.jdt.internal.corext.refactoring.rename.RefactoringScanner.TextMatch;
51 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameFieldProcessor;
52 import org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor.ImportsManager.ImportChange;
53 import org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor.PackageRenamer;
54 import org.eclipse.jdt.internal.corext.refactoring.rename.TextMatchUpdater;
55 import org.eclipse.jdt.internal.corext.refactoring.structure.ChangeTypeRefactoring;
56 import org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor;
57 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.ASTCreator;
58 import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil;
59 import org.eclipse.jdt.internal.corext.util.Messages;
60 import org.eclipse.jdt.internal.corext.util.SearchUtils;
61
62 public class SearchResultGroup {
63
64         private final IResource fResouce;
65         private final List<SearchMatch> fSearchMatches;
66
67         public SearchResultGroup(IResource res, SearchMatch[] matches){
68                 Assert.isNotNull(matches);
69                 fResouce= res;
70                 fSearchMatches= new ArrayList<SearchMatch>(Arrays.asList(matches));
71         }
72
73         public void add(SearchMatch match) {
74                 Assert.isNotNull(match);
75                 fSearchMatches.add(match);
76         }
77
78         public IResource getResource() {
79                 return fResouce;
80         }
81
82         public SearchMatch[] getSearchResults() {
83                 return fSearchMatches.toArray(new SearchMatch[fSearchMatches.size()]);
84         }
85
86         public static IResource[] getResources(SearchResultGroup[] searchResultGroups){
87                 Set<IResource> resourceSet= new HashSet<IResource>(searchResultGroups.length);
88                 for (int i= 0; i < searchResultGroups.length; i++) {
89                         resourceSet.add(searchResultGroups[i].getResource());
90                 }
91                 return resourceSet.toArray(new IResource[resourceSet.size()]);
92         }
93
94         public ICompilationUnit getCompilationUnit(){
95                 if (getSearchResults() == null || getSearchResults().length == 0)
96                         return null;
97                 return SearchUtils.getCompilationUnit(getSearchResults()[0]);
98         }
99
100         @Override
101         public String toString() {
102                 StringBuffer buf= new StringBuffer(fResouce.getFullPath().toString());
103                 buf.append('\n');
104                 for (int i= 0; i < fSearchMatches.size(); i++) {
105                         SearchMatch match= fSearchMatches.get(i);
106                         buf.append("  ").append(match.getOffset()).append(", ").append(match.getLength()); //$NON-NLS-1$//$NON-NLS-2$
107                         buf.append(match.getAccuracy() == SearchMatch.A_ACCURATE ? "; acc" : "; inacc"); //$NON-NLS-1$//$NON-NLS-2$
108                         if (match.isInsideDocComment())
109                                 buf.append("; inDoc"); //$NON-NLS-1$
110                         if (match.getElement() instanceof IJavaElement)
111                                 buf.append("; in: ").append(((IJavaElement) match.getElement()).getElementName()); //$NON-NLS-1$
112                         buf.append('\n');
113                 }
114                 return buf.toString();
115         }
116
117         public void generated_9219966501974804659(final Map<IJavaProject, Set<SearchResultGroup>> map) {
118                 IJavaProject project;
119                 ICompilationUnit unit;
120                 unit= getCompilationUnit();
121                 if (unit != null) {
122                         project= unit.getJavaProject();
123                         if (project != null) {
124                                 Set<SearchResultGroup> set= map.get(project);
125                                 if (set == null) {
126                                         set= new HashSet<SearchResultGroup>();
127                                         map.put(project, set);
128                                 }
129                                 set.add(this);
130                         }
131                 }
132         }
133
134         public void generated_2512581688664264112(Collection<SearchResultGroup> result, IntroduceFactoryRefactoring introducefactoryrefactoring) {
135                 ICompilationUnit        unit= getCompilationUnit();
136         
137                 if (unit != null) // ignore hits within a binary unit
138                         result.add(this);
139                 else
140                         introducefactoryrefactoring.fCallSitesInBinaryUnits= true;
141         }
142
143         public boolean generated_944990252320201359(IntroduceFactoryRefactoring introducefactoryrefactoring, CompilationUnit unit, ASTRewrite unitRewriter, CompilationUnitChange unitChange) throws CoreException {
144                 Assert.isTrue(ASTCreator.getCu(unit).equals(getCompilationUnit()));
145                 SearchMatch[] hits= getSearchResults();
146                 Arrays.sort(hits, new Comparator<SearchMatch>() {
147                         /**
148                          * Sort by descending offset, such that nested constructor calls are processed first.
149                          * This is necessary, since they can only be moved into the factory method invocation
150                          * after they have been rewritten.
151                          */
152                         public int compare(SearchMatch m1, SearchMatch m2) {
153                                 return m2.getOffset() - m1.getOffset();
154                         }
155                 });
156                 
157                 boolean someCallPatched= false;
158         
159                 for (int i=0; i < hits.length; i++) {
160                         ASTNode ctrCall= introducefactoryrefactoring.getCtorCallAt(hits[i].getOffset(), hits[i].getLength(), unit);
161         
162                         if (ctrCall instanceof ClassInstanceCreation) {
163                                 TextEditGroup gd= new TextEditGroup(RefactoringCoreMessages.IntroduceFactory_replaceCalls);
164         
165                                 introducefactoryrefactoring.rewriteFactoryMethodCall((ClassInstanceCreation) ctrCall, unitRewriter, gd);
166                                 unitChange.addTextEditGroup(gd);
167                                 someCallPatched= true;
168                         } else if (ctrCall instanceof MethodRef) {
169                                 TextEditGroup gd= new TextEditGroup(RefactoringCoreMessages.IntroduceFactoryRefactoring_replaceJavadocReference);
170         
171                                 introducefactoryrefactoring.rewriteJavadocReference((MethodRef) ctrCall, unitRewriter, gd);
172                                 unitChange.addTextEditGroup(gd);
173                                 someCallPatched= true;
174                         }
175                 }
176                 return someCallPatched;
177         }
178
179         public void generated_5675285520323392266(IntroduceIndirectionRefactoring introduceindirectionrefactoring) throws CoreException {
180                 if (!introduceindirectionrefactoring.isRewriteKept(getCompilationUnit()))
181                         introduceindirectionrefactoring.createChangeAndDiscardRewrite(getCompilationUnit());
182         }
183
184         public void generated_7814155347866335892(IMethod existingAccessor, RefactoringStatus result, RenameFieldProcessor renamefieldprocessor) {
185                 Assert.isTrue(getSearchResults().length > 0);
186                 if (getSearchResults().length != 1){
187                         String message= Messages.format(RefactoringCoreMessages.RenameFieldRefactoring_overridden_or_overrides,
188                                                                 JavaElementUtil.createMethodSignature(existingAccessor));
189                         result.addError(message);
190                 }
191         }
192
193         public void generated_4444902487714718191(PackageRenamer packagerenamer) throws JavaModelException {
194                 SearchMatch[] searchResults= getSearchResults();
195                 for (int i= 0; i < searchResults.length; i++) {
196                         SearchMatch result= searchResults[i];
197                         IJavaElement enclosingElement= SearchUtils.getEnclosingJavaElement(result);
198                         if (! (enclosingElement instanceof IImportDeclaration)) {
199                                 String reference= PackageRenamer.getNormalizedTypeReference(result);
200                                 if (! reference.startsWith(packagerenamer.fPackage.getElementName())) {
201                                         // is unqualified
202                                         reference= PackageRenamer.cutOffInnerTypes(reference);
203                                         ImportChange importChange= packagerenamer.fImportsManager.getImportChange(getCompilationUnit());
204                                         importChange.addImport(packagerenamer.fPackage.getElementName() + '.' + reference);
205                                 }
206                         }
207                 }
208         }
209
210         public void generated_6410720931124845059(PackageRenamer packagerenamer) throws JavaModelException {
211                 SearchMatch[] searchResults= getSearchResults();
212                 for (int i= 0; i < searchResults.length; i++) {
213                         SearchMatch result= searchResults[i];
214                         IJavaElement enclosingElement= SearchUtils.getEnclosingJavaElement(result);
215                         if (enclosingElement instanceof IImportDeclaration) {
216                                 IImportDeclaration importDeclaration= (IImportDeclaration) enclosingElement;
217                                 packagerenamer.updateImport(getCompilationUnit(), importDeclaration, packagerenamer.getUpdatedImport(importDeclaration));
218                         } else {
219                                 String reference= PackageRenamer.getNormalizedTypeReference(result);
220                                 if (! reference.startsWith(packagerenamer.fPackage.getElementName())) {
221                                         reference= PackageRenamer.cutOffInnerTypes(reference);
222                                         ImportChange importChange= packagerenamer.fImportsManager.getImportChange(getCompilationUnit());
223                                         importChange.removeImport(packagerenamer.fPackage.getElementName() + '.' + reference);
224                                         importChange.addImport(packagerenamer.getNewPackageName() + '.' + reference);
225                                 } // else: already found & updated with package reference search
226                         }
227                 }
228         }
229
230         public void generated_8571484088675671927(ICompilationUnit cu, Set<TextMatch> matches, TextMatchUpdater textmatchupdater) {
231                 if (cu.equals(getCompilationUnit())) {
232                         textmatchupdater.removeReferences(matches, this);
233                 }
234         }
235
236         public void generated_7694260576410740004(final IProgressMonitor monitor, MemberVisibilityAdjustor membervisibilityadjustor) throws JavaModelException {
237                 IJavaElement element;
238                 SearchMatch[] matches;
239                 element= JavaCore.create(getResource());
240                 if (element instanceof ICompilationUnit) {
241                         matches= getSearchResults();
242                         for (int offset= 0; offset < matches.length; offset++)
243                                 membervisibilityadjustor.adjustOutgoingVisibility(matches[offset], new SubProgressMonitor(monitor, 1));
244                 } // else if (element != null)
245                 // fStatus.merge(RefactoringStatus.createStatus(fFailureSeverity, RefactoringCoreMessages.getFormattedString("MemberVisibilityAdjustor.binary.outgoing.project", new String[] { element.getJavaProject().getElementName(), getLabel(fReferenced)}), null, null, RefactoringStatusEntry.NO_CODE, null)); //$NON-NLS-1$
246                 // else if (group.getResource() != null)
247                 // fStatus.merge(RefactoringStatus.createStatus(fFailureSeverity, RefactoringCoreMessages.getFormattedString("MemberVisibilityAdjustor.binary.outgoing.resource", new String[] { group.getResource().getName(), getLabel(fReferenced)}), null, null, RefactoringStatusEntry.NO_CODE, null)); //$NON-NLS-1$
248         
249                 // TW: enable when bug 78387 is fixed
250         
251                 monitor.worked(1);
252         }
253
254         public void generated_3750388673956263140(List<ICompilationUnit> result, ChangeTypeRefactoring changetyperefactoring) {
255                 ICompilationUnit cu= getCompilationUnit();
256                 if (cu != null) {
257                         result.add(cu);
258                         changetyperefactoring.fCuToSearchResultGroup.put(cu, this);
259                 }
260         }
261 }