Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
8250219: Proxy::newProxyInstance spec should specify the behavior if …
…a given proxy interface is hidden

Reviewed-by: alanb
  • Loading branch information
Mandy Chung committed Aug 4, 2020
1 parent 4de9999 commit 41a8bfdc4920cdf2a6b0504afdfee1a0760883e5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 7 deletions.
@@ -674,12 +674,6 @@ private static void validateProxyInterfaces(ClassLoader loader,
{
Map<Class<?>, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.size());
for (Class<?> intf : interfaces) {
/*
* Verify that the class loader resolves the name of this
* interface to the same Class object.
*/
ensureVisible(loader, intf);

/*
* Verify that the Class object actually represents an
* interface.
@@ -688,6 +682,16 @@ private static void validateProxyInterfaces(ClassLoader loader,
throw new IllegalArgumentException(intf.getName() + " is not an interface");
}

if (intf.isHidden()) {
throw new IllegalArgumentException(intf.getName() + " is a hidden interface");
}

/*
* Verify that the class loader resolves the name of this
* interface to the same Class object.
*/
ensureVisible(loader, intf);

/*
* Verify that this interface is not a duplicate.
*/
@@ -905,7 +909,8 @@ private static Module getDynamicModule(ClassLoader loader) {
* if any of the following restrictions is violated:</a>
* <ul>
* <li>All of {@code Class} objects in the given {@code interfaces} array
* must represent interfaces, not classes or primitive types.
* must represent {@linkplain Class#isHidden() non-hidden} interfaces,
* not classes or primitive types.
*
* <li>No two elements in the {@code interfaces} array may
* refer to identical {@code Class} objects.
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8250219
* @run testng HiddenProxyInterface
*/

import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.testng.annotations.Test;
import static org.testng.Assert.*;

public class HiddenProxyInterface {
private static final Path CLASSES_DIR = Paths.get(System.getProperty("test.classes"));

interface Intf {
void m();
}

@Test(expectedExceptions = { IllegalArgumentException.class })
public void testHiddenInterface() throws Exception {
Path classFile = CLASSES_DIR.resolve("HiddenProxyInterface$Intf.class");
byte[] bytes = Files.readAllBytes(classFile);
Class<?> hiddenIntf = MethodHandles.lookup().defineHiddenClass(bytes, false).lookupClass();
Proxy.newProxyInstance(HiddenProxyInterface.class.getClassLoader(),
new Class<?>[]{ hiddenIntf },
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return null;
}
});
}
}

0 comments on commit 41a8bfd

Please sign in to comment.