changing mapping of methods checkpoint
authorThomas Walker Lynch <eknp9n@reasoningtechnology.com>
Fri, 20 Dec 2024 02:50:46 +0000 (02:50 +0000)
committerThomas Walker Lynch <eknp9n@reasoningtechnology.com>
Fri, 20 Dec 2024 02:50:46 +0000 (02:50 +0000)
developer/javacđź–‰/Mosaic_Dispatcher.java
release/Mosaic.jar
tester/javacđź–‰/Dispatcher_0.java
tester/javacđź–‰/Dispatcher_2.java
tester/javacđź–‰/TestClasses_2.java

index 5c83a19..8cc567b 100644 (file)
@@ -230,6 +230,7 @@ class MethodSignature_To_Handle_Map{
             MethodType method_type=MethodType.methodType(method.getReturnType(),parameter_type_list);
             MethodHandle method_handle;
 
+            /* throws access exception due to public methods of private classes going down the public method branch
             if((method.getModifiers() & Modifier.STATIC) != 0){
               if((method.getModifiers() & Modifier.PRIVATE) != 0){
                 // Private static method
@@ -245,6 +246,12 @@ class MethodSignature_To_Handle_Map{
               // Public or protected instance method
               method_handle = lookup.findVirtual(class_metadata, method.getName(), method_type);
             }
+            */
+            if((method.getModifiers() & Modifier.STATIC) != 0){
+              method_handle = private_lookup.findStatic(class_metadata, method.getName(), method_type);
+            }else{
+              method_handle = private_lookup.findSpecial(class_metadata, method.getName(), method_type, class_metadata);
+            }
 
             add_entry(signature,method_handle);
 
@@ -384,7 +391,7 @@ public class Mosaic_Dispatcher{
     public Mosaic_Dispatcher(String fully_qualified_class_name) throws ClassNotFoundException{
       this.map = new MethodSignature_To_Handle_Map();
       this.target = Class.forName(fully_qualified_class_name);
-      test_print("Mosaic_Dispatcher:: mapping methods from class specified by string:" + to_string_target());
+      test_print("Mosaic_Dispatcher:: mapping methods from class specified by string: \"" + to_string_target() + "\"");
       this.map.add_methods(target);
       this.map.add_constructors(target);
     }
@@ -430,6 +437,40 @@ public class Mosaic_Dispatcher{
       }
     }
 
+    public <T> void write(Object instance, String field_name, T value){
+      try{
+        test_print("Call to Mosaic_Dispatcher::write");
+
+        // Private field lookup
+        MethodHandles.Lookup lookup=MethodHandles.privateLookupIn(target,MethodHandles.lookup());
+        Field field=target.getDeclaredField(field_name);
+
+        // Access the field using the lookup handle
+        MethodHandle handle;
+        if((field.getModifiers() & Modifier.STATIC) != 0){
+          // Static field
+          handle=lookup.unreflectSetter(field);
+          handle.invoke(value);
+        }else{
+          // Instance-bound field
+          if(instance == null || !target.isInstance(instance)){
+            throw new IllegalArgumentException(
+              "Provided instance is not of target type: "+target.getName()
+              +", but received: "+(instance == null ? "null" : instance.getClass().getName())
+            );
+          }
+          handle=lookup.unreflectSetter(field);
+          handle.bindTo(instance).invoke(value);
+        }
+      }catch(NoSuchFieldException | IllegalAccessException e){
+        System.out.println("Mosaic_Dispatcher::write_field exception:");
+        e.printStackTrace();
+      }catch(Throwable t){
+        System.out.println("Mosaic_Dispatcher::write_field exception:");
+        t.printStackTrace();
+      }
+    }
+
     @SuppressWarnings("unchecked")
     public <T> T make(Object... arg_list){
       test_print("Call to Mosaic_Dispatcher::make");
index a7c41b7..18cae87 100644 (file)
Binary files a/release/Mosaic.jar and b/release/Mosaic.jar differ
index 491b8f4..869f6ff 100644 (file)
@@ -72,6 +72,29 @@ public class Dispatcher_0{
     return result;
   }
 
+  public static boolean test_defaultClass_access(){
+    try{
+      Mosaic_Dispatcher d2=new Mosaic_Dispatcher("tester.DefaultTestClass_01");
+      Object instance=d2.make();
+      boolean result1=d2.dispatch(
+        instance   // target instance
+        ,boolean.class  // return type
+        ,"a_public_method_7"  // public method name
+      );
+
+      boolean result2=d2.dispatch(
+        instance   // target instance
+        ,boolean.class  // return type
+        ,"a_private_method_8"  // private method name
+      );
+
+      return result1 && result2;
+    }catch(Throwable t){
+      t.printStackTrace();
+      return false;
+    }
+  }
+
   // Extend the run method to include static method tests
   public static boolean run(){
     try{
@@ -113,6 +136,15 @@ public class Dispatcher_0{
         result = false;
       }
 
+      System.out.println("");
+      System.out.println("running test: defaultClass_access");
+      if(Boolean.TRUE.equals(test_defaultClass_access())){
+        System.out.println("passed");
+      }else{
+        System.out.println("FAILED");
+        result=false;
+      }
+
       System.out.println("");
       return result;
 
index f5aefe3..caf8672 100644 (file)
@@ -7,13 +7,14 @@ public class Dispatcher_2{
   private static Mosaic_Dispatcher dispatcher;
 
   static{
-    dispatcher = new Mosaic_Dispatcher(TestClasses_2.class);
+    TestClasses_2.initialize_static_fields();
+    dispatcher=new Mosaic_Dispatcher(TestClasses_2.class);
   }
 
   public static boolean test_publicStaticField(){
     try{
-      Integer value = dispatcher.read(Integer.class, "i_200");
-      return value != null && value == 200; // Replace 200 with initialized value
+      Integer value=dispatcher.read(Integer.class,"i_200");
+      return value != null && value == 200;
     }catch(Throwable t){
       t.printStackTrace();
       return false;
@@ -22,8 +23,8 @@ public class Dispatcher_2{
 
   public static boolean test_privateStaticField(){
     try{
-      String value = dispatcher.read(String.class, "s_201");
-      return value != null && value.equals("Test"); // Replace "Test" with initialized value
+      String value=dispatcher.read(String.class,"s_201");
+      return value != null && value.equals("Static Private String");
     }catch(Throwable t){
       t.printStackTrace();
       return false;
@@ -32,9 +33,10 @@ public class Dispatcher_2{
 
   public static boolean test_publicInstanceField(){
     try{
-      TestClasses_2 instance = dispatcher.make();
-      Integer value = dispatcher.read(instance, "i_202");
-      return value != null && value == 202; // Replace 202 with initialized value
+      TestClasses_2 instance=dispatcher.make();
+      instance.initialize_instance_fields();
+      Integer value=dispatcher.read(instance,"i_202");
+      return value != null && value == 202;
     }catch(Throwable t){
       t.printStackTrace();
       return false;
@@ -43,9 +45,56 @@ public class Dispatcher_2{
 
   public static boolean test_privateInstanceField(){
     try{
-      TestClasses_2 instance = dispatcher.make();
-      Integer value = dispatcher.read(instance, "i_203");
-      return value != null && value == 203; // Replace 203 with initialized value
+      TestClasses_2 instance=dispatcher.make();
+      instance.initialize_instance_fields();
+      Integer value=dispatcher.read(instance,"i_203");
+      return value != null && value == 203;
+    }catch(Throwable t){
+      t.printStackTrace();
+      return false;
+    }
+  }
+
+  public static boolean test_writePublicStaticField(){
+    try{
+      dispatcher.write(null,"i_200",300);
+      Integer value=dispatcher.read(Integer.class,"i_200");
+      return value != null && value == 300;
+    }catch(Throwable t){
+      t.printStackTrace();
+      return false;
+    }
+  }
+
+  public static boolean test_writePrivateStaticField(){
+    try{
+      dispatcher.write(null,"s_201","New Static Private String");
+      String value=dispatcher.read(String.class,"s_201");
+      return value != null && value.equals("New Static Private String");
+    }catch(Throwable t){
+      t.printStackTrace();
+      return false;
+    }
+  }
+
+  public static boolean test_writePublicInstanceField(){
+    try{
+      TestClasses_2 instance=dispatcher.make();
+      dispatcher.write(instance,"i_202",400);
+      Integer value=dispatcher.read(instance,"i_202");
+      return value != null && value == 400;
+    }catch(Throwable t){
+      t.printStackTrace();
+      return false;
+    }
+  }
+
+  public static boolean test_writePrivateInstanceField(){
+    try{
+      TestClasses_2 instance=dispatcher.make();
+      dispatcher.write(instance,"i_203",500);
+      Integer value=dispatcher.read(instance,"i_203");
+      return value != null && value == 500;
     }catch(Throwable t){
       t.printStackTrace();
       return false;
@@ -54,15 +103,16 @@ public class Dispatcher_2{
 
   public static boolean run(){
     try{
-      boolean result = true;
+      boolean result=true;
 
+      // Existing read tests
       System.out.println("");
       System.out.println("running test: publicStaticField");
       if(Boolean.TRUE.equals(test_publicStaticField())){
         System.out.println("PASSED");
       }else{
         System.out.println("FAILED");
-        result = false;
+        result=false;
       }
 
       System.out.println("");
@@ -71,7 +121,7 @@ public class Dispatcher_2{
         System.out.println("PASSED");
       }else{
         System.out.println("FAILED");
-        result = false;
+        result=false;
       }
 
       System.out.println("");
@@ -80,7 +130,7 @@ public class Dispatcher_2{
         System.out.println("PASSED");
       }else{
         System.out.println("FAILED");
-        result = false;
+        result=false;
       }
 
       System.out.println("");
@@ -89,7 +139,44 @@ public class Dispatcher_2{
         System.out.println("PASSED");
       }else{
         System.out.println("FAILED");
-        result = false;
+        result=false;
+      }
+
+      // New write tests
+      System.out.println("");
+      System.out.println("running test: writePublicStaticField");
+      if(Boolean.TRUE.equals(test_writePublicStaticField())){
+        System.out.println("PASSED");
+      }else{
+        System.out.println("FAILED");
+        result=false;
+      }
+
+      System.out.println("");
+      System.out.println("running test: writePrivateStaticField");
+      if(Boolean.TRUE.equals(test_writePrivateStaticField())){
+        System.out.println("PASSED");
+      }else{
+        System.out.println("FAILED");
+        result=false;
+      }
+
+      System.out.println("");
+      System.out.println("running test: writePublicInstanceField");
+      if(Boolean.TRUE.equals(test_writePublicInstanceField())){
+        System.out.println("PASSED");
+      }else{
+        System.out.println("FAILED");
+        result=false;
+      }
+
+      System.out.println("");
+      System.out.println("running test: writePrivateInstanceField");
+      if(Boolean.TRUE.equals(test_writePrivateInstanceField())){
+        System.out.println("PASSED");
+      }else{
+        System.out.println("FAILED");
+        result=false;
       }
 
       System.out.println("");
@@ -102,16 +189,6 @@ public class Dispatcher_2{
     }
   }
 
-  public static boolean logPass(){
-    System.out.println("PASSED");
-    return true;
-  }
-
-  public static boolean logFail(){
-    System.out.println("FAILED");
-    return false;
-  }
-
   public static void main(String[] args){
     if(run()){
       System.exit(0);
@@ -120,3 +197,4 @@ public class Dispatcher_2{
     }
   }
 }
+
index b576f0f..42a6e82 100644 (file)
@@ -1,6 +1,7 @@
 package tester;
 
-public class TestClasses_2 {
+public class TestClasses_2{
+
   // Static fields
   public static int i_200;
   private static String s_201;
@@ -10,71 +11,33 @@ public class TestClasses_2 {
   private Integer i_203;
 
   // Nested class
-  public static class Class_Nested_21 {
+  public static class Class_Nested_21{
     public static Integer i_210;
     private static String s_211;
 
     public Integer i_212;
     private Integer i_213;
 
-    public static void initialize_static_data() {
-      i_210 = 210;
-      s_211 = "Static Nested Private String";
+    public static void initialize_static_fields(){
+      i_210=210;
+      s_211="Static Nested Private String";
     }
 
-    public void initialize_instance_data() {
-      i_212 = 212;
-      i_213 = 213;
+    public void initialize_instance_fields(){
+      i_212=212;
+      i_213=213;
     }
   }
 
-  public static void initialize_static_data() {
-    i_200 = 200;
-    s_201 = "Static Private String";
+  public static void initialize_static_fields(){
+    i_200=200;
+    s_201="Static Private String";
   }
 
-  public void initialize_instance_data() {
-    i_202 = 202;
-    i_203 = 203;
+  public void initialize_instance_fields(){
+    i_202=202;
+    i_203=203;
   }
 }
 
-// Default (package-private) class
-class DefaultTestClass {
-  // Static fields
-  public static double d_300;
-  private static boolean b_301;
-
-  // Instance fields
-  public Float f_302;
-  private Long l_303;
-
-  // Nested class
-  public static class Class_Nested_31 {
-    public static Character c_310;
-    private static String s_311;
-
-    public Byte b_312;
-    private Short s_313;
-
-    public static void initialize_static_data() {
-      c_310 = 'C';
-      s_311 = "Default Static Nested Private String";
-    }
-
-    public void initialize_instance_data() {
-      b_312 = (byte) 12;
-      s_313 = (short) 313;
-    }
-  }
 
-  public static void initialize_static_data() {
-    d_300 = 300.5;
-    b_301 = true;
-  }
-
-  public void initialize_instance_data() {
-    f_302 = 302.5f;
-    l_303 = 303L;
-  }
-}