aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉamonn McManus <emcmanus@google.com>2021-11-03 06:51:20 -0700
committerGoogle Java Core Libraries <java-libraries-firehose+copybara@google.com>2021-11-03 06:52:13 -0700
commitd8083fded3f919a77be8ee3f68d23ce43e7d99a2 (patch)
treeb7935f378b0329e4df8b9e1fcc815b57472c8ad8
parentce31cc18c042bf64995f92d6ca917a94b1e419fb (diff)
downloadauto-d8083fded3f919a77be8ee3f68d23ce43e7d99a2.tar.gz
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 `"<error>"`, 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
-rw-r--r--service/processor/src/main/java/com/google/auto/service/processor/AutoServiceProcessor.java15
-rw-r--r--service/processor/src/test/java/com/google/auto/service/processor/AutoServiceProcessorTest.java15
-rw-r--r--service/processor/src/test/resources/test/GenericServiceProviderWithMissingServiceClass.java25
3 files changed, 53 insertions, 2 deletions
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 f12299a5..85a24cb4 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.common.MoreTypes;
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<String> 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 class AutoServiceProcessor extends AbstractProcessor {
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<String> exceptionStacks() {
+ return ImmutableList.copyOf(exceptionStacks);
+ }
+
private void processImpl(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
generateConfigFiles();
@@ -291,7 +302,7 @@ public class AutoServiceProcessor extends AbstractProcessor {
private ImmutableSet<DeclaredType> getValueFieldOfClasses(AnnotationMirror annotationMirror) {
return getAnnotationValue(annotationMirror, "value")
.accept(
- new SimpleAnnotationValueVisitor8<ImmutableSet<DeclaredType>, Void>() {
+ new SimpleAnnotationValueVisitor8<ImmutableSet<DeclaredType>, Void>(ImmutableSet.of()) {
@Override
public ImmutableSet<DeclaredType> 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 35615689..7a176dd9 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 @@ package com.google.auto.service.processor;
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 class AutoServiceProcessorTest {
.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 00000000..3ca34454
--- /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<T> implements MissingServiceClass<T> {}