[jdom-interest] using JDOM 1.1 on Android's Dalvik VM

Jason Hunter jhunter at servlets.com
Tue Dec 9 16:10:45 PST 2008


Here's a diff for the change I just committed.

http://markmail.org/message/gamwpsmeieovaroq

-jh-

Index: JDOMException.java
===================================================================
RCS file: /home/cvs/jdom/src/java/org/jdom/JDOMException.java,v
retrieving revision 1.24
diff -u -r1.24 JDOMException.java
--- JDOMException.java	10 Nov 2007 05:28:59 -0000	1.24
+++ JDOMException.java	10 Dec 2008 00:02:17 -0000
@@ -291,14 +291,16 @@
              return  
((ExceptionInInitializerError)parent).getException();
          }

-        if (parent instanceof RemoteException) {
-            return ((RemoteException)parent).detail;
+        // The RMI classes are not present in Android's Dalvik VM, so  
we use reflection to access them.
+
+        Throwable nestedException =  
getNestedExceptionFromField(parent, "java.rmi.RemoteException",  
"detail");
+        if (nestedException != null) {
+            return nestedException;
          }

-        // These classes are not part of standard JDK 1.1 or 1.2, so we
-        // use reflection to access them.
+        // These classes are not part of standard JDK 1.1 or 1.2, so  
again we use reflection to access them.

-        Throwable nestedException = getNestedException(parent,  
"javax.naming.NamingException", "getRootCause");
+        nestedException = getNestedException(parent,  
"javax.naming.NamingException", "getRootCause");
          if (nestedException != null) {
              return nestedException;
          }
@@ -336,4 +338,31 @@

          return null;
      }
+
+    // This method is similar to getNestedException() except it looks  
for a field instead
+    // of a method.
+    private static Throwable getNestedExceptionFromField(
+                                 Throwable parent, String className,  
String fieldName) {
+        try {
+            // See if this Throwable is of the desired type, by using  
isAssignableFrom().
+            Class testClass = Class.forName(className);
+            Class objectClass = parent.getClass();
+            if (testClass.isAssignableFrom(objectClass)) {
+                // Use reflection to call the specified method.
+                Class[] argClasses = new Class[0];
+                Field field = testClass.getField(fieldName);
+                return (Throwable)field.get(parent);
+            }
+        }
+        catch(Exception ex) {
+            // Most likely, the desired class is not available in  
this VM. That's fine.
+            // Could be that the named field isn't of type Throwable,  
but that should happen
+            // with proper call usage.
+            // Even if it's caused by something else, we don't want  
to display an error
+            // here, since we're already in the process of trying to  
display the original
+            // error - another error here will just confuse things.
+        }
+
+        return null;
+    }
  }

On Dec 3, 2008, at 9:48 AM, Jason Hunter wrote:

> Yes, I'll be doing it.
>
> -jh-
>
> On Dec 2, 2008, at 6:08 PM, Sean Sullivan wrote:
>
>>
>> ping
>>
>>
>> Is there anybody on the JDOM dev team who can commit this  
>> enhancement to the source tree?
>>
>> Sean
>>
>>
>> On Wed, Nov 26, 2008 at 2:55 PM, Sean Sullivan  
>> <sean at seansullivan.com> wrote:
>> Thanks for the feedback.  Your suggestion sounds good to me.
>>
>> Is there anybody on the JDOM dev team who can commit this enhancement
>> to the source tree?
>>
>> This enhancement would allow Android developers to consume
>> XML web services using JDOM.
>>
>> Sean
>>
>> On Wed, Nov 26, 2008 at 1:27 PM, Laurent Bihanic
>> <laurent.bihanic at atosorigin.com> wrote:
>> >
>> > > Is there a better way to code this?  Any other comments?
>> >
>> > Seems OK but you may wish to stick to the existing way of  
>> handling possibly not available exception classes in JDOMException  
>> by :
>> > - adding a new getNestedException() method e.g.  
>> getNestedExceptionFromField(Throwable, String, String)
>> > - using Class.forName() and isAssignableFrom as in :
>> >            Class testClass = Class.forName(className);
>> >            Class objectClass = parent.getClass();
>> >            if (testClass.isAssignableFrom(objectClass)) {
>> >               ...
>> >
>>
>> _______________________________________________
>> To control your jdom-interest membership:
>> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com
>
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/ 
> youraddr at yourhost.com



More information about the jdom-interest mailing list