From 73bd3791052212f8d2ee7069d452dcadffe795cb Mon Sep 17 00:00:00 2001 From: alanb Date: Mon, 16 Nov 2009 18:13:15 +0000 Subject: [PATCH] 6890458: Datatransfer API should not require RMI to be present Reviewed-by: uta --- .../java/awt/datatransfer/DataFlavor.java | 2 +- .../sun/awt/datatransfer/DataTransferer.java | 112 +++++++++++++++++- 2 files changed, 107 insertions(+), 7 deletions(-) diff --git a/src/share/classes/java/awt/datatransfer/DataFlavor.java b/src/share/classes/java/awt/datatransfer/DataFlavor.java index 0e559e5d3..36378882e 100644 --- a/src/share/classes/java/awt/datatransfer/DataFlavor.java +++ b/src/share/classes/java/awt/datatransfer/DataFlavor.java @@ -1184,7 +1184,7 @@ public class DataFlavor implements Externalizable, Cloneable { */ public boolean isRepresentationClassRemote() { - return java.rmi.Remote.class.isAssignableFrom(representationClass); + return DataTransferer.isRemote(representationClass); } /** diff --git a/src/share/classes/sun/awt/datatransfer/DataTransferer.java b/src/share/classes/sun/awt/datatransfer/DataTransferer.java index 521e69102..277d3bd9e 100644 --- a/src/share/classes/sun/awt/datatransfer/DataTransferer.java +++ b/src/share/classes/sun/awt/datatransfer/DataTransferer.java @@ -63,8 +63,6 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.rmi.MarshalledObject; - import java.security.AccessControlContext; import java.security.AccessControlException; import java.security.AccessController; @@ -493,6 +491,13 @@ public abstract class DataTransferer { } } + /** + * Returns {@code true} if the given type is a java.rmi.Remote. + */ + public static boolean isRemote(Class type) { + return RMI.isRemote(type); + } + /** * Returns an Iterator which traverses a SortedSet of Strings which are * a total order of the standard character sets supported by the JRE. The @@ -1360,7 +1365,7 @@ search: // Source data is an RMI object } else if (flavor.isRepresentationClassRemote()) { - MarshalledObject mo = new MarshalledObject(obj); + Object mo = RMI.newMarshalledObject(obj); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(mo); oos.close(); @@ -1671,7 +1676,7 @@ search: try { byte[] ba = inputStreamToByteArray(str); ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(ba)); - Object ret = ((MarshalledObject)(ois.readObject())).get(); + Object ret = RMI.getMarshalledObject(ois.readObject()); ois.close(); str.close(); return ret; @@ -2669,8 +2674,12 @@ search: Integer.valueOf(0)); nonTextRepresentationsMap.put(java.io.Serializable.class, Integer.valueOf(1)); - nonTextRepresentationsMap.put(java.rmi.Remote.class, - Integer.valueOf(2)); + + Class remoteClass = RMI.remoteClass(); + if (remoteClass != null) { + nonTextRepresentationsMap.put(remoteClass, + Integer.valueOf(2)); + } nonTextRepresentations = Collections.unmodifiableMap(nonTextRepresentationsMap); @@ -2900,4 +2909,95 @@ search: } } } + + /** + * A class that provides access to java.rmi.Remote and java.rmi.MarshalledObject + * without creating a static dependency. + */ + private static class RMI { + private static final Class remoteClass = getClass("java.rmi.Remote"); + private static final Class marshallObjectClass = + getClass("java.rmi.MarshalledObject"); + private static final Constructor marshallCtor = + getConstructor(marshallObjectClass, Object.class); + private static final Method marshallGet = + getMethod(marshallObjectClass, "get"); + + private static Class getClass(String name) { + try { + return Class.forName(name, true, null); + } catch (ClassNotFoundException e) { + return null; + } + } + + private static Constructor getConstructor(Class c, Class... types) { + try { + return (c == null) ? null : c.getDeclaredConstructor(types); + } catch (NoSuchMethodException x) { + throw new AssertionError(x); + } + } + + private static Method getMethod(Class c, String name, Class... types) { + try { + return (c == null) ? null : c.getMethod(name, types); + } catch (NoSuchMethodException e) { + throw new AssertionError(e); + } + } + + /** + * Returns {@code true} if the given class is java.rmi.Remote. + */ + static boolean isRemote(Class c) { + return (remoteClass == null) ? null : remoteClass.isAssignableFrom(c); + } + + /** + * Returns java.rmi.Remote.class if RMI is present; otherwise {@code null}. + */ + static Class remoteClass() { + return remoteClass; + } + + /** + * Returns a new MarshalledObject containing the serialized representation + * of the given object. + */ + static Object newMarshalledObject(Object obj) throws IOException { + try { + return marshallCtor.newInstance(obj); + } catch (InstantiationException x) { + throw new AssertionError(x); + } catch (IllegalAccessException x) { + throw new AssertionError(x); + } catch (InvocationTargetException x) { + Throwable cause = x.getCause(); + if (cause instanceof IOException) + throw (IOException)cause; + throw new AssertionError(x); + } + } + + /** + * Returns a new copy of the contained marshalled object. + */ + static Object getMarshalledObject(Object obj) + throws IOException, ClassNotFoundException + { + try { + return marshallGet.invoke(obj); + } catch (IllegalAccessException x) { + throw new AssertionError(x); + } catch (InvocationTargetException x) { + Throwable cause = x.getCause(); + if (cause instanceof IOException) + throw (IOException)cause; + if (cause instanceof ClassNotFoundException) + throw (ClassNotFoundException)cause; + throw new AssertionError(x); + } + } + } } -- GitLab