Skip to content

Commit 0c7af73

Browse files
committed
chore!: switch from name based regex matching to @AutoMethod based id matching
1 parent 0232848 commit 0c7af73

File tree

8 files changed

+79
-26
lines changed

8 files changed

+79
-26
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.interguess.autoimpl.annotationprocessor.collectors.MethodCollector;
2929
import com.interguess.autoimpl.annotationprocessor.utils.ResourceFileLoaderUtil;
3030
import com.interguess.autoimpl.api.annotations.AutoImpl;
31+
import com.interguess.autoimpl.api.annotations.AutoMethod;
3132
import com.interguess.autoimpl.api.method.MethodType;
3233
import com.interguess.autoimpl.api.method.MethodTypeMatcher;
3334
import com.interguess.autoimpl.common.method.MethodTypeMatcherImpl;
@@ -89,7 +90,9 @@ public void generateForInterface(final @NotNull TypeElement interfaceElement) {
8990
final MethodCollector methodCollector = new MethodCollector();
9091

9192
for (final Element enclosed : interfaceElement.getEnclosedElements()) {
92-
if (enclosed.getKind() == ElementKind.METHOD) {
93+
final AutoMethod autoMethodAnnotation = enclosed.getAnnotation(AutoMethod.class);
94+
95+
if (enclosed.getKind() == ElementKind.METHOD && autoMethodAnnotation != null) {
9396
final ExecutableElement method = (ExecutableElement) enclosed;
9497

9598
final MethodType methodType = this.methodTypeMatcher.match(method);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
106106
for (final Element element : roundEnv.getElementsAnnotatedWith(AutoImpl.class)) {
107107
if (element.getKind() == ElementKind.INTERFACE) {
108108
this.classGenerator.generateForInterface((TypeElement) element);
109+
} else {
110+
this.processingEnv.getMessager().printMessage(
111+
Diagnostic.Kind.ERROR,
112+
"@AutoImpl can only be applied to interfaces. " +
113+
"Invalid element: " + element.getSimpleName(),
114+
element
115+
);
109116
}
110117
}
111118

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
public @interface AutoMethod {
30+
31+
/**
32+
* The id of the method's code-generator which should be used to generate the implementation of the annotated method.
33+
*
34+
* @return the id of the method's code-generator to use.
35+
*/
36+
@NotNull String value();
37+
}

api/src/main/java/com/interguess/autoimpl/api/annotations/RegisterMethod.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@
4141
public @interface RegisterMethod {
4242

4343
/**
44-
* The regex pattern to match the method name.
44+
* The id of the method type.
45+
* Every method type must have a unique id which then can be used in the @AutoMethod annotation to set
46+
* the code generator for this specific method.
4547
*
46-
* @return The regex pattern as a string.
48+
* @return the id of the method type.
4749
*/
4850
@NotNull String value();
4951
}

api/src/main/java/com/interguess/autoimpl/api/method/MethodTypeMatcher.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,22 @@ public static boolean isInitialized() {
6060
public abstract @NotNull List<MethodType> getRegisteredTypes();
6161

6262
/**
63-
* Registers a method type with a regex pattern for matching method names.
63+
* Registers a method type with a specific id.
6464
*
65-
* @param methodNameRegex the regex pattern to match method names
66-
* @param type the MethodType to register
65+
* @param methodTypeId the id of the MethodType to register, as specified in the AutoMethod annotation
66+
* @param type the MethodType to register
6767
*/
68-
public abstract void registerType(@NotNull String methodNameRegex, @NotNull MethodType type);
68+
public abstract void registerType(@NotNull String methodTypeId, @NotNull MethodType type);
6969

7070
/**
71-
* Unregisters a method type.
71+
* Unregisters a method type by its id.
7272
*
73-
* @param type the MethodType to unregister
73+
* @param methodTypeId the id of the MethodType to unregister
7474
*/
75-
public abstract void unregisterType(@NotNull MethodType type);
75+
public abstract void unregisterType(@NotNull String methodTypeId);
7676

7777
/**
78-
* Matches a method type based on the method name of the provided ExecutableElement.
78+
* Matches a method type based on the @AutoMethod annotation of the given ExecutableElement.
7979
*
8080
* @param method the ExecutableElement representing the method to match
8181
* @return the matched MethodType, or null if no match is found

common/src/main/java/com/interguess/autoimpl/common/method/MethodTypeMatcherImpl.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package com.interguess.autoimpl.common.method;
2626

27+
import com.interguess.autoimpl.api.annotations.AutoMethod;
2728
import com.interguess.autoimpl.api.exception.MethodTypeRegistrationException;
2829
import com.interguess.autoimpl.api.method.MethodType;
2930
import com.interguess.autoimpl.api.method.MethodTypeMatcher;
@@ -52,30 +53,33 @@ public MethodTypeMatcherImpl() {
5253
}
5354

5455
@Override
55-
public void registerType(@NotNull String methodNameRegex, @NotNull MethodType type) {
56-
if (this.types.containsKey(methodNameRegex)) {
57-
throw new MethodTypeRegistrationException("Method type with name regex '" + methodNameRegex + "' is already registered.");
56+
public void registerType(@NotNull String methodTypeId, @NotNull MethodType type) {
57+
if (this.types.containsKey(methodTypeId)) {
58+
throw new MethodTypeRegistrationException("Method type with the id '" + methodTypeId + "' is already registered.");
5859
}
5960

60-
this.types.put(methodNameRegex, type);
61+
this.types.put(methodTypeId, type);
6162
}
6263

6364
@Override
64-
public void unregisterType(@NotNull MethodType type) {
65-
this.types.values().removeIf(existingType -> existingType.equals(type));
65+
public void unregisterType(@NotNull String methodTypeId) {
66+
this.types.remove(methodTypeId);
6667
}
6768

6869
@Override
6970
public @Nullable MethodType match(@NotNull ExecutableElement method) {
70-
for (final Map.Entry<String, MethodType> entry : this.types.entrySet()) {
71-
final String methodNameRegex = entry.getKey();
72-
final MethodType methodType = entry.getValue();
71+
final AutoMethod annotation = method.getAnnotation(AutoMethod.class);
7372

74-
if (method.getSimpleName().toString().matches(methodNameRegex)) {
75-
return methodType;
76-
}
73+
if (annotation == null) {
74+
throw new IllegalStateException("Method " + method.getSimpleName() + " is not annotated with @AutoMethod");
7775
}
7876

79-
return null;
77+
final MethodType type = this.types.get(annotation.value());
78+
79+
if (type == null) {
80+
throw new IllegalStateException("No method type registered with the id '" + annotation.value() + "'");
81+
}
82+
83+
return type;
8084
}
8185
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import javax.lang.model.element.ExecutableElement;
3636
import java.util.List;
3737

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

4141
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import javax.lang.model.element.ExecutableElement;
3636
import java.util.List;
3737

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

4141
@Override

0 commit comments

Comments
 (0)