Skip to content

Commit

Permalink
8336756: Improve ClassFile Annotation writing
Browse files Browse the repository at this point in the history
  • Loading branch information
liach committed Aug 19, 2024
1 parent 55851a3 commit 549d1bd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ sealed interface OfArray extends AnnotationValue
* @since 22
*/
@PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API)
sealed interface OfConstant
extends AnnotationValue
permits OfString, OfDouble, OfFloat, OfLong, OfInt, OfShort, OfChar, OfByte,
OfBoolean, AnnotationImpl.OfConstantImpl {
sealed interface OfConstant extends AnnotationValue {
/**
* {@return the constant pool entry backing this constant element}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,66 +32,34 @@
import static java.lang.classfile.ClassFile.*;

public record AnnotationImpl(Utf8Entry className, List<AnnotationElement> elements)
implements Annotation, Util.Writable {
implements Annotation {
public AnnotationImpl {
elements = List.copyOf(elements);
}

@Override
public void writeTo(BufWriterImpl buf) {
buf.writeIndex(className());
buf.writeU2(elements().size());
for (var e : elements) {
buf.writeIndex(e.name());
AnnotationReader.writeAnnotationValue(buf, e.value());
}
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder("Annotation[");
sb.append(className().stringValue());
List<AnnotationElement> evps = elements();
if (!evps.isEmpty())
sb.append(" [");
for (AnnotationElement evp : evps) {
sb.append(evp.name().stringValue())
.append("=")
.append(evp.value().toString())
.append(", ");
}
if (!evps.isEmpty()) {
sb.delete(sb.length()-1, sb.length());
sb.append("]");
sb.append(' ').append(evps);
}
sb.append("]");
return sb.toString();
}

public record AnnotationElementImpl(Utf8Entry name,
AnnotationValue value)
implements AnnotationElement, Util.Writable {

implements AnnotationElement {
@Override
public void writeTo(BufWriterImpl buf) {
buf.writeIndex(name());
AnnotationReader.writeAnnotationValue(buf, value());
public String toString() {
return name + "=" + value;
}
}

public sealed interface OfConstantImpl extends AnnotationValue.OfConstant, Util.Writable {

@Override
default void writeTo(BufWriterImpl buf) {
buf.writeU1(tag());
buf.writeIndex(constant());
}

}

public record OfStringImpl(Utf8Entry constant)
implements OfConstantImpl, AnnotationValue.OfString {

implements AnnotationValue.OfString {
@Override
public char tag() {
return AEV_STRING;
Expand All @@ -104,8 +72,7 @@ public String stringValue() {
}

public record OfDoubleImpl(DoubleEntry constant)
implements OfConstantImpl, AnnotationValue.OfDouble {

implements AnnotationValue.OfDouble {
@Override
public char tag() {
return AEV_DOUBLE;
Expand All @@ -118,8 +85,7 @@ public double doubleValue() {
}

public record OfFloatImpl(FloatEntry constant)
implements OfConstantImpl, AnnotationValue.OfFloat {

implements AnnotationValue.OfFloat {
@Override
public char tag() {
return AEV_FLOAT;
Expand All @@ -132,8 +98,7 @@ public float floatValue() {
}

public record OfLongImpl(LongEntry constant)
implements OfConstantImpl, AnnotationValue.OfLong {

implements AnnotationValue.OfLong {
@Override
public char tag() {
return AEV_LONG;
Expand All @@ -146,8 +111,7 @@ public long longValue() {
}

public record OfIntImpl(IntegerEntry constant)
implements OfConstantImpl, AnnotationValue.OfInt {

implements AnnotationValue.OfInt {
@Override
public char tag() {
return AEV_INT;
Expand All @@ -160,8 +124,7 @@ public int intValue() {
}

public record OfShortImpl(IntegerEntry constant)
implements OfConstantImpl, AnnotationValue.OfShort {

implements AnnotationValue.OfShort {
@Override
public char tag() {
return AEV_SHORT;
Expand All @@ -174,8 +137,7 @@ public short shortValue() {
}

public record OfCharImpl(IntegerEntry constant)
implements OfConstantImpl, AnnotationValue.OfChar {

implements AnnotationValue.OfChar {
@Override
public char tag() {
return AEV_CHAR;
Expand All @@ -188,8 +150,7 @@ public char charValue() {
}

public record OfByteImpl(IntegerEntry constant)
implements OfConstantImpl, AnnotationValue.OfByte {

implements AnnotationValue.OfByte {
@Override
public char tag() {
return AEV_BYTE;
Expand All @@ -202,8 +163,7 @@ public byte byteValue() {
}

public record OfBooleanImpl(IntegerEntry constant)
implements OfConstantImpl, AnnotationValue.OfBoolean {

implements AnnotationValue.OfBoolean {
@Override
public char tag() {
return AEV_BOOLEAN;
Expand All @@ -216,8 +176,7 @@ public boolean booleanValue() {
}

public record OfArrayImpl(List<AnnotationValue> values)
implements AnnotationValue.OfArray, Util.Writable {

implements AnnotationValue.OfArray {
public OfArrayImpl {
values = List.copyOf(values);
}
Expand All @@ -226,61 +185,29 @@ public record OfArrayImpl(List<AnnotationValue> values)
public char tag() {
return AEV_ARRAY;
}

@Override
public void writeTo(BufWriterImpl buf) {
buf.writeU1(tag());
buf.writeU2(values.size());
for (var e : values) {
AnnotationReader.writeAnnotationValue(buf, e);
}
}

}

public record OfEnumImpl(Utf8Entry className, Utf8Entry constantName)
implements AnnotationValue.OfEnum, Util.Writable {
implements AnnotationValue.OfEnum {
@Override
public char tag() {
return AEV_ENUM;
}

@Override
public void writeTo(BufWriterImpl buf) {
buf.writeU1(tag());
buf.writeIndex(className);
buf.writeIndex(constantName);
}

}

public record OfAnnotationImpl(Annotation annotation)
implements AnnotationValue.OfAnnotation, Util.Writable {
implements AnnotationValue.OfAnnotation {
@Override
public char tag() {
return AEV_ANNOTATION;
}

@Override
public void writeTo(BufWriterImpl buf) {
buf.writeU1(tag());
AnnotationReader.writeAnnotation(buf, annotation);
}

}

public record OfClassImpl(Utf8Entry className)
implements AnnotationValue.OfClass, Util.Writable {
implements AnnotationValue.OfClass {
@Override
public char tag() {
return AEV_CLASS;
}

@Override
public void writeTo(BufWriterImpl buf) {
buf.writeU1(tag());
buf.writeIndex(className);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,13 @@ private static int skipTypeAnnotation(ClassReader classReader, int p) {
}

public static void writeAnnotation(BufWriterImpl buf, Annotation annotation) {
// TODO annotation cleanup later
((Util.Writable) annotation).writeTo(buf);
buf.writeIndex(annotation.className());
var elements = annotation.elements();
buf.writeU2(elements.size());
for (var e : elements) {
buf.writeIndex(e.name());
AnnotationReader.writeAnnotationValue(buf, e.value());
}
}

public static void writeAnnotations(BufWriter buf, List<Annotation> list) {
Expand Down Expand Up @@ -354,7 +359,26 @@ public static void writeTypeAnnotations(BufWriter buf, List<TypeAnnotation> list
}

public static void writeAnnotationValue(BufWriterImpl buf, AnnotationValue value) {
// TODO annotation cleanup later
((Util.Writable) value).writeTo(buf);
var tag = value.tag();
buf.writeU1(tag);
switch (value.tag()) {
case AEV_BOOLEAN, AEV_BYTE, AEV_CHAR, AEV_DOUBLE, AEV_FLOAT, AEV_INT, AEV_LONG, AEV_SHORT, AEV_STRING ->
buf.writeIndex(((AnnotationValue.OfConstant) value).constant());
case AEV_CLASS -> buf.writeIndex(((AnnotationValue.OfClass) value).className());
case AEV_ENUM -> {
var enumValue = (AnnotationValue.OfEnum) value;
buf.writeIndex(enumValue.className());
buf.writeIndex(enumValue.constantName());
}
case AEV_ANNOTATION -> writeAnnotation(buf, ((AnnotationValue.OfAnnotation) value).annotation());
case AEV_ARRAY -> {
var array = ((AnnotationValue.OfArray) value).values();
buf.writeU2(array.size());
for (var e : array) {
writeAnnotationValue(buf, e);
}
}
default -> throw new InternalError("Unknown value " + value);
}
}
}

0 comments on commit 549d1bd

Please sign in to comment.