Skip to content

Commit

Permalink
Handle missing classes better in AutoService.
Browse files Browse the repository at this point in the history
Fixes #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
  • Loading branch information
eamonnmcmanus authored and Google Java Core Libraries committed Nov 3, 2021
1 parent ce31cc1 commit d8083fd
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -109,11 +114,17 @@ public boolean process(Set<? extends TypeElement> 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<String> exceptionStacks() {
return ImmutableList.copyOf(exceptionStacks);
}

private void processImpl(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
generateConfigFiles();
Expand Down Expand Up @@ -291,7 +302,7 @@ private String getBinaryNameImpl(TypeElement element, String className) {
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.
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
@@ -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> {}

0 comments on commit d8083fd

Please sign in to comment.