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

8255856: Generate RestrictedField attributes from annotations #277

Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,49 @@
/*
* 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.
*/

package java.lang.invoke;

import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;

/**
* Annotation to facilitate type-restrictions experiments.
*
* When javac generates code for a field whose type is annotated by @RestrictedType("QFoo;"),
* it generates a RestrictedField attribute pointing to a Utf8 constant representing
* the given String.
* The @RestrictedType annotation supports ad hoc attribute generation,
* for more fine-grained control.
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
public @interface RestrictedType {
/**
* @return the type descriptor string to be encoded in the RestrictedField attribute as a Utf8 constant
*/
String value();
}
@@ -223,6 +223,7 @@ public static Symtab instance(Context context) {
public final Type typeDescriptorType;
public final Type recordType;
public final Type identityObjectType;
public final Type restrictedTypeType;

/** The symbol representing the length field of an array.
*/
@@ -595,6 +596,7 @@ public String toString() {
previewFeatureType = enterClass("jdk.internal.PreviewFeature");
previewFeatureInternalType = enterSyntheticAnnotation("jdk.internal.PreviewFeature+Annotation");
typeDescriptorType = enterClass("java.lang.invoke.TypeDescriptor");
restrictedTypeType = enterClass("java.lang.invoke.RestrictedType");
recordType = enterClass("java.lang.Record");
identityObjectType = enterClass("java.lang.IdentityObject");

@@ -974,11 +974,21 @@ void writeField(VarSymbol v) {
}
databuf.appendChar(poolWriter.putName(v.name));
boolean emitRestrictedField = false;
int restrictedFieldDescriptor = 0;
if (types.flattenWithTypeRestrictions && v.type.isValue()) {
emitRestrictedField = true;
databuf.appendChar(poolWriter.putDescriptor(v.type.referenceProjection()));
} else {
databuf.appendChar(poolWriter.putDescriptor(v));
for (Attribute.Compound anno : v.type.getAnnotationMirrors()) {
if (anno.type.tsym == syms.restrictedTypeType.tsym) {
Attribute member = anno.member(names.value);
Assert.check(member.type.tsym == syms.stringType.tsym);
String utf8 = (String) member.getValue();
restrictedFieldDescriptor = poolWriter.putName(names.fromString(utf8));
emitRestrictedField = true;
}
}
}

int acountIdx = beginAttrs();
@@ -991,7 +1001,11 @@ void writeField(VarSymbol v) {
}
if (emitRestrictedField) {
int alenIdx = writeAttr(names.RestrictedField);
databuf.appendChar(poolWriter.putDescriptor(v));
if (types.flattenWithTypeRestrictions && v.type.isValue()) {
databuf.appendChar(poolWriter.putDescriptor(v));
} else {
databuf.appendChar(restrictedFieldDescriptor);
}
endAttr(alenIdx);
acount++;
}
@@ -0,0 +1,91 @@
/*
* 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 8255856
* @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
*/

import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Paths;

import java.lang.invoke.RestrictedType;

final class PointBox {

@RestrictedType("QPoint;") Object p;

}

public class RestrictedTypeAnnotationCodegenTest {

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

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;\""
});

}

void runCheck(String [] params, String [] expectedOut) {
StringWriter s;
String out;

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++;
}
}
if (errors > 0) {
throw new AssertionError("Unexpected javap output: " + out);
}
}
}