Search in sources :

Example 61 with JCodeModel

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

the class TypeRuleTest method applyGeneratesIntegerUsingJavaTypeLongPrimitive.

@Test
public void applyGeneratesIntegerUsingJavaTypeLongPrimitive() {
    JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
    ObjectNode objectNode = new ObjectMapper().createObjectNode();
    objectNode.put("type", "integer");
    objectNode.put("javaType", "long");
    when(config.isUsePrimitives()).thenReturn(false);
    JType result = rule.apply("fooBar", objectNode, jpackage, null);
    assertThat(result.fullName(), is("long"));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JPackage(com.sun.codemodel.JPackage) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JType(com.sun.codemodel.JType) Test(org.junit.Test)

Example 62 with JCodeModel

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

the class TypeRuleTest method applyGeneratesIntegerUsingJavaTypeLongWhenMaximumLessThanIntegerMin.

@Test
public void applyGeneratesIntegerUsingJavaTypeLongWhenMaximumLessThanIntegerMin() {
    JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
    ObjectNode objectNode = new ObjectMapper().createObjectNode();
    objectNode.put("type", "integer");
    objectNode.put("maximum", Integer.MIN_VALUE - 1L);
    when(config.isUsePrimitives()).thenReturn(false);
    JType result = rule.apply("fooBar", objectNode, jpackage, null);
    assertThat(result.fullName(), is(Long.class.getName()));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JPackage(com.sun.codemodel.JPackage) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JType(com.sun.codemodel.JType) Test(org.junit.Test)

Example 63 with JCodeModel

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

the class TypeRuleTest method applyGeneratesNullAsObject.

@Test
public void applyGeneratesNullAsObject() {
    JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
    ObjectNode objectNode = new ObjectMapper().createObjectNode();
    objectNode.put("type", "null");
    JType result = rule.apply("fooBar", objectNode, jpackage, null);
    assertThat(result.fullName(), is(Object.class.getName()));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JPackage(com.sun.codemodel.JPackage) JCodeModel(com.sun.codemodel.JCodeModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JType(com.sun.codemodel.JType) Test(org.junit.Test)

Example 64 with JCodeModel

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

the class Jsonschema2Pojo method generate.

/**
     * Reads the contents of the given source and initiates schema generation.
     *
     * @param config
     *            the configuration options (including source and target paths,
     *            and other behavioural options) that will control code
     *            generation
     * @throws FileNotFoundException
     *             if the source path is not found
     * @throws IOException
     *             if the application is unable to read data from the source
     */
public static void generate(GenerationConfig config) throws IOException {
    Annotator annotator = getAnnotator(config);
    RuleFactory ruleFactory = createRuleFactory(config);
    ruleFactory.setAnnotator(annotator);
    ruleFactory.setGenerationConfig(config);
    SchemaMapper mapper = new SchemaMapper(ruleFactory, new SchemaGenerator());
    JCodeModel codeModel = new JCodeModel();
    if (config.isRemoveOldOutput()) {
        removeOldOutput(config.getTargetDirectory());
    }
    for (Iterator<URL> sources = config.getSource(); sources.hasNext(); ) {
        URL source = sources.next();
        if (URLUtil.parseProtocol(source.toString()) == URLProtocol.FILE && URLUtil.getFileFromURL(source).isDirectory()) {
            generateRecursive(config, mapper, codeModel, defaultString(config.getTargetPackage()), Arrays.asList(URLUtil.getFileFromURL(source).listFiles(config.getFileFilter())));
        } else {
            mapper.generate(codeModel, getNodeName(source, config), defaultString(config.getTargetPackage()), source);
        }
    }
    if (config.getTargetDirectory().exists() || config.getTargetDirectory().mkdirs()) {
        CodeWriter sourcesWriter = new FileCodeWriterWithEncoding(config.getTargetDirectory(), config.getOutputEncoding());
        CodeWriter resourcesWriter = new FileCodeWriterWithEncoding(config.getTargetDirectory(), config.getOutputEncoding());
        codeModel.build(sourcesWriter, resourcesWriter);
    } else {
        throw new GenerationException("Could not create or access target directory " + config.getTargetDirectory().getAbsolutePath());
    }
}
Also used : RuleFactory(org.jsonschema2pojo.rules.RuleFactory) GenerationException(org.jsonschema2pojo.exception.GenerationException) JCodeModel(com.sun.codemodel.JCodeModel) CodeWriter(com.sun.codemodel.CodeWriter) URL(java.net.URL)

Example 65 with JCodeModel

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

the class SelfRefIT method nestedSelfRefsInStringContentWithoutParentFile.

@Test
public void nestedSelfRefsInStringContentWithoutParentFile() throws NoSuchMethodException, ClassNotFoundException, IOException {
    String schemaContents = IOUtils.toString(CodeGenerationHelper.class.getResource("/schema/ref/nestedSelfRefsReadAsString.json"));
    JCodeModel codeModel = new JCodeModel();
    new SchemaMapper().generate(codeModel, "NestedSelfRefsInString", "com.example", schemaContents);
    codeModel.build(schemaRule.getGenerateDir());
    ClassLoader classLoader = schemaRule.compile();
    Class<?> nestedSelfRefs = classLoader.loadClass("com.example.NestedSelfRefsInString");
    assertThat(nestedSelfRefs.getMethod("getThings").getReturnType().getSimpleName(), equalTo("List"));
    Class<?> listEntryType = (Class<?>) ((ParameterizedType) nestedSelfRefs.getMethod("getThings").getGenericReturnType()).getActualTypeArguments()[0];
    assertThat(listEntryType.getName(), equalTo("com.example.Thing"));
    Class<?> thingClass = classLoader.loadClass("com.example.Thing");
    assertThat(thingClass.getMethod("getNamespace").getReturnType().getSimpleName(), equalTo("String"));
    assertThat(thingClass.getMethod("getName").getReturnType().getSimpleName(), equalTo("String"));
    assertThat(thingClass.getMethod("getVersion").getReturnType().getSimpleName(), equalTo("String"));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) CodeGenerationHelper(org.jsonschema2pojo.integration.util.CodeGenerationHelper) BeforeClass(org.junit.BeforeClass) JCodeModel(com.sun.codemodel.JCodeModel) SchemaMapper(org.jsonschema2pojo.SchemaMapper) Test(org.junit.Test)

Aggregations

JCodeModel (com.sun.codemodel.JCodeModel)77 Test (org.junit.Test)59 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)52 JPackage (com.sun.codemodel.JPackage)47 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)45 JType (com.sun.codemodel.JType)41 JDefinedClass (com.sun.codemodel.JDefinedClass)15 JClass (com.sun.codemodel.JClass)12 Schema (org.jsonschema2pojo.Schema)11 RuleFactory (org.jsonschema2pojo.rules.RuleFactory)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 TextNode (com.fasterxml.jackson.databind.node.TextNode)6 File (java.io.File)6 IOException (java.io.IOException)5 JBlock (com.sun.codemodel.JBlock)4 JCatchBlock (com.sun.codemodel.JCatchBlock)4 JMethod (com.sun.codemodel.JMethod)4 JTryBlock (com.sun.codemodel.JTryBlock)4 JVar (com.sun.codemodel.JVar)4 URL (java.net.URL)4