-
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 both
javax.inject.*
and jakarta.inject.*
.
Previously AutoFactory recognized and generated `javax.inject` only. Now, it will prefer `jakarta.inject` if that is on the classpath, but it will still recognize `javax.inject` if only that is on the classpath. A new compiler option can be used to select `javax.inject` even if `jakarta.inject` is also available. Also remove some obsolete tests that would otherwise have had to be updated. Closes #1184. RELNOTES=AutoFactory now recognizes and generates both `javax.inject` and `jakarta.inject`. If `jakarta.inject` is on the classpath then it will be used, and otherwise `javax.inject`. A new compiler option `-Acom.google.auto.factory.InjectApi` can be set to either `javax` or `jakarta` to force the use of one or the other. PiperOrigin-RevId: 575915977
- Loading branch information
1 parent
ef19949
commit 67772b2
Showing
11 changed files
with
261 additions
and
154 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
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
78 changes: 78 additions & 0 deletions
78
factory/src/main/java/com/google/auto/factory/processor/InjectApi.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,78 @@ | ||
/* | ||
* Copyright 2023 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 com.google.auto.factory.processor; | ||
|
||
import static com.google.auto.common.MoreStreams.toImmutableMap; | ||
import static java.util.stream.Collectors.joining; | ||
|
||
import com.google.auto.common.MoreTypes; | ||
import com.google.auto.value.AutoValue; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableSet; | ||
import java.util.AbstractMap.SimpleEntry; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.lang.model.type.TypeKind; | ||
import javax.lang.model.type.TypeMirror; | ||
import javax.lang.model.util.Elements; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
/** Encapsulates the choice of {@code jakarta.inject} or {@code javax.inject}. */ | ||
@AutoValue | ||
abstract class InjectApi { | ||
abstract TypeElement inject(); | ||
|
||
abstract TypeElement provider(); | ||
|
||
abstract TypeElement qualifier(); | ||
|
||
private static final ImmutableList<String> PREFIXES_IN_ORDER = | ||
ImmutableList.of("jakarta.inject.", "javax.inject."); | ||
|
||
static InjectApi from(Elements elementUtils, @Nullable String apiPrefix) { | ||
ImmutableList<String> apiPackages = | ||
(apiPrefix == null) ? PREFIXES_IN_ORDER : ImmutableList.of(apiPrefix + ".inject."); | ||
for (String apiPackage : apiPackages) { | ||
ImmutableMap<String, TypeElement> apiMap = apiMap(elementUtils, apiPackage); | ||
TypeElement inject = apiMap.get("Inject"); | ||
TypeElement provider = apiMap.get("Provider"); | ||
TypeElement qualifier = apiMap.get("Qualifier"); | ||
if (inject != null && provider != null && qualifier != null) { | ||
return new AutoValue_InjectApi(inject, provider, qualifier); | ||
} | ||
} | ||
String classes = "{" + String.join(",", API_CLASSES) + "}"; | ||
String missing = apiPackages.stream().sorted().map(s -> s + classes).collect(joining(" or ")); | ||
throw new IllegalStateException("Class path for AutoFactory class must include " + missing); | ||
} | ||
|
||
/** True if {@code type} is a {@code Provider}. */ | ||
boolean isProvider(TypeMirror type) { | ||
return type.getKind().equals(TypeKind.DECLARED) | ||
&& MoreTypes.asTypeElement(type).equals(provider()); | ||
} | ||
|
||
private static ImmutableMap<String, TypeElement> apiMap( | ||
Elements elementUtils, String apiPackage) { | ||
return API_CLASSES.stream() | ||
.map(name -> new SimpleEntry<>(name, elementUtils.getTypeElement(apiPackage + name))) | ||
.filter(entry -> entry.getValue() != null) | ||
.collect(toImmutableMap(SimpleEntry::getKey, SimpleEntry::getValue)); | ||
} | ||
|
||
private static final ImmutableSet<String> API_CLASSES = | ||
ImmutableSet.of("Inject", "Provider", "Qualifier"); | ||
} |
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
Oops, something went wrong.