/* * 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 8255560 * @summary Class::isRecord should check that the current class is final and not abstract * @modules java.base/jdk.internal.org.objectweb.asm * @library /test/lib * @run testng/othervm IsRecordTest * @run testng/othervm/java.security.policy=allPermissions.policy IsRecordTest
*/
/** * Tests the valid combinations of i) final/non-final, ii) abstract/non-abstract, * along with the presence or absence of a record attribute, where the class has * a superclass whose superclass is j.l.Record.
*/
@Test(dataProvider = "scenarios") publicvoid testIndirectSubClass(boolean isFinal, boolean isAbstract, boolean unused1, boolean withRecordAttr, boolean unused2) throws Exception {
out.println("\n--- testIndirectSubClass isFinal=%s, isAbstract=%s withRecordAttr=%s ---"
.formatted(isFinal, isAbstract, withRecordAttr));
List<RecordComponentEntry> rc = null; if (withRecordAttr)
rc = List.of(new RecordComponentEntry("x", "I")); var supFooClassBytes = generateClassBytes("SupFoo", false, isAbstract, "java/lang/Record", rc); var subFooClassBytes = generateClassBytes("SubFoo", isFinal, isAbstract, "SupFoo", rc); var allClassBytes = Map.of("SupFoo", supFooClassBytes, "SubFoo", subFooClassBytes);
record FooRecord (int x) { }
assertTrue(FooRecord.class.isRecord());
assertTrue(FooRecord.class.getRecordComponents() != null);
final record FinalFooRecord (int x) { }
assertTrue(FinalFooRecord.class.isRecord());
assertTrue(FinalFooRecord.class.getRecordComponents() != null);
class A { }
assertFalse(A.class.isRecord());
assertFalse(A.class.getRecordComponents() != null);
finalclass B { }
assertFalse(B.class.isRecord());
assertFalse(B.class.getRecordComponents() != null);
}
// -- infra
// Generates a class with the given properties. byte[] generateClassBytes(String className, boolean isFinal, boolean isAbstract,
String superName,
List<RecordComponentEntry> components) {
ClassWriter cw = new ClassWriter(COMPUTE_MAXS | COMPUTE_FRAMES);
int access = 0; if (isFinal)
access = access | Opcodes.ACC_FINAL; if (isAbstract)
access = access | Opcodes.ACC_ABSTRACT;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.