diff --git a/lang/src/main/java/patternmatching/Main.java b/lang/src/main/java/patternmatching/Main.java index cdaa70d..dad6add 100644 --- a/lang/src/main/java/patternmatching/Main.java +++ b/lang/src/main/java/patternmatching/Main.java @@ -31,7 +31,7 @@ public class Main { return String.format("Object %s", o); } - private static long[] runNTimes(int numberOfRuns, Object[] toTest, Consumer code) { + private static long[] runNTimes(int numberOfRuns, T[] toTest, Consumer code) { long runs[] = new long[numberOfRuns]; for (int i = 0; i < numberOfRuns; i++) { long startTime = System.nanoTime(); @@ -52,7 +52,7 @@ public class Main { .orWhen(Byte.class::isInstance, b -> "byte " + b) .orWhen(Double.class::isInstance, d -> "double " + d) .orWhen(String.class::isInstance, s -> "String " + s) - .otherwise(o -> "Object " + o); + .otherwise(o -> String.format("Object %s", o)); final int nRuns = 100000; final Statistics formatObjectStats = Statistics.from(runNTimes(nRuns, toTest, Main::formatObject)); diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/xml/StudentXmlDao.java b/persistence/src/main/java/fr/gasser/daoexample/dao/xml/StudentXmlDao.java new file mode 100644 index 0000000..7ff8912 --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/xml/StudentXmlDao.java @@ -0,0 +1,33 @@ +package fr.gasser.daoexample.dao.xml; + +import fr.gasser.daoexample.dao.Dao; +import fr.gasser.daoexample.model.Student; + +import java.util.List; + +public class StudentXmlDao implements Dao { + @Override + public boolean create(Student obj) { + return false; + } + + @Override + public Student find(int id) { + return null; + } + + @Override + public List findAll() { + return null; + } + + @Override + public boolean update(Student obj) { + return false; + } + + @Override + public boolean delete(Student obj) { + return false; + } +} diff --git a/reflection/src/main/java/fr/gasser/reflection/DynamicLoadDemo.java b/reflection/src/main/java/fr/gasser/reflection/DynamicLoadDemo.java index eaa4a18..29aedea 100644 --- a/reflection/src/main/java/fr/gasser/reflection/DynamicLoadDemo.java +++ b/reflection/src/main/java/fr/gasser/reflection/DynamicLoadDemo.java @@ -4,6 +4,7 @@ import javax.tools.JavaCompiler; import javax.tools.ToolProvider; import java.io.File; import java.io.IOException; +import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; @@ -12,13 +13,12 @@ import java.nio.file.Files; import java.util.Arrays; public class DynamicLoadDemo { - /** * The source code to load dynamically */ - private static final String SOURCE = + private static final String SOURCE = "package test;" - + "import static DynamicLoadDemo.*;" + + "import static fr.gasser.reflection.DynamicLoadDemo.*;" + "public class Test implements IDynamicLoad {" + " static { System.out.println(\"Test\"); }" + " @Override public String doSomething() {" @@ -28,7 +28,7 @@ public class DynamicLoadDemo { + " return \"Hello reflection! \" + arg;" + " }" + "}"; - + /** * Represents the contract for the dynamically loaded class */ @@ -42,32 +42,36 @@ public class DynamicLoadDemo { * @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(); + if (!sourceFile.getParentFile().mkdirs()) { + System.err.format("Cannot create %s%n", sourceFile.getCanonicalPath()); + } 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(); + final URLClassLoader classLoader = + URLClassLoader.newInstance(new URL[]{ root.toURI().toURL() }); + final Class cls = Class.forName("test.Test", true, classLoader) + .asSubclass(IDynamicLoad.class); + final Constructor ctor = cls.getConstructor(); + IDynamicLoad instance = ctor.newInstance(); System.out.format( - "Methods declared by the class : %s%n", + "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); + Method method = cls.getDeclaredMethod("doSomethingReflection", String.class); System.out.format("invoking %s.doSomethingReflection()%n", cls.getName()); System.out.println(method.invoke(instance, "With an argument")); } diff --git a/reflection/src/main/java/fr/gasser/reflection/GenericsTest.java b/reflection/src/main/java/fr/gasser/reflection/GenericsTest.java index 64aaf03..5477ae7 100644 --- a/reflection/src/main/java/fr/gasser/reflection/GenericsTest.java +++ b/reflection/src/main/java/fr/gasser/reflection/GenericsTest.java @@ -1,14 +1,16 @@ package fr.gasser.reflection; +import java.util.List; + public class GenericsTest { - private TypeToken typeToken; + private TypeToken typeToken; private T data; - public GenericsTest(T data) { + public GenericsTest(T data, TypeToken typeToken) { this.data = data; - this.typeToken = new TypeToken() {}; + this.typeToken = typeToken; } @Override @@ -20,7 +22,8 @@ public class GenericsTest { } public static void main(String[] args) { - GenericsTest g = new GenericsTest<>(5L); + List longs = List.of(5L); + GenericsTest> g = new GenericsTest<>(longs, new TypeToken>(){}); System.out.println(g); } } diff --git a/reflection/src/main/java/fr/gasser/reflection/TypeToken.java b/reflection/src/main/java/fr/gasser/reflection/TypeToken.java index 59a3121..9095738 100644 --- a/reflection/src/main/java/fr/gasser/reflection/TypeToken.java +++ b/reflection/src/main/java/fr/gasser/reflection/TypeToken.java @@ -3,7 +3,7 @@ package fr.gasser.reflection; import java.lang.reflect.*; import java.util.List; -/* +/** * {@code TypeToken> list = new TypeToken>() {};} */ public class TypeToken { @@ -70,8 +70,9 @@ public class TypeToken { } else { String className = type == null ? "null" : type.getClass().getName(); - throw new IllegalArgumentException("Expected a Class, ParameterizedType, or " - + "GenericArrayType, but <" + type + "> is of type " + className); + throw new IllegalArgumentException(String.format( + "Expected a Class, ParameterizedType, or GenericArrayType, but <%s> is of type %s", type, className + )); } } @@ -92,7 +93,15 @@ public class TypeToken { * Gets type literal for the given {@code Class} instance. */ public static TypeToken get(Class type) { - return new TypeToken(type); + return new TypeToken<>(type); + } + + @Override + public String toString() { + return "TypeToken{" + + "rawType=" + rawType + + ", type=" + type + + '}'; } public static void main(String[] args) {