< prev index next >

test/jdk/java/lang/invoke/defineHiddenClass/BasicTest.java

Print this page

  1 /*
  2  * Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 8330467
 27  * @modules jdk.compiler

 28  * @library /test/lib
 29  * @compile BadClassFile.jcod
 30  *          BadClassFile2.jcod
 31  *          BadClassFileVersion.jcod
 32  * @build jdk.test.lib.Utils
 33  *        jdk.test.lib.compiler.CompilerUtils
 34  * @run junit/othervm BasicTest
 35  */
 36 
 37 import java.io.File;
 38 import java.io.IOException;
 39 import java.lang.classfile.ClassFile;
 40 import java.lang.constant.ClassDesc;
 41 import java.lang.invoke.MethodHandles.Lookup;
 42 import java.lang.reflect.Array;
 43 import java.lang.reflect.Method;
 44 import java.nio.charset.StandardCharsets;
 45 import java.nio.file.Files;
 46 import java.nio.file.Path;
 47 import java.nio.file.Paths;
 48 import java.util.Arrays;
 49 import java.util.List;
 50 import java.util.stream.Stream;
 51 

 52 import jdk.test.lib.compiler.CompilerUtils;
 53 import jdk.test.lib.Utils;
 54 
 55 import static java.lang.classfile.ClassFile.*;
 56 import static java.lang.constant.ConstantDescs.CD_Enum;
 57 import static java.lang.constant.ConstantDescs.CD_Object;
 58 import static java.lang.invoke.MethodHandles.lookup;
 59 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;
 60 import static org.junit.jupiter.api.Assertions.*;
 61 import org.junit.jupiter.api.BeforeAll;
 62 import org.junit.jupiter.api.Test;
 63 import org.junit.jupiter.params.ParameterizedTest;
 64 import org.junit.jupiter.params.provider.MethodSource;
 65 
 66 interface HiddenTest {
 67     void test();
 68 }
 69 
 70 public class BasicTest {
 71 

289                 assertFalse(hc.isEnum());
290                 assertFalse(hc.isAnnotation());
291                 assertFalse(hc.isInterface());
292                 break;
293             case ACC_ABSTRACT|ACC_INTERFACE:
294                 assertFalse(hc.isSynthetic());
295                 assertFalse(hc.isEnum());
296                 assertFalse(hc.isAnnotation());
297                 assertTrue(hc.isInterface());
298                 break;
299             case ACC_ANNOTATION|ACC_ABSTRACT|ACC_INTERFACE:
300                 assertFalse(hc.isSynthetic());
301                 assertFalse(hc.isEnum());
302                 assertTrue(hc.isAnnotation());
303                 assertTrue(hc.isInterface());
304                 break;
305             default:
306                 throw new IllegalArgumentException("unexpected access flag: " + accessFlags);
307         }
308         assertTrue(hc.isHidden());
309         assertEquals(hc.getModifiers(), ACC_PUBLIC | accessFlags);




310         assertFalse(hc.isLocalClass());
311         assertFalse(hc.isMemberClass());
312         assertFalse(hc.isAnonymousClass());
313         assertFalse(hc.isArray());
314     }
315 
316     // These class files can't be defined as hidden classes
317     private static Object[][] cantBeHiddenClasses() {
318         return new Object[][] {
319                 // a hidden class can't be a field's declaring type
320                 // enum class with static final HiddenEnum[] $VALUES:
321                 new Object[] { "HiddenEnum" },
322                 // supertype of this class is a hidden class
323                 new Object[] { "HiddenSuper" },
324                 // a record class whose equals(HiddenRecord, Object) method
325                 // refers to a hidden class in the parameter type and fails
326                 // verification.  Perhaps this method signature should be reconsidered.
327                 new Object[] { "HiddenRecord" },
328         };
329     }

  1 /*
  2  * Copyright (c) 2019, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 8330467
 27  * @modules jdk.compiler
 28  *          java.base/jdk.internal.misc
 29  * @library /test/lib
 30  * @compile BadClassFile.jcod
 31  *          BadClassFile2.jcod
 32  *          BadClassFileVersion.jcod
 33  * @build jdk.test.lib.Utils
 34  *        jdk.test.lib.compiler.CompilerUtils
 35  * @run junit/othervm BasicTest
 36  */
 37 
 38 import java.io.File;
 39 import java.io.IOException;
 40 import java.lang.classfile.ClassFile;
 41 import java.lang.constant.ClassDesc;
 42 import java.lang.invoke.MethodHandles.Lookup;
 43 import java.lang.reflect.Array;
 44 import java.lang.reflect.Method;
 45 import java.nio.charset.StandardCharsets;
 46 import java.nio.file.Files;
 47 import java.nio.file.Path;
 48 import java.nio.file.Paths;
 49 import java.util.Arrays;
 50 import java.util.List;
 51 import java.util.stream.Stream;
 52 
 53 import jdk.internal.misc.PreviewFeatures;
 54 import jdk.test.lib.compiler.CompilerUtils;
 55 import jdk.test.lib.Utils;
 56 
 57 import static java.lang.classfile.ClassFile.*;
 58 import static java.lang.constant.ConstantDescs.CD_Enum;
 59 import static java.lang.constant.ConstantDescs.CD_Object;
 60 import static java.lang.invoke.MethodHandles.lookup;
 61 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;
 62 import static org.junit.jupiter.api.Assertions.*;
 63 import org.junit.jupiter.api.BeforeAll;
 64 import org.junit.jupiter.api.Test;
 65 import org.junit.jupiter.params.ParameterizedTest;
 66 import org.junit.jupiter.params.provider.MethodSource;
 67 
 68 interface HiddenTest {
 69     void test();
 70 }
 71 
 72 public class BasicTest {
 73 

291                 assertFalse(hc.isEnum());
292                 assertFalse(hc.isAnnotation());
293                 assertFalse(hc.isInterface());
294                 break;
295             case ACC_ABSTRACT|ACC_INTERFACE:
296                 assertFalse(hc.isSynthetic());
297                 assertFalse(hc.isEnum());
298                 assertFalse(hc.isAnnotation());
299                 assertTrue(hc.isInterface());
300                 break;
301             case ACC_ANNOTATION|ACC_ABSTRACT|ACC_INTERFACE:
302                 assertFalse(hc.isSynthetic());
303                 assertFalse(hc.isEnum());
304                 assertTrue(hc.isAnnotation());
305                 assertTrue(hc.isInterface());
306                 break;
307             default:
308                 throw new IllegalArgumentException("unexpected access flag: " + accessFlags);
309         }
310         assertTrue(hc.isHidden());
311         int expectedAccessFlags = ACC_PUBLIC | accessFlags;
312         if ((accessFlags & ACC_INTERFACE) == 0 && PreviewFeatures.isEnabled()) {
313             expectedAccessFlags |= ACC_IDENTITY;
314         }
315         assertEquals(hc.getModifiers(), expectedAccessFlags);
316         assertFalse(hc.isLocalClass());
317         assertFalse(hc.isMemberClass());
318         assertFalse(hc.isAnonymousClass());
319         assertFalse(hc.isArray());
320     }
321 
322     // These class files can't be defined as hidden classes
323     private static Object[][] cantBeHiddenClasses() {
324         return new Object[][] {
325                 // a hidden class can't be a field's declaring type
326                 // enum class with static final HiddenEnum[] $VALUES:
327                 new Object[] { "HiddenEnum" },
328                 // supertype of this class is a hidden class
329                 new Object[] { "HiddenSuper" },
330                 // a record class whose equals(HiddenRecord, Object) method
331                 // refers to a hidden class in the parameter type and fails
332                 // verification.  Perhaps this method signature should be reconsidered.
333                 new Object[] { "HiddenRecord" },
334         };
335     }
< prev index next >