1 19 20 package org.netbeans.modules.j2ee.common.queries.api; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import javax.lang.model.element.TypeElement; 26 import org.netbeans.api.java.source.CompilationController; 27 import org.netbeans.api.java.source.JavaSource; 28 import org.netbeans.modules.j2ee.common.queries.spi.InjectionTargetQueryImplementation; 29 import org.netbeans.modules.j2ee.common.source.AbstractTask; 30 import org.openide.filesystems.FileObject; 31 import org.openide.util.Lookup; 32 import org.openide.util.LookupEvent; 33 import org.openide.util.LookupListener; 34 35 39 public class InjectionTargetQuery { 40 41 private static Lookup.Result<InjectionTargetQueryImplementation> implementations; 42 43 private static List <InjectionTargetQueryImplementation> cache; 44 45 private InjectionTargetQuery() { 46 } 47 48 55 public static boolean isInjectionTarget(CompilationController controller, TypeElement typeElement) { 56 if (typeElement == null || controller==null) { 57 throw new NullPointerException ("Passed null to InjectionTargetQuery.isInjectionTarget(CompilationController, TypeElement)"); } 59 for (InjectionTargetQueryImplementation elem : getInstances()) { 60 if (elem.isInjectionTarget(controller, typeElement)) { 61 return true; 62 } 63 } 64 return false; 65 } 66 67 public static boolean isInjectionTarget(FileObject fileObject, final String className) throws IOException { 68 JavaSource javaSource = JavaSource.forFileObject(fileObject); 69 final boolean[] result = new boolean[1]; 70 javaSource.runUserActionTask(new AbstractTask<CompilationController>() { 71 public void run(CompilationController controller) throws Exception { 72 TypeElement typeElement = controller.getElements().getTypeElement(className); 73 result[0] = isInjectionTarget(controller, typeElement); 74 } 75 }, true); 76 return result[0]; 77 } 78 79 89 public static boolean isStaticReferenceRequired(CompilationController controller, TypeElement typeElement) { 90 if (typeElement == null || controller==null) { 91 throw new NullPointerException ("Passed null to InjectionTargetQuery.isStaticReferenceRequired(CompilationController, TypeElement)"); } 93 for (InjectionTargetQueryImplementation elem : getInstances()) { 94 if (elem.isStaticReferenceRequired(controller, typeElement)) { 95 return true; 96 } 97 } 98 return false; 99 } 100 101 public static boolean isStaticReferenceRequired(FileObject fileObject, final String className) throws IOException { 102 JavaSource javaSource = JavaSource.forFileObject(fileObject); 103 final boolean[] result = new boolean[1]; 104 javaSource.runUserActionTask(new AbstractTask<CompilationController>() { 105 public void run(CompilationController controller) throws Exception { 106 TypeElement typeElement = controller.getElements().getTypeElement(className); 107 result[0] = isStaticReferenceRequired(controller, typeElement); 108 } 109 }, true); 110 return result[0]; 111 } 112 113 private static synchronized List <InjectionTargetQueryImplementation> getInstances() { 114 if (implementations == null) { 115 implementations = Lookup.getDefault().lookup(new Lookup.Template<InjectionTargetQueryImplementation>(InjectionTargetQueryImplementation.class)); 116 implementations.addLookupListener(new LookupListener() { 117 public void resultChanged (LookupEvent ev) { 118 synchronized (InjectionTargetQuery.class) { 119 cache = null; 120 } 121 }}); 122 } 123 if (cache == null) { 124 cache = new ArrayList <InjectionTargetQueryImplementation>(implementations.allInstances()); 125 } 126 return cache; 127 } 128 129 } 130 | Popular Tags |