1 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.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 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 fSource; 59 private final int fType; 60 61 private TypedSource(String 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 source, int type) { 69 if (source == null || ! canCreateForType(type)) 70 return null; 71 return new TypedSource(source, type); 72 } 73 74 public String getSource() { 75 return fSource; 76 } 77 78 public int getType() { 79 return fType; 80 } 81 82 85 public boolean equals(Object 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 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 createTypeComparator() { 116 return new Comparator (){ 117 public int compare(Object arg0, Object arg1) { 118 return ((TypedSource)arg0).getType() - ((TypedSource)arg1).getType(); 119 } 120 }; 121 } 122 public static TypedSource[] createTypedSources(IJavaElement[] javaElements) throws CoreException { 123 Map grouped= ReorgUtils.groupByCompilationUnit(Arrays.asList(javaElements)); 125 List result= new ArrayList (javaElements.length); 126 for (Iterator iter= grouped.keySet().iterator(); iter.hasNext();) { 127 ICompilationUnit cu= (ICompilationUnit) iter.next(); 128 for (Iterator iterator= ((List ) 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 result= new ArrayList (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 getFieldSource(IField field, SourceTuple tuple) throws CoreException { 158 if (Flags.isEnum(field.getFlags())) { 159 String 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 buff= new StringBuffer (); 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(";"); return buff.toString(); 178 } 179 return ""; } 181 182 private static String 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 source= reference.getSource(); 187 if (source != null) 188 return Strings.trimIndentation(source, cu.getJavaProject(), false); 189 } 190 return ""; } 192 } 193 | Popular Tags |