Added missing final modifiers.
This commit is contained in:
@@ -52,7 +52,7 @@ public class JavaCompiler
|
||||
|
||||
public Map<String, byte[]> compile(String source, String fileName)
|
||||
{
|
||||
PrintWriter err = new PrintWriter(System.err);
|
||||
final PrintWriter err = new PrintWriter(System.err);
|
||||
return compile(source, fileName, err, null, null);
|
||||
}
|
||||
|
||||
@@ -78,17 +78,17 @@ public class JavaCompiler
|
||||
public Map<String, byte[]> compile(String fileName, String source, Writer err, String sourcePath, String classPath)
|
||||
{
|
||||
// to collect errors, warnings etc.
|
||||
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
|
||||
final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
|
||||
|
||||
// create a new memory JavaFileManager
|
||||
MemoryJavaFileManager manager = new MemoryJavaFileManager();
|
||||
final MemoryJavaFileManager manager = new MemoryJavaFileManager();
|
||||
|
||||
// prepare the compilation unit
|
||||
List<JavaFileObject> compUnits = new ArrayList<>(1);
|
||||
final List<JavaFileObject> compUnits = new ArrayList<>(1);
|
||||
compUnits.add(MemoryJavaFileManager.makeStringSource(fileName, source));
|
||||
|
||||
// javac options
|
||||
List<String> options = new ArrayList<>();
|
||||
final List<String> options = new ArrayList<>();
|
||||
options.add("-warn:-enumSwitch");
|
||||
options.add("-g");
|
||||
options.add("-deprecation");
|
||||
@@ -105,11 +105,11 @@ public class JavaCompiler
|
||||
}
|
||||
|
||||
// create a compilation task
|
||||
CompilationTask task = tool.getTask(err, manager, diagnostics, options, null, compUnits);
|
||||
final CompilationTask task = tool.getTask(err, manager, diagnostics, options, null, compUnits);
|
||||
|
||||
if (!task.call())
|
||||
{
|
||||
PrintWriter perr = new PrintWriter(err);
|
||||
final PrintWriter perr = new PrintWriter(err);
|
||||
for (Diagnostic<?> diagnostic : diagnostics.getDiagnostics())
|
||||
{
|
||||
perr.println(diagnostic.getMessage(Locale.getDefault()));
|
||||
@@ -118,7 +118,7 @@ public class JavaCompiler
|
||||
return null;
|
||||
}
|
||||
|
||||
Map<String, byte[]> classBytes = manager.getClassBytes();
|
||||
final Map<String, byte[]> classBytes = manager.getClassBytes();
|
||||
manager.close();
|
||||
return classBytes;
|
||||
}
|
||||
|
||||
@@ -87,9 +87,9 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
{
|
||||
if (_class == null)
|
||||
{
|
||||
Map<String, byte[]> classBytesCopy = new HashMap<>();
|
||||
final Map<String, byte[]> classBytesCopy = new HashMap<>();
|
||||
classBytesCopy.putAll(_classBytes);
|
||||
MemoryClassLoader loader = new MemoryClassLoader(classBytesCopy, _classPath, JavaScriptEngine.getParentLoader(ctx));
|
||||
final MemoryClassLoader loader = new MemoryClassLoader(classBytesCopy, _classPath, JavaScriptEngine.getParentLoader(ctx));
|
||||
_class = JavaScriptEngine.parseMain(loader, ctx);
|
||||
}
|
||||
return JavaScriptEngine.evalClass(_class, ctx);
|
||||
@@ -111,7 +111,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
@Override
|
||||
public Object eval(String str, ScriptContext ctx) throws ScriptException
|
||||
{
|
||||
Class<?> clazz = parse(str, ctx);
|
||||
final Class<?> clazz = parse(str, ctx);
|
||||
return evalClass(clazz, ctx);
|
||||
}
|
||||
|
||||
@@ -149,9 +149,9 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
|
||||
private Class<?> parse(String str, ScriptContext ctx) throws ScriptException
|
||||
{
|
||||
String fileName = getFileName(ctx);
|
||||
String sourcePath = getSourcePath(ctx);
|
||||
String classPath = getClassPath(ctx);
|
||||
final String fileName = getFileName(ctx);
|
||||
final String sourcePath = getSourcePath(ctx);
|
||||
final String classPath = getClassPath(ctx);
|
||||
|
||||
Writer err = ctx.getErrorWriter();
|
||||
if (err == null)
|
||||
@@ -159,7 +159,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
err = new StringWriter();
|
||||
}
|
||||
|
||||
Map<String, byte[]> classBytes = compiler.compile(fileName, str, err, sourcePath, classPath);
|
||||
final Map<String, byte[]> classBytes = compiler.compile(fileName, str, err, sourcePath, classPath);
|
||||
|
||||
if (classBytes == null)
|
||||
{
|
||||
@@ -171,19 +171,19 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
}
|
||||
|
||||
// create a ClassLoader to load classes from MemoryJavaFileManager
|
||||
MemoryClassLoader loader = new MemoryClassLoader(classBytes, classPath, getParentLoader(ctx));
|
||||
final MemoryClassLoader loader = new MemoryClassLoader(classBytes, classPath, getParentLoader(ctx));
|
||||
return parseMain(loader, ctx);
|
||||
}
|
||||
|
||||
protected static Class<?> parseMain(MemoryClassLoader loader, ScriptContext ctx) throws ScriptException
|
||||
{
|
||||
String mainClassName = getMainClassName(ctx);
|
||||
final String mainClassName = getMainClassName(ctx);
|
||||
if (mainClassName != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
Class<?> clazz = loader.load(mainClassName);
|
||||
Method mainMethod = findMainMethod(clazz);
|
||||
final Class<?> clazz = loader.load(mainClassName);
|
||||
final Method mainMethod = findMainMethod(clazz);
|
||||
if (mainMethod == null)
|
||||
{
|
||||
throw new ScriptException("no main method in " + mainClassName);
|
||||
@@ -209,7 +209,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
}
|
||||
|
||||
// search for class with main method
|
||||
Class<?> c = findMainClass(classes);
|
||||
final Class<?> c = findMainClass(classes);
|
||||
if (c != null)
|
||||
{
|
||||
return c;
|
||||
@@ -217,7 +217,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
|
||||
// if class with "main" method, then
|
||||
// return first class
|
||||
Iterator<Class<?>> itr = classes.iterator();
|
||||
final Iterator<Class<?>> itr = classes.iterator();
|
||||
if (itr.hasNext())
|
||||
{
|
||||
return itr.next();
|
||||
@@ -227,9 +227,9 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
|
||||
private JavaCompiledScript compile(String str, ScriptContext ctx) throws ScriptException
|
||||
{
|
||||
String fileName = getFileName(ctx);
|
||||
String sourcePath = getSourcePath(ctx);
|
||||
String classPath = getClassPath(ctx);
|
||||
final String fileName = getFileName(ctx);
|
||||
final String sourcePath = getSourcePath(ctx);
|
||||
final String classPath = getClassPath(ctx);
|
||||
|
||||
Writer err = ctx.getErrorWriter();
|
||||
if (err == null)
|
||||
@@ -237,7 +237,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
err = new StringWriter();
|
||||
}
|
||||
|
||||
Map<String, byte[]> classBytes = compiler.compile(fileName, str, err, sourcePath, classPath);
|
||||
final Map<String, byte[]> classBytes = compiler.compile(fileName, str, err, sourcePath, classPath);
|
||||
if (classBytes == null)
|
||||
{
|
||||
if (err instanceof StringWriter)
|
||||
@@ -255,10 +255,10 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
// find a public class with public static main method
|
||||
for (Class<?> clazz : classes)
|
||||
{
|
||||
int modifiers = clazz.getModifiers();
|
||||
final int modifiers = clazz.getModifiers();
|
||||
if (Modifier.isPublic(modifiers))
|
||||
{
|
||||
Method mainMethod = findMainMethod(clazz);
|
||||
final Method mainMethod = findMainMethod(clazz);
|
||||
if (mainMethod != null)
|
||||
{
|
||||
return clazz;
|
||||
@@ -270,7 +270,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
// has public static main method
|
||||
for (Class<?> clazz : classes)
|
||||
{
|
||||
Method mainMethod = findMainMethod(clazz);
|
||||
final Method mainMethod = findMainMethod(clazz);
|
||||
if (mainMethod != null)
|
||||
{
|
||||
return clazz;
|
||||
@@ -286,11 +286,11 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
{
|
||||
try
|
||||
{
|
||||
Method mainMethod = clazz.getMethod("main", new Class[]
|
||||
final Method mainMethod = clazz.getMethod("main", new Class[]
|
||||
{
|
||||
String[].class
|
||||
});
|
||||
int modifiers = mainMethod.getModifiers();
|
||||
final int modifiers = mainMethod.getModifiers();
|
||||
if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers))
|
||||
{
|
||||
return mainMethod;
|
||||
@@ -307,11 +307,11 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
{
|
||||
try
|
||||
{
|
||||
Method setCtxMethod = clazz.getMethod("setScriptContext", new Class[]
|
||||
final Method setCtxMethod = clazz.getMethod("setScriptContext", new Class[]
|
||||
{
|
||||
ScriptContext.class
|
||||
});
|
||||
int modifiers = setCtxMethod.getModifiers();
|
||||
final int modifiers = setCtxMethod.getModifiers();
|
||||
if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers))
|
||||
{
|
||||
return setCtxMethod;
|
||||
@@ -325,7 +325,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
|
||||
private static String getFileName(ScriptContext ctx)
|
||||
{
|
||||
int scope = ctx.getAttributesScope("javax.script.filename");
|
||||
final int scope = ctx.getAttributesScope("javax.script.filename");
|
||||
if (scope != -1)
|
||||
{
|
||||
return ctx.getAttribute("javax.script.filename", scope).toString();
|
||||
@@ -342,10 +342,10 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
|
||||
private static String[] getArguments(ScriptContext ctx)
|
||||
{
|
||||
int scope = ctx.getAttributesScope(ARGUMENTS);
|
||||
final int scope = ctx.getAttributesScope(ARGUMENTS);
|
||||
if (scope != -1)
|
||||
{
|
||||
Object obj = ctx.getAttribute(ARGUMENTS, scope);
|
||||
final Object obj = ctx.getAttribute(ARGUMENTS, scope);
|
||||
if (obj instanceof String[])
|
||||
{
|
||||
return (String[]) obj;
|
||||
@@ -359,7 +359,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
|
||||
private static String getSourcePath(ScriptContext ctx)
|
||||
{
|
||||
int scope = ctx.getAttributesScope(SOURCEPATH);
|
||||
final int scope = ctx.getAttributesScope(SOURCEPATH);
|
||||
if (scope != -1)
|
||||
{
|
||||
return ctx.getAttribute(SOURCEPATH).toString();
|
||||
@@ -373,7 +373,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
|
||||
private static String getClassPath(ScriptContext ctx)
|
||||
{
|
||||
int scope = ctx.getAttributesScope(CLASSPATH);
|
||||
final int scope = ctx.getAttributesScope(CLASSPATH);
|
||||
if (scope != -1)
|
||||
{
|
||||
return ctx.getAttribute(CLASSPATH).toString();
|
||||
@@ -392,7 +392,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
|
||||
private static String getMainClassName(ScriptContext ctx)
|
||||
{
|
||||
int scope = ctx.getAttributesScope(MAINCLASS);
|
||||
final int scope = ctx.getAttributesScope(MAINCLASS);
|
||||
if (scope != -1)
|
||||
{
|
||||
return ctx.getAttribute(MAINCLASS).toString();
|
||||
@@ -406,10 +406,10 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
|
||||
protected static ClassLoader getParentLoader(ScriptContext ctx)
|
||||
{
|
||||
int scope = ctx.getAttributesScope(PARENTLOADER);
|
||||
final int scope = ctx.getAttributesScope(PARENTLOADER);
|
||||
if (scope != -1)
|
||||
{
|
||||
Object loader = ctx.getAttribute(PARENTLOADER);
|
||||
final Object loader = ctx.getAttribute(PARENTLOADER);
|
||||
if (loader instanceof ClassLoader)
|
||||
{
|
||||
return (ClassLoader) loader;
|
||||
@@ -428,10 +428,10 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
}
|
||||
try
|
||||
{
|
||||
boolean isPublicClazz = Modifier.isPublic(clazz.getModifiers());
|
||||
final boolean isPublicClazz = Modifier.isPublic(clazz.getModifiers());
|
||||
|
||||
// find the setScriptContext method
|
||||
Method setCtxMethod = findSetScriptContextMethod(clazz);
|
||||
final Method setCtxMethod = findSetScriptContextMethod(clazz);
|
||||
// call setScriptContext and pass current ctx variable
|
||||
if (setCtxMethod != null)
|
||||
{
|
||||
@@ -447,7 +447,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
}
|
||||
|
||||
// find the main method
|
||||
Method mainMethod = findMainMethod(clazz);
|
||||
final Method mainMethod = findMainMethod(clazz);
|
||||
if (mainMethod != null)
|
||||
{
|
||||
if (!isPublicClazz)
|
||||
@@ -457,7 +457,7 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
}
|
||||
|
||||
// get "command line" args for the main method
|
||||
String args[] = getArguments(ctx);
|
||||
final String args[] = getArguments(ctx);
|
||||
|
||||
// call main method
|
||||
mainMethod.invoke(null, new Object[]
|
||||
@@ -479,8 +479,8 @@ public class JavaScriptEngine extends AbstractScriptEngine implements Compilable
|
||||
// read a Reader fully and return the content as string
|
||||
private String readFully(Reader reader) throws ScriptException
|
||||
{
|
||||
char[] arr = new char[8 * 1024]; // 8K at a time
|
||||
StringBuilder buf = new StringBuilder();
|
||||
final char[] arr = new char[8 * 1024]; // 8K at a time
|
||||
final StringBuilder buf = new StringBuilder();
|
||||
int numChars;
|
||||
try
|
||||
{
|
||||
|
||||
@@ -69,7 +69,7 @@ public class JavaScriptEngineFactory implements ScriptEngineFactory
|
||||
@Override
|
||||
public String getMethodCallSyntax(String obj, String m, String... args)
|
||||
{
|
||||
StringBuilder buf = new StringBuilder();
|
||||
final StringBuilder buf = new StringBuilder();
|
||||
buf.append(obj);
|
||||
buf.append('.');
|
||||
buf.append(m);
|
||||
@@ -102,12 +102,12 @@ public class JavaScriptEngineFactory implements ScriptEngineFactory
|
||||
@Override
|
||||
public String getOutputStatement(String toDisplay)
|
||||
{
|
||||
StringBuilder buf = new StringBuilder();
|
||||
final StringBuilder buf = new StringBuilder();
|
||||
buf.append("System.out.print(\"");
|
||||
int len = toDisplay.length();
|
||||
final int len = toDisplay.length();
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
char ch = toDisplay.charAt(i);
|
||||
final char ch = toDisplay.charAt(i);
|
||||
switch (ch)
|
||||
{
|
||||
case 34: // '"'
|
||||
@@ -161,7 +161,7 @@ public class JavaScriptEngineFactory implements ScriptEngineFactory
|
||||
// we generate a Main class with main method
|
||||
// that contains all the given statements
|
||||
|
||||
StringBuilder buf = new StringBuilder();
|
||||
final StringBuilder buf = new StringBuilder();
|
||||
buf.append("class ");
|
||||
buf.append(getClassName());
|
||||
buf.append(" {\n");
|
||||
@@ -183,7 +183,7 @@ public class JavaScriptEngineFactory implements ScriptEngineFactory
|
||||
@Override
|
||||
public ScriptEngine getScriptEngine()
|
||||
{
|
||||
JavaScriptEngine engine = new JavaScriptEngine();
|
||||
final JavaScriptEngine engine = new JavaScriptEngine();
|
||||
engine.setFactory(this);
|
||||
return engine;
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ public final class MemoryClassLoader extends URLClassLoader
|
||||
|
||||
public Iterable<Class<?>> loadAll() throws ClassNotFoundException
|
||||
{
|
||||
List<Class<?>> classes = new ArrayList<>(classBytes.size());
|
||||
final List<Class<?>> classes = new ArrayList<>(classBytes.size());
|
||||
for (String name : classBytes.keySet())
|
||||
{
|
||||
classes.add(loadClass(name));
|
||||
@@ -69,7 +69,7 @@ public final class MemoryClassLoader extends URLClassLoader
|
||||
@Override
|
||||
protected Class<?> findClass(String className) throws ClassNotFoundException
|
||||
{
|
||||
byte buf[] = classBytes.get(className);
|
||||
final byte buf[] = classBytes.get(className);
|
||||
if (buf != null)
|
||||
{
|
||||
// clear the bytes in map -- we don't need it anymore
|
||||
@@ -86,12 +86,12 @@ public final class MemoryClassLoader extends URLClassLoader
|
||||
return new URL[0];
|
||||
}
|
||||
|
||||
List<URL> list = new ArrayList<>();
|
||||
StringTokenizer st = new StringTokenizer(classPath, File.pathSeparator);
|
||||
final List<URL> list = new ArrayList<>();
|
||||
final StringTokenizer st = new StringTokenizer(classPath, File.pathSeparator);
|
||||
while (st.hasMoreTokens())
|
||||
{
|
||||
String token = st.nextToken();
|
||||
File file = new File(token);
|
||||
final String token = st.nextToken();
|
||||
final File file = new File(token);
|
||||
if (file.exists())
|
||||
{
|
||||
try
|
||||
@@ -116,7 +116,7 @@ public final class MemoryClassLoader extends URLClassLoader
|
||||
}
|
||||
}
|
||||
|
||||
URL res[] = new URL[list.size()];
|
||||
final URL res[] = new URL[list.size()];
|
||||
list.toArray(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ public final class MemoryJavaFileManager extends EclipseFileManager
|
||||
public void close() throws IOException
|
||||
{
|
||||
out.close();
|
||||
ByteArrayOutputStream bos = (ByteArrayOutputStream) out;
|
||||
final ByteArrayOutputStream bos = (ByteArrayOutputStream) out;
|
||||
classBytes.put(name, bos.toByteArray());
|
||||
}
|
||||
};
|
||||
@@ -138,7 +138,7 @@ public final class MemoryJavaFileManager extends EclipseFileManager
|
||||
|
||||
static URI toURI(String name)
|
||||
{
|
||||
File file = new File(name);
|
||||
final File file = new File(name);
|
||||
if (file.exists())
|
||||
{
|
||||
return file.toURI();
|
||||
|
||||
Reference in New Issue
Block a user