1 19 20 package org.netbeans.lib.java.parser; 21 22 import org.openide.ErrorManager; 23 import org.openide.modules.InstalledFileLocator; 24 import java.lang.reflect.*; 25 import java.io.Reader ; 26 import java.io.File ; 27 import java.net.URLClassLoader ; 28 import java.net.URL ; 29 import java.security.*; 30 31 34 public final class Factory { 35 private static Factory instance = null; 36 private static Constructor newParser; 37 private static Method newScanner; 38 private static Constructor newErrorChecker; 39 40 public static synchronized Factory getDefault() { 41 if (instance == null) { 42 instance = new Factory(); 43 44 Class [] newParserTypes = new Class [] { 45 ASTContext.class, Reader .class, String .class 46 }; 47 48 Class [] newScannerTypes = new Class [] { 49 Reader .class, String .class, Boolean.TYPE 50 }; 51 52 Class [] newCheckerTypes = new Class [] { 53 ECRequestDesc.class 54 }; 55 56 File gjastJar = InstalledFileLocator.getDefault().locate("modules/ext/gjast.jar", "org.netbeans.modules.javacore", false); 57 58 if (gjastJar != null) { 59 try { 61 ClassLoader loader = new GJASTClassLoader(gjastJar.toURI().toURL()); 62 Class c = Class.forName("org.netbeans.lib.gjast.ASParser", true, loader); 63 newParser = c.getConstructor(newParserTypes); 64 c = Class.forName("org.netbeans.lib.gjast.ASScanner$Factory", true, loader); 65 newScanner = c.getMethod("newScanner", newScannerTypes); 66 c = Class.forName("org.netbeans.lib.gjast.ASErrorChecker", true, loader); 67 newErrorChecker = c.getConstructor(newCheckerTypes); 68 } catch (Exception e) { 70 throw new RuntimeException ("Cannot load javac bridge classes: " + e); 71 } 72 } else { 73 ErrorManager.getDefault().log(ErrorManager.USER, "javac bridge not present"); 74 try { 75 newParser = Parser.class.getConstructor(newParserTypes); 76 newScanner = Scanner.class.getMethod("newScanner", newScannerTypes); 77 newErrorChecker = Factory.DummyErrorChecker.class.getConstructor(newCheckerTypes); 78 } catch (Exception e) { 79 throw new RuntimeException ("Cannot load parser classes: " + e); 80 } 81 } 82 } 83 return instance; 84 } 85 86 public JParser getParser(ASTContext context, Reader in, String filename) { 87 try { 88 return (JParser)newParser.newInstance(new Object [] { 89 context, in, filename 90 }); 91 } catch (Exception e) { 92 throw (RuntimeException ) new RuntimeException ("Cannot create parser: " + e).initCause(e); 93 } 94 } 95 96 public JScanner getScanner(Reader in,String sourceLevel) { 97 return getScanner(in, sourceLevel, false); 98 } 99 100 public JScanner getScanner(Reader in,String sourceLevel, 101 boolean liteScanning) { 102 try { 103 return (JScanner)newScanner.invoke(null, new Object [] { 104 in, sourceLevel, Boolean.valueOf (liteScanning) 105 }); 106 } catch (Exception e) { 107 Throwable t = e.getCause(); 108 System.err.println("Factory: cannot create scanner " + 109 (t != null ? t : e)); 110 throw new RuntimeException ("Cannot create scanner: " + 111 t != null ? t : e); 112 } 113 } 114 115 public ErrorChecker getErrorChecker(ECRequestDesc desc) { 116 try { 117 return (ErrorChecker) newErrorChecker.newInstance(new Object [] { desc }); 118 } catch (Exception e) { 119 Throwable t = e.getCause(); 120 throw new RuntimeException ("Cannot create errorChecker: " + 121 t != null ? t : e); 122 } 123 } 124 125 private static class GJASTClassLoader extends URLClassLoader { 126 private final PermissionCollection permissions = new Permissions(); 127 128 public GJASTClassLoader(URL gjastJar) { 129 super(new URL [] {gjastJar}, Factory.class.getClassLoader()); 130 permissions.add(new AllPermission()); 131 } 132 133 protected Class loadClass(String n, boolean r) throws ClassNotFoundException { 134 if (n.startsWith("com.sun.tools.javac") || n.startsWith("org.netbeans.lib.gjast")) { Class c = findLoadedClass(n); 137 if (c != null) return c; 138 c = findClass(n); 139 if (r) resolveClass(c); 140 return c; 141 } else { 142 return super.loadClass(n, r); 143 } 144 } 145 146 protected PermissionCollection getPermissions(CodeSource codesource) { 147 return permissions; 148 } 149 } 150 151 private static class DummyErrorChecker implements ErrorChecker { 152 153 public DummyErrorChecker(ECRequestDesc desc) { 154 } 155 156 public int parse() throws CompilerException { 157 return 0; 158 } 159 160 } 161 } 162 | Popular Tags |