Skip to content

Commit

Permalink
Migrate some type annotation implementation classes to AutoValue
Browse files Browse the repository at this point in the history
I have plans to rely on equals semantics for these classes to match type annotations
read from bytecode up with the type they apply to.

PiperOrigin-RevId: 575852385
  • Loading branch information
cushon authored and Javac Team committed Oct 24, 2023
1 parent 5584bf5 commit 298124b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 72 deletions.
115 changes: 49 additions & 66 deletions java/com/google/turbine/bytecode/ClassFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static java.util.Objects.requireNonNull;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.turbine.bytecode.ClassFile.AnnotationInfo.ElementValue;
import com.google.turbine.model.Const;
Expand Down Expand Up @@ -612,16 +613,13 @@ public enum Kind {
}

/** A JVMS 4.7.20.1 type_parameter_target. */
public static class TypeParameterTarget extends Target {
private final int index;

public TypeParameterTarget(int index) {
this.index = index;
@AutoValue
public abstract static class TypeParameterTarget extends Target {
public static TypeParameterTarget create(int index) {
return new AutoValue_ClassFile_TypeAnnotationInfo_TypeParameterTarget(index);
}

public int index() {
return index;
}
public abstract int index();

@Override
public Target.Kind kind() {
Expand All @@ -630,45 +628,36 @@ public Target.Kind kind() {
}

/** A JVMS 4.7.20.1 supertype_target. */
public static class SuperTypeTarget extends Target {
private final int index;

public SuperTypeTarget(int index) {
this.index = index;
@AutoValue
public abstract static class SuperTypeTarget extends Target {
public static SuperTypeTarget create(int index) {
return new AutoValue_ClassFile_TypeAnnotationInfo_SuperTypeTarget(index);
}

@Override
public Target.Kind kind() {
return Target.Kind.SUPERTYPE;
}

public int index() {
return index;
}
public abstract int index();
}

/** A JVMS 4.7.20.1 type_parameter_bound_target. */
public static class TypeParameterBoundTarget extends Target {
private final int typeParameterIndex;
private final int boundIndex;

public TypeParameterBoundTarget(int typeParameterIndex, int boundIndex) {
this.typeParameterIndex = typeParameterIndex;
this.boundIndex = boundIndex;
@AutoValue
public abstract static class TypeParameterBoundTarget extends Target {
public static TypeParameterBoundTarget create(int typeParameterIndex, int boundIndex) {
return new AutoValue_ClassFile_TypeAnnotationInfo_TypeParameterBoundTarget(
typeParameterIndex, boundIndex);
}

@Override
public Target.Kind kind() {
return Target.Kind.TYPE_PARAMETER_BOUND;
}

public int typeParameterIndex() {
return typeParameterIndex;
}
public abstract int typeParameterIndex();

public int boundIndex() {
return boundIndex;
}
public abstract int boundIndex();
}

/** A JVMS 4.7.20.1 empty_target. */
Expand All @@ -681,39 +670,35 @@ public Target.Kind kind() {
};

/** A JVMS 4.7.20.1 formal_parameter_target. */
public static class FormalParameterTarget extends Target {
private final int index;
@AutoValue
public abstract static class FormalParameterTarget extends Target {

public FormalParameterTarget(int index) {
this.index = index;
public static FormalParameterTarget create(int index) {
return new AutoValue_ClassFile_TypeAnnotationInfo_FormalParameterTarget(index);
}

@Override
public Target.Kind kind() {
return Target.Kind.FORMAL_PARAMETER;
}

public int index() {
return index;
}
public abstract int index();
}

/** A JVMS 4.7.20.1 throws_target. */
public static class ThrowsTarget extends Target {
private final int index;
@AutoValue
public abstract static class ThrowsTarget extends Target {

public ThrowsTarget(int index) {
this.index = index;
public static ThrowsTarget create(int index) {
return new AutoValue_ClassFile_TypeAnnotationInfo_ThrowsTarget(index);
}

@Override
public Target.Kind kind() {
return Target.Kind.THROWS;
}

public int index() {
return index;
}
public abstract int index();
}

/**
Expand All @@ -722,35 +707,36 @@ public int index() {
* <p>Represented as an immutable linked-list of nodes, which is built out by {@code Lower}
* while recursively searching for type annotations to process.
*/
public static class TypePath {
@AutoValue
public abstract static class TypePath {

/** The root type_path_kind, used for initialization. */
public static TypePath root() {
return new TypePath(null, null);
return create(null, null);
}

/** Adds an array type_path_kind entry. */
public TypePath array() {
return new TypePath(TypePath.Kind.ARRAY, this);
return create(TypePath.Kind.ARRAY, this);
}

/** Adds a nested type type_path_kind entry. */
public TypePath nested() {
return new TypePath(TypePath.Kind.NESTED, this);
return create(TypePath.Kind.NESTED, this);
}

/** Adds a wildcard bound type_path_kind entry. */
public TypePath wild() {
return new TypePath(TypePath.Kind.WILDCARD_BOUND, this);
return create(TypePath.Kind.WILDCARD_BOUND, this);
}

/** Adds a type argument type_path_kind entry. */
public TypePath typeArgument(int idx) {
return new TypePath(idx, TypePath.Kind.TYPE_ARGUMENT, this);
return create(idx, TypePath.Kind.TYPE_ARGUMENT, this);
}

/** A type_path_kind. */
enum Kind {
public enum Kind {
ARRAY(0),
NESTED(1),
WILDCARD_BOUND(2),
Expand All @@ -763,35 +749,32 @@ enum Kind {
}
}

private final @Nullable TypePath parent;
private final TypePath.@Nullable Kind kind;
private final int index;
/** The type argument index; set only if the kind is {@code TYPE_ARGUMENT}. */
public abstract int typeArgumentIndex();

private TypePath(TypePath.@Nullable Kind kind, @Nullable TypePath parent) {
// JVMS 4.7.20.2: type_argument_index is 0 if the bound kind is not TYPE_ARGUMENT
this(0, kind, parent);
}
public abstract @Nullable Kind kind();

public abstract @Nullable TypePath parent();

private TypePath(int index, TypePath.@Nullable Kind kind, @Nullable TypePath parent) {
this.index = index;
this.kind = kind;
this.parent = parent;
private static TypePath create(TypePath.@Nullable Kind kind, @Nullable TypePath parent) {
// JVMS 4.7.20.2: type_argument_index is 0 if the bound kind is not TYPE_ARGUMENT
return create(0, kind, parent);
}

/** The type argument index; set only if the kind is {@code TYPE_ARGUMENT}. */
public int typeArgumentIndex() {
return index;
private static TypePath create(
int index, TypePath.@Nullable Kind kind, @Nullable TypePath parent) {
return new AutoValue_ClassFile_TypeAnnotationInfo_TypePath(index, kind, parent);
}

/** The JVMS 4.7.20.2-A serialized value of the type_path_kind. */
public byte tag() {
return (byte) requireNonNull(kind).tag;
return (byte) requireNonNull(kind()).tag;
}

/** Returns a flattened view of the type path. */
public ImmutableList<TypePath> flatten() {
Deque<TypePath> flat = new ArrayDeque<>();
for (TypePath curr = this; requireNonNull(curr).kind != null; curr = curr.parent) {
for (TypePath curr = this; requireNonNull(curr).kind() != null; curr = curr.parent()) {
flat.addFirst(curr);
}
return ImmutableList.copyOf(flat);
Expand Down
12 changes: 6 additions & 6 deletions java/com/google/turbine/lower/Lower.java
Original file line number Diff line number Diff line change
Expand Up @@ -723,12 +723,12 @@ private ImmutableList<TypeAnnotationInfo> classTypeAnnotations(SourceTypeBoundCl
result,
info.superClassType(),
TargetType.SUPERTYPE,
new TypeAnnotationInfo.SuperTypeTarget(-1));
TypeAnnotationInfo.SuperTypeTarget.create(-1));
}
int idx = 0;
for (Type i : info.interfaceTypes()) {
lowerTypeAnnotations(
result, i, TargetType.SUPERTYPE, new TypeAnnotationInfo.SuperTypeTarget(idx++));
result, i, TargetType.SUPERTYPE, TypeAnnotationInfo.SuperTypeTarget.create(idx++));
}
}
typeParameterAnnotations(
Expand All @@ -752,7 +752,7 @@ private ImmutableList<TypeAnnotationInfo> methodTypeAnnotations(MethodInfo m) {
{
int idx = 0;
for (Type e : m.exceptions()) {
lowerTypeAnnotations(result, e, TargetType.METHOD_THROWS, new ThrowsTarget(idx++));
lowerTypeAnnotations(result, e, TargetType.METHOD_THROWS, ThrowsTarget.create(idx++));
}
}

Expand All @@ -777,7 +777,7 @@ private ImmutableList<TypeAnnotationInfo> methodTypeAnnotations(MethodInfo m) {
result,
p.type(),
TargetType.METHOD_FORMAL_PARAMETER,
new TypeAnnotationInfo.FormalParameterTarget(idx++));
TypeAnnotationInfo.FormalParameterTarget.create(idx++));
}
}

Expand All @@ -803,7 +803,7 @@ private void typeParameterAnnotations(
result.add(
new TypeAnnotationInfo(
targetType,
new TypeAnnotationInfo.TypeParameterTarget(typeParameterIndex),
TypeAnnotationInfo.TypeParameterTarget.create(typeParameterIndex),
TypePath.root(),
info));
}
Expand All @@ -817,7 +817,7 @@ private void typeParameterAnnotations(
result,
i,
boundTargetType,
new TypeAnnotationInfo.TypeParameterBoundTarget(typeParameterIndex, boundIndex++));
TypeAnnotationInfo.TypeParameterBoundTarget.create(typeParameterIndex, boundIndex++));
}
typeParameterIndex++;
}
Expand Down

0 comments on commit 298124b

Please sign in to comment.