Add dynamic class compilation example
This commit is contained in:
parent
1869975ccd
commit
e533d9de63
2
.gitignore
vendored
2
.gitignore
vendored
@ -21,3 +21,5 @@
|
|||||||
|
|
||||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||||
hs_err_pid*
|
hs_err_pid*
|
||||||
|
|
||||||
|
target/
|
19
pom.xml
Normal file
19
pom.xml
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>fr.gasser</groupId>
|
||||||
|
<artifactId>java-cookbook</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>reflection</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
11
reflection/pom.xml
Normal file
11
reflection/pom.xml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>fr.gasser</groupId>
|
||||||
|
<artifactId>java-cookbook</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<artifactId>reflection</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
</project>
|
@ -0,0 +1,75 @@
|
|||||||
|
package fr.gasser.reflection.dynamicload;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLClassLoader;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import javax.tools.JavaCompiler;
|
||||||
|
import javax.tools.ToolProvider;
|
||||||
|
|
||||||
|
public class DynamicLoadDemo {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The source code to load dynamically
|
||||||
|
*/
|
||||||
|
private static final String SOURCE =
|
||||||
|
"package test;"
|
||||||
|
+ "import static fr.gasser.reflection.dynamicload.DynamicLoadDemo.*;"
|
||||||
|
+ "public class Test implements IDynamicLoad {"
|
||||||
|
+ " static { System.out.println(\"Test\"); }"
|
||||||
|
+ " @Override public String doSomething() {"
|
||||||
|
+ " return \"Hello world!\";"
|
||||||
|
+ " }"
|
||||||
|
+ " public String doSomethingReflection(String arg) {"
|
||||||
|
+ " return \"Hello reflection! \" + \"arg\";"
|
||||||
|
+ " }"
|
||||||
|
+ "}";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the contract for the dynamically loaded class
|
||||||
|
*/
|
||||||
|
public static interface IDynamicLoad {
|
||||||
|
public String doSomething();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param args the command line arguments
|
||||||
|
* @throws java.io.IOException
|
||||||
|
* @throws java.lang.ReflectiveOperationException
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) throws IOException, ReflectiveOperationException {
|
||||||
|
|
||||||
|
// Save source in .java file.
|
||||||
|
final File root = Files.createTempDirectory(null).toFile();
|
||||||
|
final File sourceFile = new File(root, "test/Test.java");
|
||||||
|
sourceFile.getParentFile().mkdirs();
|
||||||
|
Files.write(sourceFile.toPath(), SOURCE.getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
|
// Compile source file.
|
||||||
|
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||||
|
compiler.run(null, null, null, sourceFile.getPath());
|
||||||
|
|
||||||
|
// Load and instantiate compiled class.
|
||||||
|
final URLClassLoader classLoader =
|
||||||
|
URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
|
||||||
|
final Class<?> cls = Class.forName("test.Test", true, classLoader);
|
||||||
|
IDynamicLoad instance = (IDynamicLoad) cls.newInstance();
|
||||||
|
System.out.format(
|
||||||
|
"Methods declared by the class : %s%n",
|
||||||
|
Arrays.toString(cls.getDeclaredMethods())
|
||||||
|
);
|
||||||
|
|
||||||
|
// Invoke methods of the contract IDynamicLoad
|
||||||
|
System.out.println(instance.doSomething());
|
||||||
|
|
||||||
|
// Invoke methods using the reflection API
|
||||||
|
Method method = cls.getDeclaredMethod("doSomethingReflection", String.class);
|
||||||
|
String arg = "argument";
|
||||||
|
System.out.format("invoking %s.doSomethingReflection()%n", cls.getName());
|
||||||
|
System.out.println(method.invoke(instance, arg));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user