diff options
author | kmb <kmb@google.com> | 2017-10-03 02:14:25 +0200 |
---|---|---|
committer | Colin Cross <ccross@android.com> | 2017-10-24 12:20:33 -0700 |
commit | 59adc3acdd6a3dd50fa2ffae0cba2370209c86d6 (patch) | |
tree | 1e827642a3d559eca344a4a237625d708a2dda13 /test/java/com/google | |
parent | 5846f33fd5435036751ecf36331386cf6fe655b5 (diff) | |
download | desugar-59adc3acdd6a3dd50fa2ffae0cba2370209c86d6.tar.gz |
add flags to desugar to emit metadata that can be used for double-checking correctness of default and static interface desugaring.
RELNOTES: none
PiperOrigin-RevId: 170779637
GitOrigin-RevId: bdb12ceeb7c23d7d2293e8006d0aa7127a91b973
Change-Id: I6969cc42323b5299a90af9e8084658e9f654825f
Diffstat (limited to 'test/java/com/google')
9 files changed, 244 insertions, 1 deletions
diff --git a/test/java/com/google/devtools/build/android/desugar/DefaultMethodClassFixerTest.java b/test/java/com/google/devtools/build/android/desugar/DefaultMethodClassFixerTest.java index cdc3263..c74febb 100644 --- a/test/java/com/google/devtools/build/android/desugar/DefaultMethodClassFixerTest.java +++ b/test/java/com/google/devtools/build/android/desugar/DefaultMethodClassFixerTest.java @@ -98,7 +98,12 @@ public class DefaultMethodClassFixerTest { private byte[] desugar(ClassReader reader) { ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); DefaultMethodClassFixer fixer = - new DefaultMethodClassFixer(writer, classpathReader, bootclassPath, classLoader); + new DefaultMethodClassFixer( + writer, + classpathReader, + DependencyCollector.NoWriteCollectors.FAIL_ON_MISSING, + bootclassPath, + classLoader); reader.accept(fixer, 0); return writer.toByteArray(); } diff --git a/test/java/com/google/devtools/build/android/desugar/DesugarJava8FunctionalTest.java b/test/java/com/google/devtools/build/android/desugar/DesugarJava8FunctionalTest.java index 8321d75..63d8d27 100644 --- a/test/java/com/google/devtools/build/android/desugar/DesugarJava8FunctionalTest.java +++ b/test/java/com/google/devtools/build/android/desugar/DesugarJava8FunctionalTest.java @@ -23,6 +23,9 @@ import com.google.devtools.build.android.desugar.testdata.java8.ConcreteDefaultI import com.google.devtools.build.android.desugar.testdata.java8.ConcreteOverridesDefaultWithLambda; import com.google.devtools.build.android.desugar.testdata.java8.DefaultInterfaceMethodWithStaticInitializer; import com.google.devtools.build.android.desugar.testdata.java8.DefaultInterfaceWithBridges; +import com.google.devtools.build.android.desugar.testdata.java8.DefaultMethodFromSeparateJava8Target; +import com.google.devtools.build.android.desugar.testdata.java8.DefaultMethodFromSeparateJava8TargetOverridden; +import com.google.devtools.build.android.desugar.testdata.java8.DefaultMethodTransitivelyFromSeparateJava8Target; import com.google.devtools.build.android.desugar.testdata.java8.FunctionWithDefaultMethod; import com.google.devtools.build.android.desugar.testdata.java8.FunctionalInterfaceWithInitializerAndDefaultMethods; import com.google.devtools.build.android.desugar.testdata.java8.GenericDefaultInterfaceWithLambda; @@ -394,4 +397,16 @@ public class DesugarJava8FunctionalTest extends DesugarFunctionalTest { .getExpectedInitializationOrder()); } } + + /** + * Tests that default methods on the classpath are correctly handled. We'll also verify the + * metadata that's emitted for this case to make sure the binary-wide double-check for correct + * desugaring of default and static interface methods keeps working (b/65645388). + */ + @Test + public void testDefaultMethodsInSeparateTarget() { + assertThat(new DefaultMethodFromSeparateJava8Target().dflt()).isEqualTo("dflt"); + assertThat(new DefaultMethodTransitivelyFromSeparateJava8Target().dflt()).isEqualTo("dflt"); + assertThat(new DefaultMethodFromSeparateJava8TargetOverridden().dflt()).isEqualTo("override"); + } } diff --git a/test/java/com/google/devtools/build/android/desugar/dependencies/MetadataCollectorTest.java b/test/java/com/google/devtools/build/android/desugar/dependencies/MetadataCollectorTest.java new file mode 100644 index 0000000..e99abc4 --- /dev/null +++ b/test/java/com/google/devtools/build/android/desugar/dependencies/MetadataCollectorTest.java @@ -0,0 +1,113 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.android.desugar.dependencies; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.collect.ImmutableList; +import com.google.devtools.build.android.desugar.proto.DesugarDeps.Dependency; +import com.google.devtools.build.android.desugar.proto.DesugarDeps.DesugarDepsInfo; +import com.google.devtools.build.android.desugar.proto.DesugarDeps.InterfaceDetails; +import com.google.devtools.build.android.desugar.proto.DesugarDeps.InterfaceWithCompanion; +import com.google.devtools.build.android.desugar.proto.DesugarDeps.Type; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link MetadataCollector}. */ +@RunWith(JUnit4.class) +public class MetadataCollectorTest { + + @Test + public void testEmptyAvoidsOutput() { + assertThat(new MetadataCollector(false).toByteArray()).isNull(); + } + + @Test + public void testAssumeCompanionClass() throws Exception { + MetadataCollector collector = new MetadataCollector(false); + collector.assumeCompanionClass("a", "b$$CC"); + collector.assumeCompanionClass("b", "b$$CC"); + collector.assumeCompanionClass("a", "a$$CC"); + + DesugarDepsInfo info = extractProto(collector); + assertThat(info.getAssumePresentList()) + .containsExactly( + Dependency.newBuilder().setOrigin(wrapType("a")).setTarget(wrapType("b$$CC")).build(), + Dependency.newBuilder().setOrigin(wrapType("b")).setTarget(wrapType("b$$CC")).build(), + Dependency.newBuilder().setOrigin(wrapType("a")).setTarget(wrapType("a$$CC")).build()); + } + + @Test + public void testMissingImplementedInterface() throws Exception { + MetadataCollector collector = new MetadataCollector(true); + collector.missingImplementedInterface("a", "b"); + collector.missingImplementedInterface("a", "c"); + collector.missingImplementedInterface("c", "b"); + + DesugarDepsInfo info = extractProto(collector); + assertThat(info.getMissingInterfaceList()) + .containsExactly( + Dependency.newBuilder().setOrigin(wrapType("a")).setTarget(wrapType("b")).build(), + Dependency.newBuilder().setOrigin(wrapType("a")).setTarget(wrapType("c")).build(), + Dependency.newBuilder().setOrigin(wrapType("c")).setTarget(wrapType("b")).build()); + } + + @Test + public void testRecordExtendedInterfaces() throws Exception { + MetadataCollector collector = new MetadataCollector(false); + collector.recordExtendedInterfaces("a", "b", "c"); + collector.recordExtendedInterfaces("b"); + collector.recordExtendedInterfaces("c", "d"); + + DesugarDepsInfo info = extractProto(collector); + assertThat(info.getInterfaceWithSupertypesList()) + .containsExactly( + InterfaceDetails.newBuilder() + .setOrigin(wrapType("a")) + .addAllExtendedInterface(ImmutableList.of(wrapType("b"), wrapType("c"))) + .build(), + InterfaceDetails.newBuilder() + .setOrigin(wrapType("c")) + .addAllExtendedInterface(ImmutableList.of(wrapType("d"))) + .build()); + } + + @Test + public void testRecordDefaultMethods() throws Exception { + MetadataCollector collector = new MetadataCollector(false); + collector.recordDefaultMethods("a", 0); + collector.recordDefaultMethods("b", 1); + + DesugarDepsInfo info = extractProto(collector); + assertThat(info.getInterfaceWithCompanionList()) + .containsExactly( + InterfaceWithCompanion.newBuilder() + .setOrigin(wrapType("a")) + .setNumDefaultMethods(0) + .build(), + InterfaceWithCompanion.newBuilder() + .setOrigin(wrapType("b")) + .setNumDefaultMethods(1) + .build()); + } + + private static Type wrapType(String name) { + return Type.newBuilder().setBinaryName(name).build(); + } + + private DesugarDepsInfo extractProto(MetadataCollector collector) throws Exception { + return DesugarDepsInfo.parseFrom(collector.toByteArray()); + } +} diff --git a/test/java/com/google/devtools/build/android/desugar/testdata/java8/DefaultMethodFromSeparateJava8Target.java b/test/java/com/google/devtools/build/android/desugar/testdata/java8/DefaultMethodFromSeparateJava8Target.java new file mode 100644 index 0000000..e0e8703 --- /dev/null +++ b/test/java/com/google/devtools/build/android/desugar/testdata/java8/DefaultMethodFromSeparateJava8Target.java @@ -0,0 +1,20 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.android.desugar.testdata.java8; + +import com.google.devtools.build.android.desugar.testdata.separate8.SeparateInterfaceWithDefaultMethod; + +/** Test class that inherits default method defined in separate target for testing b/65645388. */ +public class DefaultMethodFromSeparateJava8Target + implements SeparateInterfaceWithDefaultMethod {} diff --git a/test/java/com/google/devtools/build/android/desugar/testdata/java8/DefaultMethodFromSeparateJava8TargetOverridden.java b/test/java/com/google/devtools/build/android/desugar/testdata/java8/DefaultMethodFromSeparateJava8TargetOverridden.java new file mode 100644 index 0000000..1613f8e --- /dev/null +++ b/test/java/com/google/devtools/build/android/desugar/testdata/java8/DefaultMethodFromSeparateJava8TargetOverridden.java @@ -0,0 +1,25 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.android.desugar.testdata.java8; + +import com.google.devtools.build.android.desugar.testdata.separate8.SeparateInterfaceThatInheritsDefaultMethod; + +/** Test class that overrides default method defined in separate target for testing b/65645388. */ +public class DefaultMethodFromSeparateJava8TargetOverridden + implements SeparateInterfaceThatInheritsDefaultMethod { + @Override + public String dflt() { + return "override"; + } +} diff --git a/test/java/com/google/devtools/build/android/desugar/testdata/java8/DefaultMethodTransitivelyFromSeparateJava8Target.java b/test/java/com/google/devtools/build/android/desugar/testdata/java8/DefaultMethodTransitivelyFromSeparateJava8Target.java new file mode 100644 index 0000000..693eaf7 --- /dev/null +++ b/test/java/com/google/devtools/build/android/desugar/testdata/java8/DefaultMethodTransitivelyFromSeparateJava8Target.java @@ -0,0 +1,23 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.android.desugar.testdata.java8; + +import com.google.devtools.build.android.desugar.testdata.separate8.SeparateInterfaceThatInheritsDefaultMethod; + +/** + * Test class that transitively inherits default method defined in separate target for testing + * b/65645388. + */ +public class DefaultMethodTransitivelyFromSeparateJava8Target + implements SeparateInterfaceThatInheritsDefaultMethod {} diff --git a/test/java/com/google/devtools/build/android/desugar/testdata/separate8/SeparateInterfaceThatInheritsDefaultMethod.java b/test/java/com/google/devtools/build/android/desugar/testdata/separate8/SeparateInterfaceThatInheritsDefaultMethod.java new file mode 100644 index 0000000..400a6f1 --- /dev/null +++ b/test/java/com/google/devtools/build/android/desugar/testdata/separate8/SeparateInterfaceThatInheritsDefaultMethod.java @@ -0,0 +1,18 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.android.desugar.testdata.separate8; + +/** Interface that inherits default method in separate compilation target for testing b/65645388. */ +public interface SeparateInterfaceThatInheritsDefaultMethod + extends SeparateInterfaceWithDefaultMethod {} diff --git a/test/java/com/google/devtools/build/android/desugar/testdata/separate8/SeparateInterfaceWithDefaultMethod.java b/test/java/com/google/devtools/build/android/desugar/testdata/separate8/SeparateInterfaceWithDefaultMethod.java new file mode 100644 index 0000000..b3c04c8 --- /dev/null +++ b/test/java/com/google/devtools/build/android/desugar/testdata/separate8/SeparateInterfaceWithDefaultMethod.java @@ -0,0 +1,21 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.android.desugar.testdata.separate8; + +/** Interface with default method in separate compilation target for testing b/65645388. */ +public interface SeparateInterfaceWithDefaultMethod { + default String dflt() { + return "dflt"; + } +} diff --git a/test/java/com/google/devtools/build/android/desugar/testdata_desugared_java8_jar_toc_golden.txt b/test/java/com/google/devtools/build/android/desugar/testdata_desugared_java8_jar_toc_golden.txt index 8664932..907edd0 100644 --- a/test/java/com/google/devtools/build/android/desugar/testdata_desugared_java8_jar_toc_golden.txt +++ b/test/java/com/google/devtools/build/android/desugar/testdata_desugared_java8_jar_toc_golden.txt @@ -56,6 +56,9 @@ com/google/devtools/build/android/desugar/testdata/java8/DefaultInterfaceMethodW com/google/devtools/build/android/desugar/testdata/java8/DefaultInterfaceMethodWithStaticInitializer.class com/google/devtools/build/android/desugar/testdata/java8/DefaultInterfaceWithBridges.class com/google/devtools/build/android/desugar/testdata/java8/DefaultInterfaceWithLambda.class +com/google/devtools/build/android/desugar/testdata/java8/DefaultMethodFromSeparateJava8Target.class +com/google/devtools/build/android/desugar/testdata/java8/DefaultMethodFromSeparateJava8TargetOverridden.class +com/google/devtools/build/android/desugar/testdata/java8/DefaultMethodTransitivelyFromSeparateJava8Target.class com/google/devtools/build/android/desugar/testdata/java8/FunctionWithDefaultMethod$DoubleInts.class com/google/devtools/build/android/desugar/testdata/java8/FunctionWithDefaultMethod$DoubleInts2.class com/google/devtools/build/android/desugar/testdata/java8/FunctionWithDefaultMethod.class |