From 7ef841275f620043690128b9aba1f9476609f449 Mon Sep 17 00:00:00 2001 From: cushon Date: Mon, 22 Oct 2018 11:06:36 -0700 Subject: [PATCH] Fix class literal-valued annotation defaults The type of a class literal isn't necessarily a class, as in `void.class`. MOE_MIGRATED_REVID=218197987 --- .../binder/bytecode/BytecodeBinder.java | 8 ++++++-- .../bytecode/BytecodeBoundClassTest.java | 19 +++++++++++++++++++ .../turbine/lower/LowerIntegrationTest.java | 1 + .../turbine/lower/testdata/anno_void.test | 8 ++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 javatests/com/google/turbine/lower/testdata/anno_void.test diff --git a/java/com/google/turbine/binder/bytecode/BytecodeBinder.java b/java/com/google/turbine/binder/bytecode/BytecodeBinder.java index 2fc600f2..bfe45be3 100644 --- a/java/com/google/turbine/binder/bytecode/BytecodeBinder.java +++ b/java/com/google/turbine/binder/bytecode/BytecodeBinder.java @@ -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; @@ -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()); } diff --git a/javatests/com/google/turbine/binder/bytecode/BytecodeBoundClassTest.java b/javatests/com/google/turbine/binder/bytecode/BytecodeBoundClassTest.java index da59aa6e..5529e30e 100644 --- a/javatests/com/google/turbine/binder/bytecode/BytecodeBoundClassTest.java +++ b/javatests/com/google/turbine/binder/bytecode/BytecodeBoundClassTest.java @@ -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; @@ -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); diff --git a/javatests/com/google/turbine/lower/LowerIntegrationTest.java b/javatests/com/google/turbine/lower/LowerIntegrationTest.java index 5bcab470..f7e9c182 100644 --- a/javatests/com/google/turbine/lower/LowerIntegrationTest.java +++ b/javatests/com/google/turbine/lower/LowerIntegrationTest.java @@ -307,6 +307,7 @@ public static Iterable parameters() { // https://bugs.openjdk.java.net/browse/JDK-8054064 ? "shadow_inherited.test", "static_final_boxed.test", + "anno_void.test", }; List tests = ImmutableList.copyOf(testCases).stream().map(x -> new Object[] {x}).collect(toList()); diff --git a/javatests/com/google/turbine/lower/testdata/anno_void.test b/javatests/com/google/turbine/lower/testdata/anno_void.test new file mode 100644 index 00000000..280302eb --- /dev/null +++ b/javatests/com/google/turbine/lower/testdata/anno_void.test @@ -0,0 +1,8 @@ +=== V.java === +@interface V { + Class value() default void.class; +} +=== A.java === +@V() +class A { +} \ No newline at end of file