From d8083fded3f919a77be8ee3f68d23ce43e7d99a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Wed, 3 Nov 2021 06:51:20 -0700 Subject: [PATCH] Handle missing classes better in AutoService. Fixes https://github.com/google/auto/issues/1189. Closes https://github.com/google/auto/pull/1190/files. Many thanks to @astubbs for the bug report and repro, which was the basis for the new test here. If an `@AutoService` annotation references a missing class, the `value` on [this line](https://github.com/google/auto/blob/15c76191f79a2a26a9f914541a5c9ac4965dd0c7/service/processor/src/main/java/com/google/auto/service/processor/AutoServiceProcessor.java#L306)... ```java .flatMap(value -> value.accept(this, null).stream()) ``` is the string `""`, due to quirky javac behaviour. Currently, the visitor expects an array of class literals, or, in its recursive call, a single class literal, but not a string. Since the visitor has no override for [`visitString`](https://docs.oracle.com/en/java/javase/11/docs/api/java.compiler/javax/lang/model/element/AnnotationValueVisitor.html#visitString(java.lang.String,P)), a null default value is returned. We can simply provide an empty `ImmutableSet` as the default value to fix the problem. There is no need to handle this error case specially, since the compiler will be outputting its own errors about the missing class anyway. RELNOTES=AutoService no longer throws an exception for a missing service class. PiperOrigin-RevId: 407325586 --- .../processor/AutoServiceProcessor.java | 15 +++++++++-- .../processor/AutoServiceProcessorTest.java | 15 +++++++++++ ...erviceProviderWithMissingServiceClass.java | 25 +++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 service/processor/src/test/resources/test/GenericServiceProviderWithMissingServiceClass.java diff --git a/service/processor/src/main/java/com/google/auto/service/processor/AutoServiceProcessor.java b/service/processor/src/main/java/com/google/auto/service/processor/AutoServiceProcessor.java index f12299a510..85a24cb455 100644 --- a/service/processor/src/main/java/com/google/auto/service/processor/AutoServiceProcessor.java +++ b/service/processor/src/main/java/com/google/auto/service/processor/AutoServiceProcessor.java @@ -25,12 +25,15 @@ import com.google.auto.service.AutoService; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.HashMultimap; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; import com.google.common.collect.Sets; import java.io.IOException; import java.io.OutputStream; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -68,6 +71,8 @@ public class AutoServiceProcessor extends AbstractProcessor { @VisibleForTesting static final String MISSING_SERVICES_ERROR = "No service interfaces provided for element!"; + private final List exceptionStacks = Collections.synchronizedList(new ArrayList<>()); + /** * Maps the class names of service provider interfaces to the * class names of the concrete classes which implement them. @@ -109,11 +114,17 @@ public boolean process(Set annotations, RoundEnvironment processImpl(annotations, roundEnv); } catch (RuntimeException e) { // We don't allow exceptions of any kind to propagate to the compiler - fatalError(getStackTraceAsString(e)); + String trace = getStackTraceAsString(e); + exceptionStacks.add(trace); + fatalError(trace); } return false; } + ImmutableList exceptionStacks() { + return ImmutableList.copyOf(exceptionStacks); + } + private void processImpl(Set annotations, RoundEnvironment roundEnv) { if (roundEnv.processingOver()) { generateConfigFiles(); @@ -291,7 +302,7 @@ private String getBinaryNameImpl(TypeElement element, String className) { private ImmutableSet getValueFieldOfClasses(AnnotationMirror annotationMirror) { return getAnnotationValue(annotationMirror, "value") .accept( - new SimpleAnnotationValueVisitor8, Void>() { + new SimpleAnnotationValueVisitor8, Void>(ImmutableSet.of()) { @Override public ImmutableSet visitType(TypeMirror typeMirror, Void v) { // TODO(ronshapiro): class literals may not always be declared types, i.e. diff --git a/service/processor/src/test/java/com/google/auto/service/processor/AutoServiceProcessorTest.java b/service/processor/src/test/java/com/google/auto/service/processor/AutoServiceProcessorTest.java index 35615689c0..7a176dd93c 100644 --- a/service/processor/src/test/java/com/google/auto/service/processor/AutoServiceProcessorTest.java +++ b/service/processor/src/test/java/com/google/auto/service/processor/AutoServiceProcessorTest.java @@ -17,6 +17,7 @@ import static com.google.auto.service.processor.AutoServiceProcessor.MISSING_SERVICES_ERROR; import static com.google.testing.compile.CompilationSubject.assertThat; +import static com.google.common.truth.Truth.assertThat; import com.google.common.io.Resources; import com.google.testing.compile.Compilation; @@ -144,4 +145,18 @@ public void nestedGenericWithVerifyOptionAndSuppressWarnings() { .contentsAsUtf8String() .isEqualTo("test.EnclosingGeneric$GenericServiceProvider\n"); } + + @Test + public void missing() { + AutoServiceProcessor processor = new AutoServiceProcessor(); + Compilation compilation = + Compiler.javac() + .withProcessors(processor) + .withOptions("-Averify=true") + .compile( + JavaFileObjects.forResource( + "test/GenericServiceProviderWithMissingServiceClass.java")); + assertThat(compilation).failed(); + assertThat(processor.exceptionStacks()).isEmpty(); + } } diff --git a/service/processor/src/test/resources/test/GenericServiceProviderWithMissingServiceClass.java b/service/processor/src/test/resources/test/GenericServiceProviderWithMissingServiceClass.java new file mode 100644 index 0000000000..3ca344548d --- /dev/null +++ b/service/processor/src/test/resources/test/GenericServiceProviderWithMissingServiceClass.java @@ -0,0 +1,25 @@ +/* + * Copyright 2021 Google LLC + * + * 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 test; + +import com.google.auto.service.AutoService; + +/** + * A service that references a missing class. This is useful for testing that the processor behaves + * correctly. + */ +@AutoService(MissingServiceClass.class) +public class GenericServiceProviderWithMissingServiceClass implements MissingServiceClass {}