-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from infobip/SingleArgumentPropertiesCreatorMod…
…eAnnotationIntrospector added SingleArgumentPropertiesCreatorModeAnnotationIntrospector
- Loading branch information
Showing
16 changed files
with
298 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...n/java/com/infobip/jackson/SingleArgumentPropertiesCreatorModeAnnotationIntrospector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.infobip.jackson; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.cfg.MapperConfig; | ||
import com.fasterxml.jackson.databind.introspect.*; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Parameter; | ||
import java.util.Objects; | ||
import java.util.stream.Stream; | ||
|
||
public class SingleArgumentPropertiesCreatorModeAnnotationIntrospector extends NopAnnotationIntrospector { | ||
|
||
@Override | ||
public JsonCreator.Mode findCreatorAnnotation(MapperConfig<?> config, Annotated a) { | ||
if (!(a instanceof AnnotatedConstructor)) { | ||
return null; | ||
} | ||
|
||
AnnotatedConstructor annotatedConstructor = (AnnotatedConstructor) a; | ||
|
||
if (annotatedConstructor.getParameterCount() != 1) { | ||
return null; | ||
} | ||
|
||
Class<?> declaringClass = annotatedConstructor.getDeclaringClass(); | ||
|
||
if (declaringClass.getDeclaredConstructors().length > 1) { | ||
return null; | ||
} | ||
|
||
if (doesntHaveMatchingFieldAndParameter(annotatedConstructor, declaringClass)) { | ||
return null; | ||
} | ||
|
||
return JsonCreator.Mode.PROPERTIES; | ||
} | ||
|
||
private boolean doesntHaveMatchingFieldAndParameter(AnnotatedConstructor annotatedConstructor, | ||
Class<?> declaringClass) { | ||
Parameter parameter = annotatedConstructor.getAnnotated().getParameters()[0]; | ||
return Stream.of(declaringClass.getDeclaredFields()) | ||
.noneMatch(field -> doesFieldMatchParameter(field, parameter)); | ||
} | ||
|
||
private boolean doesFieldMatchParameter(Field field, Parameter parameter) { | ||
|
||
if(!field.getType().equals(parameter.getType())) { | ||
return false; | ||
} | ||
|
||
String parameterName = parameter.getName(); | ||
JsonProperty annotation = field.getAnnotation(JsonProperty.class); | ||
|
||
if (Objects.nonNull(annotation) && annotation.value().equals(parameterName)) { | ||
return true; | ||
} | ||
|
||
return field.getName().equals(parameterName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.