diff --git a/.gitignore b/.gitignore index a1c2a23..8044f1e 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +target/ \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..8c93729 --- /dev/null +++ b/pom.xml @@ -0,0 +1,19 @@ + + + 4.0.0 + fr.gasser + java-cookbook + 1.0-SNAPSHOT + pom + + + reflection + + + + UTF-8 + 1.8 + 1.8 + + + \ No newline at end of file diff --git a/reflection/pom.xml b/reflection/pom.xml new file mode 100644 index 0000000..a646a4e --- /dev/null +++ b/reflection/pom.xml @@ -0,0 +1,11 @@ + + + 4.0.0 + + fr.gasser + java-cookbook + 1.0-SNAPSHOT + + reflection + jar + \ No newline at end of file diff --git a/reflection/src/main/java/fr/gasser/reflection/dynamicload/DynamicLoadDemo.java b/reflection/src/main/java/fr/gasser/reflection/dynamicload/DynamicLoadDemo.java new file mode 100644 index 0000000..44e2305 --- /dev/null +++ b/reflection/src/main/java/fr/gasser/reflection/dynamicload/DynamicLoadDemo.java @@ -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)); + } +}