KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > java > parser > Factory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
26 import java.io.File JavaDoc;
27 import java.net.URLClassLoader JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.security.*;
30
31 /**
32  * Generates Parser and Scanner instances.
33  */

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 JavaDoc[] newParserTypes = new Class JavaDoc[] {
45                 ASTContext.class, Reader JavaDoc.class, String JavaDoc.class
46             };
47
48             Class JavaDoc[] newScannerTypes = new Class JavaDoc[] {
49                 Reader JavaDoc.class, String JavaDoc.class, Boolean.TYPE
50             };
51
52             Class JavaDoc[] newCheckerTypes = new Class JavaDoc[] {
53                 ECRequestDesc.class
54             };
55
56             File JavaDoc gjastJar = InstalledFileLocator.getDefault().locate("modules/ext/gjast.jar", "org.netbeans.modules.javacore", false);
57
58             if (gjastJar != null) {
59                 //ErrorManager.getDefault().log(ErrorManager.USER, "javac bridge found in: " + gjastJar.getAbsolutePath());
60
try {
61                     ClassLoader JavaDoc loader = new GJASTClassLoader(gjastJar.toURI().toURL());
62                     Class JavaDoc 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                     //ErrorManager.getDefault().log(ErrorManager.USER, "loaded javac bridge");
69
} catch (Exception JavaDoc e) {
70                     throw new RuntimeException JavaDoc("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 JavaDoc e) {
79                     throw new RuntimeException JavaDoc("Cannot load parser classes: " + e);
80                 }
81             }
82         }
83         return instance;
84     }
85
86     public JParser getParser(ASTContext context, Reader JavaDoc in, String JavaDoc filename) {
87     try {
88         return (JParser)newParser.newInstance(new Object JavaDoc[] {
89         context, in, filename
90         });
91     } catch (Exception JavaDoc e) {
92         throw (RuntimeException JavaDoc) new RuntimeException JavaDoc("Cannot create parser: " + e).initCause(e);
93     }
94     }
95
96     public JScanner getScanner(Reader JavaDoc in,String JavaDoc sourceLevel) {
97     return getScanner(in, sourceLevel, false);
98     }
99
100     public JScanner getScanner(Reader JavaDoc in,String JavaDoc sourceLevel,
101                    boolean liteScanning) {
102     try {
103         return (JScanner)newScanner.invoke(null, new Object JavaDoc[] {
104         in, sourceLevel, Boolean.valueOf (liteScanning)
105         });
106     } catch (Exception JavaDoc e) {
107         Throwable JavaDoc t = e.getCause();
108         System.err.println("Factory: cannot create scanner " +
109                    (t != null ? t : e));
110         throw new RuntimeException JavaDoc("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 JavaDoc[] { desc });
118     } catch (Exception JavaDoc e) {
119         Throwable JavaDoc t = e.getCause();
120         throw new RuntimeException JavaDoc("Cannot create errorChecker: " +
121                                        t != null ? t : e);
122     }
123     }
124
125     private static class GJASTClassLoader extends URLClassLoader JavaDoc {
126         private final PermissionCollection permissions = new Permissions();
127
128         public GJASTClassLoader(URL JavaDoc gjastJar) {
129             super(new URL JavaDoc[] {gjastJar}, Factory.class.getClassLoader());
130             permissions.add(new AllPermission());
131         }
132
133         protected Class JavaDoc loadClass(String JavaDoc n, boolean r) throws ClassNotFoundException JavaDoc {
134             if (n.startsWith("com.sun.tools.javac") || n.startsWith("org.netbeans.lib.gjast")) { // NOI18N
135
// Do not proxy to parent!
136
Class JavaDoc 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