Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.x: Introduction of Helidon Service Inject. #9249

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ jobs:
strategy:
matrix:
os: [ ubuntu-20.04, macos-14 ]
module: [ mp-1, mp-2, mp-3, se-1 ]
module: [ mp-1, mp-2, mp-3, se-1, inject ]
include:
- { os: ubuntu-20.04, platform: linux }
- { os: macos-14, platform: macos }
Expand Down
8 changes: 8 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,14 @@
<groupId>io.helidon.service</groupId>
<artifactId>helidon-service-codegen</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.service.inject</groupId>
<artifactId>helidon-service-inject-api</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.service.inject</groupId>
<artifactId>helidon-service-inject</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.metadata</groupId>
<artifactId>helidon-metadata-hson</artifactId>
Expand Down
10 changes: 10 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,16 @@
<artifactId>helidon-service-codegen</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.service.inject</groupId>
<artifactId>helidon-service-inject-api</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.service.inject</groupId>
<artifactId>helidon-service-inject</artifactId>
<version>${helidon.version}</version>
</dependency>

<!-- Metadata -->
<dependency>
Expand Down
63 changes: 35 additions & 28 deletions codegen/apt/src/main/java/io/helidon/codegen/apt/AptProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import javax.annotation.processing.AbstractProcessor;
Expand Down Expand Up @@ -100,8 +101,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
// we want everything to execute in the classloader of this type, so service loaders
// use the classpath of the annotation processor, and not some "random" classloader, such as a maven one
try {
doProcess(annotations, roundEnv);
return true;
return doProcess(annotations, roundEnv);
} catch (CodegenException e) {
Object originatingElement = e.originatingElement()
.orElse(null);
Expand All @@ -120,47 +120,52 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
}
}

private void doProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
private boolean doProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
ctx.logger().log(TRACE, "Process annotations: " + annotations + ", processing over: " + roundEnv.processingOver());

if (roundEnv.processingOver()) {
codegen.processingOver();
return;
return annotations.isEmpty();
}

Set<UsedAnnotation> usedAnnotations = usedAnnotations(annotations);

if (usedAnnotations.isEmpty()) {
// no annotations, no types, still call the codegen, maybe it has something to do
codegen.process(List.of());
return;
return annotations.isEmpty();
}

List<TypeInfo> allTypes = discoverTypes(usedAnnotations, roundEnv);
codegen.process(allTypes);

return usedAnnotations.stream()
.map(UsedAnnotation::annotationElement)
.collect(Collectors.toSet())
.equals(annotations);
}

private Set<UsedAnnotation> usedAnnotations(Set<? extends TypeElement> annotations) {
var exactTypes = codegen.supportedAnnotations()
.stream()
.map(TypeName::fqName)
.collect(Collectors.toSet());
var prefixes = codegen.supportedAnnotationPackagePrefixes();
var typePredicate = typePredicate(codegen.supportedAnnotations(), codegen.supportedAnnotationPackagePrefixes());
var metaPredicate = typePredicate(codegen.supportedMetaAnnotations(), Set.of());

Set<UsedAnnotation> result = new HashSet<>();

for (TypeElement annotation : annotations) {
TypeName typeName = TypeName.create(annotation.getQualifiedName().toString());

Set<TypeName> supportedAnnotations = new HashSet<>();
// first check direct support (through exact type or prefix)
if (typePredicate.test(typeName)) {
supportedAnnotations.add(typeName);
}
/*
find meta annotations that are supported:
- annotation that annotates the current annotation
*/
Set<TypeName> supportedAnnotations = new HashSet<>();
if (supportedAnnotation(exactTypes, prefixes, typeName)) {
supportedAnnotations.add(typeName);
}
addSupportedAnnotations(exactTypes, prefixes, supportedAnnotations, typeName);
addSupportedAnnotations(metaPredicate, supportedAnnotations, typeName);

// and add all the annotations
if (!supportedAnnotations.isEmpty()) {
result.add(new UsedAnnotation(typeName, annotation, supportedAnnotations));
}
Expand All @@ -169,21 +174,23 @@ private Set<UsedAnnotation> usedAnnotations(Set<? extends TypeElement> annotatio
return result;
}

private boolean supportedAnnotation(Set<String> exactTypes, Set<String> prefixes, TypeName annotationType) {
if (exactTypes.contains(annotationType.fqName())) {
return true;
}
String packagePrefix = annotationType.packageName() + ".";
for (String prefix : prefixes) {
if (packagePrefix.startsWith(prefix)) {
private Predicate<TypeName> typePredicate(Set<TypeName> typeNames, Set<String> prefixes) {
return typeName -> {
if (typeNames.contains(typeName)) {
return true;
}
}
return false;

String packagePrefix = typeName.packageName() + ".";
for (String prefix : prefixes) {
if (packagePrefix.startsWith(prefix)) {
return true;
}
}
return false;
};
}

private void addSupportedAnnotations(Set<String> exactTypes,
Set<String> prefixes,
private void addSupportedAnnotations(Predicate<TypeName> typeNamePredicate,
Set<TypeName> supportedAnnotations,
TypeName annotationType) {
Optional<TypeInfo> foundInfo = AptTypeInfoFactory.create(ctx, annotationType);
Expand All @@ -192,9 +199,9 @@ private void addSupportedAnnotations(Set<String> exactTypes,
List<Annotation> annotations = annotationInfo.annotations();
for (Annotation annotation : annotations) {
TypeName typeName = annotation.typeName();
if (supportedAnnotation(exactTypes, prefixes, typeName)) {
if (typeNamePredicate.test(typeName)) {
if (supportedAnnotations.add(typeName)) {
addSupportedAnnotations(exactTypes, prefixes, supportedAnnotations, typeName);
addSupportedAnnotations(typeNamePredicate, supportedAnnotations, typeName);
}
}
}
Expand Down
Loading