use of com.sun.codemodel.JAnnotationUse in project jsonschema2pojo by joelittlejohn.
the class MinItemsMaxItemsRule method apply.
@Override
public JFieldVar apply(String nodeName, JsonNode node, JsonNode parent, JFieldVar field, Schema currentSchema) {
if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations() && (node.has("minItems") || node.has("maxItems")) && isApplicableType(field)) {
final Class<? extends Annotation> sizeClass = ruleFactory.getGenerationConfig().isUseJakartaValidation() ? Size.class : javax.validation.constraints.Size.class;
JAnnotationUse annotation = field.annotate(sizeClass);
if (node.has("minItems")) {
annotation.param("min", node.get("minItems").asInt());
}
if (node.has("maxItems")) {
annotation.param("max", node.get("maxItems").asInt());
}
}
return field;
}
use of com.sun.codemodel.JAnnotationUse in project jsonschema2pojo by joelittlejohn.
the class AnnotationHelper method tryToAnnotate.
private static boolean tryToAnnotate(JDefinedClass jclass, String annotationClassName) {
try {
Class.forName(annotationClassName);
JClass annotationClass = jclass.owner().ref(annotationClassName);
JAnnotationUse generated = jclass.annotate(annotationClass);
generated.param("value", GENERATOR_NAME);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
use of com.sun.codemodel.JAnnotationUse in project jsonschema2pojo by joelittlejohn.
the class RequiredArrayRuleTest method shouldUpdateAnnotations.
@Test
public void shouldUpdateAnnotations() throws JClassAlreadyExistsException {
setupRuleFactoryToIncludeJsr303();
JDefinedClass jclass = new JCodeModel()._class(TARGET_CLASS_NAME);
jclass.field(JMod.PRIVATE, jclass.owner().ref(String.class), "fooBar");
jclass.field(JMod.PRIVATE, jclass.owner().ref(String.class), "foo");
ObjectMapper mapper = new ObjectMapper();
ArrayNode requiredNode = mapper.createArrayNode().add("foo_bar");
rule.apply("Class", requiredNode, null, jclass, new Schema(null, requiredNode, null));
Collection<JAnnotationUse> fooBarAnnotations = jclass.fields().get("fooBar").annotations();
Collection<JAnnotationUse> fooAnnotations = jclass.fields().get("foo").annotations();
assertThat(fooBarAnnotations.size(), is(1));
assertThat(fooBarAnnotations.iterator().next().getAnnotationClass().name(), is(notNullClass.getSimpleName()));
assertThat(fooAnnotations.size(), is(0));
}
use of com.sun.codemodel.JAnnotationUse in project rest.li by linkedin.
the class JavaCodeUtil method annotate.
/**
* Create Java {@link Generated} annotation for a class.
*
* @param cls CodeModel class to annotate
* @param classType type of the specified class
* @param location location of where the specified class is generated from
* @param rootPath root path to relativize the location
*/
public static void annotate(JDefinedClass cls, String classType, String location, String rootPath) {
final JAnnotationUse generatedAnnotation = cls.annotate(Generated.class);
generatedAnnotation.param("value", JavaCodeUtil.class.getName());
String comments = "Rest.li " + classType;
if (location != null) {
if (rootPath == null) {
comments += ". Generated from " + location + '.';
} else {
comments += ". Generated from " + Paths.get(rootPath).relativize(Paths.get(location)) + '.';
}
}
generatedAnnotation.param("comments", comments);
}
use of com.sun.codemodel.JAnnotationUse in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method addJsonTypeInfoAnnotation.
private void addJsonTypeInfoAnnotation(JDefinedClass jclass, JsonNode node) {
if (ruleFactory.getGenerationConfig().getAnnotationStyle() == AnnotationStyle.JACKSON2) {
String annotationName = node.get("deserializationClassProperty").asText();
JAnnotationUse jsonTypeInfo = jclass.annotate(JsonTypeInfo.class);
jsonTypeInfo.param("use", JsonTypeInfo.Id.CLASS);
jsonTypeInfo.param("include", JsonTypeInfo.As.PROPERTY);
jsonTypeInfo.param("property", annotationName);
}
}
Aggregations