use of com.sun.codemodel.JClass in project jsonschema2pojo by joelittlejohn.
the class TypeUtil method buildClass.
private static JClass buildClass(JClassContainer _package, ClassOrInterfaceType c, int arrayCount) {
final String packagePrefix = (c.getScope() != null) ? c.getScope().toString() + "." : "";
JClass _class;
try {
_class = _package.owner().ref(Thread.currentThread().getContextClassLoader().loadClass(packagePrefix + c.getName()));
} catch (ClassNotFoundException e) {
_class = _package.owner().ref(packagePrefix + c.getName());
}
for (int i = 0; i < arrayCount; i++) {
_class = _class.array();
}
List<Type> typeArgs = c.getTypeArgs();
if (typeArgs != null && typeArgs.size() > 0) {
JClass[] genericArgumentClasses = new JClass[typeArgs.size()];
for (int i = 0; i < typeArgs.size(); i++) {
genericArgumentClasses[i] = buildClass(_package, (ClassOrInterfaceType) ((ReferenceType) typeArgs.get(i)).getType(), ((ReferenceType) typeArgs.get(i)).getArrayCount());
}
_class = _class.narrow(genericArgumentClasses);
}
return _class;
}
use of com.sun.codemodel.JClass in project jsonschema2pojo by joelittlejohn.
the class ArrayRuleTest method arrayWithNonUniqueItemsProducesList.
@Test
public void arrayWithNonUniqueItemsProducesList() {
JCodeModel codeModel = new JCodeModel();
JPackage jpackage = codeModel._package(getClass().getPackage().getName());
ObjectMapper mapper = new ObjectMapper();
ObjectNode itemsNode = mapper.createObjectNode();
itemsNode.put("type", "number");
ObjectNode propertyNode = mapper.createObjectNode();
propertyNode.set("uniqueItems", BooleanNode.FALSE);
propertyNode.set("items", itemsNode);
Schema schema = mock(Schema.class);
when(schema.getId()).thenReturn(URI.create("http://example/nonUniqueArray"));
when(config.isUseDoubleNumbers()).thenReturn(true);
JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, schema);
assertThat(propertyType, notNullValue());
assertThat(propertyType.erasure(), is(codeModel.ref(List.class)));
assertThat(propertyType.getTypeParameters().get(0).fullName(), is(Double.class.getName()));
}
use of com.sun.codemodel.JClass in project jsonschema2pojo by joelittlejohn.
the class ArrayRuleTest method arrayWithUniqueItemsProducesSet.
@Test
public void arrayWithUniqueItemsProducesSet() {
JCodeModel codeModel = new JCodeModel();
JPackage jpackage = codeModel._package(getClass().getPackage().getName());
ObjectMapper mapper = new ObjectMapper();
ObjectNode itemsNode = mapper.createObjectNode();
itemsNode.put("type", "integer");
ObjectNode propertyNode = mapper.createObjectNode();
propertyNode.set("uniqueItems", BooleanNode.TRUE);
propertyNode.set("items", itemsNode);
JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, mock(Schema.class));
assertThat(propertyType, notNullValue());
assertThat(propertyType.erasure(), is(codeModel.ref(Set.class)));
assertThat(propertyType.getTypeParameters().get(0).fullName(), is(Integer.class.getName()));
}
use of com.sun.codemodel.JClass in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method createClass.
/**
* Creates a new Java class that will be generated.
*
* @param nodeName
* the node name which may be used to dictate the new class name
* @param node
* the node representing the schema that caused the need for a
* new class. This node may include a 'javaType' property which
* if present will override the fully qualified name of the newly
* generated class.
* @param _package
* the package which may contain a new class after this method
* call
* @return a reference to a newly created class
* @throws ClassAlreadyExistsException
* if the given arguments cause an attempt to create a class
* that already exists, either on the classpath or in the
* current map of classes to be generated.
*/
private JDefinedClass createClass(String nodeName, JsonNode node, JPackage _package) throws ClassAlreadyExistsException {
JDefinedClass newType;
try {
boolean usePolymorphicDeserialization = usesPolymorphicDeserialization(node);
if (node.has("javaType")) {
String fqn = substringBefore(node.get("javaType").asText(), "<");
if (isPrimitive(fqn, _package.owner())) {
throw new ClassAlreadyExistsException(primitiveType(fqn, _package.owner()));
}
JClass existingClass;
try {
_package.owner().ref(Thread.currentThread().getContextClassLoader().loadClass(fqn));
existingClass = resolveType(_package, fqn + (node.get("javaType").asText().contains("<") ? "<" + substringAfter(node.get("javaType").asText(), "<") : ""));
throw new ClassAlreadyExistsException(existingClass);
} catch (ClassNotFoundException e) {
}
int index = fqn.lastIndexOf(".") + 1;
if (index >= 0 && index < fqn.length()) {
fqn = fqn.substring(0, index) + ruleFactory.getGenerationConfig().getClassNamePrefix() + fqn.substring(index) + ruleFactory.getGenerationConfig().getClassNameSuffix();
}
try {
_package.owner().ref(Thread.currentThread().getContextClassLoader().loadClass(fqn));
existingClass = resolveType(_package, fqn + (node.get("javaType").asText().contains("<") ? "<" + substringAfter(node.get("javaType").asText(), "<") : ""));
throw new ClassAlreadyExistsException(existingClass);
} catch (ClassNotFoundException e) {
}
if (usePolymorphicDeserialization) {
newType = _package.owner()._class(JMod.PUBLIC, fqn, ClassType.CLASS);
} else {
newType = _package.owner()._class(fqn);
}
} else {
if (usePolymorphicDeserialization) {
newType = _package._class(JMod.PUBLIC, getClassName(nodeName, node, _package), ClassType.CLASS);
} else {
newType = _package._class(getClassName(nodeName, node, _package));
}
}
} catch (JClassAlreadyExistsException e) {
throw new ClassAlreadyExistsException(e.getExistingClass());
}
ruleFactory.getAnnotator().propertyInclusion(newType, node);
return newType;
}
use of com.sun.codemodel.JClass in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method searchSuperClassesForField.
/**
* This is recursive with searchClassAndSuperClassesForField
*/
private JFieldVar searchSuperClassesForField(String property, JDefinedClass jclass) {
JClass superClass = jclass._extends();
JDefinedClass definedSuperClass = definedClassOrNullFromType(superClass);
if (definedSuperClass == null) {
return null;
}
return searchClassAndSuperClassesForField(property, definedSuperClass);
}
Aggregations