All:

After reading about Reflection in the Sams book "Java Unleashed" and some trial & error, I found what works. So just in case someone else wants to pass an XMLbeans-generated method as a parameter, here is the solution:

The getClass() method call needs to on a specific object of the generated class, not the array of objects. That is, the 6th line of code below should be changed from:

Class c2 = house.getClass();

to:

Class c2 = house[0].getClass();

This will allow passing a method name (as a String) as a parameter to an intermediate method that, in turn, invokes a call to that named method. In other words, a method to be called can be determined at run-time, not just compile-time. Any source-level debugger must use this.

Gene

PS:  It's truely amazing that 206 methods are generated for the object!

------- Forwarded message -------

Was a Nubie, now I'm just dangerous...

I found I could use a class for more objects if I could pass methods to be called as parameters to my constructor. A lot of postings, how-to atricles and a Sun Tutorial led me to Reflection, aka Class Method. Sample code, which I incorporated, is at <http://java.sun.com/docs/books/tutorial/reflect/object/invoke.html>

This example worked (embedded into a class of mine) before I added my cloning of statements (each is marked below). I get a "NoSuchMethodException" catch from the statement marked "//me -X". "getHOUSENAME" is an XMLBeans generated method and I've used it earlier in the same class.

Here's the code segment (full files attached):

public static String append(String firstWord, String secondWord)   //Sun
 {                                                                  //Sun
    String result = null;                                           //Sun
    String result2 = null;                                          //me
    Class c = String.class;                                         //Sun
    Class c2 = house.getClass();                                    //me
    Class[] parameterTypes = new Class[] {String.class};            //Sun
    Method concatMethod;                                            //Sun
    Method houseGetMethod;                                          //me
    Method houseSetMethod;                                          //me
    Object[] arguments = new Object[] {secondWord};                 //Sun
    System.out.println(nameTextCO.getText());                       //me
    String shortName = nameTextCO.getText();                        //me
    Object[] arguments2 = new Object[] {shortName};                 //me
    try {                                                           //Sun
      concatMethod = c.getMethod("concat", parameterTypes);         //Sun
houseGetMethod = c2.getMethod("getHOUSENAME", null); //me-X
      result = (String) concatMethod.invoke(firstWord, arguments);  //Sun
      result2 = (String) houseGetMethod.invoke(house[0], null);     //me
      System.out.println(result2);                                  //me
    } catch (NoSuchMethodException e) {                             //Sun
        System.out.println(e);                                      //Sun
    } catch (IllegalAccessException e) {                            //Sun
        System.out.println(e);                                      //Sun
    } catch (InvocationTargetException e) {                         //Sun
        System.out.println(e);                                      //Sun
    }                                                               //Sun
    return result;                                                  //Sun
 }

Any suggestions?

Appreciations,
Gene



--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to