pax_global_header 0000666 0000000 0000000 00000000064 12222640623 0014511 g ustar 00root root 0000000 0000000 52 comment=2c27618650dc03a426f70007a552a1952dae7c47
janino-2.7.0/ 0000775 0000000 0000000 00000000000 12222640623 0012775 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-jdk/ 0000775 0000000 0000000 00000000000 12222640623 0017026 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-jdk/.checkstyle 0000664 0000000 0000000 00000001017 12222640623 0021164 0 ustar 00root root 0000000 0000000
janino-2.7.0/commons-compiler-jdk/pom.xml 0000664 0000000 0000000 00000001727 12222640623 0020352 0 ustar 00root root 0000000 0000000
4.0.0org.codehaus.janinojanino-parent2.6.2-SNAPSHOTcommons-compiler-jdkCommons Compiler Jdkorg.codehaus.janinocommons-compilerorg.apache.maven.pluginsmaven-compiler-plugin1.61.6
janino-2.7.0/commons-compiler-jdk/src/ 0000775 0000000 0000000 00000000000 12222640623 0017615 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-jdk/src/org.codehaus.commons.compiler.properties 0000664 0000000 0000000 00000000103 12222640623 0027571 0 ustar 00root root 0000000 0000000 compilerFactory=org.codehaus.commons.compiler.jdk.CompilerFactory
janino-2.7.0/commons-compiler-jdk/src/org/ 0000775 0000000 0000000 00000000000 12222640623 0020404 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-jdk/src/org/codehaus/ 0000775 0000000 0000000 00000000000 12222640623 0022177 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-jdk/src/org/codehaus/commons/ 0000775 0000000 0000000 00000000000 12222640623 0023652 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-jdk/src/org/codehaus/commons/compiler/ 0000775 0000000 0000000 00000000000 12222640623 0025464 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-jdk/src/org/codehaus/commons/compiler/jdk/ 0000775 0000000 0000000 00000000000 12222640623 0026234 5 ustar 00root root 0000000 0000000 ByteArrayJavaFileManager.java 0000664 0000000 0000000 00000017010 12222640623 0033616 0 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-jdk/src/org/codehaus/commons/compiler/jdk
/*
* 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.jdk;
import java.io.*;
import java.net.URI;
import java.util.*;
import java.util.Map.Entry;
import javax.tools.*;
import javax.tools.JavaFileObject.Kind;
/**
* A {@link ForwardingJavaFileManager} that stores {@link JavaFileObject}s in byte arrays, i.e. in
* memory (as opposed to the {@link StandardJavaFileManager}, which stores them in files).
*/
public
class ByteArrayJavaFileManager extends ForwardingJavaFileManager {
/** location => kind => className => JavaFileObject */
Map>> javaFiles = (
new HashMap>>()
);
public
ByteArrayJavaFileManager(M delegate) {
super(delegate);
}
@Override public FileObject
getFileForInput(Location location, String packageName, String relativeName) {
throw new UnsupportedOperationException("getFileForInput");
}
@Override public FileObject
getFileForOutput(
Location location,
String packageName,
String relativeName,
FileObject sibling
) {
throw new UnsupportedOperationException("getFileForInput");
}
@Override public JavaFileObject
getJavaFileForInput(Location location, String className, Kind kind) throws IOException {
Map> locationJavaFiles = this.javaFiles.get(location);
if (locationJavaFiles == null) return null;
Map kindJavaFiles = locationJavaFiles.get(kind);
if (kindJavaFiles == null) return null;
return kindJavaFiles.get(className);
}
@Override public JavaFileObject
getJavaFileForOutput(
Location location,
final String className,
Kind kind,
FileObject sibling
) throws IOException {
/**
* {@link StringWriter}-based implementation of {@link JavaFileObject}.
*
* Notice that {@link #getCharContent(boolean)} is much more efficient than {@link
* ByteArrayJavaFileObject#getCharContent(boolean)}. However, memory consumption is
* roughly double, and {@link #openInputStream()} and {@link #openOutputStream()} are
* not available.
*/
class StringWriterJavaFileObject extends SimpleJavaFileObject {
final StringWriter buffer = new StringWriter();
public
StringWriterJavaFileObject(Kind kind) {
super(
URI.create("stringbuffer:///" + className.replace('.', '/') + kind.extension),
kind
);
}
@Override public Writer
openWriter() throws IOException {
return this.buffer;
}
@Override public Reader
openReader(boolean ignoreEncodingErrors) throws IOException {
return new StringReader(this.buffer.toString());
}
@Override public CharSequence
getCharContent(boolean ignoreEncodingErrors) {
return this.buffer.getBuffer();
}
}
JavaFileObject fileObject = (
kind == Kind.SOURCE
? new StringWriterJavaFileObject(kind)
: new ByteArrayJavaFileObject(className, kind)
);
Map> locationJavaFiles = this.javaFiles.get(location);
if (locationJavaFiles == null) {
locationJavaFiles = new HashMap>();
this.javaFiles.put(location, locationJavaFiles);
}
Map kindJavaFiles = locationJavaFiles.get(kind);
if (kindJavaFiles == null) {
kindJavaFiles = new HashMap();
locationJavaFiles.put(kind, kindJavaFiles);
}
kindJavaFiles.put(className, fileObject);
return fileObject;
}
@Override public Iterable
list(
Location location,
String packageName,
Set kinds,
boolean recurse
) throws IOException {
Map> locationFiles = this.javaFiles.get(location);
if (locationFiles == null) return super.list(location, packageName, kinds, recurse);
String prefix = packageName.length() == 0 ? "" : packageName + ".";
int pl = prefix.length();
List result = new ArrayList();
for (Kind kind : kinds) {
Map kindFiles = locationFiles.get(kind);
if (kindFiles == null) continue;
for (Entry e : kindFiles.entrySet()) {
String className = e.getKey();
if (!className.startsWith(prefix)) continue;
if (!recurse && className.indexOf('.', pl) != -1) continue;
result.add(e.getValue());
}
}
return result;
}
/**
* Byte array-based implementation of {@link JavaFileObject}.
*/
public static
class ByteArrayJavaFileObject extends SimpleJavaFileObject {
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
public
ByteArrayJavaFileObject(String className, Kind kind) {
super(
URI.create("bytearray:///" + className.replace('.', '/') + kind.extension),
kind
);
}
@Override public OutputStream
openOutputStream() throws IOException { return this.buffer; }
public byte[]
toByteArray() { return this.buffer.toByteArray(); }
@Override public InputStream
openInputStream() throws IOException { return new ByteArrayInputStream(this.toByteArray()); }
}
}
janino-2.7.0/commons-compiler-jdk/src/org/codehaus/commons/compiler/jdk/ClassBodyEvaluator.java 0000664 0000000 0000000 00000025336 12222640623 0032656 0 ustar 00root root 0000000 0000000
/*
* 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.jdk;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.codehaus.commons.compiler.CompileException;
import org.codehaus.commons.compiler.IClassBodyEvaluator;
import org.codehaus.commons.io.MultiReader;
/**
* To set up a {@link ClassBodyEvaluator} object, proceed as described for {@link
* IClassBodyEvaluator}. Alternatively, a number of "convenience constructors" exist that execute
* the described steps instantly.
*
* Notice that this implementation of {@link IClassBodyEvaluator} is prone to "Java
* injection", i.e. an application could get more than one class body compiled by passing a
* bogus input document.
*
* Also notice that the parsing of leading IMPORT declarations is heuristic and has certain
* limitations; see {@link #parseImportDeclarations(Reader)}.
*
* @see IClassBodyEvaluator
*/
public
class ClassBodyEvaluator extends SimpleCompiler implements IClassBodyEvaluator {
private String[] optionalDefaultImports;
private String className = IClassBodyEvaluator.DEFAULT_CLASS_NAME;
private Class> optionalExtendedType;
private Class>[] implementedTypes = new Class[0];
private Class> result;
@Override public void
setClassName(String className) {
assertNotCooked();
this.className = className;
}
@Override public void
setDefaultImports(String[] optionalDefaultImports) {
assertNotCooked();
this.optionalDefaultImports = optionalDefaultImports;
}
@Override public void
setExtendedClass(@SuppressWarnings("rawtypes") Class optionalExtendedType) {
assertNotCooked();
this.optionalExtendedType = optionalExtendedType;
}
/** @deprecated */
@Deprecated @Override public void
setExtendedType(@SuppressWarnings("rawtypes") Class optionalExtendedClass) {
this.setExtendedClass(optionalExtendedClass);
}
@Override public void
setImplementedInterfaces(@SuppressWarnings("rawtypes") Class[] implementedTypes) {
assertNotCooked();
this.implementedTypes = implementedTypes;
}
/** @deprecated */
@Deprecated @Override public void
setImplementedTypes(@SuppressWarnings("rawtypes") Class[] implementedInterfaces) {
this.setImplementedInterfaces(implementedInterfaces);
}
@Override public void
cook(String optionalFileName, Reader r) throws CompileException, IOException {
if (!r.markSupported()) r = new BufferedReader(r);
this.cook(optionalFileName, ClassBodyEvaluator.parseImportDeclarations(r), r);
}
/**
* @param imports E.g. "java.io.*" or "static java.util.Arrays.asList"
* @param r The class body to cook, without leading IMPORT declarations
*/
protected void
cook(String optionalFileName, String[] imports, Reader r) throws CompileException, IOException {
// Wrap the class body in a compilation unit.
{
StringWriter sw1 = new StringWriter();
{
PrintWriter pw = new PrintWriter(sw1);
// Break the class name up into package name and simple class name.
String packageName; // null means default package.
String simpleClassName;
{
int idx = this.className.lastIndexOf('.');
if (idx == -1) {
packageName = "";
simpleClassName = this.className;
} else
{
packageName = this.className.substring(0, idx);
simpleClassName = this.className.substring(idx + 1);
}
}
// Print PACKAGE directive.
if (!packageName.isEmpty()) {
pw.print("package ");
pw.print(packageName);
pw.println(";");
}
// Print default imports.
if (this.optionalDefaultImports != null) {
for (String defaultImport : this.optionalDefaultImports) {
pw.print("import ");
pw.print(defaultImport);
pw.println(";");
}
}
// Print imports as declared in the document.
if (!r.markSupported()) r = new BufferedReader(r);
for (String imporT : imports) {
pw.print("import ");
pw.print(imporT);
pw.println(";");
}
// Print the class declaration.
pw.print("public class ");
pw.print(simpleClassName);
if (this.optionalExtendedType != null) {
pw.print(" extends ");
pw.print(this.optionalExtendedType.getCanonicalName());
}
if (this.implementedTypes.length > 0) {
pw.print(" implements ");
pw.print(this.implementedTypes[0].getName());
for (int i = 1; i < this.implementedTypes.length; ++i) {
pw.print(", ");
pw.print(this.implementedTypes[i].getName());
}
}
pw.println(" {");
pw.close();
}
StringWriter sw2 = new StringWriter();
{
PrintWriter pw = new PrintWriter(sw2);
pw.println("}");
pw.close();
}
r = new MultiReader(new Reader[] {
new StringReader(sw1.toString()),
r,
new StringReader(sw2.toString()),
});
}
/**
* Compile the generated compilation unit.
*/
super.cook(optionalFileName, r);
try {
// Load the "main" class through the ClassLoader that was created by
// "SimpleCompiler.cook()". More classes (e.g. member types will be loaded
// automatically by the JVM.
this.result = this.getClassLoader().loadClass(this.className);
} catch (ClassNotFoundException cnfe) {
throw new IOException(cnfe);
}
}
/**
* @return The {@link Class} created by the preceding call to {@link #cook(Reader)}
*/
@Override public Class>
getClazz() { return this.result; }
/**
* Heuristically parse IMPORT declarations at the beginning of the character stream produced
* by the given {@link Reader}. After this method returns, all characters up to and including
* that last IMPORT declaration have been read from the {@link Reader}.
*
* This method does not handle comments and string literals correctly, i.e. if a pattern that
* looks like an IMPORT declaration appears within a comment or a string literal, it will be
* taken as an IMPORT declaration.
*
* @param r A {@link Reader} that supports MARK, e.g. a {@link BufferedReader}
* @return The parsed imports, e.g. {@code { "java.util.*", "static java.util.Map.Entry" }}
*/
protected static String[]
parseImportDeclarations(Reader r) throws IOException {
final CharBuffer cb = CharBuffer.allocate(10000);
r.mark(cb.limit());
r.read(cb);
cb.rewind();
List imports = new ArrayList();
int afterLastImport = 0;
for (Matcher matcher = IMPORT_STATEMENT_PATTERN.matcher(cb); matcher.find();) {
imports.add(matcher.group(1));
afterLastImport = matcher.end();
}
r.reset();
r.skip(afterLastImport);
return imports.toArray(new String[imports.size()]);
}
private static final Pattern IMPORT_STATEMENT_PATTERN = Pattern.compile(
"\\bimport\\s+"
+ "("
+ "(?:static\\s+)?"
+ "[\\p{javaLowerCase}\\p{javaUpperCase}_\\$][\\p{javaLowerCase}\\p{javaUpperCase}\\d_\\$]*"
+ "(?:\\.[\\p{javaLowerCase}\\p{javaUpperCase}_\\$][\\p{javaLowerCase}\\p{javaUpperCase}\\d_\\$]*)*"
+ "(?:\\.\\*)?"
+ ");"
);
@Override public Object
createInstance(Reader reader) throws CompileException, IOException {
this.cook(reader);
try {
return this.getClazz().newInstance();
} catch (InstantiationException ie) {
throw new CompileException((
"Class is abstract, an interface, an array class, a primitive type, or void; "
+ "or has no zero-parameter constructor"
), null, ie);
} catch (IllegalAccessException iae) {
throw new CompileException("The class or its zero-parameter constructor is not accessible", null, iae);
}
}
}
janino-2.7.0/commons-compiler-jdk/src/org/codehaus/commons/compiler/jdk/CompilerFactory.java 0000664 0000000 0000000 00000006021 12222640623 0032200 0 ustar 00root root 0000000 0000000
/*
* 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.jdk;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.codehaus.commons.compiler.*;
public
class CompilerFactory extends AbstractCompilerFactory {
@Override public String
getId() { return "org.codehaus.commons.compiler.jdk"; }
@Override public String
getImplementationVersion() { return CompilerFactory.class.getPackage().getImplementationVersion(); }
@Override public IExpressionEvaluator
newExpressionEvaluator() { return new ExpressionEvaluator(); }
@Override public IScriptEvaluator
newScriptEvaluator() { return new ScriptEvaluator(); }
@Override public IClassBodyEvaluator
newClassBodyEvaluator() { return new ClassBodyEvaluator(); }
@Override public ISimpleCompiler
newSimpleCompiler() { return new SimpleCompiler(); }
@Override public AbstractJavaSourceClassLoader
newJavaSourceClassLoader() {
return AccessController.doPrivileged(new PrivilegedAction() {
@Override public JavaSourceClassLoader run() { return new JavaSourceClassLoader(); }
});
}
@Override public AbstractJavaSourceClassLoader
newJavaSourceClassLoader(final ClassLoader parentClassLoader) {
return AccessController.doPrivileged(new PrivilegedAction() {
@Override public JavaSourceClassLoader run() { return new JavaSourceClassLoader(parentClassLoader); }
});
}
}
janino-2.7.0/commons-compiler-jdk/src/org/codehaus/commons/compiler/jdk/ExpressionEvaluator.java 0000664 0000000 0000000 00000024417 12222640623 0033131 0 ustar 00root root 0000000 0000000
/*
* 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.jdk;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import org.codehaus.commons.compiler.CompileException;
import org.codehaus.commons.compiler.Cookable;
import org.codehaus.commons.compiler.IExpressionEvaluator;
/**
* This {@link IExpressionEvaluator} is implemented by creating and compiling a temporary compilation unit defining one
* class with one static method with one RETURN statement.
*
* A number of "convenience constructors" exist that execute the set-up steps described for {@link
* IExpressionEvaluator} instantly.
*
* If the parameter and return types of the expression are known at compile time, then a "fast" expression evaluator can
* be instantiated through {@link #createFastEvaluator(String, Class, String[])}. Expression evaluation is faster than
* through {@link #evaluate(Object[])}, because it is not done through reflection but through direct method invocation.
*
* Example:
*
* public interface Foo {
* int bar(int a, int b);
* }
* ...
* Foo f = (Foo) ExpressionEvaluator.createFastExpressionEvaluator(
* "a + b", // expression to evaluate
* Foo.class, // interface that describes the expression's signature
* new String[] { "a", "b" }, // the parameters' names
* (ClassLoader) null // Use current thread's context class loader
* );
* System.out.println("1 + 2 = " + f.bar(1, 2)); // Evaluate the expression
*
* Notice: The {@code interfaceToImplement} must either be declared {@code public},
* or with package scope in the root package (i.e. "no" package).
*
* On my system (Intel P4, 2 GHz, MS Windows XP, JDK 1.4.1), expression "x + 1"
* evaluates as follows:
*
*
Server JVM
Client JVM
*
Normal EE
23.7 ns
64.0 ns
*
Fast EE
31.2 ns
42.2 ns
*
* (How can it be that interface method invocation is slower than reflection for
* the server JVM?)
*/
public
class ExpressionEvaluator extends ScriptEvaluator implements IExpressionEvaluator {
private Class>[] optionalExpressionTypes;
/**
* Equivalent to
* ExpressionEvaluator ee = new ExpressionEvaluator();
* ee.setExpressionType(expressionType);
* ee.setParameters(parameterNames, parameterTypes);
* ee.cook(expression);
*
* @see #ExpressionEvaluator()
* @see ExpressionEvaluator#setExpressionType(Class)
* @see ScriptEvaluator#setParameters(String[], Class[])
* @see ScriptEvaluator#setThrownExceptions(Class[])
* @see ClassBodyEvaluator#setExtendedClass(Class)
* @see ClassBodyEvaluator#setImplementedInterfaces(Class[])
* @see SimpleCompiler#setParentClassLoader(ClassLoader)
* @see Cookable#cook(String)
*/
public
ExpressionEvaluator(
String expression,
Class> expressionType,
String[] parameterNames,
Class>[] parameterTypes,
Class>[] thrownExceptions,
Class> optionalExtendedType,
Class>[] implementedTypes,
ClassLoader optionalParentClassLoader
) throws CompileException {
this.setExpressionType(expressionType);
this.setParameters(parameterNames, parameterTypes);
this.setThrownExceptions(thrownExceptions);
this.setExtendedClass(optionalExtendedType);
this.setImplementedInterfaces(implementedTypes);
this.setParentClassLoader(optionalParentClassLoader);
this.cook(expression);
}
public ExpressionEvaluator() {}
@Override public void
setExpressionType(@SuppressWarnings("rawtypes") Class expressionType) {
this.setExpressionTypes(new Class[] { expressionType });
}
@Override public void
setExpressionTypes(@SuppressWarnings("rawtypes") Class[] expressionTypes) {
assertNotCooked();
this.optionalExpressionTypes = expressionTypes;
Class>[] returnTypes = new Class[expressionTypes.length];
for (int i = 0; i < returnTypes.length; ++i) {
Class> et = expressionTypes[i];
returnTypes[i] = et == ANY_TYPE ? Object.class : et;
}
super.setReturnTypes(returnTypes);
}
@Override @Deprecated public final void
setReturnType(@SuppressWarnings("rawtypes") Class returnType) {
throw new AssertionError("Must not be used on an ExpressionEvaluator; use 'setExpressionType()' instead");
}
@Override @Deprecated public final void
setReturnTypes(@SuppressWarnings("rawtypes") Class[] returnTypes) {
throw new AssertionError("Must not be used on an ExpressionEvaluator; use 'setExpressionTypes()' instead");
}
@Override protected Class>
getDefaultReturnType() { return Object.class; }
@Override public void
cook(String[] optionalFileNames, Reader[] readers) throws CompileException, IOException {
readers = readers.clone(); // Don't modify the argument array.
String[] imports;
if (readers.length == 1) {
if (!readers[0].markSupported()) readers[0] = new BufferedReader(readers[0]);
imports = parseImportDeclarations(readers[0]);
} else
{
imports = new String[0];
}
Class>[] returnTypes = new Class[readers.length];
for (int i = 0; i < readers.length; ++i) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
if (this.optionalExpressionTypes == null || this.optionalExpressionTypes[i] == ANY_TYPE) {
returnTypes[i] = Object.class;
pw.print("return org.codehaus.commons.compiler.PrimitiveWrapper.wrap(");
pw.write(readString(readers[i]));
pw.println(");");
} else {
returnTypes[i] = this.optionalExpressionTypes[i];
if (returnTypes[i] != void.class && returnTypes[i] != Void.class) {
pw.print("return ");
}
pw.write(readString(readers[i]));
pw.println(";");
}
pw.close();
readers[i] = new StringReader(sw.toString());
}
super.setReturnTypes(returnTypes);
this.cook(optionalFileNames, readers, imports);
}
}
FileInputJavaFileManager.java 0000664 0000000 0000000 00000016516 12222640623 0033625 0 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-jdk/src/org/codehaus/commons/compiler/jdk
/*
* 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.jdk;
import java.io.*;
import java.util.*;
import javax.tools.*;
import javax.tools.JavaFileManager.Location;
import javax.tools.JavaFileObject.Kind;
import org.codehaus.commons.compiler.Cookable;
/**
* A {@link ForwardingJavaFileManager} that maps accesses to a particular {@link Location} and {@link Kind} to
* a path-based search in the file system.
*/
final
class FileInputJavaFileManager extends ForwardingJavaFileManager {
private final Location location;
private final Kind kind;
private final File[] path;
private final String optionalCharacterEncoding;
/**
* @param path List of directories to look through
* @param optionalCharacterEncoding Encoding of the files being read
*/
FileInputJavaFileManager(
JavaFileManager delegate,
Location location,
Kind kind,
File[] path,
String optionalCharacterEncoding
) {
super(delegate);
this.location = location;
this.kind = kind;
this.path = path;
this.optionalCharacterEncoding = optionalCharacterEncoding;
}
@Override public Iterable
list(Location location, String packageName, Set kinds, boolean recurse) throws IOException {
if (location == this.location && kinds.contains(this.kind)) {
Collection result = new ArrayList();
String rel = packageName.replace('.', File.separatorChar);
for (File directory : this.path) {
File packageDirectory = new File(directory, rel);
result.addAll(this.list(
packageDirectory,
packageName.isEmpty() ? "" : packageName + ".",
this.kind,
recurse
));
}
return result;
}
return super.list(location, packageName, kinds, recurse);
}
/**
* @param qualification E.g. "", or "pkg1.pkg2."
* @return All {@link JavaFileObject}s of the given {@code kind} in the given {@code directory}
*/
private Collection
list(File directory, String qualification, Kind kind, boolean recurse) throws IOException {
if (!directory.isDirectory()) return Collections.emptyList();
Collection result = new ArrayList();
for (String name : directory.list()) {
File file = new File(directory, name);
if (name.endsWith(kind.extension)) {
result.add(new InputFileJavaFileObject(
file,
qualification + name.substring(0, name.length() - kind.extension.length())
));
} else if (recurse && file.isDirectory()) {
result.addAll(this.list(file, qualification + name + ".", kind, true));
}
}
return result;
}
@Override public String
inferBinaryName(Location location, JavaFileObject file) {
if (location == this.location) return ((InputFileJavaFileObject) file).getBinaryName();
return super.inferBinaryName(location, file);
}
@Override public boolean
hasLocation(Location location) { return location == this.location || super.hasLocation(location); }
@Override public JavaFileObject
getJavaFileForInput(Location location, String className, Kind kind) throws IOException {
if (location == this.location && kind == this.kind) {
// Find the source file through the source path.
final File sourceFile;
FIND_SOURCE: {
String rel = className.replace('.', File.separatorChar) + kind.extension;
for (File sourceDirectory : this.path) {
File f = new File(sourceDirectory, rel);
if (f.exists()) {
sourceFile = f.getCanonicalFile();
break FIND_SOURCE;
}
}
return null;
}
// Create and return a JavaFileObject.
return new InputFileJavaFileObject(sourceFile, className);
}
return super.getJavaFileForInput(location, className, kind);
}
/**
* A {@link JavaFileObject} that reads from a {@link File}.
*/
private
class InputFileJavaFileObject extends SimpleJavaFileObject {
private final File file;
private final String binaryName;
public
InputFileJavaFileObject(File file, String binaryName) {
super(file.toURI(), FileInputJavaFileManager.this.kind);
this.file = file;
this.binaryName = binaryName;
}
@SuppressWarnings("resource") @Override public Reader
openReader(boolean ignoreEncodingErrors) throws IOException {
return (
FileInputJavaFileManager.this.optionalCharacterEncoding == null
? new FileReader(this.file)
: new InputStreamReader(
new FileInputStream(this.file),
FileInputJavaFileManager.this.optionalCharacterEncoding
)
);
}
@Override public CharSequence
getCharContent(boolean ignoreEncodingErrors) throws IOException {
Reader r = this.openReader(true);
try {
return Cookable.readString(r);
} finally {
r.close();
}
}
String
getBinaryName() { return this.binaryName; }
}
}
JavaFileManagerClassLoader.java 0000664 0000000 0000000 00000006654 12222640623 0034124 0 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-jdk/src/org/codehaus/commons/compiler/jdk
/*
* 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.jdk;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;
import javax.tools.JavaFileObject.Kind;
public
class JavaFileManagerClassLoader extends ClassLoader {
private final JavaFileManager javaFileManager;
public
JavaFileManagerClassLoader(JavaFileManager javaFileManager) { this.javaFileManager = javaFileManager; }
public
JavaFileManagerClassLoader(JavaFileManager javaFileManager, ClassLoader parentClassLoader) {
super(parentClassLoader);
this.javaFileManager = javaFileManager;
}
@Override protected Class>
findClass(String className) throws ClassNotFoundException {
byte[] ba;
try {
JavaFileObject classFile = this.javaFileManager.getJavaFileForInput(
StandardLocation.CLASS_OUTPUT,
className,
Kind.CLASS
);
if (classFile == null) throw new ClassNotFoundException(className);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
{
InputStream is = classFile.openInputStream();
try {
byte[] buffer = new byte[8192];
for (;;) {
int count = is.read(buffer);
if (count == -1) break;
baos.write(buffer, 0, count);
}
} finally {
try { is.close(); } catch (Exception e) {}
}
}
ba = baos.toByteArray();
} catch (IOException ioe) {
throw new ClassNotFoundException(className, ioe);
}
return this.defineClass(className, ba, 0, ba.length);
}
}
janino-2.7.0/commons-compiler-jdk/src/org/codehaus/commons/compiler/jdk/JavaSourceClassLoader.java 0000664 0000000 0000000 00000025350 12222640623 0033263 0 ustar 00root root 0000000 0000000
/*
* 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.jdk;
import java.io.*;
import java.util.*;
import javax.tools.*;
import javax.tools.JavaFileObject.Kind;
import org.codehaus.commons.compiler.*;
import org.codehaus.commons.compiler.jdk.ByteArrayJavaFileManager.ByteArrayJavaFileObject;
public
class JavaSourceClassLoader extends AbstractJavaSourceClassLoader {
private File[] sourcePath;
private String optionalCharacterEncoding;
private boolean debuggingInfoLines;
private boolean debuggingInfoVars;
private boolean debuggingInfoSource;
private Collection compilerOptions = new ArrayList();
private JavaCompiler compiler;
private JavaFileManager fileManager;
/**
* @see ICompilerFactory#newJavaSourceClassLoader()
*/
public
JavaSourceClassLoader() { this.init(); }
/**
* @see ICompilerFactory#newJavaSourceClassLoader(ClassLoader)
*/
public
JavaSourceClassLoader(ClassLoader parentClassLoader) {
super(parentClassLoader);
this.init();
}
private void
init() {
this.compiler = ToolProvider.getSystemJavaCompiler();
if (this.compiler == null) {
throw new UnsupportedOperationException(
"JDK Java compiler not available - probably you're running a JRE, not a JDK"
);
}
}
/**
* Creates the underlying {@link JavaFileManager} lazily, because {@link #setSourcePath(File[])} and consorts
* are called after initialization.
*/
JavaFileManager
getJavaFileManager() {
if (this.fileManager == null) {
// Get the original FM, which reads class files through this JVM's BOOTCLASSPATH and
// CLASSPATH.
JavaFileManager jfm = this.compiler.getStandardFileManager(null, null, null);
// Wrap it so that the output files (in our case class files) are stored in memory rather
// than in files.
jfm = new ByteArrayJavaFileManager(jfm);
// Wrap it in a file manager that finds source files through the source path.
jfm = new FileInputJavaFileManager(
jfm,
StandardLocation.SOURCE_PATH,
Kind.SOURCE,
this.sourcePath,
this.optionalCharacterEncoding
);
this.fileManager = jfm;
}
return this.fileManager;
}
@Override public void
setSourcePath(File[] sourcePath) { this.sourcePath = sourcePath; }
@Override public void
setSourceFileCharacterEncoding(String optionalCharacterEncoding) {
this.optionalCharacterEncoding = optionalCharacterEncoding;
}
@Override public void
setDebuggingInfo(boolean lines, boolean vars, boolean source) {
this.debuggingInfoLines = lines;
this.debuggingInfoVars = vars;
this.debuggingInfoSource = source;
}
/**
* Notice: Don't use the '-g' options - these are controlled through {@link #setDebuggingInfo(boolean, boolean,
* boolean)}.
*
* @param compilerOptions All command line options supported by the JDK JAVAC tool
*/
public void
setCompilerOptions(String[] compilerOptions) { this.compilerOptions = Arrays.asList(compilerOptions); }
/**
* Implementation of {@link ClassLoader#findClass(String)}.
*
* @throws ClassNotFoundException
*/
@Override protected Class>
findClass(String className) throws ClassNotFoundException {
byte[] ba;
int size;
try {
// Maybe the bytecode is already there, because the class was compiled as a side effect of a preceding
// compilation.
JavaFileObject classFileObject = this.getJavaFileManager().getJavaFileForInput(
StandardLocation.CLASS_OUTPUT,
className,
Kind.CLASS
);
if (classFileObject == null) {
// Get the sourceFile.
JavaFileObject sourceFileObject = this.getJavaFileManager().getJavaFileForInput(
StandardLocation.SOURCE_PATH,
className,
Kind.SOURCE
);
if (sourceFileObject == null) {
throw new DiagnosticException("Source for '" + className + "' not found");
}
// Compose the effective compiler options.
List options = new ArrayList(this.compilerOptions);
options.add(this.debuggingInfoLines ? (
this.debuggingInfoSource ? (
this.debuggingInfoVars
? "-g"
: "-g:lines,source"
) : this.debuggingInfoVars ? "-g:lines,vars" : "-g:lines"
) : this.debuggingInfoSource ? (
this.debuggingInfoVars
? "-g:source,vars"
: "-g:source"
) : this.debuggingInfoVars ? "-g:vars" : "-g:none");
// Run the compiler.
if (!this.compiler.getTask(
null, // out
this.getJavaFileManager(), // fileManager
new DiagnosticListener() { // diagnosticListener
@Override public void
report(final Diagnostic extends JavaFileObject> diagnostic) {
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
throw new DiagnosticException(diagnostic);
}
}
},
options, // options
null, // classes
Collections.singleton(sourceFileObject) // compilationUnits
).call()) {
throw new ClassNotFoundException(className + ": Compilation failed");
}
classFileObject = this.getJavaFileManager().getJavaFileForInput(
StandardLocation.CLASS_OUTPUT,
className,
Kind.CLASS
);
if (classFileObject == null) {
throw new ClassNotFoundException(className + ": Class file not created by compilation");
}
}
if (classFileObject instanceof ByteArrayJavaFileObject) {
ByteArrayJavaFileObject bajfo = (ByteArrayJavaFileObject) classFileObject;
ba = bajfo.toByteArray();
size = ba.length;
} else
{
ba = new byte[4096];
size = 0;
InputStream is = classFileObject.openInputStream();
try {
for (;;) {
int res = is.read(ba, size, ba.length - size);
if (res == -1) break;
size += res;
if (size == ba.length) {
byte[] tmp = new byte[2 * size];
System.arraycopy(ba, 0, tmp, 0, size);
ba = tmp;
}
}
} finally {
is.close();
}
}
} catch (IOException ioe) {
throw new DiagnosticException(ioe);
}
return this.defineClass(className, ba, 0, size, (
this.optionalProtectionDomainFactory == null
? null
: this.optionalProtectionDomainFactory.getProtectionDomain(getSourceResourceName(className))
));
}
/**
* Construct the name of a resource that could contain the source code of
* the class with the given name.
*
* Notice that member types are declared inside a different type, so the relevant source file
* is that of the outermost declaring class.
*
* @param className Fully qualified class name, e.g. "pkg1.pkg2.Outer$Inner"
* @return the name of the resource, e.g. "pkg1/pkg2/Outer.java"
*/
private static String
getSourceResourceName(String className) {
// Strip nested type suffixes.
{
int idx = className.lastIndexOf('.') + 1;
idx = className.indexOf('$', idx);
if (idx != -1) className = className.substring(0, idx);
}
return className.replace('.', '/') + ".java";
}
public static
class DiagnosticException extends RuntimeException {
private static final long serialVersionUID = 5589635876875819926L;
public
DiagnosticException(String message) { super(message); }
public
DiagnosticException(Throwable cause) { super(cause); }
public
DiagnosticException(Diagnostic extends JavaFileObject> diagnostic) { super(diagnostic.toString()); }
}
}
janino-2.7.0/commons-compiler-jdk/src/org/codehaus/commons/compiler/jdk/ScriptEvaluator.java 0000664 0000000 0000000 00000054327 12222640623 0032241 0 ustar 00root root 0000000 0000000
/*
* 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.jdk;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import org.codehaus.commons.compiler.*;
import org.codehaus.commons.io.MultiReader;
/**
* To set up a {@link ScriptEvaluator} object, proceed as described for {@link IScriptEvaluator}.
* Alternatively, a number of "convenience constructors" exist that execute the described steps
* instantly.
*
* Alternatively, a number of "convenience constructors" exist that execute the steps described
* above instantly. Their use is discouraged.
*
* Notice that this implementation of {@link IClassBodyEvaluator} is prone to "Java
* injection", i.e. an application could get more than one class body compiled by passing a
* bogus input document.
*
* Also notice that the parsing of leading IMPORT declarations is heuristic and has certain
* limitations; see {@link #parseImportDeclarations(Reader)}.
*/
public
class ScriptEvaluator extends ClassBodyEvaluator implements IScriptEvaluator {
/** Whether methods override a method declared by a supertype; {@code null} means "none". */
protected boolean[] optionalOverrideMethod;
/** Whether methods are static; {@code null} means "all". */
protected boolean[] optionalStaticMethod;
protected Class>[] optionalReturnTypes;
protected String[] optionalMethodNames;
protected String[][] optionalParameterNames;
protected Class>[][] optionalParameterTypes;
protected Class>[][] optionalThrownExceptions;
/** null=uncooked */
private Method[] result;
/**
* Equivalent to
* ScriptEvaluator se = new ScriptEvaluator();
* se.cook(script);
*
* @see #ScriptEvaluator()
* @see Cookable#cook(String)
*/
public
ScriptEvaluator(String script) throws CompileException { this.cook(script); }
/**
* Equivalent to
* ScriptEvaluator se = new ScriptEvaluator();
* se.setReturnType(returnType);
* se.cook(script);
* ScriptEvaluator se = new ScriptEvaluator();
* se.setReturnType(returnType);
* se.setParameters(parameterNames, parameterTypes);
* se.setThrownExceptions(thrownExceptions);
* se.setParentClassLoader(optionalParentClassLoader);
* se.cook(reader);
*
* @see #ScriptEvaluator()
* @see #setReturnType(Class)
* @see #setParameters(String[], Class[])
* @see #setThrownExceptions(Class[])
* @see SimpleCompiler#setParentClassLoader(ClassLoader)
* @see Cookable#cook(String, Reader)
*/
public
ScriptEvaluator(
String optionalFileName,
Reader reader,
Class> returnType,
String[] parameterNames,
Class>[] parameterTypes,
Class>[] thrownExceptions,
ClassLoader optionalParentClassLoader // null = use current thread's context class loader
) throws CompileException, IOException {
this.setReturnType(returnType);
this.setParameters(parameterNames, parameterTypes);
this.setThrownExceptions(thrownExceptions);
this.setParentClassLoader(optionalParentClassLoader);
this.cook(optionalFileName, reader);
}
public ScriptEvaluator() {}
@Override public void
setOverrideMethod(boolean overrideMethod) { this.setOverrideMethod(new boolean[] { overrideMethod }); }
@Override public void
setStaticMethod(final boolean staticMethod) { this.setStaticMethod(new boolean[] { staticMethod }); }
@Override public void
setReturnType(@SuppressWarnings("rawtypes") Class returnType) {
this.setReturnTypes(new Class>[] { returnType });
}
@Override public void
setMethodName(String methodName) { this.setMethodNames(new String[] { methodName }); }
@Override public void
setParameters(String[] names, @SuppressWarnings("rawtypes") Class[] types) {
this.setParameters(new String[][] { names }, new Class>[][] { types });
}
@Override public void
setThrownExceptions(@SuppressWarnings("rawtypes") Class[] thrownExceptions) {
this.setThrownExceptions(new Class[][] { thrownExceptions });
}
@Override public void
cook(String optionalFileName, Reader r) throws CompileException, IOException {
this.cook(new String[] { optionalFileName }, new Reader[] { r });
}
@Override public Object
evaluate(Object[] arguments) throws InvocationTargetException { return this.evaluate(0, arguments); }
@Override public Method
getMethod() { return this.getMethod(0); }
@Override public void
setOverrideMethod(boolean[] overrideMethod) {
assertNotCooked();
this.optionalOverrideMethod = overrideMethod.clone();
}
@Override public void
setStaticMethod(boolean[] staticMethod) {
assertNotCooked();
this.optionalStaticMethod = staticMethod.clone();
}
@Override public void
setReturnTypes(@SuppressWarnings("rawtypes") Class[] returnTypes) {
assertNotCooked();
this.optionalReturnTypes = returnTypes.clone();
}
@Override public void
setMethodNames(String[] methodNames) {
assertNotCooked();
this.optionalMethodNames = methodNames.clone();
}
@Override public void
setParameters(String[][] names, @SuppressWarnings("rawtypes") Class[][] types) {
assertNotCooked();
this.optionalParameterNames = names.clone();
this.optionalParameterTypes = types.clone();
}
@Override public void
setThrownExceptions(@SuppressWarnings("rawtypes") Class[][] thrownExceptions) {
assertNotCooked();
this.optionalThrownExceptions = thrownExceptions.clone();
}
@Override public final void
cook(Reader[] readers) throws CompileException, IOException { this.cook(null, readers); }
@Override public void
cook(String[] optionalFileNames, Reader[] readers) throws CompileException, IOException {
String[] imports;
if (readers.length == 1) {
if (!readers[0].markSupported()) {
readers = new Reader[] { new BufferedReader(readers[0]) };
}
imports = parseImportDeclarations(readers[0]);
} else
{
imports = new String[0];
}
this.cook(optionalFileNames, readers, imports);
}
@Override public final void
cook(String[] strings) throws CompileException { this.cook(null, strings); }
@Override public void
cook(String[] optionalFileNames, String[] strings) throws CompileException {
Reader[] readers = new Reader[strings.length];
for (int i = 0; i < strings.length; ++i) readers[i] = new StringReader(strings[i]);
try {
this.cook(optionalFileNames, readers);
} catch (IOException ioe) {
throw new RuntimeException("SNO: IOException despite StringReader", ioe);
}
}
/**
* @param readers The scripts to cook
*/
protected final void
cook(String[] optionalFileNames, Reader[] readers, String[] imports) throws CompileException, IOException {
// The "dimension" of this ScriptEvaluator, i.e. how many scripts are cooked at the same
// time.
int count = readers.length;
// Check array sizes.
if (this.optionalMethodNames != null && this.optionalMethodNames.length != count) {
throw new IllegalStateException("methodName");
}
if (this.optionalParameterNames != null && this.optionalParameterNames.length != count) {
throw new IllegalStateException("parameterNames");
}
if (this.optionalParameterTypes != null && this.optionalParameterTypes.length != count) {
throw new IllegalStateException("parameterTypes");
}
if (this.optionalReturnTypes != null && this.optionalReturnTypes.length != count) {
throw new IllegalStateException("returnTypes");
}
if (this.optionalOverrideMethod != null && this.optionalOverrideMethod.length != count) {
throw new IllegalStateException("overrideMethod");
}
if (this.optionalStaticMethod != null && this.optionalStaticMethod.length != count) {
throw new IllegalStateException("staticMethod");
}
if (this.optionalThrownExceptions != null && this.optionalThrownExceptions.length != count) {
throw new IllegalStateException("thrownExceptions");
}
// Determine method names.
String[] methodNames;
if (this.optionalMethodNames == null) {
methodNames = new String[count];
for (int i = 0; i < count; ++i) methodNames[i] = "eval" + i;
} else
{
methodNames = this.optionalMethodNames;
}
// Create compilation unit.
List classBody = new ArrayList();
// Create methods with one block each.
for (int i = 0; i < count; ++i) {
boolean overrideMethod = this.optionalOverrideMethod != null && this.optionalOverrideMethod[i];
boolean staticMethod = this.optionalStaticMethod == null || this.optionalStaticMethod[i];
Class> returnType = (
this.optionalReturnTypes == null
? this.getDefaultReturnType()
: this.optionalReturnTypes[i]
);
String[] parameterNames = (
this.optionalParameterNames == null
? new String[0]
: this.optionalParameterNames[i]
);
Class>[] parameterTypes = (
this.optionalParameterTypes == null
? new Class>[0]
: this.optionalParameterTypes[i]
);
Class>[] thrownExceptions = (
this.optionalThrownExceptions == null
? new Class>[0]
: this.optionalThrownExceptions[i]
);
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
if (overrideMethod) pw.print("@Override ");
pw.print("public ");
if (staticMethod) pw.print("static ");
pw.print(returnType.getName());
pw.print(" ");
pw.print(methodNames[i]);
pw.print("(");
for (int j = 0; j < parameterNames.length; ++j) {
if (j > 0) pw.print(", ");
pw.print(parameterTypes[j].getName());
pw.print(" ");
pw.print(parameterNames[j]);
}
pw.print(")");
for (int j = 0; j < thrownExceptions.length; ++j) {
pw.print(j == 0 ? " throws " : ", ");
pw.print(thrownExceptions[j].getName());
}
pw.println(" {");
pw.close();
classBody.add(new StringReader(sw.toString()));
}
classBody.add(readers[i]);
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.println("}");
pw.close();
classBody.add(new StringReader(sw.toString()));
}
}
super.cook(optionalFileNames == null ? null : optionalFileNames[0], imports, new MultiReader(classBody));
Class> c = this.getClazz();
// Find the script methods by name.
this.result = new Method[count];
if (count <= 10) {
for (int i = 0; i < count; ++i) {
try {
this.result[i] = c.getDeclaredMethod(
methodNames[i],
this.optionalParameterTypes == null ? new Class[0] : this.optionalParameterTypes[i]
);
} catch (NoSuchMethodException ex) {
throw new RuntimeException(
"SNO: Loaded class does not declare method \"" + methodNames[i] + "\"",
ex
);
}
}
} else
{
// "getDeclaredMethod()" implements a linear search which is inefficient like hell for
// classes with MANY methods (like an ExpressionEvaluator with a 10000 methods). Thus
// we se "getDeclaredMethods()" and sort things out ourselves with a HashMap.
class MethodWrapper {
private final String name;
private final Class>[] parameterTypes;
MethodWrapper(String name, Class>[] parameterTypes) {
this.name = name;
this.parameterTypes = parameterTypes;
}
@Override public boolean
equals(Object o) {
if (!(o instanceof MethodWrapper)) return false;
MethodWrapper that = (MethodWrapper) o;
return (
this.name.equals(that.name)
&& Arrays.equals(this.parameterTypes, that.parameterTypes)
);
}
@Override public int
hashCode() { return this.name.hashCode() ^ Arrays.hashCode(this.parameterTypes); }
}
Method[] ma = c.getDeclaredMethods();
Map dms = new HashMap(2 * count);
for (int i = 0; i < ma.length; ++i) {
Method m = ma[i];
dms.put(new MethodWrapper(m.getName(), m.getParameterTypes()), m);
}
for (int i = 0; i < count; ++i) {
Method m = dms.get(new MethodWrapper(
methodNames[i],
this.optionalParameterTypes == null ? new Class[0] : this.optionalParameterTypes[i]
));
if (m == null) {
throw new RuntimeException("SNO: Loaded class does not declare method \"" + methodNames[i] + "\"");
}
this.result[i] = m;
}
}
}
protected Class>
getDefaultReturnType() { return void.class; }
/**
* @param script Contains the sequence of script tokens
* @see #createFastEvaluator(String, Class, String[])
*/
@Override public Object
createFastEvaluator(
String script,
@SuppressWarnings("rawtypes") Class interfaceToImplement,
String[] parameterNames
) throws CompileException {
try {
return this.createFastEvaluator(new StringReader(script), interfaceToImplement, parameterNames);
} catch (IOException ioe) {
throw new RuntimeException("SNO: IOException despite StringReader", ioe);
}
}
/**
* Don't use.
*/
@Override public final Object
createInstance(Reader reader) { throw new UnsupportedOperationException("createInstance"); }
@Override public Object
createFastEvaluator(
Reader r,
@SuppressWarnings("rawtypes") Class interfaceToImplement,
String[] parameterNames
) throws CompileException, IOException {
if (!interfaceToImplement.isInterface()) {
throw new RuntimeException("\"" + interfaceToImplement + "\" is not an interface");
}
Method[] methods = interfaceToImplement.getDeclaredMethods();
if (methods.length != 1) {
throw new RuntimeException("Interface \"" + interfaceToImplement + "\" must declare exactly one method");
}
Method methodToImplement = methods[0];
this.setImplementedInterfaces(new Class[] { interfaceToImplement });
this.setStaticMethod(false);
if (this instanceof IExpressionEvaluator) {
// Must not call "IExpressionEvaluator.setReturnType()".
((IExpressionEvaluator) this).setExpressionType(methodToImplement.getReturnType());
} else {
this.setReturnType(methodToImplement.getReturnType());
}
this.setMethodName(methodToImplement.getName());
this.setParameters(parameterNames, methodToImplement.getParameterTypes());
this.setThrownExceptions(methodToImplement.getExceptionTypes());
this.cook(r);
Class> c = this.getMethod().getDeclaringClass();
try {
return c.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException("SNO - Declared class is always non-abstract", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("SNO - interface methods are always PUBLIC", e);
}
}
@Override public Object
evaluate(int idx, Object[] arguments) throws InvocationTargetException {
if (this.result == null) throw new IllegalStateException("Must only be called after \"cook()\"");
try {
return this.result[idx].invoke(null, arguments);
} catch (IllegalAccessException ex) {
throw new RuntimeException(ex.toString(), ex);
}
}
@Override public Method
getMethod(int idx) {
if (this.result == null) throw new IllegalStateException("Must only be called after \"cook()\"");
return this.result[idx];
}
}
janino-2.7.0/commons-compiler-jdk/src/org/codehaus/commons/compiler/jdk/SimpleCompiler.java 0000664 0000000 0000000 00000027145 12222640623 0032034 0 ustar 00root root 0000000 0000000
/*
* 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.jdk;
import java.io.*;
import java.net.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;
import javax.tools.*;
import javax.tools.JavaFileObject.Kind;
import org.codehaus.commons.compiler.*;
public
class SimpleCompiler extends Cookable implements ISimpleCompiler {
private ClassLoader parentClassLoader = Thread.currentThread().getContextClassLoader();
private ClassLoader result;
private boolean debugSource;
private boolean debugLines;
private boolean debugVars;
@Override public ClassLoader
getClassLoader() { assertCooked(); return this.result; }
@Override public void
cook(String optionalFileName, final Reader r) throws CompileException, IOException {
assertNotCooked();
// Create one Java source file in memory, which will be compiled later.
JavaFileObject compilationUnit;
{
URI uri;
try {
uri = new URI("simplecompiler");
} catch (URISyntaxException use) {
throw new RuntimeException(use);
}
compilationUnit = new SimpleJavaFileObject(uri, Kind.SOURCE) {
@Override public boolean
isNameCompatible(String simpleName, Kind kind) { return true; }
@Override public Reader
openReader(boolean ignoreEncodingErrors) throws IOException { return r; }
@Override public CharSequence
getCharContent(boolean ignoreEncodingErrors) throws IOException {
return readString(this.openReader(ignoreEncodingErrors));
}
};
}
// Find the JDK Java compiler.
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new CompileException(
"JDK Java compiler not available - probably you're running a JRE, not a JDK",
null
);
}
// Get the original FM, which reads class files through this JVM's BOOTCLASSPATH and
// CLASSPATH.
final JavaFileManager fm = compiler.getStandardFileManager(null, null, null);
// Wrap it so that the output files (in our case class files) are stored in memory rather
// than in files.
final JavaFileManager fileManager = new ByteArrayJavaFileManager(fm);
// Run the compiler.
try {
if (!compiler.getTask(
null, // out
fileManager, // fileManager
new DiagnosticListener() { // diagnosticListener
@Override public void
report(Diagnostic extends JavaFileObject> diagnostic) {
//System.err.println("*** " + diagnostic.toString() + " *** " + diagnostic.getCode());
Location loc = new Location(
diagnostic.getSource().toString(),
(short) diagnostic.getLineNumber(),
(short) diagnostic.getColumnNumber()
);
String code = diagnostic.getCode();
String message = diagnostic.getMessage(null) + " (" + code + ")";
// Wrap the exception in a RuntimeException, because "report()" does not declare checked
// exceptions.
throw new RuntimeException(new CompileException(message, loc));
}
},
Collections.singletonList( // options
this.debugSource
? "-g:source" + (this.debugLines ? ",lines" : "") + (this.debugVars ? ",vars" : "")
: this.debugLines
? "-g:lines" + (this.debugVars ? ",vars" : "")
: this.debugVars
? "-g:vars"
: "-g:none"
),
null, // classes
Collections.singleton(compilationUnit) // compilationUnits
).call()) {
throw new CompileException("Compilation failed", null);
}
} catch (RuntimeException rte) {
// Unwrap the compilation exception and throw it.
Throwable cause = rte.getCause();
if (cause != null) {
cause = cause.getCause();
if (cause instanceof CompileException) {
throw (CompileException) cause; // SUPPRESS CHECKSTYLE AvoidHidingCause
}
if (cause instanceof IOException) {
throw (IOException) cause; // SUPPRESS CHECKSTYLE AvoidHidingCause
}
}
throw rte;
}
// Create a ClassLoader that reads class files from our FM.
this.result = AccessController.doPrivileged(new PrivilegedAction() {
@Override public JavaFileManagerClassLoader
run() { return new JavaFileManagerClassLoader(fileManager, SimpleCompiler.this.parentClassLoader); }
});
}
protected void
cook(JavaFileObject compilationUnit) throws CompileException, IOException {
// Find the JDK Java compiler.
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new CompileException(
"JDK Java compiler not available - probably you're running a JRE, not a JDK",
null
);
}
// Get the original FM, which reads class files through this JVM's BOOTCLASSPATH and
// CLASSPATH.
final JavaFileManager fm = compiler.getStandardFileManager(null, null, null);
// Wrap it so that the output files (in our case class files) are stored in memory rather
// than in files.
final JavaFileManager fileManager = new ByteArrayJavaFileManager(fm);
// Run the compiler.
try {
if (!compiler.getTask(
null, // out
fileManager, // fileManager
new DiagnosticListener() { // diagnosticListener
@Override public void
report(Diagnostic extends JavaFileObject> diagnostic) {
System.err.println("*** " + diagnostic.toString() + " *** " + diagnostic.getCode());
Location loc = new Location(
diagnostic.getSource().toString(),
(short) diagnostic.getLineNumber(),
(short) diagnostic.getColumnNumber()
);
String code = diagnostic.getCode();
String message = diagnostic.getMessage(null) + " (" + code + ")";
// Wrap the exception in a RuntimeException, because "report()" does not declare checked
// exceptions.
throw new RuntimeException(new CompileException(message, loc));
}
},
null, // options
null, // classes
Collections.singleton(compilationUnit) // compilationUnits
).call()) {
throw new CompileException("Compilation failed", null);
}
} catch (RuntimeException rte) {
// Unwrap the compilation exception and throw it.
Throwable cause = rte.getCause();
if (cause != null) {
cause = cause.getCause();
if (cause instanceof CompileException) {
throw (CompileException) cause; // SUPPRESS CHECKSTYLE AvoidHidingCause
}
if (cause instanceof IOException) {
throw (IOException) cause; // SUPPRESS CHECKSTYLE AvoidHidingCause
}
}
throw rte;
}
// Create a ClassLoader that reads class files from our FM.
this.result = AccessController.doPrivileged(new PrivilegedAction() {
@Override public JavaFileManagerClassLoader
run() { return new JavaFileManagerClassLoader(fileManager, SimpleCompiler.this.parentClassLoader); }
});
}
@Override public void
setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars) {
this.debugSource = debugSource;
this.debugLines = debugLines;
this.debugVars = debugVars;
}
@Override public void
setParentClassLoader(ClassLoader optionalParentClassLoader) {
assertNotCooked();
this.parentClassLoader = (
optionalParentClassLoader != null
? optionalParentClassLoader
: Thread.currentThread().getContextClassLoader()
);
}
/**
* Auxiliary classes never really worked... don't use them.
*
* @param optionalParentClassLoader
* @param auxiliaryClasses
* @deprecated
*/
@Deprecated public void
setParentClassLoader(ClassLoader optionalParentClassLoader, Class>[] auxiliaryClasses) {
this.setParentClassLoader(optionalParentClassLoader);
}
/**
* Throw an {@link IllegalStateException} if this {@link Cookable} is not yet cooked.
*/
protected void
assertCooked() { if (this.result == null) throw new IllegalStateException("Not yet cooked"); }
/**
* Throw an {@link IllegalStateException} if this {@link Cookable} is already cooked.
*/
protected void
assertNotCooked() { if (this.result != null) throw new IllegalStateException("Already cooked"); }
}
janino-2.7.0/commons-compiler-jdk/src/org/codehaus/commons/compiler/jdk/package-info.java 0000664 0000000 0000000 00000003277 12222640623 0031434 0 ustar 00root root 0000000 0000000
/*
* cs-contrib - Additional checks, filters and quickfixes for CheckStyle and Eclipse-CS
*
* Copyright (c) 2013, 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.
*/
/**
* Main package of the plugin.
*/
@de.unkrig.commons.nullanalysis.NotNullByDefault package org.codehaus.commons.compiler.jdk;
janino-2.7.0/commons-compiler-jdk/src/org/codehaus/commons/io/ 0000775 0000000 0000000 00000000000 12222640623 0024261 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-jdk/src/org/codehaus/commons/io/MultiReader.java 0000664 0000000 0000000 00000007450 12222640623 0027347 0 ustar 00root root 0000000 0000000
/*
* 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.io;
import java.io.*;
import java.util.*;
/**
* Similar to {@link FilterReader}, but when the first delegate is at end-of-input, it continues
* with reading from the next delegate.
*
* This {@link Reader} does not support MARK.
*/
public
class MultiReader extends Reader {
private static final Reader EMPTY_READER = new StringReader("");
private final List delegates;
private final Iterator delegateIterator;
private Reader currentDelegate = EMPTY_READER;
public
MultiReader(List delegates) {
this.delegates = delegates;
this.delegateIterator = delegates.iterator();
}
public
MultiReader(Reader[] delegates) { this(Arrays.asList(delegates)); }
/**
* Closes all delegates.
*/
public void
close() throws IOException { for (Reader delegate : this.delegates) delegate.close(); }
public int
read() throws IOException {
for (;;) {
int result = this.currentDelegate.read();
if (result != -1) return result;
if (!this.delegateIterator.hasNext()) return -1;
this.currentDelegate = this.delegateIterator.next();
}
}
public long
skip(long n) throws IOException {
long skipped = 0L;
for (;;) {
long result = this.currentDelegate.skip(n - skipped);
if (result != -1L) {
skipped += result;
if (skipped == n) return skipped;
continue;
}
if (!this.delegateIterator.hasNext()) return skipped;
this.currentDelegate = this.delegateIterator.next();
}
}
public int
read(final char[] cbuf, final int off, final int len) throws IOException {
int read = 0;
for (;;) {
long result = this.currentDelegate.read(cbuf, off + read, len - read);
if (result != -1L) {
read += result;
if (read == len) return read;
continue;
}
if (!this.delegateIterator.hasNext()) return read == 0 ? -1 : read;
this.currentDelegate = this.delegateIterator.next();
}
}
}
janino-2.7.0/commons-compiler-tests/ 0000775 0000000 0000000 00000000000 12222640623 0017420 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-tests/.checkstyle 0000664 0000000 0000000 00000001253 12222640623 0021560 0 ustar 00root root 0000000 0000000
janino-2.7.0/commons-compiler-tests/aux-files/ 0000775 0000000 0000000 00000000000 12222640623 0021315 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-tests/aux-files/Bug 106/ 0000775 0000000 0000000 00000000000 12222640623 0022321 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-tests/aux-files/Bug 106/a/ 0000775 0000000 0000000 00000000000 12222640623 0022541 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-tests/aux-files/Bug 106/a/C2.java 0000664 0000000 0000000 00000000134 12222640623 0023646 0 ustar 00root root 0000000 0000000
package a;
public class C2 {
private static int A = 0;
public static int B = 0;
}
janino-2.7.0/commons-compiler-tests/aux-files/Bug 106/b/ 0000775 0000000 0000000 00000000000 12222640623 0022542 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-tests/aux-files/Bug 106/b/C1.java 0000664 0000000 0000000 00000000104 12222640623 0023643 0 ustar 00root root 0000000 0000000
package b;
public class C1 {
public static int A = 0;
}
janino-2.7.0/commons-compiler-tests/aux-files/Bug 106/b/C3.java 0000664 0000000 0000000 00000000257 12222640623 0023656 0 ustar 00root root 0000000 0000000
package b;
import static a.C2.*;
import static b.C1.*;
public class C3 {
public static void main(String[] args) {
System.out.println(A + B);
}
}
janino-2.7.0/commons-compiler-tests/aux-files/testCircularSingleTypeImports/ 0000775 0000000 0000000 00000000000 12222640623 0027343 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-tests/aux-files/testCircularSingleTypeImports/test/ 0000775 0000000 0000000 00000000000 12222640623 0030322 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-tests/aux-files/testCircularSingleTypeImports/test/Func1.java 0000664 0000000 0000000 00000000236 12222640623 0032142 0 ustar 00root root 0000000 0000000
package test;
import test.Func2;
public class Func1 {
public static boolean func1() throws Exception {
return Func2.func2();
}
}
janino-2.7.0/commons-compiler-tests/aux-files/testCircularSingleTypeImports/test/Func2.java 0000664 0000000 0000000 00000000236 12222640623 0032143 0 ustar 00root root 0000000 0000000
package test;
import test.Func1;
public class Func2 {
public static boolean func2() throws Exception {
return Func1.func1();
}
}
janino-2.7.0/commons-compiler-tests/aux-files/testCircularStaticImports/ 0000775 0000000 0000000 00000000000 12222640623 0026507 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-tests/aux-files/testCircularStaticImports/test/ 0000775 0000000 0000000 00000000000 12222640623 0027466 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-tests/aux-files/testCircularStaticImports/test/Func1.java 0000664 0000000 0000000 00000000242 12222640623 0031303 0 ustar 00root root 0000000 0000000
package test;
import static test.Func2.func2;
public class Func1 {
public static boolean func1() throws Exception {
return true;
}
}
janino-2.7.0/commons-compiler-tests/aux-files/testCircularStaticImports/test/Func2.java 0000664 0000000 0000000 00000000242 12222640623 0031304 0 ustar 00root root 0000000 0000000
package test;
import static test.Func1.func1;
public class Func2 {
public static boolean func2() throws Exception {
return true;
}
}
janino-2.7.0/commons-compiler-tests/janino common-tests.launch 0000664 0000000 0000000 00000005472 12222640623 0024513 0 ustar 00root root 0000000 0000000
janino-2.7.0/commons-compiler-tests/janino+jdk common-tests.launch 0000664 0000000 0000000 00000005401 12222640623 0025247 0 ustar 00root root 0000000 0000000
janino-2.7.0/commons-compiler-tests/jdk common-tests.launch 0000664 0000000 0000000 00000005043 12222640623 0023777 0 ustar 00root root 0000000 0000000
janino-2.7.0/commons-compiler-tests/src/ 0000775 0000000 0000000 00000000000 12222640623 0020207 5 ustar 00root root 0000000 0000000 janino-2.7.0/commons-compiler-tests/src/EvaluatorTests.java 0000664 0000000 0000000 00000102231 12222640623 0024036 0 ustar 00root root 0000000 0000000
/*
* 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.
*/
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.Properties;
import org.codehaus.commons.compiler.CompileException;
import org.codehaus.commons.compiler.IClassBodyEvaluator;
import org.codehaus.commons.compiler.ICompilerFactory;
import org.codehaus.commons.compiler.IExpressionEvaluator;
import org.codehaus.commons.compiler.IScriptEvaluator;
import org.codehaus.commons.compiler.ISimpleCompiler;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import util.TestUtil;
import for_sandbox_tests.OverridesWithDifferingVisibility;
@RunWith(Parameterized.class) public
class EvaluatorTests {
private final ICompilerFactory compilerFactory;
@Parameters public static Collection