From 5b25596359469661475fa22cc34eabd1d557347e Mon Sep 17 00:00:00 2001 From: Thibaud Date: Tue, 23 Oct 2018 23:50:20 +0200 Subject: [PATCH] Update dynamic compilation example --- .../reflection/dynamicload/DynamicLoadDemo.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/reflection/src/main/java/fr/gasser/reflection/dynamicload/DynamicLoadDemo.java b/reflection/src/main/java/fr/gasser/reflection/dynamicload/DynamicLoadDemo.java index 44e2305..4126841 100644 --- a/reflection/src/main/java/fr/gasser/reflection/dynamicload/DynamicLoadDemo.java +++ b/reflection/src/main/java/fr/gasser/reflection/dynamicload/DynamicLoadDemo.java @@ -1,5 +1,7 @@ package fr.gasser.reflection.dynamicload; +import javax.tools.JavaCompiler; +import javax.tools.ToolProvider; import java.io.File; import java.io.IOException; import java.lang.reflect.Method; @@ -8,8 +10,6 @@ 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 { @@ -25,15 +25,15 @@ public class DynamicLoadDemo { + " return \"Hello world!\";" + " }" + " public String doSomethingReflection(String arg) {" - + " return \"Hello reflection! \" + \"arg\";" + + " return \"Hello reflection! \" + arg;" + " }" + "}"; /** * Represents the contract for the dynamically loaded class */ - public static interface IDynamicLoad { - public String doSomething(); + public interface IDynamicLoad { + String doSomething(); } /** @@ -67,9 +67,8 @@ public class DynamicLoadDemo { System.out.println(instance.doSomething()); // Invoke methods using the reflection API - Method method = cls.getDeclaredMethod("doSomethingReflection", String.class); - String arg = "argument"; + Method method = cls.getDeclaredMethod("doSomethingReflection", String.class); System.out.format("invoking %s.doSomethingReflection()%n", cls.getName()); - System.out.println(method.invoke(instance, arg)); + System.out.println(method.invoke(instance, "With an argument")); } }