Skip to content

Commit 8a61f7d

Browse files
authored
[MNG-8249] Provide an annotation processor for DI (#1704)
1 parent 96b1239 commit 8a61f7d

File tree

16 files changed

+138
-173
lines changed

16 files changed

+138
-173
lines changed

api/maven-api-di/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,7 @@
3030
<name>Maven 4 API :: Dependency Injection</name>
3131
<description>Maven 4 API - Dependency Injection</description>
3232

33+
<properties>
34+
<maven.compiler.proc>none</maven.compiler.proc>
35+
</properties>
3336
</project>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.di.tool;
20+
21+
import javax.annotation.processing.AbstractProcessor;
22+
import javax.annotation.processing.RoundEnvironment;
23+
import javax.annotation.processing.SupportedAnnotationTypes;
24+
import javax.annotation.processing.SupportedSourceVersion;
25+
import javax.lang.model.SourceVersion;
26+
import javax.lang.model.element.Element;
27+
import javax.lang.model.element.TypeElement;
28+
import javax.tools.Diagnostic;
29+
import javax.tools.FileObject;
30+
import javax.tools.StandardLocation;
31+
32+
import java.io.IOException;
33+
import java.io.Writer;
34+
import java.util.Set;
35+
36+
import org.apache.maven.api.di.Named;
37+
38+
// Auto-register the annotation processor
39+
@SupportedAnnotationTypes("org.apache.maven.api.di.Named")
40+
@SupportedSourceVersion(SourceVersion.RELEASE_17)
41+
public class DiIndexProcessor extends AbstractProcessor {
42+
43+
@Override
44+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
45+
// Collect the fully qualified names of classes annotated with @Named
46+
StringBuilder builder = new StringBuilder();
47+
48+
processingEnv
49+
.getMessager()
50+
.printMessage(
51+
Diagnostic.Kind.NOTE,
52+
"Processing " + roundEnv.getRootElements().size() + " classes");
53+
54+
for (Element element : roundEnv.getElementsAnnotatedWith(Named.class)) {
55+
if (element instanceof TypeElement typeElement) {
56+
// Get the fully qualified class name
57+
String className = typeElement.getQualifiedName().toString();
58+
59+
// Handle inner classes by checking if the enclosing element is a class or interface
60+
Element enclosingElement = typeElement.getEnclosingElement();
61+
if (enclosingElement instanceof TypeElement) {
62+
// It's an inner class, replace the last dot with a '$'
63+
String enclosingClassName =
64+
((TypeElement) enclosingElement).getQualifiedName().toString();
65+
className = enclosingClassName + "$" + typeElement.getSimpleName();
66+
}
67+
68+
builder.append(className).append("\n");
69+
}
70+
}
71+
72+
if (!builder.isEmpty()) { // Check if the StringBuilder is non-empty
73+
try {
74+
writeFile(builder.toString());
75+
} catch (IOException e) {
76+
processingEnv
77+
.getMessager()
78+
.printMessage(Diagnostic.Kind.ERROR, "Error writing file: " + e.getMessage());
79+
}
80+
}
81+
82+
return true; // Indicate that annotations are claimed by this processor
83+
}
84+
85+
private void writeFile(String content) throws IOException {
86+
// Create the file META-INF/maven/org.apache.maven.api.di.Inject
87+
FileObject fileObject = processingEnv
88+
.getFiler()
89+
.createResource(StandardLocation.CLASS_OUTPUT, "", "META-INF/maven/org.apache.maven.api.di.Inject");
90+
91+
try (Writer writer = fileObject.openWriter()) {
92+
writer.write(content);
93+
}
94+
}
95+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.apache.maven.di.tool.DiIndexProcessor

api/maven-api-spi/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
<groupId>org.apache.maven</groupId>
4444
<artifactId>maven-api-core</artifactId>
4545
</dependency>
46+
<dependency>
47+
<groupId>org.apache.maven</groupId>
48+
<artifactId>maven-api-di</artifactId>
49+
</dependency>
4650
</dependencies>
4751

4852
</project>

api/maven-api-spi/src/main/java/org/apache/maven/api/spi/LanguageProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import org.apache.maven.api.Language;
2222
import org.apache.maven.api.annotations.Consumer;
2323
import org.apache.maven.api.annotations.Experimental;
24+
import org.apache.maven.api.di.Named;
2425

2526
/**
2627
* @since 4.0.0
2728
*/
2829
@Experimental
2930
@Consumer
31+
@Named
3032
public interface LanguageProvider extends ExtensibleEnumProvider<Language> {}

api/maven-api-spi/src/main/java/org/apache/maven/api/spi/LifecycleProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import org.apache.maven.api.Lifecycle;
2222
import org.apache.maven.api.annotations.Consumer;
2323
import org.apache.maven.api.annotations.Experimental;
24+
import org.apache.maven.api.di.Named;
2425

2526
@Experimental
2627
@Consumer
28+
@Named
2729
public interface LifecycleProvider extends ExtensibleEnumProvider<Lifecycle> {}

api/maven-api-spi/src/main/java/org/apache/maven/api/spi/ModelParser.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.maven.api.annotations.Experimental;
2727
import org.apache.maven.api.annotations.Nonnull;
2828
import org.apache.maven.api.annotations.Nullable;
29+
import org.apache.maven.api.di.Named;
2930
import org.apache.maven.api.model.Model;
3031
import org.apache.maven.api.services.Source;
3132

@@ -37,6 +38,7 @@
3738
*/
3839
@Experimental
3940
@Consumer
41+
@Named
4042
public interface ModelParser extends SpiService {
4143

4244
/**

api/maven-api-spi/src/main/java/org/apache/maven/api/spi/PackagingProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import org.apache.maven.api.Packaging;
2222
import org.apache.maven.api.annotations.Consumer;
2323
import org.apache.maven.api.annotations.Experimental;
24+
import org.apache.maven.api.di.Named;
2425

2526
@Experimental
2627
@Consumer
28+
@Named
2729
public interface PackagingProvider extends ExtensibleEnumProvider<Packaging> {}

api/maven-api-spi/src/main/java/org/apache/maven/api/spi/PathScopeProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import org.apache.maven.api.PathScope;
2222
import org.apache.maven.api.annotations.Consumer;
2323
import org.apache.maven.api.annotations.Experimental;
24+
import org.apache.maven.api.di.Named;
2425

2526
/**
2627
* @since 4.0.0
2728
*/
2829
@Experimental
2930
@Consumer
31+
@Named
3032
public interface PathScopeProvider extends ExtensibleEnumProvider<PathScope> {}

api/maven-api-spi/src/main/java/org/apache/maven/api/spi/ProjectScopeProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import org.apache.maven.api.ProjectScope;
2222
import org.apache.maven.api.annotations.Consumer;
2323
import org.apache.maven.api.annotations.Experimental;
24+
import org.apache.maven.api.di.Named;
2425

2526
/**
2627
* @since 4.0.0
2728
*/
2829
@Experimental
2930
@Consumer
31+
@Named
3032
public interface ProjectScopeProvider extends ExtensibleEnumProvider<ProjectScope> {}

api/maven-api-spi/src/main/java/org/apache/maven/api/spi/PropertyContributor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.apache.maven.api.annotations.Consumer;
2424
import org.apache.maven.api.annotations.Experimental;
25+
import org.apache.maven.api.di.Named;
2526

2627
/**
2728
* Component able to contribute to Maven session user properties. This SPI component is invoked
@@ -31,6 +32,7 @@
3132
*/
3233
@Experimental
3334
@Consumer
35+
@Named
3436
public interface PropertyContributor extends SpiService {
3537
/**
3638
* Invoked just before session is created with a mutable map that carries collected user properties so far.

api/maven-api-spi/src/main/java/org/apache/maven/api/spi/TypeProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import org.apache.maven.api.Type;
2222
import org.apache.maven.api.annotations.Consumer;
2323
import org.apache.maven.api.annotations.Experimental;
24+
import org.apache.maven.api.di.Named;
2425

2526
/**
2627
* @since 4.0.0
2728
*/
2829
@Experimental
2930
@Consumer
31+
@Named
3032
public interface TypeProvider extends ExtensibleEnumProvider<Type> {}

0 commit comments

Comments
 (0)