alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.lib.java.parser;

import org.openide.ErrorManager;
import org.openide.modules.InstalledFileLocator;
import java.lang.reflect.*;
import java.io.Reader;
import java.io.File;
import java.net.URLClassLoader;
import java.net.URL;
import java.security.*;

/**
 * Generates Parser and Scanner instances.
 */
public final class Factory {
    private static Factory instance = null;
    private static Constructor newParser;
    private static Method newScanner;
    private static Constructor newErrorChecker;
    
    public static synchronized Factory getDefault() {
        if (instance == null) {
            instance = new Factory();

            Class[] newParserTypes = new Class[] {
                ASTContext.class, Reader.class, String.class
            };

            Class[] newScannerTypes = new Class[] {
                Reader.class, String.class, Boolean.TYPE
            };

            Class[] newCheckerTypes = new Class[] {
                ECRequestDesc.class
            };

            File gjastJar = InstalledFileLocator.getDefault().locate("modules/ext/gjast.jar", "org.netbeans.modules.javacore", false);

            if (gjastJar != null) {
                //ErrorManager.getDefault().log(ErrorManager.USER, "javac bridge found in: " + gjastJar.getAbsolutePath());
                try {
                    ClassLoader loader = new GJASTClassLoader(gjastJar.toURI().toURL());
                    Class c = Class.forName("org.netbeans.lib.gjast.ASParser", true, loader);
                    newParser = c.getConstructor(newParserTypes);
                    c = Class.forName("org.netbeans.lib.gjast.ASScanner$Factory", true, loader);
                    newScanner = c.getMethod("newScanner", newScannerTypes);
                    c = Class.forName("org.netbeans.lib.gjast.ASErrorChecker", true, loader);
                    newErrorChecker = c.getConstructor(newCheckerTypes);
                    //ErrorManager.getDefault().log(ErrorManager.USER, "loaded javac bridge");
                } catch (Exception e) {
                    throw new RuntimeException("Cannot load javac bridge classes: " + e);
                }
            } else {
                ErrorManager.getDefault().log(ErrorManager.USER, "javac bridge not present");
                try {
                    newParser = Parser.class.getConstructor(newParserTypes);
                    newScanner = Scanner.class.getMethod("newScanner", newScannerTypes);
                    newErrorChecker = Factory.DummyErrorChecker.class.getConstructor(newCheckerTypes);
                } catch (Exception e) {
                    throw new RuntimeException("Cannot load parser classes: " + e);
                }
            }
        }
        return instance;
    }

    public JParser getParser(ASTContext context, Reader in, String filename) {
	try {
	    return (JParser)newParser.newInstance(new Object[] { 
		context, in, filename 
	    });
	} catch (Exception e) {
	    throw new RuntimeException("Cannot create parser: " + e);
	}
    }

    public JScanner getScanner(Reader in,String sourceLevel) {
	return getScanner(in, sourceLevel, false);
    }

    public JScanner getScanner(Reader in,String sourceLevel, 
			       boolean liteScanning) {
	try {
	    return (JScanner)newScanner.invoke(null, new Object[] { 
		in, sourceLevel, new Boolean(liteScanning)
	    });
	} catch (Exception e) {
	    Throwable t = e.getCause();
	    System.err.println("Factory: cannot create scanner " + 
			       (t != null ? t : e));
	    throw new RuntimeException("Cannot create scanner: " +
				       t != null ? t : e);
	}
    }

    public ErrorChecker getErrorChecker(ECRequestDesc desc) {
        try {
            return (ErrorChecker) newErrorChecker.newInstance(new Object[] { desc });
	} catch (Exception e) {
	    Throwable t = e.getCause();
	    throw new RuntimeException("Cannot create errorChecker: " +
                                       t != null ? t : e);
	}
    }

    private static class GJASTClassLoader extends URLClassLoader {
        private final PermissionCollection permissions = new Permissions();

        public GJASTClassLoader(URL gjastJar) {
            super(new URL[] {gjastJar}, Factory.class.getClassLoader());
            permissions.add(new AllPermission());
        }

        protected Class loadClass(String n, boolean r) throws ClassNotFoundException {
            if (n.startsWith("com.sun.tools.javac") || n.startsWith("org.netbeans.lib.gjast")) { // NOI18N
                // Do not proxy to parent!
                Class c = findLoadedClass(n);
                if (c != null) return c;
                c = findClass(n);
                if (r) resolveClass(c);
                return c;
            } else {
                return super.loadClass(n, r);
            }
        }

        protected PermissionCollection getPermissions(CodeSource codesource) {
            return permissions;
        }
    }
    
    private static class DummyErrorChecker implements ErrorChecker {
        
        public DummyErrorChecker(ECRequestDesc desc) {
        }

        public int parse() {
            return 0;
        }
        
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.