1 11 package org.eclipse.jdt.internal.corext.util; 12 13 import java.util.Arrays ; 14 import java.util.Comparator ; 15 16 import org.eclipse.core.resources.ResourcesPlugin; 17 18 import org.eclipse.jdt.core.IJavaModel; 19 import org.eclipse.jdt.core.IJavaProject; 20 import org.eclipse.jdt.core.JavaCore; 21 import org.eclipse.jdt.core.JavaModelException; 22 import org.eclipse.jdt.core.search.IJavaSearchScope; 23 24 25 public class TypeInfoFactory { 26 27 private String [] fProjects; 28 private TypeInfo fLast; 29 private char[] fBuffer; 30 31 private static final String CLASS= "class"; private static final String JAVA= "java"; 34 public TypeInfoFactory() { 35 super(); 36 fProjects= getProjectList(); 37 fLast= null; 38 fBuffer= new char[512]; 39 } 40 41 public TypeInfo create(char[] packageName, char[] typeName, char[][] enclosingName, int modifiers, String path) { 42 String pn= getPackageName(packageName); 43 String tn= new String (typeName); 44 TypeInfo result= null; 45 int index= path.indexOf(IJavaSearchScope.JAR_FILE_ENTRY_SEPARATOR); 46 if (index != -1) { 47 result= createJarFileEntryTypeInfo(pn, tn, enclosingName, modifiers, path, getJarFileEntryTypeInfo(fLast), index); 48 } else { 49 String project= getProject(path); 50 if (project != null) { 51 result= createIFileTypeInfo(pn, tn, enclosingName, modifiers, path, getIFileTypeInfo(fLast), project); 52 } 53 } 54 if (result == null) { 55 result= new UnresolvableTypeInfo(pn, tn, enclosingName, modifiers, path); 56 } else { 57 fLast= result; 58 } 59 return result; 60 } 61 62 private static JarFileEntryTypeInfo getJarFileEntryTypeInfo(TypeInfo info) { 63 if (info == null || info.getElementType() != TypeInfo.JAR_FILE_ENTRY_TYPE_INFO) 64 return null; 65 return (JarFileEntryTypeInfo)info; 66 } 67 68 private static IFileTypeInfo getIFileTypeInfo(TypeInfo info) { 69 if (info == null || info.getElementType() != TypeInfo.IFILE_TYPE_INFO) 70 return null; 71 return (IFileTypeInfo)info; 72 } 73 74 private TypeInfo createJarFileEntryTypeInfo(String packageName, String typeName, char[][] enclosingName, int modifiers, String path, JarFileEntryTypeInfo last, int index) { 75 String jar= path.substring(0, index); 76 String rest= path.substring(index + 1); 77 index= rest.lastIndexOf(TypeInfo.SEPARATOR); 78 if (index != -1) { 79 rest= rest.substring(index + 1); 80 } 81 String file= null; 82 String extension= null; 83 index= rest.lastIndexOf(TypeInfo.EXTENSION_SEPARATOR); 84 if (index != -1) { 85 file= rest.substring(0, index); 86 extension= rest.substring(index + 1); 87 } else { 88 return null; 89 } 90 if (last != null) { 91 if (last.getJar().equals(jar)) { 92 jar= last.getJar(); 93 } else { 94 jar= createString(jar); 96 } 97 } else { 98 jar= createString(jar); 99 } 100 if (typeName.equals(file)) { 101 file= typeName; 102 } else { 103 file= createString(file); 104 } 105 if (CLASS.equals(extension)) 106 extension= CLASS; 107 else if (JAVA.equals(extension)) 108 extension= JAVA; 109 else 110 extension= createString(extension); 111 return new JarFileEntryTypeInfo(packageName, typeName, enclosingName, modifiers, jar, file, extension); 112 } 113 114 private TypeInfo createIFileTypeInfo(String packageName, String typeName, char[][] enclosingName, int modifiers, String path, IFileTypeInfo last, String project) { 115 String rest= path.substring(project.length() + 1); int index= rest.lastIndexOf(TypeInfo.SEPARATOR); 117 if (index == -1) 118 return null; 119 String middle= rest.substring(0, index); 120 rest= rest.substring(index + 1); 121 index= rest.lastIndexOf(TypeInfo.EXTENSION_SEPARATOR); 122 String file= null; 123 String extension= null; 124 if (index != -1) { 125 file= rest.substring(0, index); 126 extension= rest.substring(index + 1); 127 } else { 128 return null; 129 } 130 String SRC= null; 131 int ml= middle.length(); 132 int pl= packageName.length(); 133 if (ml > 0 && ml - 1 > pl) { 135 SRC= middle.substring(1, ml - pl - (pl > 0 ? 1 : 0)); 137 } 138 if (last != null) { 139 if (src != null && src.equals(last.getFolder())) 140 src= last.getFolder(); 141 } 142 if (typeName.equals(file)) { 143 file= typeName; 144 } else { 145 file= createString(file); 146 } 147 if (CLASS.equals(extension)) 148 extension= CLASS; 149 else if (JAVA.equals(extension)) 150 extension= JAVA; 151 else 152 extension= createString(extension); 153 154 return new IFileTypeInfo(packageName, typeName, enclosingName, modifiers, project, src, file, extension); 155 } 156 157 private String getPackageName(char[] packageName) { 158 if (fLast == null) 159 return new String (packageName); 160 String lastPackageName= fLast.getPackageName(); 161 if (Strings.equals(lastPackageName, packageName)) 162 return lastPackageName; 163 return new String (packageName); 164 } 165 166 private String getProject(String path) { 167 for (int i= 0; i < fProjects.length; i++) { 168 String project= fProjects[i]; 169 if (path.startsWith(project, 1)) 170 return project; 171 } 172 return null; 173 } 174 175 private String createString(String s) { 176 if (s == null) 177 return null; 178 int length= s.length(); 179 if (length > fBuffer.length) 180 fBuffer= new char[length]; 181 s.getChars(0, length, fBuffer, 0); 182 return new String (fBuffer, 0, length); 183 } 184 185 private static String [] getProjectList() { 186 IJavaModel model= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); 187 String [] result; 188 try { 189 IJavaProject[] projects= model.getJavaProjects(); 190 result= new String [projects.length]; 191 for (int i= 0; i < projects.length; i++) { 192 result[i]= projects[i].getElementName(); 193 } 194 } catch (JavaModelException e) { 195 result= new String [0]; 196 } 197 Arrays.sort(result, new Comparator () { 201 public int compare(Object o1, Object o2) { 202 int l1= ((String )o1).length(); 203 int l2= ((String )o2).length(); 204 if (l1 < l2) 205 return 1; 206 if (l2 < l1) 207 return -1; 208 return 0; 209 } 210 public boolean equals(Object obj) { 211 return super.equals(obj); 212 } 213 }); 214 return result; 215 } 216 } 217 | Popular Tags |