Skip to content

Commit

Permalink
Fix class literal-valued annotation defaults
Browse files Browse the repository at this point in the history
The type of a class literal isn't necessarily a class, as in
`void.class`.

MOE_MIGRATED_REVID=218197987
  • Loading branch information
cushon committed Oct 22, 2018
1 parent 7867013 commit 7ef8412
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
8 changes: 6 additions & 2 deletions java/com/google/turbine/binder/bytecode/BytecodeBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.turbine.bytecode.sig.Sig.LowerBoundTySig;
import com.google.turbine.bytecode.sig.Sig.UpperBoundTySig;
import com.google.turbine.bytecode.sig.Sig.WildTySig;
import com.google.turbine.bytecode.sig.SigParser;
import com.google.turbine.model.Const;
import com.google.turbine.model.Const.ArrayInitValue;
import com.google.turbine.type.AnnoInfo;
Expand Down Expand Up @@ -118,8 +119,11 @@ public static Const bindValue(Type type, ElementValue value) {
return bindArrayValue(type, (ArrayValue) value);
case CLASS:
return new ClassValue(
Type.ClassTy.asNonParametricClassTy(
asClassSymbol(((ConstClassValue) value).className())));
bindTy(
new SigParser(((ConstClassValue) value).className()).parseType(),
x -> {
throw new IllegalStateException(x);
}));
case ANNOTATION:
return bindAnnotationValue(type, ((ElementValue.AnnotationValue) value).annotation());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteStreams;
import com.google.turbine.binder.bound.ClassValue;
import com.google.turbine.binder.bound.TypeBoundClass;
import com.google.turbine.binder.bound.TypeBoundClass.MethodInfo;
import com.google.turbine.binder.env.CompoundEnv;
import com.google.turbine.binder.env.Env;
import com.google.turbine.binder.env.SimpleEnv;
import com.google.turbine.binder.sym.ClassSymbol;
import com.google.turbine.type.Type;
import com.google.turbine.type.Type.ClassTy;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -92,6 +94,23 @@ public void methodTypes() {
assertThat(m.exceptions()).hasSize(2);
}

@interface VoidAnno {
Class<?> a() default void.class;

Class<?> b() default int[].class;
}

@Test
public void voidAnno() {
BytecodeBoundClass c = getBytecodeBoundClass(VoidAnno.class);

assertThat(c.methods()).hasSize(2);
assertThat(((ClassValue) c.methods().get(0).defaultValue()).type().tyKind())
.isEqualTo(Type.TyKind.VOID_TY);
assertThat(((ClassValue) c.methods().get(1).defaultValue()).type().tyKind())
.isEqualTo(Type.TyKind.ARRAY_TY);
}

private static byte[] toByteArrayOrDie(InputStream is) {
try {
return ByteStreams.toByteArray(is);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ public static Iterable<Object[]> parameters() {
// https://bugs.openjdk.java.net/browse/JDK-8054064 ?
"shadow_inherited.test",
"static_final_boxed.test",
"anno_void.test",
};
List<Object[]> tests =
ImmutableList.copyOf(testCases).stream().map(x -> new Object[] {x}).collect(toList());
Expand Down
8 changes: 8 additions & 0 deletions javatests/com/google/turbine/lower/testdata/anno_void.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== V.java ===
@interface V {
Class<?> value() default void.class;
}
=== A.java ===
@V()
class A {
}

0 comments on commit 7ef8412

Please sign in to comment.