1 19 package org.netbeans.modules.java.source.usages; 20 21 import javax.lang.model.element.ElementKind; 22 import javax.lang.model.element.TypeElement; 23 import org.netbeans.api.java.source.ElementHandle; 24 import org.netbeans.modules.java.source.ElementHandleAccessor; 25 import org.netbeans.modules.java.source.parsing.FileObjects; 26 import org.openide.filesystems.FileObject; 27 28 32 public abstract class ResultConvertor<T> { 33 34 public abstract T convert (ElementKind kind, String value); 35 36 37 public static ResultConvertor<FileObject> fileObjectConvertor (final FileObject... roots) { 38 assert roots != null; 39 return new FileObjectConvertor (roots); 40 } 41 42 public static ResultConvertor<ElementHandle<TypeElement>> elementHandleConvertor () { 43 return new ElementHandleConvertor (); 44 } 45 46 public static ResultConvertor<String > identityConvertor () { 47 return new IdentityConvertor (); 48 } 49 50 51 private static class FileObjectConvertor extends ResultConvertor<FileObject> { 52 53 private FileObject[] roots; 54 55 private FileObjectConvertor (final FileObject... roots) { 56 this.roots = roots; 57 } 58 59 public FileObject convert (ElementKind kind, String value) { 60 for (int i=0; i<roots.length; i++) { 61 FileObject result = resolveFile (roots[i], value); 62 if (result != null) { 63 return result; 64 } 65 } 66 return null; 67 } 68 69 private static FileObject resolveFile (final FileObject root, String classBinaryName) { 70 assert classBinaryName != null; 71 classBinaryName = classBinaryName.replace('.', '/'); int index = classBinaryName.lastIndexOf('/'); FileObject folder; 74 String name; 75 if (index<0) { 76 folder = root; 77 name = classBinaryName; 78 } 79 else { 80 assert index>0 : classBinaryName; 81 assert index<classBinaryName.length() - 1 : classBinaryName; 82 folder = root.getFileObject(classBinaryName.substring(0,index)); 83 name = classBinaryName.substring(index+1); 84 } 85 if (folder == null) { 86 return null; 87 } 88 index = name.indexOf('$'); if (index>0) { 90 name = name.substring(0, index); 91 } 92 for (FileObject child : folder.getChildren()) { 93 if (FileObjects.JAVA.equalsIgnoreCase(child.getExt()) && name.equals(child.getName())) { 94 return child; 95 } 96 } 97 return null; 98 } 99 } 100 101 private static class ElementHandleConvertor extends ResultConvertor<ElementHandle<TypeElement>> { 102 103 public ElementHandle<TypeElement> convert (final ElementKind kind, final String value) { 104 return createTypeHandle(kind, value); 105 } 106 107 @SuppressWarnings ("unchecked") private static ElementHandle<TypeElement> createTypeHandle (final ElementKind kind, final String binaryName) { 109 assert binaryName != null; 110 return ElementHandleAccessor.INSTANCE.create(kind, binaryName); 111 } 112 113 } 114 115 private static class IdentityConvertor extends ResultConvertor<String > { 116 117 public String convert (ElementKind kind, String value) { 118 return value; 119 } 120 } 121 } 122 | Popular Tags |