adds Mosaic_Util.make_all_public_methods_proxy
authorThomas Walker Lynch <eknp9n@reasoningtechnology.com>
Mon, 2 Dec 2024 04:01:58 +0000 (04:01 +0000)
committerThomas Walker Lynch <eknp9n@reasoningtechnology.com>
Mon, 2 Dec 2024 04:01:58 +0000 (04:01 +0000)
developer/javac/Mosaic_Util.java
release/Mosaic.jar

index 3a2cdbf..bb6474f 100644 (file)
@@ -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
+      );
+    }
+  }
+
+
 }
index 576d66e..e264a8a 100644 (file)
Binary files a/release/Mosaic.jar and b/release/Mosaic.jar differ