Skip to content
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

8257028: [type-restrictions] Assorted issues with generation of RestrictedField attributes from annotations #278

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -39,7 +39,7 @@
* for more fine-grained control.
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE_USE)
public @interface RestrictedType {
/**
Expand Down
Expand Up @@ -461,7 +461,7 @@ private Type typeWithAnnotations(final JCTree typetree, final Type type,
enclTy != null &&
enclTy.getKind() != TypeKind.NONE &&
enclTy.getKind() != TypeKind.ERROR &&
(enclTr.getKind() == JCTree.Kind.MEMBER_SELECT ||
((enclTr.getKind() == JCTree.Kind.MEMBER_SELECT && !enclTy.isReferenceProjection()) || // .ref is only a pseudo member select.
enclTr.getKind() == JCTree.Kind.PARAMETERIZED_TYPE ||
enclTr.getKind() == JCTree.Kind.ANNOTATED_TYPE)) {
// Iterate also over the type tree, not just the type: the type is already
Expand Down
Expand Up @@ -1270,8 +1270,10 @@ protected boolean accepts(AttributeKind kind) {
protected void read(Symbol sym, int attrLen) {
if (sym.kind == VAR && sym.owner.kind == TYP) {
final Type type = poolReader.getType(nextChar());
Assert.check(((ClassSymbol)((ClassType)sym.type).tsym).projection == type.tsym);
sym.flags_field |= RESTRICTED_FIELD;
if (types.flattenWithTypeRestrictions) {
Assert.check(((ClassSymbol)((ClassType)sym.type).tsym).projection == type.tsym);
sym.flags_field |= RESTRICTED_FIELD;
}
}
}
},
Expand Down
45 changes: 45 additions & 0 deletions test/langtools/tools/javac/valhalla/lworld-values/PointBox.java
@@ -0,0 +1,45 @@
/*
* 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/

import java.lang.invoke.RestrictedType;

class PointBox {

static inline class Point {
public double x;
public double y;
public Point(double x, double y) { this.x = x; this.y = y; }
}

@RestrictedType("QPoint;")
public Object p368;

public static void main(String... args) {
PointBox b = new PointBox();
if (b.p368 != new Point(0,0)) throw new RuntimeException();
b.p368 = new Point(1.0, 2.0);
if (b.p368 != new Point(1.0, 2.0)) throw new RuntimeException();
}
}
@@ -0,0 +1,68 @@
/*
* 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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 8257028
* @summary Javac crashes on separate compilation.
* @compile PointBox.java PointBoxTest.java
* @compile PointBoxTest.java
*/

public class PointBoxTest {
public static void main(String[] args) {
for (int i = 0; i < 1000; i++) {
test1();
test2();
test3();
}
}

static void test1() {
double x = 0.0D;
for (int i = 0; i < 50000; i++) {
PointBox pb = new PointBox();
x = ((PointBox.Point)(pb.p368)).x;
pb.p368 = new PointBox.Point(2.0, 3.0);
}
}

static void test2() {
for (int i = 0; i < 50000; i++) {
PointBox pb = new PointBox();
pb.p368 = new PointBox.Point(2.0, 3.0);
}
}

static PointBox.Point.ref spoint = new PointBox.Point(1.0, 2.0);

static void test3() {
spoint = null;
for (int i = 0; i < 50000; i++) {
PointBox pb = new PointBox();
pb.p368 = spoint;
}
}
}
Expand Up @@ -25,67 +25,50 @@

/*
* @test
* @bug 8255856
* @bug 8255856 8257028
* @summary Generate RestrictedField attributes from annotations
* @modules jdk.compiler/com.sun.tools.javac.util jdk.jdeps/com.sun.tools.javap
* @compile RestrictedTypeAnnotationCodegenTest.java
* @run main/othervm -Xverify:none RestrictedTypeAnnotationCodegenTest
* @modules jdk.compiler
* @modules jdk.jdeps/com.sun.tools.classfile
* @compile -XDallowWithFieldOperator Point.java
* @run main RestrictedTypeAnnotationCodegenTest
*/

import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Paths;
import com.sun.tools.classfile.*;
import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info;

import java.lang.invoke.RestrictedType;

final class PointBox {

@RestrictedType("QPoint;") Object p;

}

public class RestrictedTypeAnnotationCodegenTest {

public static void main(String [] args) {
new RestrictedTypeAnnotationCodegenTest().run();
}
inline class Point {}
inline class Line {}

void run() {
String [] params = new String [] { "-v",
Paths.get(System.getProperty("test.classes"),
"PointBox.class").toString() };
runCheck(params, new String [] {
"java.lang.Object p;",
"descriptor: Ljava/lang/Object;",
"RestrictedField: #11 // QPoint;",
"RuntimeVisibleTypeAnnotations:",
"0: #14(#15=s#11): FIELD",
"java.lang.invoke.RestrictedType(",
"value=\"QPoint;\""
});
@RestrictedType("QRestrictedTypeAnnotationCodegenTest$Line;")
public Object jloFld = null;

}
@RestrictedType("QRestrictedTypeAnnotationCodegenTest$Point;")
public Point.ref refProjFld = null;

void runCheck(String [] params, String [] expectedOut) {
StringWriter s;
String out;
public static void main(String[] args) throws Exception {
ClassFile cls = ClassFile.read(InlineNestingAttributesTest.class.getResourceAsStream("RestrictedTypeAnnotationCodegenTest.class"));

System.out.println("Checking javap");
try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
com.sun.tools.javap.Main.run(params, pw);
out = s.toString();
}
System.out.println("Javap = " + out);
int errors = 0;
for (String eo: expectedOut) {
if (!out.contains(eo)) {
System.err.println("Match not found for string: " + eo);
errors++;
int goodFlds = 0;
for (Field fld: cls.fields) {
if (fld.getName(cls.constant_pool).equals("jloFld")) {
String desc = fld.descriptor.getValue(cls.constant_pool);
RestrictedField_attribute rfa =
(RestrictedField_attribute) fld.attributes.get(Attribute.RestrictedField);
if (rfa.getRestrictedType(cls.constant_pool).equals("QRestrictedTypeAnnotationCodegenTest$Line;") && desc.equals("Ljava/lang/Object;"))
goodFlds++;
} else if (fld.getName(cls.constant_pool).equals("refProjFld")) {
String desc = fld.descriptor.getValue(cls.constant_pool);
RestrictedField_attribute rfa =
(RestrictedField_attribute) fld.attributes.get(Attribute.RestrictedField);
if (rfa.getRestrictedType(cls.constant_pool).equals("QRestrictedTypeAnnotationCodegenTest$Point;") && desc.equals("LRestrictedTypeAnnotationCodegenTest$Point$ref;"))
goodFlds++;
}
}
if (errors > 0) {
throw new AssertionError("Unexpected javap output: " + out);
}
if (goodFlds != 2) {
throw new AssertionError("Lookup for 2 fields failed: Found only " + goodFlds);
}
}
}