You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bsf-dev@jakarta.apache.org by "Rony G. Flatscher" <Ro...@wu-wien.ac.at> on 2003/02/14 00:41:11 UTC

Patch of "EngineUtils.java" to allow invoking public methods of inner classes

Hi there,

this is the "diff -uw" of the changes applied to 
com.ibm.bsf.util.EngineUtils.java (it depicts the already reported 
char-patch as well). This version uses reflection to find out whether 
setAccessible() is available (so it is not dependent on the official 
java version, thanks to Igor to point out that importance!).

Regards,

---rony

----------------------------- cut here ------------------------
--- bkp\EngineUtils.java    2001-01-30 17:46:18.000000000 +0100
+++ EngineUtils.java    2003-02-13 22:31:28.000000000 +0100
@@ -20,6 +20,23 @@
   // temp directory
   static BSFClassLoader bsfCL;
 
+  // ---rgf, 2003-02-13, determine whether changing accessibility of 
Methods is possible
+  static boolean bMethodHasSetAccessible=false;
+
+  // ---rgf, 2003-02-13, determine whether changing accessibility of 
Methods is possible
+  static {
+      Class mc=Method.class;            // get the "Method" class object
+      Class arg[]={boolean.class};      // define an array with the 
primitive "boolean" pseudo class object
+      try {
+          Object o=mc.getMethod("setAccessible", arg ); // is this 
method available?
+          bMethodHasSetAccessible=true; // no exception, hence method 
exists
+      }
+      catch (Exception e)
+      {
+          bMethodHasSetAccessible=false;// exception occurred, hence 
method does not exist
+      }
+  }
+
   
//////////////////////////////////////////////////////////////////////////
 
   /**
@@ -137,6 +154,8 @@
           else if(args[i] instanceof Double ) argTypes[i]= double.class;
         } else if (args[i] instanceof Boolean) {
           argTypes[i] = boolean.class;
+            } else if (args[i] instanceof Character) {
+              argTypes[i] = char.class;
         }
       }
       m = MethodUtils.getMethod (beanClass, methodName, argTypes,
@@ -147,6 +166,14 @@
     }
       }
 
+      // in order to allow access to public methods of inner classes on 
Java 1.2, 1.3, 1.4
+      // one needs to shut off the access-check; otherwise an 
"IllegalAccessException" is thrown
+      // ---rgf, 2003-02-13 (no general solution for Java 1.1)
+      if ( bMethodHasSetAccessible && 
Modifier.isPublic(m.getModifiers()) ) // available since Java 1.2
+      {
+         m.setAccessible(true);  // ---rgf, 2003-02-13, no access 
checks for this method!
+      }
+
       // call it, and return the result
       return m.invoke (bean, args);
     } catch (Exception e) {
----------------------------- cut here ------------------------

With the above patch the following Object Rexx program now works 
flawlessly and dumps the System properties:

----------------------------- cut here ------------------------
/* Object Rexx (message operator is the tilde: ~) */
/* this version works on Java 1.2 and higher, *not* on 1.1 (see other 
variants which work on Java 1.1) */

system=.bsf.cls ~ Class.JC ~ forName("string", "java.lang.System")  -- 
get the "System" class object

properties=system~getProperties     -- get the System properties
enum=properties~propertyNames       -- get an enumeration of the 
property names
say copies("=", 70)

do while enum~hasMoreElements       -- loop over enumeration
   key=enum~nextElement             -- get next element
   say "key:" left("["key"]", 30) "value: 
["properties~getProperty("String", key)"]"
end


::requires "BSF.cls" -- get the Object Rexx support for bsf4rexx
----------------------------- cut here ------------------------

If no more errors pop up with the Rexx and Object Rexx support until the 
end of February, I will adapt the Rexx engine to Apache BSF and apply 
the necessary licenses.