com.hazelcast.shaded.org.codehaus.commons.compiler.AbstractJavaSourceClassLoader Maven / Gradle / Ivy
/*
* 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. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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 com.hazelcast.shaded.org.codehaus.commons.compiler;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import com.hazelcast.shaded.org.codehaus.commons.compiler.util.resource.ResourceFinder;
import com.hazelcast.shaded.org.codehaus.commons.nullanalysis.NotNullByDefault;
import com.hazelcast.shaded.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 protectionDomainFactory;
public
AbstractJavaSourceClassLoader() {}
public
AbstractJavaSourceClassLoader(ClassLoader parentClassLoader) { super(parentClassLoader); }
// Override and delegate to parent.
@Override @NotNullByDefault(false) public InputStream
getResourceAsStream(String name) { return super.getParent().getResourceAsStream(name); }
/**
* @param sourcePath The sequence of directories to search for Java source files
*/
public abstract void setSourcePath(File[] sourcePath);
/**
* @param sourceFinder Is used when searching for Java source files
*/
public abstract void setSourceFinder(ResourceFinder sourceFinder);
/**
* @param charsetName if {@code null}, use platform default encoding
*/
public void
setSourceFileCharacterEncoding(@Nullable String charsetName) {
this.setSourceCharset(charsetName == null ? Charset.defaultCharset() : Charset.forName(charsetName));
}
/**
* @param charset The character set to using when reading characters from a source file
*/
public abstract void setSourceCharset(Charset charset);
/**
* @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 protectionDomainFactory) {
this.protectionDomainFactory = protectionDomainFactory;
}
/**
* @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[] sourcePath = null;
String characterEncoding = 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)) {
sourcePath = AbstractJavaSourceClassLoader.splitPath(args[++i]);
} else
if ("-encoding".equals(arg)) {
characterEncoding = 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