Package org.apache.storm.sql.javac
Class CompilingClassLoader
- java.lang.Object
-
- java.lang.ClassLoader
-
- org.apache.storm.sql.javac.CompilingClassLoader
-
public class CompilingClassLoader extends ClassLoader
This is a Java ClassLoader that will attempt to load a class from a string of source code.Example
String className = "com.foo.MyClass"; String classSource = "package com.foo;\n" + "public class MyClass implements Runnable {\n" + " @Override public void run() {\n" + " log(\"Hello world\");\n" + " }\n" + "}"; // Load class from source. ClassLoader classLoader = new CompilingClassLoader( parentClassLoader, className, classSource); Class myClass = classLoader.loadClass(className); // Use it. Runnable instance = (Runnable)myClass.newInstance(); instance.run();
Only one chunk of source can be compiled per instance of CompilingClassLoader. If you need to compile more, create multiple CompilingClassLoader instances. Uses Java 1.6's in built compiler API. If the class cannot be compiled, loadClass() will throw a ClassNotFoundException and log the compile errors to System.err. If you don't want the messages logged, or want to explicitly handle the messages you can provide your ownDiagnosticListener
through {#setDiagnosticListener()}.- See Also:
ClassLoader
,JavaCompiler
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CompilingClassLoader.CompilerException
Thrown when code cannot be compiled.
-
Constructor Summary
Constructors Constructor Description CompilingClassLoader(ClassLoader parent, String className, String sourceCode, DiagnosticListener<JavaFileObject> diagnosticListener)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Class<?>
findClass(String name)
Override ClassLoader's class resolving method.Map<String,ByteArrayOutputStream>
getClasses()
-
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findResource, findResources, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
-
-
-
Constructor Detail
-
CompilingClassLoader
public CompilingClassLoader(ClassLoader parent, String className, String sourceCode, DiagnosticListener<JavaFileObject> diagnosticListener) throws CompilingClassLoader.CompilerException
Constructor.- Parameters:
parent
- Parent classloader to resolve dependencies from.className
- Name of class to compile. eg. "com.foo.MyClass".sourceCode
- Java source for class. e.g. "package com.foo; class MyClass { ... }".diagnosticListener
- Notified of compiler errors (may be null).- Throws:
CompilingClassLoader.CompilerException
-
-
Method Detail
-
getClasses
public Map<String,ByteArrayOutputStream> getClasses()
-
findClass
public Class<?> findClass(String name) throws ClassNotFoundException
Override ClassLoader's class resolving method. Don't call this directly, instead useClassLoader.loadClass(String)
.- Overrides:
findClass
in classClassLoader
- Throws:
ClassNotFoundException
-
-