KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > TypedSource


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
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.AST;
32 import org.eclipse.jdt.core.dom.ASTNode;
33 import org.eclipse.jdt.core.dom.ASTParser;
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
38 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils;
39 import org.eclipse.jdt.internal.corext.refactoring.structure.ASTNodeSearchUtil;
40 import org.eclipse.jdt.internal.corext.util.Strings;
41
42 /**
43  * A tuple used to keep source of an element and its type.
44  * @see IJavaElement
45  * @see org.eclipse.jdt.core.ISourceReference
46  */

47 public class TypedSource {
48
49     private static class SourceTuple {
50
51         private SourceTuple(ICompilationUnit unit) {
52             this.unit= unit;
53         }
54         private ICompilationUnit unit;
55         private CompilationUnit node;
56     }
57
58     private final String JavaDoc fSource;
59     private final int fType;
60
61     private TypedSource(String JavaDoc source, int type){
62         Assert.isNotNull(source);
63         Assert.isTrue(canCreateForType(type));
64         fSource= source;
65         fType= type;
66     }
67     
68     public static TypedSource create(String JavaDoc source, int type) {
69         if (source == null || ! canCreateForType(type))
70             return null;
71         return new TypedSource(source, type);
72     }
73
74     public String JavaDoc getSource() {
75         return fSource;
76     }
77
78     public int getType() {
79         return fType;
80     }
81     
82     /* (non-Javadoc)
83      * @see java.lang.Object#equals(java.lang.Object)
84      */

85     public boolean equals(Object JavaDoc other) {
86         if (! (other instanceof TypedSource))
87             return false;
88         
89         TypedSource ts= (TypedSource)other;
90         return ts.getSource().equals(getSource()) && ts.getType() == getType();
91     }
92
93     /* (non-Javadoc)
94      * @see java.lang.Object#hashCode()
95      */

96     public int hashCode() {
97         return getSource().hashCode() ^ (97 * getType());
98     }
99
100     private static boolean canCreateForType(int type){
101         return type == IJavaElement.FIELD
102                 || type == IJavaElement.TYPE
103                 || type == IJavaElement.IMPORT_CONTAINER
104                 || type == IJavaElement.IMPORT_DECLARATION
105                 || type == IJavaElement.INITIALIZER
106                 || type == IJavaElement.METHOD
107                 || type == IJavaElement.PACKAGE_DECLARATION;
108     }
109     
110     
111     public static void sortByType(TypedSource[] typedSources){
112         Arrays.sort(typedSources, createTypeComparator());
113     }
114
115     public static Comparator JavaDoc createTypeComparator() {
116         return new Comparator JavaDoc(){
117             public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
118                 return ((TypedSource)arg0).getType() - ((TypedSource)arg1).getType();
119             }
120         };
121     }
122     public static TypedSource[] createTypedSources(IJavaElement[] javaElements) throws CoreException {
123         //Map<ICompilationUnit, List<IJavaElement>>
124
Map JavaDoc grouped= ReorgUtils.groupByCompilationUnit(Arrays.asList(javaElements));
125         List JavaDoc result= new ArrayList JavaDoc(javaElements.length);
126         for (Iterator JavaDoc iter= grouped.keySet().iterator(); iter.hasNext();) {
127             ICompilationUnit cu= (ICompilationUnit) iter.next();
128             for (Iterator JavaDoc iterator= ((List JavaDoc) grouped.get(cu)).iterator(); iterator.hasNext();) {
129                 SourceTuple tuple= new SourceTuple(cu);
130                 TypedSource[] ts= createTypedSources((IJavaElement) iterator.next(), tuple);
131                 if (ts != null)
132                     result.addAll(Arrays.asList(ts));
133             }
134         }
135         return (TypedSource[]) result.toArray(new TypedSource[result.size()]);
136     }
137
138     private static TypedSource[] createTypedSources(IJavaElement elem, SourceTuple tuple) throws CoreException {
139         if (! ReorgUtils.isInsideCompilationUnit(elem))
140             return null;
141         if (elem.getElementType() == IJavaElement.IMPORT_CONTAINER)
142             return createTypedSourcesForImportContainer(tuple, (IImportContainer)elem);
143         else if (elem.getElementType() == IJavaElement.FIELD)
144             return new TypedSource[] {create(getFieldSource((IField)elem, tuple), elem.getElementType())};
145         return new TypedSource[] {create(getSourceOfDeclararationNode(elem, tuple.unit), elem.getElementType())};
146     }
147
148     private static TypedSource[] createTypedSourcesForImportContainer(SourceTuple tuple, IImportContainer container) throws JavaModelException, CoreException {
149         IJavaElement[] imports= container.getChildren();
150         List JavaDoc result= new ArrayList JavaDoc(imports.length);
151         for (int i= 0; i < imports.length; i++) {
152             result.addAll(Arrays.asList(createTypedSources(imports[i], tuple)));
153         }
154         return (TypedSource[]) result.toArray(new TypedSource[result.size()]);
155     }
156
157     private static String JavaDoc getFieldSource(IField field, SourceTuple tuple) throws CoreException {
158         if (Flags.isEnum(field.getFlags())) {
159             String JavaDoc source= field.getSource();
160             if (source != null)
161                 return source;
162         } else {
163             if (tuple.node == null) {
164                 ASTParser parser= ASTParser.newParser(AST.JLS3);
165                 parser.setSource(tuple.unit);
166                 tuple.node= (CompilationUnit) parser.createAST(null);
167             }
168             FieldDeclaration declaration= ASTNodeSearchUtil.getFieldDeclarationNode(field, tuple.node);
169             if (declaration.fragments().size() == 1)
170                 return getSourceOfDeclararationNode(field, tuple.unit);
171             VariableDeclarationFragment declarationFragment= ASTNodeSearchUtil.getFieldDeclarationFragmentNode(field, tuple.node);
172             IBuffer buffer= tuple.unit.getBuffer();
173             StringBuffer JavaDoc buff= new StringBuffer JavaDoc();
174             buff.append(buffer.getText(declaration.getStartPosition(), ((ASTNode) declaration.fragments().get(0)).getStartPosition() - declaration.getStartPosition()));
175             buff.append(buffer.getText(declarationFragment.getStartPosition(), declarationFragment.getLength()));
176             buff.append(";"); //$NON-NLS-1$
177
return buff.toString();
178         }
179         return ""; //$NON-NLS-1$
180
}
181
182     private static String JavaDoc getSourceOfDeclararationNode(IJavaElement elem, ICompilationUnit cu) throws JavaModelException, CoreException {
183         Assert.isTrue(elem.getElementType() != IJavaElement.IMPORT_CONTAINER);
184         if (elem instanceof ISourceReference) {
185             ISourceReference reference= (ISourceReference) elem;
186             String JavaDoc source= reference.getSource();
187             if (source != null)
188                 return Strings.trimIndentation(source, cu.getJavaProject(), false);
189         }
190         return ""; //$NON-NLS-1$
191
}
192 }
193
Popular Tags