Search in sources :

Example 6 with JAnnotationUse

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;
}
Also used : JAnnotationUse(com.sun.codemodel.JAnnotationUse)

Example 7 with JAnnotationUse

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;
    }
}
Also used : JClass(com.sun.codemodel.JClass) JAnnotationUse(com.sun.codemodel.JAnnotationUse)

Example 8 with JAnnotationUse

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));
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) Schema(org.jsonschema2pojo.Schema) JAnnotationUse(com.sun.codemodel.JAnnotationUse) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 9 with JAnnotationUse

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);
}
Also used : JAnnotationUse(com.sun.codemodel.JAnnotationUse)

Example 10 with JAnnotationUse

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);
    }
}
Also used : JAnnotationUse(com.sun.codemodel.JAnnotationUse)

Aggregations

JAnnotationUse (com.sun.codemodel.JAnnotationUse)16 JClass (com.sun.codemodel.JClass)4 JCodeModel (com.sun.codemodel.JCodeModel)3 JDefinedClass (com.sun.codemodel.JDefinedClass)3 JMethod (com.sun.codemodel.JMethod)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 JaxWsAnnotationProcessor (org.eclipse.scout.jaxws.apt.JaxWsAnnotationProcessor)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 JAnnotationArrayMember (com.sun.codemodel.JAnnotationArrayMember)1 JBlock (com.sun.codemodel.JBlock)1 JConditional (com.sun.codemodel.JConditional)1 JFieldVar (com.sun.codemodel.JFieldVar)1 JInvocation (com.sun.codemodel.JInvocation)1 WebService (javax.jws.WebService)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 AnnotationValue (javax.lang.model.element.AnnotationValue)1 WebServiceClient (javax.xml.ws.WebServiceClient)1 LogicalMessageContext (javax.xml.ws.handler.LogicalMessageContext)1