jdk/test/java/lang/reflect/Proxy/ClassRestrictions.java
changeset 14897 99faf9db81d8
parent 7668 d4a77089c587
child 23010 6dadb192ad81
equal deleted inserted replaced
14896:9521c33d3510 14897:99faf9db81d8
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /* @test
    24 /* @test
    25  * @bug 4227192
    25  * @bug 4227192 8004928
    26  * @summary This is a test of the restrictions on the parameters that may
    26  * @summary This is a test of the restrictions on the parameters that may
    27  * be passed to the Proxy.getProxyClass method.
    27  * be passed to the Proxy.getProxyClass method.
    28  * @author Peter Jones
    28  * @author Peter Jones
    29  *
    29  *
    30  * @build ClassRestrictions
    30  * @build ClassRestrictions
    31  * @run main ClassRestrictions
    31  * @run main ClassRestrictions
    32  */
    32  */
    33 
    33 
       
    34 import java.lang.reflect.Modifier;
    34 import java.lang.reflect.Proxy;
    35 import java.lang.reflect.Proxy;
    35 import java.net.URLClassLoader;
    36 import java.net.URLClassLoader;
    36 
    37 
    37 public class ClassRestrictions {
    38 public class ClassRestrictions {
    38 
    39 
    45     }
    46     }
    46 
    47 
    47     interface Bashful {
    48     interface Bashful {
    48         void foo();
    49         void foo();
    49     }
    50     }
       
    51 
       
    52     public static final String nonPublicIntrfaceName = "java.util.zip.ZipConstants";
    50 
    53 
    51     public static void main(String[] args) {
    54     public static void main(String[] args) {
    52 
    55 
    53         System.err.println(
    56         System.err.println(
    54             "\nTest of restrictions on parameters to Proxy.getProxyClass\n");
    57             "\nTest of restrictions on parameters to Proxy.getProxyClass\n");
    63              * interfaces, not classes or primitive types.
    66              * interfaces, not classes or primitive types.
    64              */
    67              */
    65             try {
    68             try {
    66                 interfaces = new Class<?>[] { Object.class };
    69                 interfaces = new Class<?>[] { Object.class };
    67                 proxyClass = Proxy.getProxyClass(loader, interfaces);
    70                 proxyClass = Proxy.getProxyClass(loader, interfaces);
    68                 throw new RuntimeException(
    71                 throw new Error(
    69                     "proxy class created with java.lang.Object as interface");
    72                     "proxy class created with java.lang.Object as interface");
    70             } catch (IllegalArgumentException e) {
    73             } catch (IllegalArgumentException e) {
    71                 e.printStackTrace();
    74                 e.printStackTrace();
    72                 System.err.println();
    75                 System.err.println();
    73                 // assume exception is for intended failure
    76                 // assume exception is for intended failure
    74             }
    77             }
    75             try {
    78             try {
    76                 interfaces = new Class<?>[] { Integer.TYPE };
    79                 interfaces = new Class<?>[] { Integer.TYPE };
    77                 proxyClass = Proxy.getProxyClass(loader, interfaces);
    80                 proxyClass = Proxy.getProxyClass(loader, interfaces);
    78                 throw new RuntimeException(
    81                 throw new Error(
    79                     "proxy class created with int.class as interface");
    82                     "proxy class created with int.class as interface");
    80             } catch (IllegalArgumentException e) {
    83             } catch (IllegalArgumentException e) {
    81                 e.printStackTrace();
    84                 e.printStackTrace();
    82                 System.err.println();
    85                 System.err.println();
    83                 // assume exception is for intended failure
    86                 // assume exception is for intended failure
    88              * Class objects.
    91              * Class objects.
    89              */
    92              */
    90             try {
    93             try {
    91                 interfaces = new Class<?>[] { Bar.class, Bar.class };
    94                 interfaces = new Class<?>[] { Bar.class, Bar.class };
    92                 proxyClass = Proxy.getProxyClass(loader, interfaces);
    95                 proxyClass = Proxy.getProxyClass(loader, interfaces);
    93                 throw new RuntimeException(
    96                 throw new Error(
    94                     "proxy class created with repeated interfaces");
    97                     "proxy class created with repeated interfaces");
    95             } catch (IllegalArgumentException e) {
    98             } catch (IllegalArgumentException e) {
    96                 e.printStackTrace();
    99                 e.printStackTrace();
    97                 System.err.println();
   100                 System.err.println();
    98                 // assume exception is for intended failure
   101                 // assume exception is for intended failure
   107             Class altBarClass;
   110             Class altBarClass;
   108             altBarClass = Class.forName(Bar.class.getName(), false, altLoader);
   111             altBarClass = Class.forName(Bar.class.getName(), false, altLoader);
   109             try {
   112             try {
   110                 interfaces = new Class<?>[] { altBarClass };
   113                 interfaces = new Class<?>[] { altBarClass };
   111                 proxyClass = Proxy.getProxyClass(loader, interfaces);
   114                 proxyClass = Proxy.getProxyClass(loader, interfaces);
   112                 throw new RuntimeException(
   115                 throw new Error(
   113                     "proxy class created with interface " +
   116                     "proxy class created with interface " +
   114                     "not visible to class loader");
   117                     "not visible to class loader");
   115             } catch (IllegalArgumentException e) {
   118             } catch (IllegalArgumentException e) {
   116                 e.printStackTrace();
   119                 e.printStackTrace();
   117                 System.err.println();
   120                 System.err.println();
   120 
   123 
   121             /*
   124             /*
   122              * All non-public interfaces must be in the same package.
   125              * All non-public interfaces must be in the same package.
   123              */
   126              */
   124             Class<?> nonPublic1 = Bashful.class;
   127             Class<?> nonPublic1 = Bashful.class;
   125             Class<?> nonPublic2 = null;
   128             Class<?> nonPublic2 = Class.forName(nonPublicIntrfaceName);
   126             String[] nonPublicInterfaces = new String[] {
   129             if (Modifier.isPublic(nonPublic2.getModifiers())) {
   127                 "java.awt.Conditional",
   130                 throw new Error(
   128                 "java.util.zip.ZipConstants",
   131                     "Interface " + nonPublicIntrfaceName +
   129                 "javax.swing.GraphicsWrapper",
   132                     " is public and need to be changed!");
   130                 "javax.swing.JPopupMenu$Popup",
       
   131                 "javax.swing.JTable$Resizable2",
       
   132                 "javax.swing.JTable$Resizable3",
       
   133                 "javax.swing.ToolTipManager$Popup",
       
   134                 "sun.audio.Format",
       
   135                 "sun.audio.HaePlayable",
       
   136                 "sun.tools.agent.StepConstants",
       
   137             };
       
   138             for (int i = 0; i < nonPublicInterfaces.length; i++) {
       
   139                 try {
       
   140                     nonPublic2 = Class.forName(nonPublicInterfaces[i]);
       
   141                     break;
       
   142                 } catch (ClassNotFoundException e) {
       
   143                 }
       
   144             }
       
   145             if (nonPublic2 == null) {
       
   146                 throw new RuntimeException(
       
   147                     "no second non-public interface found for test");
       
   148             }
   133             }
   149             try {
   134             try {
   150                 interfaces = new Class<?>[] { nonPublic1, nonPublic2 };
   135                 interfaces = new Class<?>[] { nonPublic1, nonPublic2 };
   151                 proxyClass = Proxy.getProxyClass(loader, interfaces);
   136                 proxyClass = Proxy.getProxyClass(loader, interfaces);
   152                 throw new RuntimeException(
   137                 throw new Error(
   153                     "proxy class created with two non-public interfaces " +
   138                     "proxy class created with two non-public interfaces " +
   154                     "in different packages");
   139                     "in different packages");
   155             } catch (IllegalArgumentException e) {
   140             } catch (IllegalArgumentException e) {
   156                 e.printStackTrace();
   141                 e.printStackTrace();
   157                 System.err.println();
   142                 System.err.println();
   163              * parameter signature but different return type.
   148              * parameter signature but different return type.
   164              */
   149              */
   165             try {
   150             try {
   166                 interfaces = new Class<?>[] { Bar.class, Baz.class };
   151                 interfaces = new Class<?>[] { Bar.class, Baz.class };
   167                 proxyClass = Proxy.getProxyClass(loader, interfaces);
   152                 proxyClass = Proxy.getProxyClass(loader, interfaces);
   168                 throw new RuntimeException(
   153                 throw new Error(
   169                     "proxy class created with conflicting methods");
   154                     "proxy class created with conflicting methods");
   170             } catch (IllegalArgumentException e) {
   155             } catch (IllegalArgumentException e) {
   171                 e.printStackTrace();
   156                 e.printStackTrace();
   172                 System.err.println();
   157                 System.err.println();
   173                 // assume exception is for intended failure
   158                 // assume exception is for intended failure
   176             /*
   161             /*
   177              * All components of this test have passed.
   162              * All components of this test have passed.
   178              */
   163              */
   179             System.err.println("\nTEST PASSED");
   164             System.err.println("\nTEST PASSED");
   180 
   165 
   181         } catch (Exception e) {
   166         } catch (Throwable e) {
   182             System.err.println("\nTEST FAILED:");
   167             System.err.println("\nTEST FAILED:");
   183             e.printStackTrace();
   168             e.printStackTrace();
   184             throw new RuntimeException("TEST FAILED: " + e.toString());
   169             throw new Error("TEST FAILED: ", e);
   185         }
   170         }
   186     }
   171     }
   187 }
   172 }