Search in sources :

Example 16 with JPackage

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

the class TypeRuleTest method applyGeneratesIntegerUsingJavaTypeInteger.

@Test
public void applyGeneratesIntegerUsingJavaTypeInteger() {
    JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
    ObjectNode objectNode = new ObjectMapper().createObjectNode();
    objectNode.put("type", "integer");
    objectNode.put("javaType", "java.lang.Integer");
    when(config.isUsePrimitives()).thenReturn(true);
    JType result = rule.apply("fooBar", objectNode, jpackage, null);
    assertThat(result.fullName(), is("java.lang.Integer"));
}
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 17 with JPackage

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

the class ObjectRule method definedClassOrNullFromType.

private static JDefinedClass definedClassOrNullFromType(JType type) {
    if (type == null || type.isPrimitive()) {
        return null;
    }
    JClass fieldClass = type.boxify();
    JPackage jPackage = fieldClass._package();
    return jPackage._getClass(fieldClass.name());
}
Also used : JClass(com.sun.codemodel.JClass) JPackage(com.sun.codemodel.JPackage)

Example 18 with JPackage

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

the class SchemaMapper method generate.

public JType generate(JCodeModel codeModel, String className, String packageName, String json) throws IOException {
    JPackage jpackage = codeModel._package(packageName);
    JsonNode schemaNode = null;
    if (ruleFactory.getGenerationConfig().getSourceType() == SourceType.JSON) {
        JsonNode jsonNode = objectMapper().readTree(json);
        schemaNode = schemaGenerator.schemaFromExample(jsonNode);
    } else {
        schemaNode = objectMapper().readTree(json);
    }
    return ruleFactory.getSchemaRule().apply(className, schemaNode, jpackage, new Schema(null, schemaNode, schemaNode));
}
Also used : JPackage(com.sun.codemodel.JPackage) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 19 with JPackage

use of com.sun.codemodel.JPackage in project rest.li by linkedin.

the class JavaCodeUtil method targetFiles.

/**
   * Build the list of files need to be written from CodeModel, with the targetDirectory as base directory.
   *
   * @param targetDirectory directory for the target files
   * @param codeModel {@link JCodeModel} instance
   * @param classLoader Java {@link ClassLoader} to check if a class for the potential target file already exist
   * @param checker custom closure to check if a class should be persistent
   * @return target files to be written
   */
public static List<File> targetFiles(File targetDirectory, JCodeModel codeModel, ClassLoader classLoader, PersistentClassChecker checker) {
    final List<File> generatedFiles = new ArrayList<File>();
    for (Iterator<JPackage> packageIterator = codeModel.packages(); packageIterator.hasNext(); ) {
        for (Iterator<JDefinedClass> classIterator = packageIterator.next().classes(); classIterator.hasNext(); ) {
            final JDefinedClass definedClass = classIterator.next();
            boolean classFound;
            try {
                final Class<?> clazz = classLoader.loadClass(definedClass.fullName());
                classFound = true;
            } catch (ClassNotFoundException e) {
                classFound = false;
            }
            if (classFound) {
                _log.debug(definedClass.fullName() + " found in resolver path");
                definedClass.hide();
            } else if (!checker.isPersistent(definedClass)) {
                definedClass.hide();
            } else if (definedClass.outer() == null) {
                final File file = new File(targetDirectory, definedClass.fullName().replace('.', File.separatorChar) + ".java");
                generatedFiles.add(file);
            }
        }
    }
    return generatedFiles;
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) ArrayList(java.util.ArrayList) JPackage(com.sun.codemodel.JPackage) File(java.io.File)

Example 20 with JPackage

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

the class SchemaMapperTest method generateReadsSchemaAsObject.

@Test
public void generateReadsSchemaAsObject() throws IOException {
    final SchemaRule mockSchemaRule = mock(SchemaRule.class);
    final RuleFactory mockRuleFactory = mock(RuleFactory.class);
    when(mockRuleFactory.getSchemaRule()).thenReturn(mockSchemaRule);
    when(mockRuleFactory.getGenerationConfig()).thenReturn(new DefaultGenerationConfig());
    URL schemaContent = this.getClass().getResource("/schema/address.json");
    new SchemaMapper(mockRuleFactory, new SchemaGenerator()).generate(new JCodeModel(), "Address", "com.example.package", schemaContent);
    ArgumentCaptor<JPackage> capturePackage = ArgumentCaptor.forClass(JPackage.class);
    ArgumentCaptor<JsonNode> captureNode = ArgumentCaptor.forClass(JsonNode.class);
    verify(mockSchemaRule).apply(eq("Address"), captureNode.capture(), capturePackage.capture(), Mockito.isA(Schema.class));
    assertThat(capturePackage.getValue().name(), is("com.example.package"));
    assertThat(captureNode.getValue(), is(notNullValue()));
}
Also used : RuleFactory(org.jsonschema2pojo.rules.RuleFactory) JPackage(com.sun.codemodel.JPackage) SchemaRule(org.jsonschema2pojo.rules.SchemaRule) JsonNode(com.fasterxml.jackson.databind.JsonNode) JCodeModel(com.sun.codemodel.JCodeModel) URL(java.net.URL) Test(org.junit.Test)

Aggregations

JPackage (com.sun.codemodel.JPackage)53 JCodeModel (com.sun.codemodel.JCodeModel)47 Test (org.junit.Test)47 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)44 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)44 JType (com.sun.codemodel.JType)37 JsonNode (com.fasterxml.jackson.databind.JsonNode)7 JClass (com.sun.codemodel.JClass)7 Schema (org.jsonschema2pojo.Schema)7 RuleFactory (org.jsonschema2pojo.rules.RuleFactory)6 SchemaRule (org.jsonschema2pojo.rules.SchemaRule)4 JDefinedClass (com.sun.codemodel.JDefinedClass)3 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 DataList (com.linkedin.data.DataList)1 DataMap (com.linkedin.data.DataMap)1 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)1 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)1