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"));
}
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());
}
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));
}
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;
}
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()));
}
Aggregations