Skip to content

Commit

Permalink
Merge pull request #13 from victools/annotation-convenience-function
Browse files Browse the repository at this point in the history
  • Loading branch information
CarstenWickner authored Nov 29, 2019
2 parents d059068 + 84448ff commit 016fe91
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Introduce convenience function `MemberScope.getAnnotationConsideringFieldAndGetter(Class)`

## [3.3.0] - 2019-10-25
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<groupId>com.github.victools</groupId>
<artifactId>jsonschema-generator</artifactId>
<version>3.3.1-SNAPSHOT</version>
<version>3.4.0-SNAPSHOT</version>
<packaging>jar</packaging>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.classmate.ResolvedTypeWithMembers;
import com.fasterxml.classmate.members.ResolvedField;
import com.fasterxml.classmate.members.ResolvedMethod;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.stream.Stream;

Expand Down Expand Up @@ -112,4 +113,14 @@ public MethodScope findGetter() {
public boolean hasGetter() {
return this.findGetter() != null;
}

@Override
public <A extends Annotation> A getAnnotationConsideringFieldAndGetter(Class<A> annotationClass) {
A annotation = this.getAnnotation(annotationClass);
if (annotation == null) {
MemberScope<?, ?> associatedGetter = this.findGetter();
annotation = associatedGetter == null ? null : associatedGetter.getAnnotation(annotationClass);
}
return annotation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
return this.member.get(annotationClass);
}

/**
* Return the annotation of the given type on the member, if such an annotation is present on either the field or its getter.
*
* @param <A> type of annotation
* @param annotationClass type of annotation
* @return annotation instance (or {@code null} if no annotation of the given type is present)
*/
public abstract <A extends Annotation> A getAnnotationConsideringFieldAndGetter(Class<A> annotationClass);

/* ============================================== *
* Convenience methods for member in this context *
* ============================================== */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.classmate.ResolvedType;
import com.fasterxml.classmate.ResolvedTypeWithMembers;
import com.fasterxml.classmate.members.ResolvedMethod;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -138,6 +139,16 @@ public boolean isGetter() {
return this.findGetterField() != null;
}

@Override
public <A extends Annotation> A getAnnotationConsideringFieldAndGetter(Class<A> annotationClass) {
A annotation = this.getAnnotation(annotationClass);
if (annotation == null) {
MemberScope<?, ?> associatedField = this.findGetterField();
annotation = associatedField == null ? null : associatedField.getAnnotation(annotationClass);
}
return annotation;
}

/**
* Returns the name to be used to reference this method in its parent's "properties".
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package com.github.victools.jsonschema.generator;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Assert;
Expand Down Expand Up @@ -68,9 +72,28 @@ public void testHasGetter(String fieldName, boolean expectedResult) throws Excep
Assert.assertEquals(expectedResult, result);
}

@Test
@Parameters({
"fieldWithoutGetter, false",
"fieldWithPrivateGetter, true",
"fieldWithPublicGetter, false",
"fieldWithPublicBooleanGetter, true"
})
public void testGetAnnotationConsideringFieldAndGetter(String fieldName, boolean annotationExpectedToBeFound) {
FieldScope field = this.getTestClassField(fieldName);
TestAnnotation annotation = field.getAnnotationConsideringFieldAndGetter(TestAnnotation.class);

if (annotationExpectedToBeFound) {
Assert.assertNotNull(annotation);
} else {
Assert.assertNull(annotation);
}
}

private static class TestClass {

private String fieldWithoutGetter;
@TestAnnotation
private int fieldWithPrivateGetter;
private long fieldWithPublicGetter;
private boolean fieldWithPublicBooleanGetter;
Expand All @@ -83,8 +106,14 @@ public long getFieldWithPublicGetter() {
return this.fieldWithPublicGetter;
}

@TestAnnotation
public boolean isFieldWithPublicBooleanGetter() {
return this.fieldWithPublicBooleanGetter;
}
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
private static @interface TestAnnotation {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package com.github.victools.jsonschema.generator;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Assert;
Expand Down Expand Up @@ -76,12 +80,32 @@ public void testIsGetter(String methodName, boolean expectedResult) throws Excep
Assert.assertEquals(expectedResult, result);
}

@Test
@Parameters({
"calculateSomething, false",
"getFieldWithPrivateGetter, true",
"getFieldWithPublicGetter, false",
"isFieldWithPublicBooleanGetter, true"
})
public void testGetAnnotationConsideringFieldAndGetter(String methodName, boolean annotationExpectedToBeFound) {
MethodScope method = this.getTestClassMethod(methodName);
TestAnnotation annotation = method.getAnnotationConsideringFieldAndGetter(TestAnnotation.class);

if (annotationExpectedToBeFound) {
Assert.assertNotNull(annotation);
} else {
Assert.assertNull(annotation);
}
}

private static class TestClass {

private int fieldWithPrivateGetter;
private long fieldWithPublicGetter;
@TestAnnotation
private boolean fieldWithPublicBooleanGetter;

@TestAnnotation
private int getFieldWithPrivateGetter() {
return this.fieldWithPrivateGetter;
}
Expand Down Expand Up @@ -114,4 +138,9 @@ public int calculateSomething() {
return 42;
}
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
private static @interface TestAnnotation {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
Expand Down Expand Up @@ -99,8 +100,15 @@ private static void populateConfigPart(SchemaGeneratorConfigPart<?> configPart,
}

Object parametersForTestGenerateSchema() {
Module neutralModule = configBuilder -> {
};
Module neutralModule = configBuilder -> configBuilder.with((javaType, context) -> {
if (Integer.class == javaType.getErasedType()) {
ObjectNode customNode = configBuilder.getObjectMapper()
.createObjectNode()
.put("$comment", "custom definition for Integer.class");
return new CustomDefinition(customNode, false);
}
return null;
});
Module methodModule = configBuilder -> populateConfigPart(configBuilder.forMethods(), "looked-up from method: ");
Module fieldModule = configBuilder -> populateConfigPart(configBuilder.forFields(), "looked-up from field: ");
return new Object[][]{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"definitions": {
"Integer (nullable)": {
"$comment": "custom definition for Integer.class"
}
},
"type": "object",
"properties": {
"CONSTANT": {
Expand All @@ -15,7 +20,7 @@
"type": ["string", "null"]
},
"ignoredInternalValue": {
"type": ["integer", "null"]
"$ref": "#/definitions/Integer (nullable)"
},
"primitiveValue": {
"type": "integer"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"definitions": {
"Integer (nullable)": {
"$comment": "custom definition for Integer.class"
},
"Optional<String> (nullable)": {
"type": ["object", "null"],
"properties": {
Expand Down Expand Up @@ -71,7 +74,7 @@
"type": ["string", "null"]
},
"ignoredInternalValue": {
"type": ["integer", "null"]
"$ref": "#/definitions/Integer (nullable)"
},
"primitiveValue": {
"type": "integer"
Expand Down

0 comments on commit 016fe91

Please sign in to comment.