jdk/test/java/lang/reflect/Proxy/ClassRestrictions.java
changeset 5808 3a1f603c5ca7
parent 5506 202f599c92aa
child 7668 d4a77089c587
equal deleted inserted replaced
5807:d34ed576e234 5808:3a1f603c5ca7
    52 
    52 
    53         System.err.println(
    53         System.err.println(
    54             "\nTest of restrictions on parameters to Proxy.getProxyClass\n");
    54             "\nTest of restrictions on parameters to Proxy.getProxyClass\n");
    55 
    55 
    56         try {
    56         try {
    57             ClassLoader loader = ClassLoader.getSystemClassLoader();
    57             ClassLoader loader = ClassRestrictions.class.getClassLoader();
    58             Class[] interfaces;
    58             Class<?>[] interfaces;
    59             Class proxyClass;
    59             Class<?> proxyClass;
    60 
    60 
    61             /*
    61             /*
    62              * All of the Class objects in the interfaces array must represent
    62              * All of the Class objects in the interfaces array must represent
    63              * interfaces, not classes or primitive types.
    63              * interfaces, not classes or primitive types.
    64              */
    64              */
    65             try {
    65             try {
    66                 interfaces = new Class[] { Object.class };
    66                 interfaces = new Class<?>[] { Object.class };
    67                 proxyClass = Proxy.getProxyClass(loader, interfaces);
    67                 proxyClass = Proxy.getProxyClass(loader, interfaces);
    68                 throw new RuntimeException(
    68                 throw new RuntimeException(
    69                     "proxy class created with java.lang.Object as interface");
    69                     "proxy class created with java.lang.Object as interface");
    70             } catch (IllegalArgumentException e) {
    70             } catch (IllegalArgumentException e) {
    71                 e.printStackTrace();
    71                 e.printStackTrace();
    72                 System.err.println();
    72                 System.err.println();
    73                 // assume exception is for intended failure
    73                 // assume exception is for intended failure
    74             }
    74             }
    75             try {
    75             try {
    76                 interfaces = new Class[] { Integer.TYPE };
    76                 interfaces = new Class<?>[] { Integer.TYPE };
    77                 proxyClass = Proxy.getProxyClass(loader, interfaces);
    77                 proxyClass = Proxy.getProxyClass(loader, interfaces);
    78                 throw new RuntimeException(
    78                 throw new RuntimeException(
    79                     "proxy class created with int.class as interface");
    79                     "proxy class created with int.class as interface");
    80             } catch (IllegalArgumentException e) {
    80             } catch (IllegalArgumentException e) {
    81                 e.printStackTrace();
    81                 e.printStackTrace();
    86             /*
    86             /*
    87              * No two elements in the interfaces array may refer to identical
    87              * No two elements in the interfaces array may refer to identical
    88              * Class objects.
    88              * Class objects.
    89              */
    89              */
    90             try {
    90             try {
    91                 interfaces = new Class[] { Bar.class, Bar.class };
    91                 interfaces = new Class<?>[] { Bar.class, Bar.class };
    92                 proxyClass = Proxy.getProxyClass(loader, interfaces);
    92                 proxyClass = Proxy.getProxyClass(loader, interfaces);
    93                 throw new RuntimeException(
    93                 throw new RuntimeException(
    94                     "proxy class created with repeated interfaces");
    94                     "proxy class created with repeated interfaces");
    95             } catch (IllegalArgumentException e) {
    95             } catch (IllegalArgumentException e) {
    96                 e.printStackTrace();
    96                 e.printStackTrace();
   105             ClassLoader altLoader = new URLClassLoader(
   105             ClassLoader altLoader = new URLClassLoader(
   106                 ((URLClassLoader) loader).getURLs(), null);
   106                 ((URLClassLoader) loader).getURLs(), null);
   107             Class altBarClass;
   107             Class altBarClass;
   108             altBarClass = Class.forName(Bar.class.getName(), false, altLoader);
   108             altBarClass = Class.forName(Bar.class.getName(), false, altLoader);
   109             try {
   109             try {
   110                 interfaces = new Class[] { altBarClass };
   110                 interfaces = new Class<?>[] { altBarClass };
   111                 proxyClass = Proxy.getProxyClass(loader, interfaces);
   111                 proxyClass = Proxy.getProxyClass(loader, interfaces);
   112                 throw new RuntimeException(
   112                 throw new RuntimeException(
   113                     "proxy class created with interface " +
   113                     "proxy class created with interface " +
   114                     "not visible to class loader");
   114                     "not visible to class loader");
   115             } catch (IllegalArgumentException e) {
   115             } catch (IllegalArgumentException e) {
   119             }
   119             }
   120 
   120 
   121             /*
   121             /*
   122              * All non-public interfaces must be in the same package.
   122              * All non-public interfaces must be in the same package.
   123              */
   123              */
   124             Class nonPublic1 = Bashful.class;
   124             Class<?> nonPublic1 = Bashful.class;
   125             Class nonPublic2 = null;
   125             Class<?> nonPublic2 = null;
   126             String[] nonPublicInterfaces = new String[] {
   126             String[] nonPublicInterfaces = new String[] {
   127                 "java.awt.Conditional",
   127                 "java.awt.Conditional",
   128                 "java.util.zip.ZipConstants",
   128                 "java.util.zip.ZipConstants",
   129                 "javax.swing.GraphicsWrapper",
   129                 "javax.swing.GraphicsWrapper",
   130                 "javax.swing.JPopupMenu$Popup",
   130                 "javax.swing.JPopupMenu$Popup",
   145             if (nonPublic2 == null) {
   145             if (nonPublic2 == null) {
   146                 throw new RuntimeException(
   146                 throw new RuntimeException(
   147                     "no second non-public interface found for test");
   147                     "no second non-public interface found for test");
   148             }
   148             }
   149             try {
   149             try {
   150                 interfaces = new Class[] { nonPublic1, nonPublic2 };
   150                 interfaces = new Class<?>[] { nonPublic1, nonPublic2 };
   151                 proxyClass = Proxy.getProxyClass(loader, interfaces);
   151                 proxyClass = Proxy.getProxyClass(loader, interfaces);
   152                 throw new RuntimeException(
   152                 throw new RuntimeException(
   153                     "proxy class created with two non-public interfaces " +
   153                     "proxy class created with two non-public interfaces " +
   154                     "in different packages");
   154                     "in different packages");
   155             } catch (IllegalArgumentException e) {
   155             } catch (IllegalArgumentException e) {
   161             /*
   161             /*
   162              * No two interfaces may each have a method with the same name and
   162              * No two interfaces may each have a method with the same name and
   163              * parameter signature but different return type.
   163              * parameter signature but different return type.
   164              */
   164              */
   165             try {
   165             try {
   166                 interfaces = new Class[] { Bar.class, Baz.class };
   166                 interfaces = new Class<?>[] { Bar.class, Baz.class };
   167                 proxyClass = Proxy.getProxyClass(loader, interfaces);
   167                 proxyClass = Proxy.getProxyClass(loader, interfaces);
   168                 throw new RuntimeException(
   168                 throw new RuntimeException(
   169                     "proxy class created with conflicting methods");
   169                     "proxy class created with conflicting methods");
   170             } catch (IllegalArgumentException e) {
   170             } catch (IllegalArgumentException e) {
   171                 e.printStackTrace();
   171                 e.printStackTrace();