Skip to content

8351362: Post-process @Strict annotation for testing #1395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: lworld
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
* Internal and experimental use only
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Strict {
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
/*
* @test
* @enablePreview
* @compile --add-exports=java.base/jdk.internal.vm.annotation=ALL-UNNAMED -XDgenerateAssertUnsetFieldsFrame -XDnoLocalProxyVars StrictFinalInstanceFieldsTest.java
* @library /test/lib
* @modules java.base/jdk.internal.vm.annotation
* @run main/othervm jdk.test.lib.value.StrictCompiler StrictFinalInstanceFieldsTest.java -- -XDnoLocalProxyVars
* @run main/othervm -Xlog:verification StrictFinalInstanceFieldsTest
*/

Expand Down
132 changes: 132 additions & 0 deletions test/lib-test/jdk/test/lib/StrictCompilerSuperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright (c) 2025, 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 8351362
* @summary Unit Test for StrictCompiler super rewrite
* @enablePreview
* @library /test/lib
* @modules java.base/jdk.internal.vm.annotation
* @run main/othervm jdk.test.lib.value.StrictCompiler --deferSuperCall StrictCompilerSuperTest.java
* @run junit StrictCompilerSuperTest
*/

import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.classfile.Attributes;
import java.lang.classfile.ClassFile;
import java.lang.classfile.ClassModel;
import java.lang.classfile.Instruction;
import java.lang.classfile.Opcode;
import java.lang.reflect.AccessFlag;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.stream.Stream;

import jdk.internal.vm.annotation.Strict;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import static java.lang.classfile.ClassFile.*;
import static java.lang.constant.ConstantDescs.INIT_NAME;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;

class StrictCompilerSuperTest {
static Stream<Class<?>> testClasses() {
return Stream.of(Rec.class, Exp.class, Inner.class);
}

static Stream<ClassModel> testClassModels() {
return testClasses().map(cls -> {
try (var in = StrictCompilerSuperTest.class.getResourceAsStream("/" + cls.getName() + ".class")) {
return ClassFile.of().parse(in.readAllBytes());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
});
}

@MethodSource("testClasses")
@ParameterizedTest
void testReflectRewrittenRecord(Class<?> cls) throws Throwable {
for (var field : cls.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers()) || field.isSynthetic())
continue;
assertEquals(ACC_PRIVATE | ACC_STRICT | ACC_FINAL, field.getModifiers(), () -> "For field: " + field.getName());
}
}

@MethodSource("testClassModels")
@ParameterizedTest
void testRewrittenStrictAccessInClassFile(ClassModel cm) throws Throwable {
for (var f : cm.fields()) {
if (f.flags().has(AccessFlag.STATIC) || f.flags().has(AccessFlag.SYNTHETIC))
continue;
assertEquals(ACC_PRIVATE | ACC_STRICT | ACC_FINAL, f.flags().flagsMask(), () -> "Field " + f);
}
}

@MethodSource("testClassModels")
@ParameterizedTest
void testRewrittenCtorBytecode(ClassModel cm) throws Throwable {
var ctor = cm.methods().stream().filter(m -> m.methodName().equalsString(INIT_NAME)).findFirst().orElseThrow();
var insts = new ArrayList<Instruction>();
ctor.findAttribute(Attributes.code()).orElseThrow().forEach(ce -> {
if (ce instanceof Instruction inst) {
insts.add(inst);
}
});
assertSame(Opcode.RETURN, insts.getLast().opcode());
assertSame(Opcode.INVOKESPECIAL, insts.get(insts.size() - 2).opcode());
}

record Rec(@Strict int a, @Strict long b) {
static final String NOISE = "noise";
}

static class Exp {
private @Strict final int a;
private @Strict final long b;

Exp(int a, long b) {
this.a = a;
this.b = b;
}
}

class Inner {
private @Strict final int a;
private @Strict final long b;

Inner(int a, long b) {
this.a = a;
this.b = b;
}

@Override
public String toString() {
return a + " " + StrictCompilerSuperTest.this + " " + b;
}
}
}
61 changes: 61 additions & 0 deletions test/lib-test/jdk/test/lib/StrictCompilerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2025, 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 8351362
* @summary Unit Test for StrictCompiler
* @enablePreview
* @library /test/lib
* @modules java.base/jdk.internal.vm.annotation
* @run main/othervm jdk.test.lib.value.StrictCompiler StrictCompilerTest.java
* @run junit StrictCompilerTest
*/

import jdk.internal.vm.annotation.Strict;
import org.junit.jupiter.api.Test;

import static java.lang.classfile.ClassFile.ACC_FINAL;
import static java.lang.classfile.ClassFile.ACC_STRICT;
import static org.junit.jupiter.api.Assertions.assertEquals;

class StrictCompilerTest {
@Test
void testReflectMyself() throws Throwable {
for (var field : StrictTarget.class.getDeclaredFields()) {
assertEquals(ACC_STRICT | ACC_FINAL, field.getModifiers(), () -> field.getName());
}
}

static final class StrictTarget {
@Strict
final int a;
@Strict
final Object b;

StrictTarget() {
this.a = 1;
this.b = 2392352234L;
super();
}
}
}
8 changes: 6 additions & 2 deletions test/lib/jdk/test/lib/compiler/InMemoryJavaCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ public String getClassName() {
* @return The resulting byte code from the compilation
*/
public static Map<String, byte[]> compile(Map<String, ? extends CharSequence> inputMap) {
Collection<JavaFileObject> sourceFiles = new LinkedList<JavaFileObject>();
return compile(inputMap, new String[0]);
}

public static Map<String, byte[]> compile(Map<String, ? extends CharSequence> inputMap, String... options) {
Collection<JavaFileObject> sourceFiles = new ArrayList<>();
for (Entry<String, ? extends CharSequence> entry : inputMap.entrySet()) {
sourceFiles.add(new SourceFile(entry.getKey(), entry.getValue()));
}
Expand All @@ -225,7 +229,7 @@ public static Map<String, byte[]> compile(Map<String, ? extends CharSequence> in
FileManager fileManager = new FileManager(compiler.getStandardFileManager(null, null, null));

Writer writer = new StringWriter();
Boolean exitCode = compiler.getTask(writer, fileManager, null, null, null, sourceFiles).call();
Boolean exitCode = compiler.getTask(writer, fileManager, null, Arrays.asList(options), null, sourceFiles).call();
if (!exitCode) {
System.out.println("*********** javac output begin ***********");
System.out.println(writer.toString());
Expand Down
Loading