test/java/lang/reflect/Proxy/ClassRestrictions.java
Print this page
*** 20,38 ****
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
! * @bug 4227192
* @summary This is a test of the restrictions on the parameters that may
* be passed to the Proxy.getProxyClass method.
* @author Peter Jones
*
* @build ClassRestrictions
* @run main ClassRestrictions
*/
import java.lang.reflect.Proxy;
import java.net.URLClassLoader;
public class ClassRestrictions {
--- 20,39 ----
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
! * @bug 4227192 8004928
* @summary This is a test of the restrictions on the parameters that may
* be passed to the Proxy.getProxyClass method.
* @author Peter Jones
*
* @build ClassRestrictions
* @run main ClassRestrictions
*/
+ import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.net.URLClassLoader;
public class ClassRestrictions {
*** 46,55 ****
--- 47,58 ----
interface Bashful {
void foo();
}
+ public static final String nonPublicIntrfaceName = "java.util.zip.ZipConstants";
+
public static void main(String[] args) {
System.err.println(
"\nTest of restrictions on parameters to Proxy.getProxyClass\n");
*** 63,83 ****
* interfaces, not classes or primitive types.
*/
try {
interfaces = new Class<?>[] { Object.class };
proxyClass = Proxy.getProxyClass(loader, interfaces);
! throw new RuntimeException(
"proxy class created with java.lang.Object as interface");
} catch (IllegalArgumentException e) {
e.printStackTrace();
System.err.println();
// assume exception is for intended failure
}
try {
interfaces = new Class<?>[] { Integer.TYPE };
proxyClass = Proxy.getProxyClass(loader, interfaces);
! throw new RuntimeException(
"proxy class created with int.class as interface");
} catch (IllegalArgumentException e) {
e.printStackTrace();
System.err.println();
// assume exception is for intended failure
--- 66,86 ----
* interfaces, not classes or primitive types.
*/
try {
interfaces = new Class<?>[] { Object.class };
proxyClass = Proxy.getProxyClass(loader, interfaces);
! throw new Error(
"proxy class created with java.lang.Object as interface");
} catch (IllegalArgumentException e) {
e.printStackTrace();
System.err.println();
// assume exception is for intended failure
}
try {
interfaces = new Class<?>[] { Integer.TYPE };
proxyClass = Proxy.getProxyClass(loader, interfaces);
! throw new Error(
"proxy class created with int.class as interface");
} catch (IllegalArgumentException e) {
e.printStackTrace();
System.err.println();
// assume exception is for intended failure
*** 88,98 ****
* Class objects.
*/
try {
interfaces = new Class<?>[] { Bar.class, Bar.class };
proxyClass = Proxy.getProxyClass(loader, interfaces);
! throw new RuntimeException(
"proxy class created with repeated interfaces");
} catch (IllegalArgumentException e) {
e.printStackTrace();
System.err.println();
// assume exception is for intended failure
--- 91,101 ----
* Class objects.
*/
try {
interfaces = new Class<?>[] { Bar.class, Bar.class };
proxyClass = Proxy.getProxyClass(loader, interfaces);
! throw new Error(
"proxy class created with repeated interfaces");
} catch (IllegalArgumentException e) {
e.printStackTrace();
System.err.println();
// assume exception is for intended failure
*** 107,117 ****
Class altBarClass;
altBarClass = Class.forName(Bar.class.getName(), false, altLoader);
try {
interfaces = new Class<?>[] { altBarClass };
proxyClass = Proxy.getProxyClass(loader, interfaces);
! throw new RuntimeException(
"proxy class created with interface " +
"not visible to class loader");
} catch (IllegalArgumentException e) {
e.printStackTrace();
System.err.println();
--- 110,120 ----
Class altBarClass;
altBarClass = Class.forName(Bar.class.getName(), false, altLoader);
try {
interfaces = new Class<?>[] { altBarClass };
proxyClass = Proxy.getProxyClass(loader, interfaces);
! throw new Error(
"proxy class created with interface " +
"not visible to class loader");
} catch (IllegalArgumentException e) {
e.printStackTrace();
System.err.println();
*** 120,157 ****
/*
* All non-public interfaces must be in the same package.
*/
Class<?> nonPublic1 = Bashful.class;
! Class<?> nonPublic2 = null;
! String[] nonPublicInterfaces = new String[] {
! "java.awt.Conditional",
! "java.util.zip.ZipConstants",
! "javax.swing.GraphicsWrapper",
! "javax.swing.JPopupMenu$Popup",
! "javax.swing.JTable$Resizable2",
! "javax.swing.JTable$Resizable3",
! "javax.swing.ToolTipManager$Popup",
! "sun.audio.Format",
! "sun.audio.HaePlayable",
! "sun.tools.agent.StepConstants",
! };
! for (int i = 0; i < nonPublicInterfaces.length; i++) {
! try {
! nonPublic2 = Class.forName(nonPublicInterfaces[i]);
! break;
! } catch (ClassNotFoundException e) {
! }
! }
! if (nonPublic2 == null) {
! throw new RuntimeException(
! "no second non-public interface found for test");
}
try {
interfaces = new Class<?>[] { nonPublic1, nonPublic2 };
proxyClass = Proxy.getProxyClass(loader, interfaces);
! throw new RuntimeException(
"proxy class created with two non-public interfaces " +
"in different packages");
} catch (IllegalArgumentException e) {
e.printStackTrace();
System.err.println();
--- 123,142 ----
/*
* All non-public interfaces must be in the same package.
*/
Class<?> nonPublic1 = Bashful.class;
! Class<?> nonPublic2 = Class.forName(nonPublicIntrfaceName);
! if (Modifier.isPublic(nonPublic2.getModifiers())) {
! throw new Error(
! "Interface " + nonPublicIntrfaceName +
! " is public and need to be changed!");
}
try {
interfaces = new Class<?>[] { nonPublic1, nonPublic2 };
proxyClass = Proxy.getProxyClass(loader, interfaces);
! throw new Error(
"proxy class created with two non-public interfaces " +
"in different packages");
} catch (IllegalArgumentException e) {
e.printStackTrace();
System.err.println();
*** 163,173 ****
* parameter signature but different return type.
*/
try {
interfaces = new Class<?>[] { Bar.class, Baz.class };
proxyClass = Proxy.getProxyClass(loader, interfaces);
! throw new RuntimeException(
"proxy class created with conflicting methods");
} catch (IllegalArgumentException e) {
e.printStackTrace();
System.err.println();
// assume exception is for intended failure
--- 148,158 ----
* parameter signature but different return type.
*/
try {
interfaces = new Class<?>[] { Bar.class, Baz.class };
proxyClass = Proxy.getProxyClass(loader, interfaces);
! throw new Error(
"proxy class created with conflicting methods");
} catch (IllegalArgumentException e) {
e.printStackTrace();
System.err.println();
// assume exception is for intended failure
*** 176,187 ****
/*
* All components of this test have passed.
*/
System.err.println("\nTEST PASSED");
! } catch (Exception e) {
System.err.println("\nTEST FAILED:");
e.printStackTrace();
! throw new RuntimeException("TEST FAILED: " + e.toString());
}
}
}
--- 161,172 ----
/*
* All components of this test have passed.
*/
System.err.println("\nTEST PASSED");
! } catch (Throwable e) {
System.err.println("\nTEST FAILED:");
e.printStackTrace();
! throw new Error("TEST FAILED: ", e);
}
}
}