All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codehaus.commons.compiler.AbstractJavaSourceClassLoader Maven / Gradle / Ivy

There is a newer version: 3.1.12
Show newest version

/*
 * Janino - An embedded Java[TM] compiler
 *
 * Copyright (c) 2001-2010, Arno Unkrig
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
 * following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
 *       following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
 *       following disclaimer in the documentation and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products derived from this software without
 *       specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package org.codehaus.commons.compiler;

import java.io.File;
import java.lang.reflect.Method;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.codehaus.commons.nullanalysis.Nullable;

/**
 * A {@link ClassLoader} that, unlike usual {@link ClassLoader}s, does not load byte code, but reads Java source
 * code and then scans, parses, compiles and loads it into the virtual machine.
 * 

* As with any {@link ClassLoader}, it is not possible to "update" classes after they've been loaded. The way to * achieve this is to give up on the {@link AbstractJavaSourceClassLoader} and create a new one. *

*/ public abstract class AbstractJavaSourceClassLoader extends ClassLoader { /** * @see ClassLoader#defineClass(String, byte[], int, int, ProtectionDomain) */ @Nullable protected ProtectionDomainFactory optionalProtectionDomainFactory; public AbstractJavaSourceClassLoader() {} public AbstractJavaSourceClassLoader(ClassLoader parentClassLoader) { super(parentClassLoader); } /** * @param sourcePath The sequence of directories to search for Java source files */ public abstract void setSourcePath(File[] sourcePath); /** * @param optionalCharacterEncoding if {@code null}, use platform default encoding */ public abstract void setSourceFileCharacterEncoding(@Nullable String optionalCharacterEncoding); /** * @param lines Whether line number debugging information should be generated * @param vars Whether variables debugging information should be generated * @param source Whether source file debugging information should be generated */ public abstract void setDebuggingInfo(boolean lines, boolean vars, boolean source); /** * @see ClassLoader#defineClass(String, byte[], int, int, ProtectionDomain) */ public final void setProtectionDomainFactory(@Nullable ProtectionDomainFactory optionalProtectionDomainFactory) { this.optionalProtectionDomainFactory = optionalProtectionDomainFactory; } /** * @see AbstractJavaSourceClassLoader#setProtectionDomainFactory */ public interface ProtectionDomainFactory { /** * @param sourceResourceName E.g. "pkg1/pkg2/Outer.java" */ ProtectionDomain getProtectionDomain(String sourceResourceName); } /** * Reads Java source code for a given class name, scan, parse, compile and load it into the virtual machine, * and invoke its "main()" method with the given arguments. *

* Usage is as follows: *

*
     *   java {@link AbstractJavaSourceClassLoader} [ option ] ... class-name [ argument ] ...
     *
     *   option:
     *     -sourcepath colon-separated-list-of-source-directories
     *     -encoding character-encoding
     *     -g                           Generate all debugging info
     *     -g:none                      Generate no debugging info
     *     -g:{source,lines,vars}       Generate only some debugging info
     * 
*/ public static void main(String[] args) throws Exception { File[] optionalSourcePath = null; String optionalCharacterEncoding = null; boolean debuggingInfoLines = false; boolean debuggingInfoVars = false; boolean debuggingInfoSource = false; boolean haveDebuggingInfo = false; // Scan command line options. int i; for (i = 0; i < args.length; ++i) { String arg = args[i]; if (!arg.startsWith("-")) break; if ("-sourcepath".equals(arg)) { optionalSourcePath = AbstractJavaSourceClassLoader.splitPath(args[++i]); } else if ("-encoding".equals(arg)) { optionalCharacterEncoding = args[++i]; } else if ("-g".equals(arg)) { debuggingInfoLines = true; debuggingInfoVars = true; debuggingInfoSource = true; haveDebuggingInfo = true; } else if ("-g:none".equals(arg)) { debuggingInfoLines = false; debuggingInfoVars = false; debuggingInfoSource = false; haveDebuggingInfo = true; } else if ("-g:".startsWith(arg)) { debuggingInfoLines = arg.indexOf("lines") != -1; debuggingInfoVars = arg.indexOf("vars") != -1; debuggingInfoSource = arg.indexOf("source") != -1; haveDebuggingInfo = true; } else if ("-help".equals(arg)) { System.out.println("Usage:"); System.out.println( " java " + AbstractJavaSourceClassLoader.class.getName() + " {




© 2015 - 2024 Weber Informatics LLC | Privacy Policy