-
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.
Include type parameters of parent types in generated factory classes.
Suppose you have the following types: ``` interface Foo<M extends Bar> {...} interface FooFactory<M extends Bar> { Foo<M> create(); } @autofactory(implementing = FooFactory.class) class FooImpl<M extends Bar> implements Foo<M> {...} ``` Then the generated `FooImplFactory` class should look like this: ``` class FooImplFactory<M extends Bar> implements FooFactory<M> {...} ``` But before this change that was only the case if there was a `@Provided M` constructor parameter. Now we examine whether the requested supertypes of the generated class have type parameters, and if so we copy those onto the generated class. This may not be 100% accurate but it should work for at least the commonest cases. RELNOTES=When a requested supertype of the generated factory class has a type parameter, the generated class now always has the same parameter. PiperOrigin-RevId: 400259157
- Loading branch information
1 parent
781de86
commit 206b673
Showing
6 changed files
with
203 additions
and
0 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
48 changes: 48 additions & 0 deletions
48
factory/src/test/resources/expected/Generics_ExplicitFooImplFactory.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,48 @@ | ||
/* | ||
* 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 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 Generics_ExplicitFooImplFactory<M extends Generics.Bar> | ||
implements Generics.FooFactory<M> { | ||
private final Provider<M> unusedProvider; | ||
|
||
@Inject | ||
Generics_ExplicitFooImplFactory(Provider<M> unusedProvider) { | ||
this.unusedProvider = checkNotNull(unusedProvider, 1); | ||
} | ||
|
||
@Override | ||
public Generics.ExplicitFooImpl<M> create() { | ||
return new Generics.ExplicitFooImpl<M>(checkNotNull(unusedProvider.get(), 1)); | ||
} | ||
|
||
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; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
factory/src/test/resources/expected/Generics_FooImplFactory.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,34 @@ | ||
/* | ||
* 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 tests; | ||
|
||
import javax.annotation.processing.Generated; | ||
import javax.inject.Inject; | ||
|
||
@Generated( | ||
value = "com.google.auto.factory.processor.AutoFactoryProcessor", | ||
comments = "https://github.com/google/auto/tree/master/factory" | ||
) | ||
final class Generics_FooImplFactory<M extends Generics.Bar> implements Generics.FooFactory<M> { | ||
@Inject | ||
Generics_FooImplFactory() { | ||
} | ||
|
||
@Override | ||
public Generics.FooImpl<M> create() { | ||
return new Generics.FooImpl<M>(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
factory/src/test/resources/expected/Generics_FooImplWithClassFactory.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,34 @@ | ||
/* | ||
* 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 tests; | ||
|
||
import javax.annotation.processing.Generated; | ||
import javax.inject.Inject; | ||
|
||
@Generated( | ||
value = "com.google.auto.factory.processor.AutoFactoryProcessor", | ||
comments = "https://github.com/google/auto/tree/master/factory" | ||
) | ||
final class Generics_FooImplWithClassFactory<M extends Generics.Bar> extends Generics.FooFactoryClass<M> { | ||
@Inject | ||
Generics_FooImplWithClassFactory() { | ||
} | ||
|
||
@Override | ||
public Generics.FooImplWithClass<M> create() { | ||
return new Generics.FooImplWithClass<M>(); | ||
} | ||
} |
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,50 @@ | ||
/* | ||
* 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 tests; | ||
|
||
import com.google.auto.factory.AutoFactory; | ||
import com.google.auto.factory.Provided; | ||
|
||
class Generics { | ||
interface Bar {} | ||
|
||
interface Foo<M extends Bar> {} | ||
|
||
interface FooFactory<M extends Bar> { | ||
Foo<M> create(); | ||
} | ||
|
||
// The generated FooImplFactory should also have an <M extends Bar> type parameter, so we can | ||
// have FooImplFactory<M extends Bar> implements FooFactory<M>. | ||
@AutoFactory(implementing = FooFactory.class) | ||
static final class FooImpl<M extends Bar> implements Foo<M> { | ||
FooImpl() {} | ||
} | ||
|
||
// The generated ExplicitFooImplFactory should have an <M extends Bar> type parameter, which | ||
// serves both for FooFactory<M> and for Provider<M> in the constructor. | ||
@AutoFactory(implementing = FooFactory.class) | ||
static final class ExplicitFooImpl<M extends Bar> implements Foo<M> { | ||
ExplicitFooImpl(@Provided M unused) {} | ||
} | ||
|
||
abstract static class FooFactoryClass<M extends Bar> { | ||
abstract Foo<M> create(); | ||
} | ||
|
||
@AutoFactory(extending = FooFactoryClass.class) | ||
static final class FooImplWithClass<M extends Bar> implements Foo<M> {} | ||
} |