changeset 1962:e75fc4dc0e22

6899147: java.beans.MetaData should not require JDBC to be present Reviewed-by: malenkov
author alanb
date Tue, 10 Nov 2009 13:09:50 +0000
parents ee7a380d350e
children 18c2b761548a
files src/share/classes/java/beans/MetaData.java
diffstat 1 files changed, 38 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/beans/MetaData.java	Tue Nov 10 10:51:31 2009 +0000
+++ b/src/share/classes/java/beans/MetaData.java	Tue Nov 10 13:09:50 2009 +0000
@@ -42,12 +42,11 @@
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
 
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 
-import java.sql.Timestamp;
-
 import java.util.*;
 
 import javax.swing.Box;
@@ -290,13 +289,44 @@
  * @author Sergey A. Malenkov
  */
 final class java_sql_Timestamp_PersistenceDelegate extends java_util_Date_PersistenceDelegate {
-    protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out) {
-        Timestamp oldTime = (Timestamp)oldInstance;
-        Timestamp newTime = (Timestamp)newInstance;
+    private static final Method getNanosMethod = getNanosMethod();
+
+    private static Method getNanosMethod() {
+        try {
+            Class<?> c = Class.forName("java.sql.Timestamp", true, null);
+            return c.getMethod("getNanos");
+        } catch (ClassNotFoundException e) {
+            return null;
+        } catch (NoSuchMethodException e) {
+            throw new AssertionError(e);
+        }
+    }
 
-        int nanos = oldTime.getNanos();
-        if (nanos != newTime.getNanos()) {
-            out.writeStatement(new Statement(oldTime, "setNanos", new Object[] {nanos}));
+    /**
+     * Invoke Timstamp getNanos.
+     */
+    private static int getNanos(Object obj) {
+        if (getNanosMethod == null)
+            throw new AssertionError("Should not get here");
+        try {
+            return (Integer)getNanosMethod.invoke(obj);
+        } catch (InvocationTargetException e) {
+            Throwable cause = e.getCause();
+            if (cause instanceof RuntimeException)
+                throw (RuntimeException)cause;
+            if (cause instanceof Error)
+                throw (Error)cause;
+            throw new AssertionError(e);
+        } catch (IllegalAccessException iae) {
+            throw new AssertionError(iae);
+        }
+    }
+
+    protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder out) {
+        // assumes oldInstance and newInstance are Timestamps
+        int nanos = getNanos(oldInstance);
+        if (nanos != getNanos(newInstance)) {
+            out.writeStatement(new Statement(oldInstance, "setNanos", new Object[] {nanos}));
         }
     }
 }