]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/core refactoring/org/eclipse/jdt/internal/corext/refactoring/TypedSource.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / core refactoring / org / eclipse / jdt / internal / corext / refactoring / TypedSource.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.Comparator;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.core.runtime.CoreException;
22
23 import org.eclipse.jdt.core.Flags;
24 import org.eclipse.jdt.core.IBuffer;
25 import org.eclipse.jdt.core.ICompilationUnit;
26 import org.eclipse.jdt.core.IField;
27 import org.eclipse.jdt.core.IImportContainer;
28 import org.eclipse.jdt.core.IJavaElement;
29 import org.eclipse.jdt.core.ISourceReference;
30 import org.eclipse.jdt.core.JavaModelException;
31 import org.eclipse.jdt.core.dom.ASTNode;
32 import org.eclipse.jdt.core.dom.ASTParser;
33 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
34 import org.eclipse.jdt.core.dom.CompilationUnit;
35 import org.eclipse.jdt.core.dom.FieldDeclaration;
36 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
37 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
38
39 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils;
40 import org.eclipse.jdt.internal.corext.refactoring.structure.ASTNodeSearchUtil;
41 import org.eclipse.jdt.internal.corext.util.Strings;
42
43 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
44 import org.eclipse.jdt.internal.ui.refactoring.reorg.PasteAction.TypedSourcePaster.PasteTypedSourcesRefactoring;
45
46 /**
47  * A tuple used to keep source of an element and its type.
48  * @see IJavaElement
49  * @see org.eclipse.jdt.core.ISourceReference
50  */
51 public class TypedSource {
52
53         private static class SourceTuple {
54
55                 private SourceTuple(ICompilationUnit unit) {
56                         this.unit= unit;
57                 }
58                 private ICompilationUnit unit;
59                 private CompilationUnit node;
60         }
61
62         private final String fSource;
63         private final int fType;
64
65         private TypedSource(String source, int type){
66                 Assert.isNotNull(source);
67                 Assert.isTrue(canCreateForType(type));
68                 fSource= source;
69                 fType= type;
70         }
71
72         public static TypedSource create(String source, int type) {
73                 if (source == null || ! canCreateForType(type))
74                         return null;
75                 return new TypedSource(source, type);
76         }
77
78         public String getSource() {
79                 return fSource;
80         }
81
82         public int getType() {
83                 return fType;
84         }
85
86         /* (non-Javadoc)
87          * @see java.lang.Object#equals(java.lang.Object)
88          */
89         @Override
90         public boolean equals(Object other) {
91                 if (! (other instanceof TypedSource))
92                         return false;
93
94                 TypedSource ts= (TypedSource)other;
95                 return ts.generated_7516993050506477390(this);
96         }
97
98         /* (non-Javadoc)
99          * @see java.lang.Object#hashCode()
100          */
101         @Override
102         public int hashCode() {
103                 return getSource().hashCode() ^ (97 * getType());
104         }
105
106         public void generated_639287168146208855(CompilationUnit cuNode, ASTRewrite rewrite, PasteTypedSourcesRefactoring pastetypedsourcesrefactoring) throws JavaModelException {
107                 final ASTNode destination= pastetypedsourcesrefactoring.getDestinationNodeForSourceElement(pastetypedsourcesrefactoring.fDestination, getType(), cuNode);
108                 if (destination != null) {
109                         if (destination instanceof CompilationUnit)
110                                 PasteTypedSourcesRefactoring.insertToCu(rewrite, pastetypedsourcesrefactoring.createNewNodeToInsertToCu(this, rewrite), (CompilationUnit) destination);
111                         else if (destination instanceof AbstractTypeDeclaration)
112                                 PasteTypedSourcesRefactoring.insertToType(rewrite, pastetypedsourcesrefactoring.createNewNodeToInsertToType(this, rewrite), (AbstractTypeDeclaration) destination);
113                 }
114         }
115
116         public ASTNode generated_2915041481128989772(ASTRewrite rewrite) {
117                 switch(getType()){
118                         case IJavaElement.TYPE:
119                                 return rewrite.createStringPlaceholder(getSource(), ASTNode.TYPE_DECLARATION);
120                         case IJavaElement.PACKAGE_DECLARATION:
121                                 return rewrite.createStringPlaceholder(getSource(), ASTNode.PACKAGE_DECLARATION);
122                         case IJavaElement.IMPORT_DECLARATION:
123                                 return rewrite.createStringPlaceholder(getSource(), ASTNode.IMPORT_DECLARATION);
124                         default: Assert.isTrue(false, String.valueOf(getType()));
125                                 return null;
126                 }
127         }
128
129         public ASTNode generated_1676103505968220226(ASTRewrite rewrite) {
130                 switch(getType()){
131                         case IJavaElement.TYPE:
132                                 return rewrite.createStringPlaceholder(getSource(), ASTNode.TYPE_DECLARATION);
133                         case IJavaElement.METHOD:
134                                 return rewrite.createStringPlaceholder(getSource(), ASTNode.METHOD_DECLARATION);
135                         case IJavaElement.FIELD:
136                                 return rewrite.createStringPlaceholder(getSource(), ASTNode.FIELD_DECLARATION);
137                         case IJavaElement.INITIALIZER:
138                                 return rewrite.createStringPlaceholder(getSource(), ASTNode.INITIALIZER);
139                         default: Assert.isTrue(false);
140                                 return null;
141                 }
142         }
143
144         public boolean generated_7516993050506477390(TypedSource typedsource) {
145                 return getSource().equals(typedsource.getSource()) && getType() == typedsource.getType();
146         }
147
148         private static boolean canCreateForType(int type){
149                 return          type == IJavaElement.FIELD
150                                 ||      type == IJavaElement.TYPE
151                                 ||      type == IJavaElement.IMPORT_CONTAINER
152                                 ||      type == IJavaElement.IMPORT_DECLARATION
153                                 ||      type == IJavaElement.INITIALIZER
154                                 ||      type == IJavaElement.METHOD
155                                 ||      type == IJavaElement.PACKAGE_DECLARATION;
156         }
157
158
159         public static void sortByType(TypedSource[] typedSources){
160                 Arrays.sort(typedSources, createTypeComparator());
161         }
162
163         public static Comparator<TypedSource> createTypeComparator() {
164                 return new Comparator<TypedSource>(){
165                         public int compare(TypedSource arg0, TypedSource arg1) {
166                                 return arg0.getType() - arg1.getType();
167                         }
168                 };
169         }
170         public static TypedSource[] createTypedSources(IJavaElement[] javaElements) throws CoreException {
171                 //Map<ICompilationUnit, List<IJavaElement>>
172                 Map<ICompilationUnit, List<IJavaElement>> grouped= ReorgUtils.groupByCompilationUnit(Arrays.asList(javaElements));
173                 List<TypedSource> result= new ArrayList<TypedSource>(javaElements.length);
174                 for (Iterator<ICompilationUnit> iter= grouped.keySet().iterator(); iter.hasNext();) {
175                         ICompilationUnit cu= iter.next();
176                         for (Iterator<IJavaElement> iterator= grouped.get(cu).iterator(); iterator.hasNext();) {
177                                 SourceTuple tuple= new SourceTuple(cu);
178                                 TypedSource[] ts= createTypedSources(iterator.next(), tuple);
179                                 if (ts != null)
180                                         result.addAll(Arrays.asList(ts));
181                         }
182                 }
183                 return result.toArray(new TypedSource[result.size()]);
184         }
185
186         private static TypedSource[] createTypedSources(IJavaElement elem, SourceTuple tuple) throws CoreException {
187                 if (! ReorgUtils.isInsideCompilationUnit(elem))
188                         return null;
189                 if (elem.getElementType() == IJavaElement.IMPORT_CONTAINER)
190                         return createTypedSourcesForImportContainer(tuple, (IImportContainer)elem);
191                 else if (elem.getElementType() == IJavaElement.FIELD)
192                         return new TypedSource[] {create(getFieldSource((IField)elem, tuple), elem.getElementType())};
193                 return new TypedSource[] {create(getSourceOfDeclararationNode(elem, tuple.unit), elem.getElementType())};
194         }
195
196         private static TypedSource[] createTypedSourcesForImportContainer(SourceTuple tuple, IImportContainer container) throws JavaModelException, CoreException {
197                 IJavaElement[] imports= container.getChildren();
198                 List<TypedSource> result= new ArrayList<TypedSource>(imports.length);
199                 for (int i= 0; i < imports.length; i++) {
200                         result.addAll(Arrays.asList(createTypedSources(imports[i], tuple)));
201                 }
202                 return result.toArray(new TypedSource[result.size()]);
203         }
204
205         private static String getFieldSource(IField field, SourceTuple tuple) throws CoreException {
206                 if (Flags.isEnum(field.getFlags())) {
207                         String source= field.getSource();
208                         if (source != null)
209                                 return source;
210                 } else {
211                         if (tuple.node == null) {
212                                 ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
213                                 parser.setSource(tuple.unit);
214                                 tuple.node= (CompilationUnit) parser.createAST(null);
215                         }
216                         FieldDeclaration declaration= ASTNodeSearchUtil.getFieldDeclarationNode(field, tuple.node);
217                         if (declaration.fragments().size() == 1)
218                                 return getSourceOfDeclararationNode(field, tuple.unit);
219                         VariableDeclarationFragment declarationFragment= ASTNodeSearchUtil.getFieldDeclarationFragmentNode(field, tuple.node);
220                         IBuffer buffer= tuple.unit.getBuffer();
221                         StringBuffer buff= new StringBuffer();
222                         buff.append(buffer.getText(declaration.getStartPosition(), ((ASTNode) declaration.fragments().get(0)).getStartPosition() - declaration.getStartPosition()));
223                         buff.append(buffer.getText(declarationFragment.getStartPosition(), declarationFragment.getLength()));
224                         buff.append(";"); //$NON-NLS-1$
225                         return buff.toString();
226                 }
227                 return ""; //$NON-NLS-1$
228         }
229
230         private static String getSourceOfDeclararationNode(IJavaElement elem, ICompilationUnit cu) throws JavaModelException, CoreException {
231                 Assert.isTrue(elem.getElementType() != IJavaElement.IMPORT_CONTAINER);
232                 if (elem instanceof ISourceReference) {
233                         ISourceReference reference= (ISourceReference) elem;
234                         String source= reference.getSource();
235                         if (source != null)
236                                 return Strings.trimIndentation(source, cu.getJavaProject(), false);
237                 }
238                 return ""; //$NON-NLS-1$
239         }
240 }