using JDOM 1.1 on Android's Dalvik VM

View: New views
6 Messages — Rating Filter:   Alert me  

using JDOM 1.1 on Android's Dalvik VM

by Sean Sullivan-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I'm trying to use JDOM 1.1 on the Android 1.0 platform. 

The Android platform supports most Java 5 API's.  However, java.rmi.* classes are not part of the Android platform.

When I use JDOM 1.1 on Android, I see this error:


W/dalvikvm(  327): VFY: unable to resolve check-cast 98 (Ljava/rmi/RemoteException;) in Lorg/jdom/JDOMException;
W/dalvikvm(  327): VFY:  rejecting opcode 0x1f at 0x003d
W/dalvikvm(  327): VFY:  rejected Lorg/jdom/JDOMException;.getNestedException (Ljava/lang/Throwable;)Ljava/lang/Throwable;
W/dalvikvm(  327): Verifier rejected class Lorg/jdom/JDOMException;
D/AndroidRuntime(  327): Shutting down VM
W/dalvikvm(  327): threadid=3: thread exiting with uncaught exception (group=0x40010e28)
E/AndroidRuntime(  327): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(  327): java.lang.VerifyError: org.jdom.JDOMException


I looked at the JDOM source code and noticed that org.jdom.JDOMException uses java.rmi.RemoteException:

        if (parent instanceof RemoteException) {
            return ((RemoteException)parent).detail;
        }


In order for this code to run on Android, we would need to eliminate java.rmi.RemoteException from JDOMException.java

One possible fix (?) is to use reflection to retrieve the "detail" field:

        if (parent.getClass().getName().startsWith("java.rmi.")) {
            try    {
                Field f = parent.getClass().getField("detail");
                return (Throwable) f.get(parent);
            } catch (Exception ignore) {
                // ignored
            }
        }

Using 'startsWith' is a hack. The intent is to detect java.rmi.RemoteException as well as all
subclasses of java.rmi.RemoteException from the java.rmi pacakge.

  http://java.sun.com/j2se/1.4.2/docs/api/java/rmi/RemoteException.html

Is there a better way to code this?  Any other comments?

Sean


_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...

Re: using JDOM 1.1 on Android's Dalvik VM

by Laurent Bihanic :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

 > 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)) {
                ...

Laurent


Sean Sullivan wrote:

>
> I'm trying to use JDOM 1.1 on the Android 1.0 platform.
>
> The Android platform supports most Java 5 API's.  However, java.rmi.*
> classes are not part of the Android platform.
>
> When I use JDOM 1.1 on Android, I see this error:
>
>
> W/dalvikvm(  327): VFY: unable to resolve check-cast 98
> (Ljava/rmi/RemoteException;) in Lorg/jdom/JDOMException;
> W/dalvikvm(  327): VFY:  rejecting opcode 0x1f at 0x003d
> W/dalvikvm(  327): VFY:  rejected
> Lorg/jdom/JDOMException;.getNestedException
> (Ljava/lang/Throwable;)Ljava/lang/Throwable;
> W/dalvikvm(  327): Verifier rejected class Lorg/jdom/JDOMException;
> D/AndroidRuntime(  327): Shutting down VM
> W/dalvikvm(  327): threadid=3: thread exiting with uncaught exception
> (group=0x40010e28)
> E/AndroidRuntime(  327): Uncaught handler: thread main exiting due to
> uncaught exception
> E/AndroidRuntime(  327): java.lang.VerifyError: org.jdom.JDOMException
>
>
> I looked at the JDOM source code and noticed that org.jdom.JDOMException
> uses java.rmi.RemoteException:
>
>         if (parent instanceof RemoteException) {
>             return ((RemoteException)parent).detail;
>         }
>
>
> In order for this code to run on Android, we would need to eliminate
> java.rmi.RemoteException from JDOMException.java
>
> One possible fix (?) is to use reflection to retrieve the "detail" field:
>
>         if (parent.getClass().getName().startsWith("java.rmi.")) {
>             try    {
>                 Field f = parent.getClass().getField("detail");
>                 return (Throwable) f.get(parent);
>             } catch (Exception ignore) {
>                 // ignored
>             }
>         }
>
> Using 'startsWith' is a hack. The intent is to detect
> java.rmi.RemoteException as well as all
> subclasses of java.rmi.RemoteException from the java.rmi pacakge.
>
>   http://java.sun.com/j2se/1.4.2/docs/api/java/rmi/RemoteException.html
>
> Is there a better way to code this?  Any other comments?
>
> Sean
_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...

Re: using JDOM 1.1 on Android's Dalvik VM

by Sean Sullivan-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...> 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@...

Re: using JDOM 1.1 on Android's Dalvik VM

by Sean Sullivan-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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@...> 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@...> 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@...

Re: using JDOM 1.1 on Android's Dalvik VM

by jhunter :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...> 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@...> 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@...

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...

Re: using JDOM 1.1 on Android's Dalvik VM

by jhunter :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...> 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@...> 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@...
>
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/ 
> youraddr@...

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@...