From: Thomas Walker Lynch Date: Mon, 2 Dec 2024 04:01:58 +0000 (+0000) Subject: adds Mosaic_Util.make_all_public_methods_proxy X-Git-Url: https://git.reasoningtechnology.com/style/static/git-logo.png?a=commitdiff_plain;h=09d16ba463ab71be9855b29a952e03be988f9934;p=Mosaic adds Mosaic_Util.make_all_public_methods_proxy --- diff --git a/developer/javac/Mosaic_Util.java b/developer/javac/Mosaic_Util.java index 3a2cdbf..bb6474f 100644 --- a/developer/javac/Mosaic_Util.java +++ b/developer/javac/Mosaic_Util.java @@ -5,12 +5,16 @@ import java.io.ByteArrayOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.PrintStream; +import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.lang.reflect.Proxy; import java.time.Instant; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.function.Predicate; + public class Mosaic_Util{ // Linear search with a predicate @@ -83,4 +87,40 @@ public class Mosaic_Util{ } } + public static Object make_all_public_methods_proxy( Class class_metadata ) { + try { + // Check if the class is public + int modifiers = class_metadata.getModifiers(); + if (!java.lang.reflect.Modifier.isPublic( modifiers )) { + throw new IllegalAccessException( + "The class " + class_metadata.getName() + " is not public and cannot be proxied." + ); + } + + // Create the proxy + Object proxy = java.lang.reflect.Proxy.newProxyInstance( + class_metadata.getClassLoader() + ,class_metadata.getInterfaces() + ,(proxy_object ,method ,args) -> { + Method original_method = class_metadata.getDeclaredMethod( + method.getName() + ,method.getParameterTypes() + ); + original_method.setAccessible( true ); + Object real_instance = class_metadata.getDeclaredConstructor().newInstance(); + return original_method.invoke( real_instance ,args ); + } + ); + + return proxy; + + } catch (Exception e) { + throw new RuntimeException( + "Failed to create proxy for class: " + class_metadata.getName() + ,e + ); + } + } + + } diff --git a/release/Mosaic.jar b/release/Mosaic.jar index 576d66e..e264a8a 100644 Binary files a/release/Mosaic.jar and b/release/Mosaic.jar differ