-
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.
Propagate parameter annotations in generated factory code.
Example: ```java @autofactory final class SomeClass { ... SomeClass(@provided @AQualifier @foo String provideDepA, @bar String depB) {...} } ``` The generated `SomeClassFactory` will have a `create` method like this: ```java SomeClass create(@bar String depB) {...} ``` This was already true if `@Bar` was a `TYPE_USE` annotation, but now it is also true for other annotations. This may affect compilation. For example, if `@Bar` is Kotlin's `@NotNull`, this change plugs a hole in Kotlin's nullness checking and will break callers that were previously passing a `@Nullable` value. The generated factory already null-checked such parameters, so in Kotlin you can add `!!` with no change in effect. If the `!!` causes a `NullPointerException`, there would already have been a `NullPointerException` from the generated factory. We do not currently copy annotations, other than `@Qualifier` annotations, from `@Provided` parameters to the generated factory constructor. In the example, `@Foo` would not be copied. PiperOrigin-RevId: 481174930
- Loading branch information
1 parent
dffe1f8
commit 4bb91ca
Showing
5 changed files
with
121 additions
and
41 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
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
44 changes: 44 additions & 0 deletions
44
factory/src/test/resources/expected/ParameterAnnotationsFactory.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,44 @@ | ||
/* | ||
* Copyright 2022 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 tests; | ||
|
||
import javax.annotation.processing.Generated; | ||
import javax.inject.Inject; | ||
import javax.inject.Provider; | ||
|
||
@Generated( | ||
value = "com.google.auto.factory.processor.AutoFactoryProcessor", | ||
comments = "https://github.com/google/auto/tree/master/factory" | ||
) | ||
final class ParameterAnnotationsFactory { | ||
private final Provider<@ParameterAnnotations.NullableType String> fooProvider; | ||
|
||
@Inject | ||
ParameterAnnotationsFactory(Provider<@ParameterAnnotations.NullableType String> fooProvider) { | ||
this.fooProvider = checkNotNull(fooProvider, 1); | ||
} | ||
|
||
ParameterAnnotations create(@ParameterAnnotations.NullableParameter Integer bar, @ParameterAnnotations.Nullable Long baz, @ParameterAnnotations.NullableType Thread buh) { | ||
return new ParameterAnnotations(checkNotNull(fooProvider.get(), 1), checkNotNull(bar, 2), baz, checkNotNull(buh, 4)); | ||
} | ||
|
||
private static <T> T checkNotNull(T reference, int argumentIndex) { | ||
if (reference == null) { | ||
throw new NullPointerException("@AutoFactory method argument is null but is not marked @Nullable. Argument index: " + argumentIndex); | ||
} | ||
return reference; | ||
} | ||
} |
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,47 @@ | ||
/* | ||
* Copyright 2022 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 tests; | ||
|
||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.ElementType.TYPE_USE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import com.google.auto.factory.AutoFactory; | ||
import com.google.auto.factory.Provided; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
@AutoFactory | ||
final class ParameterAnnotations { | ||
@Retention(RUNTIME) | ||
@Target(PARAMETER) | ||
@interface NullableParameter {} | ||
|
||
// We have special treatment of @Nullable; make sure it doesn't get copied twice. | ||
@Retention(RUNTIME) | ||
@Target(PARAMETER) | ||
@interface Nullable {} | ||
|
||
@Retention(RUNTIME) | ||
@Target(TYPE_USE) | ||
@interface NullableType {} | ||
|
||
ParameterAnnotations( | ||
@Provided @NullableParameter @NullableType String foo, | ||
@NullableParameter Integer bar, | ||
@Nullable Long baz, | ||
@NullableType Thread buh) {} | ||
} |