Skip to content

Commit 6eff931

Browse files
committedDec 9, 2020
8256950: Add record attribute support to symbol generator CreateSymbols
Reviewed-by: jjg, chegar
1 parent f148915 commit 6eff931

File tree

10 files changed

+563
-110
lines changed

10 files changed

+563
-110
lines changed
 

‎make/langtools/src/classes/build/tools/symbolgenerator/CreateSymbols.java

+152-4
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
import com.sun.tools.classfile.InnerClasses_attribute;
123123
import com.sun.tools.classfile.InnerClasses_attribute.Info;
124124
import com.sun.tools.classfile.Method;
125+
import com.sun.tools.classfile.MethodParameters_attribute;
125126
import com.sun.tools.classfile.ModuleResolution_attribute;
126127
import com.sun.tools.classfile.ModuleTarget_attribute;
127128
import com.sun.tools.classfile.Module_attribute;
@@ -131,6 +132,8 @@
131132
import com.sun.tools.classfile.Module_attribute.RequiresEntry;
132133
import com.sun.tools.classfile.NestHost_attribute;
133134
import com.sun.tools.classfile.NestMembers_attribute;
135+
import com.sun.tools.classfile.Record_attribute;
136+
import com.sun.tools.classfile.Record_attribute.ComponentInfo;
134137
import com.sun.tools.classfile.RuntimeAnnotations_attribute;
135138
import com.sun.tools.classfile.RuntimeInvisibleAnnotations_attribute;
136139
import com.sun.tools.classfile.RuntimeInvisibleParameterAnnotations_attribute;
@@ -959,6 +962,22 @@ private void addAttributes(ClassHeaderDescription header,
959962
attributes.put(Attribute.NestMembers,
960963
new NestMembers_attribute(attributeString, nestMembers));
961964
}
965+
if (header.isRecord) {
966+
assert header.recordComponents != null;
967+
int attributeString = addString(constantPool, Attribute.Record);
968+
ComponentInfo[] recordComponents = new ComponentInfo[header.recordComponents.size()];
969+
int i = 0;
970+
for (RecordComponentDescription rcd : header.recordComponents) {
971+
int name = addString(constantPool, rcd.name);
972+
Descriptor desc = new Descriptor(addString(constantPool, rcd.descriptor));
973+
Map<String, Attribute> nestedAttrs = new HashMap<>();
974+
addGenericAttributes(rcd, constantPool, nestedAttrs);
975+
Attributes attrs = new Attributes(nestedAttrs);
976+
recordComponents[i++] = new ComponentInfo(name, desc, attrs);
977+
}
978+
attributes.put(Attribute.Record,
979+
new Record_attribute(attributeString, recordComponents));
980+
}
962981
addInnerClassesAttribute(header, constantPool, attributes);
963982
}
964983

@@ -1017,6 +1036,18 @@ private void addAttributes(MethodDescription desc, List<CPInfo> constantPool, Ma
10171036
new RuntimeVisibleParameterAnnotations_attribute(attributeString,
10181037
annotations));
10191038
}
1039+
if (desc.methodParameters != null && !desc.methodParameters.isEmpty()) {
1040+
int attributeString =
1041+
addString(constantPool, Attribute.MethodParameters);
1042+
MethodParameters_attribute.Entry[] entries =
1043+
desc.methodParameters
1044+
.stream()
1045+
.map(p -> new MethodParameters_attribute.Entry(addString(constantPool, p.name),
1046+
p.flags))
1047+
.toArray(s -> new MethodParameters_attribute.Entry[s]);
1048+
attributes.put(Attribute.MethodParameters,
1049+
new MethodParameters_attribute(attributeString, entries));
1050+
}
10201051
}
10211052

10221053
private void addAttributes(FieldDescription desc, List<CPInfo> constantPool, Map<String, Attribute> attributes) {
@@ -1595,7 +1626,9 @@ private void dumpDescriptions(ClassList classes,
15951626
StringWriter data = new StringWriter();
15961627
ModuleDescription module = modules.get(e.getKey());
15971628

1598-
module.write(data, desc.basePlatform, desc.version);
1629+
if (module != null) { //module == null should only be in tests.
1630+
module.write(data, desc.basePlatform, desc.version);
1631+
}
15991632

16001633
for (ClassDescription clazz : e.getValue()) {
16011634
clazz.write(data, desc.basePlatform, desc.version);
@@ -2153,6 +2186,37 @@ private boolean readAttribute(ClassFile cf, FeatureDescription feature, Attribut
21532186
.collect(Collectors.toList());
21542187
break;
21552188
}
2189+
case Attribute.Record: {
2190+
assert feature instanceof ClassHeaderDescription;
2191+
Record_attribute record = (Record_attribute) attr;
2192+
List<RecordComponentDescription> components = new ArrayList<>();
2193+
for (ComponentInfo info : record.component_info_arr) {
2194+
RecordComponentDescription rcd = new RecordComponentDescription();
2195+
rcd.name = info.getName(cf.constant_pool);
2196+
rcd.descriptor = info.descriptor.getValue(cf.constant_pool);
2197+
for (Attribute nestedAttr : info.attributes) {
2198+
readAttribute(cf, rcd, nestedAttr);
2199+
}
2200+
components.add(rcd);
2201+
}
2202+
ClassHeaderDescription chd = (ClassHeaderDescription) feature;
2203+
chd.isRecord = true;
2204+
chd.recordComponents = components;
2205+
break;
2206+
}
2207+
case Attribute.MethodParameters: {
2208+
assert feature instanceof MethodDescription;
2209+
MethodParameters_attribute params = (MethodParameters_attribute) attr;
2210+
MethodDescription method = (MethodDescription) feature;
2211+
method.methodParameters = new ArrayList<>();
2212+
for (MethodParameters_attribute.Entry e : params.method_parameter_table) {
2213+
String name = cf.constant_pool.getUTF8Value(e.name_index);
2214+
MethodDescription.MethodParam param =
2215+
new MethodDescription.MethodParam(e.flags, name);
2216+
method.methodParameters.add(param);
2217+
}
2218+
break;
2219+
}
21562220
default:
21572221
throw new IllegalStateException("Unhandled attribute: " +
21582222
attrName);
@@ -2999,6 +3063,8 @@ static class ClassHeaderDescription extends HeaderDescription {
29993063
List<String> implementsAttr;
30003064
String nestHost;
30013065
List<String> nestMembers;
3066+
boolean isRecord;
3067+
List<RecordComponentDescription> recordComponents;
30023068

30033069
@Override
30043070
public int hashCode() {
@@ -3007,6 +3073,8 @@ public int hashCode() {
30073073
hash = 17 * hash + Objects.hashCode(this.implementsAttr);
30083074
hash = 17 * hash + Objects.hashCode(this.nestHost);
30093075
hash = 17 * hash + Objects.hashCode(this.nestMembers);
3076+
hash = 17 * hash + Objects.hashCode(this.isRecord);
3077+
hash = 17 * hash + Objects.hashCode(this.recordComponents);
30103078
return hash;
30113079
}
30123080

@@ -3031,6 +3099,12 @@ public boolean equals(Object obj) {
30313099
if (!listEquals(this.nestMembers, other.nestMembers)) {
30323100
return false;
30333101
}
3102+
if (this.isRecord != other.isRecord) {
3103+
return false;
3104+
}
3105+
if (!listEquals(this.recordComponents, other.recordComponents)) {
3106+
return false;
3107+
}
30343108
return true;
30353109
}
30363110

@@ -3048,8 +3122,12 @@ public void write(Appendable output, String baselineVersion, String version) thr
30483122
output.append(" nestHost " + nestHost);
30493123
if (nestMembers != null && !nestMembers.isEmpty())
30503124
output.append(" nestMembers " + serializeList(nestMembers));
3125+
if (isRecord) {
3126+
output.append(" record true");
3127+
}
30513128
writeAttributes(output);
30523129
output.append("\n");
3130+
writeRecordComponents(output, baselineVersion, version);
30533131
writeInnerClasses(output, baselineVersion, version);
30543132
}
30553133

@@ -3065,14 +3143,37 @@ public boolean read(LineBasedReader reader) throws IOException {
30653143
nestHost = reader.attributes.get("nestHost");
30663144
String nestMembersList = reader.attributes.get("nestMembers");
30673145
nestMembers = deserializeList(nestMembersList);
3146+
isRecord = reader.attributes.containsKey("record");
30683147

30693148
readAttributes(reader);
30703149
reader.moveNext();
3150+
if (isRecord) {
3151+
readRecordComponents(reader);
3152+
}
30713153
readInnerClasses(reader);
30723154

30733155
return true;
30743156
}
30753157

3158+
protected void writeRecordComponents(Appendable output,
3159+
String baselineVersion,
3160+
String version) throws IOException {
3161+
if (recordComponents != null) {
3162+
for (RecordComponentDescription rcd : recordComponents) {
3163+
rcd.write(output, "", "");
3164+
}
3165+
}
3166+
}
3167+
3168+
protected void readRecordComponents(LineBasedReader reader) throws IOException {
3169+
recordComponents = new ArrayList<>();
3170+
3171+
while ("recordcomponent".equals(reader.lineKey)) {
3172+
RecordComponentDescription rcd = new RecordComponentDescription();
3173+
rcd.read(reader);
3174+
recordComponents.add(rcd);
3175+
}
3176+
}
30763177
}
30773178

30783179
static abstract class HeaderDescription extends FeatureDescription {
@@ -3145,6 +3246,7 @@ static class MethodDescription extends FeatureDescription {
31453246
Object annotationDefaultValue;
31463247
List<List<AnnotationDescription>> classParameterAnnotations;
31473248
List<List<AnnotationDescription>> runtimeParameterAnnotations;
3249+
List<MethodParam> methodParameters;
31483250

31493251
public MethodDescription() {
31503252
flagsNormalization = METHODS_FLAGS_NORMALIZATION;
@@ -3221,6 +3323,15 @@ public void write(Appendable output, String baselineVersion, String version) thr
32213323
output.append(";");
32223324
}
32233325
}
3326+
if (methodParameters != null && !methodParameters.isEmpty()) {
3327+
Function<MethodParam, String> param2String =
3328+
p -> Integer.toHexString(p.flags) + ":" + p.name;
3329+
List<String> paramsAsStrings =
3330+
methodParameters.stream()
3331+
.map(param2String)
3332+
.collect(Collectors.toList());
3333+
output.append(" methodParameters " + serializeList(paramsAsStrings));
3334+
}
32243335
output.append("\n");
32253336
}
32263337

@@ -3268,17 +3379,41 @@ public boolean read(LineBasedReader reader) throws IOException {
32683379
runtimeParameterAnnotations = annos;
32693380
}
32703381

3382+
String inMethodParameters = reader.attributes.get("methodParameters");
3383+
if (inMethodParameters != null) {
3384+
Function<String, MethodParam> string2Param =
3385+
p -> {
3386+
int sep = p.indexOf(':');
3387+
return new MethodParam(Integer.parseInt(p.substring(0, sep)),
3388+
p.substring(sep + 1));
3389+
};
3390+
methodParameters =
3391+
deserializeList(inMethodParameters).stream()
3392+
.map(string2Param)
3393+
.collect(Collectors.toList());
3394+
}
3395+
32713396
reader.moveNext();
32723397

32733398
return true;
32743399
}
32753400

3401+
public static class MethodParam {
3402+
public final int flags;
3403+
public final String name;
3404+
3405+
public MethodParam(int flags, String name) {
3406+
this.flags = flags;
3407+
this.name = name;
3408+
}
3409+
}
32763410
}
32773411

32783412
static class FieldDescription extends FeatureDescription {
32793413
String name;
32803414
String descriptor;
32813415
Object constantValue;
3416+
String keyName = "field";
32823417

32833418
@Override
32843419
public int hashCode() {
@@ -3315,13 +3450,13 @@ public void write(Appendable output, String baselineVersion, String version) thr
33153450
if (shouldIgnore(baselineVersion, version))
33163451
return ;
33173452
if (!versions.contains(version)) {
3318-
output.append("-field");
3453+
output.append("-" + keyName);
33193454
output.append(" name " + quote(name, false));
33203455
output.append(" descriptor " + quote(descriptor, false));
33213456
output.append("\n");
33223457
return ;
33233458
}
3324-
output.append("field");
3459+
output.append(keyName);
33253460
output.append(" name " + name);
33263461
output.append(" descriptor " + descriptor);
33273462
if (constantValue != null) {
@@ -3333,7 +3468,7 @@ public void write(Appendable output, String baselineVersion, String version) thr
33333468

33343469
@Override
33353470
public boolean read(LineBasedReader reader) throws IOException {
3336-
if (!"field".equals(reader.lineKey))
3471+
if (!keyName.equals(reader.lineKey))
33373472
return false;
33383473

33393474
name = reader.attributes.get("name");
@@ -3366,6 +3501,19 @@ public boolean read(LineBasedReader reader) throws IOException {
33663501

33673502
}
33683503

3504+
static final class RecordComponentDescription extends FieldDescription {
3505+
3506+
public RecordComponentDescription() {
3507+
this.keyName = "recordcomponent";
3508+
}
3509+
3510+
@Override
3511+
protected boolean shouldIgnore(String baselineVersion, String version) {
3512+
return false;
3513+
}
3514+
3515+
}
3516+
33693517
static final class AnnotationDescription {
33703518
String annotationType;
33713519
Map<String, Object> values;

‎src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symbol.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,7 @@ public RecordComponent getRecordComponent(JCVariableDecl var, boolean addIfMissi
15161516
}
15171517
RecordComponent rc = null;
15181518
if (addIfMissing) {
1519-
recordComponents = recordComponents.append(rc = new RecordComponent(var, annotations));
1519+
recordComponents = recordComponents.append(rc = new RecordComponent(var.sym, annotations));
15201520
}
15211521
return rc;
15221522
}
@@ -1527,6 +1527,10 @@ public List<? extends RecordComponent> getRecordComponents() {
15271527
return recordComponents;
15281528
}
15291529

1530+
public void setRecordComponents(List<RecordComponent> recordComponents) {
1531+
this.recordComponents = recordComponents;
1532+
}
1533+
15301534
@DefinedBy(Api.LANGUAGE_MODEL)
15311535
public NestingKind getNestingKind() {
15321536
apiComplete();
@@ -1790,10 +1794,17 @@ public static class RecordComponent extends VarSymbol implements RecordComponent
17901794
/**
17911795
* Construct a record component, given its flags, name, type and owner.
17921796
*/
1793-
public RecordComponent(JCVariableDecl fieldDecl, List<JCAnnotation> annotations) {
1794-
super(PUBLIC, fieldDecl.sym.name, fieldDecl.sym.type, fieldDecl.sym.owner);
1797+
public RecordComponent(Name name, Type type, Symbol owner) {
1798+
super(PUBLIC, name, type, owner);
1799+
pos = -1;
1800+
originalAnnos = List.nil();
1801+
isVarargs = false;
1802+
}
1803+
1804+
public RecordComponent(VarSymbol field, List<JCAnnotation> annotations) {
1805+
super(PUBLIC, field.name, field.type, field.owner);
17951806
this.originalAnnos = annotations;
1796-
this.pos = fieldDecl.pos;
1807+
this.pos = field.pos;
17971808
/* it is better to store the original information for this one, instead of relying
17981809
* on the info in the type of the symbol. This is because on the presence of APs
17991810
* the symbol will be blown out and we won't be able to know if the original

‎src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,16 @@ protected void read(Symbol sym, int attrLen) {
12051205
if (sym.kind == TYP) {
12061206
sym.flags_field |= RECORD;
12071207
}
1208-
bp = bp + attrLen;
1208+
int componentCount = nextChar();
1209+
ListBuffer<RecordComponent> components = new ListBuffer<>();
1210+
for (int i = 0; i < componentCount; i++) {
1211+
Name name = poolReader.getName(nextChar());
1212+
Type type = poolReader.getType(nextChar());
1213+
RecordComponent c = new RecordComponent(name, type, sym);
1214+
readAttrs(c, AttributeKind.MEMBER);
1215+
components.add(c);
1216+
}
1217+
((ClassSymbol) sym).setRecordComponents(components.toList());
12091218
}
12101219
},
12111220
new AttributeReader(names.PermittedSubclasses, V59, CLASS_ATTRIBUTE) {

‎src/jdk.compiler/share/classes/com/sun/tools/javac/processing/PrintingProcessor.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public PrintingElementVisitor visitType(TypeElement e, Boolean p) {
229229
writer.print("(");
230230
writer.print(e.getRecordComponents()
231231
.stream()
232-
.map(recordDes -> recordDes.asType().toString() + " " + recordDes.getSimpleName())
232+
.map(recordDes -> annotationsToString(recordDes) + recordDes.asType().toString() + " " + recordDes.getSimpleName())
233233
.collect(Collectors.joining(", ")));
234234
writer.print(")");
235235
}
@@ -448,15 +448,15 @@ private void printDocComment(Element e) {
448448

449449
private void printModifiers(Element e) {
450450
ElementKind kind = e.getKind();
451-
if (kind == PARAMETER) {
451+
if (kind == PARAMETER || kind == RECORD_COMPONENT) {
452452
// Print annotation inline
453453
writer.print(annotationsToString(e));
454454
} else {
455455
printAnnotations(e);
456456
indent();
457457
}
458458

459-
if (kind == ENUM_CONSTANT)
459+
if (kind == ENUM_CONSTANT || kind == RECORD_COMPONENT)
460460
return;
461461

462462
Set<Modifier> modifiers = new LinkedHashSet<>();

0 commit comments

Comments
 (0)
Please sign in to comment.