Enum CompilerUtils
- All Implemented Interfaces:
Serializable, Comparable<CompilerUtils>
This class support loading and debugging Java Classes dynamically.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final CachedCompilerstatic final booleanprivate static final Methodprivate static final Stringprivate static final org.slf4j.Logger(package private) static JavaCompiler(package private) static StandardJavaFileManagerprivate static final Charset -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanaddClassPath(@NotNull String dir) Add a directory to the class path for compiling.private static voidprivate static @NotNull StringdecodeUTF8(@NotNull byte[] bytes) static voiddefineClass(@NotNull String className, @NotNull byte[] bytes) Define a class for byte code.static ClassdefineClass(@Nullable ClassLoader classLoader, @NotNull String className, @NotNull byte[] bytes) Define a class for byte code.private static @NotNull byte[]encodeUTF8(@NotNull String text) private static @NotNull InputStreamgetInputStream(@NotNull String filename) private static booleanisDebug()private static ClassloadFromJava(@NotNull String className, @NotNull String javaCode) Load a java class from text.static ClassloadFromResource(@NotNull String className, @NotNull String resourceName) Load a java class file from the classpath or local file system.private static @Nullable byte[]private static Stringprivate static voidreset()static CompilerUtilsReturns the enum constant of this type with the specified name.static CompilerUtils[]values()Returns an array containing the constants of this enum type, in the order they are declared.static booleanwriteBytes(@NotNull File file, @NotNull byte[] bytes) static boolean
-
Field Details
-
DEBUGGING
public static final boolean DEBUGGING -
CACHED_COMPILER
-
LOGGER
private static final org.slf4j.Logger LOGGER -
DEFINE_CLASS_METHOD
-
UTF_8
-
JAVA_CLASS_PATH
- See Also:
-
s_compiler
-
s_standardJavaFileManager
-
-
Constructor Details
-
CompilerUtils
private CompilerUtils()
-
-
Method Details
-
values
Returns an array containing the constants of this enum type, in the order they are declared.- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException- if this enum type has no constant with the specified nameNullPointerException- if the argument is null
-
isDebug
private static boolean isDebug() -
reset
private static void reset() -
loadFromResource
public static Class loadFromResource(@NotNull @NotNull String className, @NotNull @NotNull String resourceName) throws IOException, ClassNotFoundException Load a java class file from the classpath or local file system.- Parameters:
className- expected class name of the outer class.resourceName- as the full file name with extension.- Returns:
- the outer class loaded.
- Throws:
IOException- the resource could not be loaded.ClassNotFoundException- the class name didn't match or failed to initialise.
-
loadFromJava
private static Class loadFromJava(@NotNull @NotNull String className, @NotNull @NotNull String javaCode) throws ClassNotFoundException Load a java class from text.- Parameters:
className- expected class name of the outer class.javaCode- to compile and load.- Returns:
- the outer class loaded.
- Throws:
ClassNotFoundException- the class name didn't match or failed to initialise.
-
addClassPath
Add a directory to the class path for compiling. This can be required with custom- Parameters:
dir- to add.- Returns:
- whether the directory was found, if not it is not added either.
-
defineClass
Define a class for byte code.- Parameters:
className- expected to load.bytes- of the byte code.
-
defineClass
public static Class defineClass(@Nullable @Nullable ClassLoader classLoader, @NotNull @NotNull String className, @NotNull @NotNull byte[] bytes) Define a class for byte code.- Parameters:
classLoader- to load the class into.className- expected to load.bytes- of the byte code.
-
readText
- Throws:
IOException
-
decodeUTF8
-
readBytes
-
close
-
writeText
-
encodeUTF8
-
writeBytes
-
getInputStream
@NotNull private static @NotNull InputStream getInputStream(@NotNull @NotNull String filename) throws FileNotFoundException - Throws:
FileNotFoundException
-