1 4 package org.terracotta.dso; 5 6 import org.eclipse.core.runtime.IProgressMonitor; 7 import org.eclipse.jdt.core.IClassFile; 8 import org.eclipse.jdt.core.ICodeAssist; 9 import org.eclipse.jdt.core.ICompilationUnit; 10 import org.eclipse.jdt.core.IJavaElement; 11 import org.eclipse.jdt.core.IJavaModelStatusConstants; 12 import org.eclipse.jdt.core.IJavaProject; 13 import org.eclipse.jdt.core.IPackageFragment; 14 import org.eclipse.jdt.core.IPackageFragmentRoot; 15 import org.eclipse.jdt.core.IType; 16 import org.eclipse.jdt.core.JavaModelException; 17 import org.eclipse.jdt.core.Signature; 18 import org.eclipse.jdt.ui.JavaUI; 19 import org.eclipse.jface.text.Assert; 20 import org.eclipse.jface.text.ITextSelection; 21 import org.eclipse.ui.IEditorInput; 22 import org.eclipse.ui.IEditorPart; 23 import org.eclipse.ui.texteditor.ITextEditor; 24 25 public class JdtUtils { 26 private static final IJavaElement[] EMPTY_RESULT= new IJavaElement[0]; 27 28 34 public static IType findType(IJavaProject jproject, String fullyQualifiedName) throws JavaModelException { 35 fullyQualifiedName = fullyQualifiedName.replace('$', '.'); 37 IType type= jproject.findType(fullyQualifiedName, (IProgressMonitor)null); 38 if (type != null) 39 return type; 40 IPackageFragmentRoot[] roots= jproject.getPackageFragmentRoots(); 41 for (int i= 0; i < roots.length; i++) { 42 IPackageFragmentRoot root= roots[i]; 43 type= findType(root, fullyQualifiedName); 44 if (type != null && type.exists()) 45 return type; 46 } 47 return null; 48 } 49 50 private static IType findType(IPackageFragmentRoot root, String fullyQualifiedName) throws JavaModelException{ 51 IJavaElement[] children= root.getChildren(); 52 for (int i= 0; i < children.length; i++) { 53 IJavaElement element= children[i]; 54 if (element.getElementType() == IJavaElement.PACKAGE_FRAGMENT){ 55 IPackageFragment pack= (IPackageFragment)element; 56 if (! fullyQualifiedName.startsWith(pack.getElementName())) 57 continue; 58 IType type= findType(pack, fullyQualifiedName); 59 if (type != null && type.exists()) 60 return type; 61 } 62 } 63 return null; 64 } 65 66 private static IType findType(IPackageFragment pack, String fullyQualifiedName) throws JavaModelException{ 67 ICompilationUnit[] cus= pack.getCompilationUnits(); 68 for (int i= 0; i < cus.length; i++) { 69 ICompilationUnit unit= cus[i]; 70 IType type= findType(unit, fullyQualifiedName); 71 if (type != null && type.exists()) 72 return type; 73 } 74 return null; 75 } 76 77 private static IType findType(ICompilationUnit cu, String fullyQualifiedName) throws JavaModelException{ 78 IType[] types= cu.getAllTypes(); 79 for (int i= 0; i < types.length; i++) { 80 IType type= types[i]; 81 if (getFullyQualifiedName(type).equals(fullyQualifiedName)) 82 return type; 83 } 84 return null; 85 } 86 87 93 public static String getFullyQualifiedName(IType type) { 94 try { 95 if (type.isBinary() && !type.isAnonymous()) { 96 IType declaringType= type.getDeclaringType(); 97 if (declaringType != null) { 98 return getFullyQualifiedName(declaringType) + '.' + type.getElementName(); 99 } 100 } 101 } catch (JavaModelException e) { 102 } 104 return type.getFullyQualifiedName('.'); 105 } 106 107 111 public static void reconcile(ICompilationUnit unit) throws JavaModelException { 112 unit.reconcile( 113 ICompilationUnit.NO_AST, 114 false , 115 null , 116 null ); 117 } 118 119 123 public static IPackageFragmentRoot getPackageFragmentRoot(IJavaElement element) { 124 return (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 125 } 126 127 134 public static String getResolvedTypeName(String refTypeSig, IType declaringType) throws JavaModelException { 135 int arrayCount= Signature.getArrayCount(refTypeSig); 136 char type= refTypeSig.charAt(arrayCount); 137 if (type == Signature.C_UNRESOLVED) { 138 String name= ""; int bracket= refTypeSig.indexOf(Signature.C_GENERIC_START, arrayCount + 1); 140 if (bracket > 0) 141 name= refTypeSig.substring(arrayCount + 1, bracket); 142 else { 143 int semi= refTypeSig.indexOf(Signature.C_SEMICOLON, arrayCount + 1); 144 if (semi == -1) { 145 throw new IllegalArgumentException (); 146 } 147 name= refTypeSig.substring(arrayCount + 1, semi); 148 } 149 String [][] resolvedNames= declaringType.resolveType(name); 150 if (resolvedNames != null && resolvedNames.length > 0) { 151 return concatenateName(resolvedNames[0][0], resolvedNames[0][1]); 152 } 153 return null; 154 } else { 155 return Signature.toString(refTypeSig.substring(arrayCount)); 156 } 157 } 158 159 public static String getResolvedTypeFileName(String refTypeSig, IType declaringType) throws JavaModelException { 160 int arrayCount= Signature.getArrayCount(refTypeSig); 161 char type= refTypeSig.charAt(arrayCount); 162 if (type == Signature.C_UNRESOLVED) { 163 String name= ""; int bracket= refTypeSig.indexOf(Signature.C_GENERIC_START, arrayCount + 1); 165 if (bracket > 0) 166 name= refTypeSig.substring(arrayCount + 1, bracket); 167 else { 168 int semi= refTypeSig.indexOf(Signature.C_SEMICOLON, arrayCount + 1); 169 if (semi == -1) { 170 throw new IllegalArgumentException (); 171 } 172 name= refTypeSig.substring(arrayCount + 1, semi); 173 } 174 String [][] resolvedNames= declaringType.resolveType(name); 175 if (resolvedNames != null && resolvedNames.length > 0) { 176 return concatenateName(resolvedNames[0][0], resolvedNames[0][1].replace('.', '$')); 177 } 178 return "*"; 179 } else { 180 return Signature.toString(refTypeSig.substring(arrayCount)); 181 } 182 } 183 184 187 public static boolean isTypeNameUnresolved(String refTypeSig) { 188 return refTypeSig.charAt(Signature.getArrayCount(refTypeSig)) == Signature.C_UNRESOLVED; 189 } 190 191 194 public static String resolveTypeName(String refTypeSig, IType declaringType) throws JavaModelException { 195 int arrayCount= Signature.getArrayCount(refTypeSig); 196 char type= refTypeSig.charAt(arrayCount); 197 if (type == Signature.C_UNRESOLVED) { 198 String name= ""; int bracket= refTypeSig.indexOf(Signature.C_GENERIC_START, arrayCount + 1); 200 if (bracket > 0) 201 name= refTypeSig.substring(arrayCount + 1, bracket); 202 else { 203 int semi= refTypeSig.indexOf(Signature.C_SEMICOLON, arrayCount + 1); 204 if (semi == -1) { 205 throw new IllegalArgumentException (); 206 } 207 name= refTypeSig.substring(arrayCount + 1, semi); 208 } 209 String [][] resolvedNames= declaringType.resolveType(name); 210 if (resolvedNames != null && resolvedNames.length > 0) { 211 return JdtUtils.concatenateName(resolvedNames[0][0], resolvedNames[0][1]); 212 } 213 else { 214 throw new JavaModelException(new Exception (name + " not resolvable"), 215 IJavaModelStatusConstants.EVALUATION_ERROR); 216 } 217 } 218 219 return refTypeSig; 220 } 221 222 225 public static void resolveTypeName(String refTypeSig, IType declaringType, StringBuffer sb) 226 throws JavaModelException 227 { 228 if(isTypeNameUnresolved(refTypeSig)) { 229 sb.append(refTypeSig.substring(0, refTypeSig.indexOf(Signature.C_UNRESOLVED))); 230 sb.append('L'); 231 sb.append(resolveTypeName(refTypeSig, declaringType)); 232 sb.append(';'); 233 } 234 else { 235 sb.append(refTypeSig); 236 } 237 } 238 239 243 public static String concatenateName(String name1, String name2) { 244 StringBuffer buf= new StringBuffer (); 245 if (name1 != null && name1.length() > 0) { 246 buf.append(name1); 247 } 248 if (name2 != null && name2.length() > 0) { 249 if (buf.length() > 0) { 250 buf.append('.'); 251 } 252 buf.append(name2); 253 } 254 return buf.toString(); 255 } 256 257 public static IJavaElement[] codeResolve(ITextEditor editor) throws JavaModelException { 258 return codeResolve(editor, true); 259 } 260 261 265 public static IJavaElement[] codeResolve(ITextEditor editor, boolean primaryOnly) throws JavaModelException { 266 return codeResolve(getInput(editor, primaryOnly), (ITextSelection)editor.getSelectionProvider().getSelection()); 267 } 268 269 public static IJavaElement getInput(IEditorPart editor) { 270 return getInput(editor, true); 271 } 272 273 277 private static IJavaElement getInput(IEditorPart editor, boolean primaryOnly) { 278 if (editor == null) 279 return null; 280 return getEditorInputJavaElement(editor, primaryOnly); 281 } 282 283 public static ICompilationUnit getInputAsCompilationUnit(ITextEditor editor) { 284 Object editorInput= getInput(editor); 285 if (editorInput instanceof ICompilationUnit) 286 return (ICompilationUnit)editorInput; 287 return null; 288 } 289 290 298 public static IJavaElement getEditorInputJavaElement(IEditorPart editor, boolean primaryOnly) { 299 Assert.isNotNull(editor); 300 IEditorInput editorInput= editor.getEditorInput(); 301 if (editorInput == null) 302 return null; 303 304 IJavaElement je= getEditorInputJavaElement(editorInput); 305 if (je != null || primaryOnly) 306 return je; 307 308 return JavaUI.getWorkingCopyManager().getWorkingCopy(editorInput); 309 } 310 311 318 public static IJavaElement getEditorInputJavaElement(IEditorInput editorInput) { 319 IJavaElement je= JavaUI.getWorkingCopyManager().getWorkingCopy(editorInput); 321 if (je != null) 322 return je; 323 324 return (IJavaElement)editorInput.getAdapter(IJavaElement.class); 325 } 326 327 public static IJavaElement[] codeResolve(IJavaElement input, ITextSelection selection) throws JavaModelException { 328 if (input instanceof ICodeAssist) { 329 if (input instanceof ICompilationUnit) { 330 reconcile((ICompilationUnit) input); 331 } 332 IJavaElement[] elements= ((ICodeAssist)input).codeSelect(selection.getOffset(), selection.getLength()); 333 if (elements != null && elements.length > 0) 334 return elements; 335 } 336 return EMPTY_RESULT; 337 } 338 339 public static IJavaElement getElementAtOffset(ITextEditor editor) throws JavaModelException { 340 return getElementAtOffset(editor, true); 341 } 342 343 347 private static IJavaElement getElementAtOffset(ITextEditor editor, boolean primaryOnly) throws JavaModelException { 348 return getElementAtOffset(getInput(editor, primaryOnly), (ITextSelection)editor.getSelectionProvider().getSelection()); 349 } 350 351 public static IJavaElement getElementAtOffset(IJavaElement input, ITextSelection selection) throws JavaModelException { 352 if (input instanceof ICompilationUnit) { 353 ICompilationUnit cunit= (ICompilationUnit) input; 354 reconcile(cunit); 355 IJavaElement ref= cunit.getElementAt(selection.getOffset()); 356 if (ref == null) 357 return input; 358 else 359 return ref; 360 } else if (input instanceof IClassFile) { 361 IJavaElement ref= ((IClassFile)input).getElementAt(selection.getOffset()); 362 if (ref == null) 363 return input; 364 else 365 return ref; 366 } 367 return null; 368 } 369 } 370 | Popular Tags |