Skip to content

Commit e5ef095

Browse files
committed
feat: add methodtype loading using service loader
1 parent 6ca83bf commit e5ef095

File tree

6 files changed

+94
-6
lines changed

6 files changed

+94
-6
lines changed

annotationprocessor/src/main/java/com/interguess/autoimpl/annotationprocessor/generator/ImplementationClassGenerator.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import com.interguess.autoimpl.api.method.MethodType;
3131
import com.interguess.autoimpl.api.method.MethodTypeMatcher;
3232
import com.interguess.autoimpl.common.method.MethodTypeMatcherImpl;
33-
import com.interguess.autoimpl.common.methodtypes.GetMethod;
34-
import com.interguess.autoimpl.common.methodtypes.SetMethod;
3533
import org.jetbrains.annotations.NotNull;
3634

3735
import javax.annotation.processing.ProcessingEnvironment;
@@ -54,10 +52,7 @@ public class ImplementationClassGenerator {
5452
public ImplementationClassGenerator(final @NotNull ProcessingEnvironment processingEnv) {
5553
this.processingEnv = processingEnv;
5654

57-
this.methodTypeMatcher = new MethodTypeMatcherImpl();
58-
59-
this.methodTypeMatcher.registerType("get[a-zA-Z0-9_]+", new GetMethod());
60-
this.methodTypeMatcher.registerType("set[a-zA-Z0-9_]+", new SetMethod());
55+
this.methodTypeMatcher = MethodTypeMatcherImpl.getInstance();
6156
}
6257

6358
public void generateForInterface(final @NotNull TypeElement interfaceElement) {

annotationprocessor/src/main/java/com/interguess/autoimpl/annotationprocessor/processors/AutoImplProcessor.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
import com.google.auto.service.AutoService;
2828
import com.interguess.autoimpl.annotationprocessor.generator.ImplementationClassGenerator;
2929
import com.interguess.autoimpl.api.annotations.AutoImpl;
30+
import com.interguess.autoimpl.api.annotations.RegisterMethod;
31+
import com.interguess.autoimpl.api.method.MethodType;
32+
import com.interguess.autoimpl.api.method.MethodTypeMatcher;
33+
import com.interguess.autoimpl.common.method.MethodTypeMatcherImpl;
3034
import org.jetbrains.annotations.NotNull;
3135
import org.jetbrains.annotations.Nullable;
3236

@@ -36,6 +40,7 @@
3640
import javax.lang.model.element.ElementKind;
3741
import javax.lang.model.element.TypeElement;
3842
import javax.tools.Diagnostic;
43+
import java.util.ServiceLoader;
3944
import java.util.Set;
4045

4146
@AutoService(Processor.class)
@@ -50,6 +55,39 @@ public class AutoImplProcessor extends AbstractProcessor {
5055
public synchronized void init(final @NotNull ProcessingEnvironment processingEnv) {
5156
super.init(processingEnv);
5257

58+
MethodTypeMatcher.setInstance(new MethodTypeMatcherImpl());
59+
60+
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Loading MethodType implementations...");
61+
62+
ServiceLoader.load(MethodType.class, MethodType.class.getClassLoader()).forEach(methodType -> {
63+
processingEnv.getMessager().printMessage(
64+
Diagnostic.Kind.NOTE,
65+
"Registering MethodType: " + methodType.getClass().getName()
66+
);
67+
68+
final RegisterMethod registerAnnotation = methodType.getClass().getAnnotation(RegisterMethod.class);
69+
70+
if (registerAnnotation == null) {
71+
processingEnv.getMessager().printMessage(
72+
Diagnostic.Kind.ERROR,
73+
"MethodType " + methodType.getClass().getName() + " is missing @RegisterMethod annotation."
74+
);
75+
return;
76+
}
77+
78+
System.out.println(methodType.getClass().getName());
79+
80+
MethodTypeMatcher.getInstance().registerType(
81+
registerAnnotation.value(),
82+
methodType
83+
);
84+
});
85+
86+
processingEnv.getMessager().printMessage(
87+
Diagnostic.Kind.NOTE,
88+
"MethodType implementations loaded successfully."
89+
);
90+
5391
this.classGenerator = new ImplementationClassGenerator(processingEnv);
5492
}
5593

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2025 Interguess.com
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package com.interguess.autoimpl.api.annotations;
26+
27+
import org.jetbrains.annotations.NotNull;
28+
29+
import java.lang.annotation.ElementType;
30+
import java.lang.annotation.Retention;
31+
import java.lang.annotation.RetentionPolicy;
32+
import java.lang.annotation.Target;
33+
34+
/**
35+
* This annotation is used to mark method type classes that should be registered.
36+
* The annotation processor will scan for classes annotated with this
37+
* annotation and registers them before processing the @AutoImpl annotation.
38+
*/
39+
@Retention(RetentionPolicy.RUNTIME)
40+
@Target(ElementType.TYPE)
41+
public @interface RegisterMethod {
42+
43+
/**
44+
* The regex pattern to match the method name.
45+
*
46+
* @return The regex pattern as a string.
47+
*/
48+
@NotNull String value();
49+
}

common/src/main/java/com/interguess/autoimpl/common/methodtypes/GetMethod.java

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

2727
import com.google.common.base.Preconditions;
2828
import com.interguess.autoimpl.api.annotations.AutoField;
29+
import com.interguess.autoimpl.api.annotations.RegisterMethod;
2930
import com.interguess.autoimpl.api.field.GeneratedField;
3031
import com.interguess.autoimpl.api.method.MethodType;
3132
import com.interguess.autoimpl.common.field.GeneratedFieldImpl;
@@ -34,6 +35,7 @@
3435
import javax.lang.model.element.ExecutableElement;
3536
import java.util.List;
3637

38+
@RegisterMethod("get[a-zA-Z0-9_]+")
3739
public class GetMethod implements MethodType {
3840

3941
@Override

common/src/main/java/com/interguess/autoimpl/common/methodtypes/SetMethod.java

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

2727
import com.google.common.base.Preconditions;
2828
import com.interguess.autoimpl.api.annotations.AutoField;
29+
import com.interguess.autoimpl.api.annotations.RegisterMethod;
2930
import com.interguess.autoimpl.api.field.GeneratedField;
3031
import com.interguess.autoimpl.api.method.MethodType;
3132
import com.interguess.autoimpl.common.field.GeneratedFieldImpl;
@@ -34,6 +35,7 @@
3435
import javax.lang.model.element.ExecutableElement;
3536
import java.util.List;
3637

38+
@RegisterMethod("set[a-zA-Z0-9_]+")
3739
public class SetMethod implements MethodType {
3840

3941
@Override
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
com.interguess.autoimpl.common.methodtypes.GetMethod
2+
com.interguess.autoimpl.common.methodtypes.SetMethod

0 commit comments

Comments
 (0)