Skip to content

Commit ad4af63

Browse files
committed
feat: add support for custom generated-class suffixes using the AutoImpl annotation
1 parent e5ef095 commit ad4af63

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.interguess.autoimpl.annotationprocessor.collectors.FieldCollector;
2828
import com.interguess.autoimpl.annotationprocessor.collectors.MethodCollector;
2929
import com.interguess.autoimpl.annotationprocessor.utils.ResourceFileLoaderUtil;
30+
import com.interguess.autoimpl.api.annotations.AutoImpl;
3031
import com.interguess.autoimpl.api.method.MethodType;
3132
import com.interguess.autoimpl.api.method.MethodTypeMatcher;
3233
import com.interguess.autoimpl.common.method.MethodTypeMatcherImpl;
@@ -56,12 +57,35 @@ public ImplementationClassGenerator(final @NotNull ProcessingEnvironment process
5657
}
5758

5859
public void generateForInterface(final @NotNull TypeElement interfaceElement) {
60+
final AutoImpl autoImplAnnotation = interfaceElement.getAnnotation(AutoImpl.class);
61+
62+
if (autoImplAnnotation == null) {
63+
this.processingEnv.getMessager().printMessage(
64+
Diagnostic.Kind.ERROR,
65+
"The interface must be annotated with @AutoImpl to generate an implementation class.",
66+
interfaceElement
67+
);
68+
69+
return;
70+
}
71+
72+
if (!autoImplAnnotation.value().matches("[A-Z][a-zA-Z0-9_]*")) {
73+
this.processingEnv.getMessager().printMessage(
74+
Diagnostic.Kind.ERROR,
75+
"The value of @AutoImpl must be a valid class name suffix (start with an uppercase letter and contain only letters, digits, and underscores).",
76+
interfaceElement
77+
);
78+
79+
return;
80+
}
81+
5982
final String interfaceName = interfaceElement.getSimpleName().toString();
6083
final String packageName = this.processingEnv.getElementUtils().getPackageOf(interfaceElement).getQualifiedName().toString();
61-
final String className = interfaceName + "Impl";
84+
final String className = interfaceName + autoImplAnnotation.value();
6285
final String qualifiedClassName = packageName + "." + className;
6386

6487
final FieldCollector fieldCollector = new FieldCollector();
88+
6589
final MethodCollector methodCollector = new MethodCollector();
6690

6791
for (final Element enclosed : interfaceElement.getEnclosedElements()) {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,20 @@
2424

2525
package com.interguess.autoimpl.api.annotations;
2626

27+
import org.jetbrains.annotations.NotNull;
28+
2729
import java.lang.annotation.Target;
2830

2931
/**
3032
* Marker annotation for classes that should be automatically implemented.
3133
*/
3234
@Target({java.lang.annotation.ElementType.TYPE})
3335
public @interface AutoImpl {
36+
37+
/**
38+
* The suffix of the generated implementation class name.
39+
*
40+
* @return for example User annotated with @AutoImpl("Impl") will generate "UserImpl" class.
41+
*/
42+
@NotNull String value() default "Impl";
3443
}

0 commit comments

Comments
 (0)