-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle missing classes better in AutoService.
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
1 parent
ce31cc1
commit d8083fd
Showing
3 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
service/processor/src/test/resources/test/GenericServiceProviderWithMissingServiceClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> {} |