Search in sources :

Example 31 with JCodeModel

use of com.sun.codemodel.JCodeModel in project jsonschema2pojo by joelittlejohn.

the class FormatRuleArraysTest method useArraysWithCustomTypeMapping.

@Test
public void useArraysWithCustomTypeMapping() {
    JType result = rule.apply("fooBar", TextNode.valueOf("test"), null, new JCodeModel().ref(Object.class), null);
    assertTrue(result.isArray());
    JType expectedJType = new JCodeModel().ref(expectedType);
    assertThat(result.fullName(), equalTo(expectedJType.fullName()));
}
Also used : JCodeModel(com.sun.codemodel.JCodeModel) JType(com.sun.codemodel.JType) Test(org.junit.Test)

Example 32 with JCodeModel

use of com.sun.codemodel.JCodeModel in project jsonschema2pojo by joelittlejohn.

the class RequiredArrayRuleTest method shouldUpdateJavaDoc.

@Test
public void shouldUpdateJavaDoc() throws JClassAlreadyExistsException {
    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("fooBar");
    rule.apply("Class", requiredNode, null, jclass, new Schema(null, requiredNode, null));
    JDocComment fooBarJavaDoc = jclass.fields().get("fooBar").javadoc();
    JDocComment fooJavaDoc = jclass.fields().get("foo").javadoc();
    assertThat(fooBarJavaDoc.size(), is(1));
    assertThat((String) fooBarJavaDoc.get(0), is("\n(Required)"));
    assertThat(fooJavaDoc.size(), is(0));
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) Schema(org.jsonschema2pojo.Schema) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JCodeModel(com.sun.codemodel.JCodeModel) JDocComment(com.sun.codemodel.JDocComment) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 33 with JCodeModel

use of com.sun.codemodel.JCodeModel 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 34 with JCodeModel

use of com.sun.codemodel.JCodeModel in project jsonschema2pojo by joelittlejohn.

the class TypeUtilTest method testResolveTypeCanHandleWildcard.

@Test
public void testResolveTypeCanHandleWildcard() {
    final JCodeModel codeModel = new JCodeModel();
    final JClass _class = TypeUtil.resolveType(codeModel.rootPackage(), "java.util.List<?>");
    assertThat(_class.erasure(), equalTo(codeModel.ref(List.class)));
    assertThat(_class.typeParams(), emptyArray());
    assertThat(_class.isParameterized(), is(Boolean.TRUE));
    assertThat(_class.getTypeParameters(), hasSize(1));
    assertThat(_class.getTypeParameters().get(0)._extends(), is(equalTo(codeModel.ref(Object.class))));
}
Also used : JClass(com.sun.codemodel.JClass) JCodeModel(com.sun.codemodel.JCodeModel) Test(org.junit.Test)

Example 35 with JCodeModel

use of com.sun.codemodel.JCodeModel in project jsonschema2pojo by joelittlejohn.

the class TypeUtilTest method testResolveTypeCanHandleExtendsWildcard.

@Test
public void testResolveTypeCanHandleExtendsWildcard() {
    final JCodeModel codeModel = new JCodeModel();
    final JClass _class = TypeUtil.resolveType(codeModel.rootPackage(), "java.util.List<? extends java.lang.Number>");
    assertThat(_class.erasure(), equalTo(codeModel.ref(List.class)));
    assertThat(_class.typeParams(), emptyArray());
    assertThat(_class.isParameterized(), is(Boolean.TRUE));
    assertThat(_class.getTypeParameters(), hasSize(1));
    assertThat(_class.getTypeParameters().get(0)._extends(), is(equalTo(codeModel.ref(Number.class))));
}
Also used : JClass(com.sun.codemodel.JClass) JCodeModel(com.sun.codemodel.JCodeModel) Test(org.junit.Test)

Aggregations

JCodeModel (com.sun.codemodel.JCodeModel)89 Test (org.junit.Test)70 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)58 JPackage (com.sun.codemodel.JPackage)50 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)45 JType (com.sun.codemodel.JType)43 JDefinedClass (com.sun.codemodel.JDefinedClass)18 JClass (com.sun.codemodel.JClass)14 RuleFactory (org.jsonschema2pojo.rules.RuleFactory)14 Schema (org.jsonschema2pojo.Schema)13 JsonNode (com.fasterxml.jackson.databind.JsonNode)9 TextNode (com.fasterxml.jackson.databind.node.TextNode)7 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)5 File (java.io.File)5 IOException (java.io.IOException)5 URL (java.net.URL)5 JBlock (com.sun.codemodel.JBlock)4 JCatchBlock (com.sun.codemodel.JCatchBlock)4 JDocComment (com.sun.codemodel.JDocComment)4 JMethod (com.sun.codemodel.JMethod)4