1 /*
   2   @test
   3   @key headful
   4   @bug 4932376
   5   @summary verifies that data transfer within one JVM works correctly if
   6            the transfer data was created with a custom class loader.
   7   @author das@sparc.spb.su area=datatransfer
   8   @library ../../regtesthelpers
   9   @build TransferableList AnotherInterface CopyClassFile CustomClassLoaderTransferTest
  10   @run main CopyClassFile -r ListInterface subdir/
  11   @run main CopyClassFile -r TransferableList subdir/
  12   @run main CustomClassLoaderTransferTest
  13 */
  14 
  15 import java.awt.*;
  16 import java.awt.datatransfer.*;
  17 import java.io.*;
  18 import java.net.URL;
  19 import java.net.URLClassLoader;
  20 
  21 public class CustomClassLoaderTransferTest {
  22     public static class DFTransferable implements Transferable {
  23         private final DataFlavor df;
  24         private final Object obj;
  25         public DFTransferable(DataFlavor df, Object obj) {
  26             this.df = df;
  27             this.obj = obj;
  28         }
  29 
  30         @Override
  31         public Object getTransferData(DataFlavor flavor)
  32           throws UnsupportedFlavorException, IOException {
  33             if (df.equals(flavor)) {
  34                 return obj;
  35             } else {
  36                 throw new UnsupportedFlavorException(flavor);
  37             }
  38         }
  39 
  40         @Override
  41         public DataFlavor[] getTransferDataFlavors(){
  42             return new DataFlavor[] { df };
  43         }
  44 
  45         @Override
  46         public boolean isDataFlavorSupported(DataFlavor flavor) {
  47             return df.equals(flavor);
  48         }
  49     }
  50 
  51     public static void main(String[] args) throws Exception {
  52         Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
  53         URL url = new File("./subdir/").toURL();
  54         ClassLoader classLoader = new URLClassLoader(new URL[] { url },
  55                 CustomClassLoaderTransferTest.class.getClassLoader());
  56         Class clazz = Class.forName("TransferableList", true, classLoader);
  57         DataFlavor df = new DataFlavor(clazz, "Transferable List");
  58         Object obj = clazz.newInstance();
  59         Transferable t = new DFTransferable(df, obj);
  60         c.setContents(t, null);
  61         Transferable ct = c.getContents(null);
  62         ct.getTransferData(df);
  63     }
  64 }