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

Fail if CustomAdapter isn't correct #135

Merged
merged 5 commits into from
Aug 6, 2023
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;

import static io.avaje.validation.generator.ProcessingContext.logError;

final class Util {

private static final Pattern WHITE_SPACE_REGEX =
Expand Down Expand Up @@ -263,24 +265,32 @@ static String baseTypeOfAdapter(String adapterFullName) {
if (element == null) {
throw new NullPointerException("Element not found for [" + adapterFullName + "]");
}
return baseTypeOfAdapter(element);
}

static String baseTypeOfAdapter(TypeElement element) {

return Optional.of(element.getSuperclass())
.filter(t -> t.toString().contains("io.avaje.validation.adapter.AbstractConstraintAdapter"))
.or(validationAdapter(element))
.map(Object::toString)
.map(GenericType::parse)
.map(GenericType::firstParamType)
.map(Util::extractTypeWithNest)
.orElseThrow(
() ->
new IllegalStateException(
"Adapter: " + adapterFullName + " does not implement ValidationAdapter"));
.orElseGet(
() -> {
logError(
element,
"Custom Constraint adapters must extend AbstractConstraintAdapter or implement ValidationAdapter");
return "Invalid";
});
}

private static Supplier<Optional<? extends TypeMirror>> validationAdapter(TypeElement element) {
return () ->
element.getInterfaces().stream()
.filter(t -> t.toString().contains("io.avaje.validation.adapter.ValidationAdapter"))
.findFirst();
element.getInterfaces().stream()
.filter(t -> t.toString().contains("io.avaje.validation.adapter.ValidationAdapter"))
.findFirst();
}

static String extractTypeWithNest(String fullType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
getElements(round, JakartaConstraintPrism.PRISM_TYPE).ifPresent(this::writeContraintAdapters);
getElements(round, CrossParamConstraintPrism.PRISM_TYPE).ifPresent(this::writeContraintAdapters);

registerCustomAdapters(
round.getElementsAnnotatedWith(element(ConstraintAdapterPrism.PRISM_TYPE)));
//register custom adapters
getElements(round, ConstraintAdapterPrism.PRISM_TYPE).ifPresent(this::registerCustomAdapters);



getElements(round, AvajeValidPrism.PRISM_TYPE).ifPresent(this::writeAdapters);
getElements(round, HttpValidPrism.PRISM_TYPE).ifPresent(this::writeAdapters);
Expand Down Expand Up @@ -107,6 +109,27 @@ private Optional<? extends Set<? extends Element>> getElements(

private void registerCustomAdapters(Set<? extends Element> elements) {
for (final var typeElement : ElementFilter.typesIn(elements)) {
final var type = Util.baseTypeOfAdapter(typeElement);

if (!CrossParamConstraintPrism.getAllOnMetaAnnotations(typeElement).isEmpty()
&& type.contains("Object[]")) {
logError(typeElement, "Cross Parameter Adapters must accept type Object[]");
}

ElementFilter.constructorsIn(typeElement.getEnclosedElements()).stream()
.filter(m -> m.getModifiers().contains(Modifier.PUBLIC))
.filter(m -> m.getParameters().size() == 1)
.map(m -> m.getParameters().get(0).asType().toString())
.map(Util::trimAnnotations)
.filter("io.avaje.validation.adapter.ValidationContext.AdapterCreateRequest"::equals)
.findAny()
.ifPresentOrElse(
x -> {},
() ->
logError(
typeElement,
"Custom Adapters must have a public constructor with a single AdapterCreateRequest parameter"));

metaData.addAnnotationAdapter(typeElement);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* Marks a type as a Constraint Adapter to be registered automatically.
*
* <p> A custom adapter registered using this annotation must have a public constructor accepting a ValidationContext instance, and must extend the AbstractConstraintAdapter class.
* <p> A custom adapter registered using this annotation must have a public constructor accepting a ValidationContext instance, and must extend the AbstractConstraintAdapter/ValidationAdapter class.
*
* <h3>Example:</h3>
*
Expand Down