use of com.sun.codemodel.JClass in project jsonschema2pojo by joelittlejohn.
the class SerializableHelper method processDefinedClassForSerializableSupport.
private static void processDefinedClassForSerializableSupport(JDefinedClass jclass, DataOutputStream dataOutputStream) throws IOException {
dataOutputStream.writeUTF(jclass.fullName());
dataOutputStream.writeInt(jclass.mods().getValue());
for (JTypeVar typeParam : jclass.typeParams()) {
dataOutputStream.writeUTF(typeParam.fullName());
}
//sorted
TreeMap<String, JDefinedClass> sortedClasses = new TreeMap<String, JDefinedClass>();
Iterator<JDefinedClass> classes = jclass.classes();
while (classes.hasNext()) {
JDefinedClass nestedClass = classes.next();
sortedClasses.put(nestedClass.fullName(), nestedClass);
}
for (JDefinedClass nestedClass : sortedClasses.values()) {
processDefinedClassForSerializableSupport(nestedClass, dataOutputStream);
}
//sorted
TreeSet<String> fieldNames = new TreeSet<String>(jclass.fields().keySet());
for (String fieldName : fieldNames) {
JFieldVar fieldVar = jclass.fields().get(fieldName);
//non private members
if ((fieldVar.mods().getValue() & JMod.PRIVATE) != JMod.PRIVATE) {
processFieldVarForSerializableSupport(jclass.fields().get(fieldName), dataOutputStream);
}
}
Iterator<JClass> interfaces = jclass._implements();
List<JClass> interfacesList = new ArrayList<JClass>();
while (interfaces.hasNext()) {
JClass aInterface = interfaces.next();
interfacesList.add(aInterface);
}
Collections.sort(interfacesList, INTERFACE_COMPARATOR);
for (JClass aInterface : interfacesList) {
dataOutputStream.writeUTF(aInterface.fullName());
}
//we should probably serialize the parent class too! (but what if it has serialversionUID on it? that would be a field and would affect the serialversionUID!)
if (jclass._extends() != null) {
dataOutputStream.writeUTF(jclass._extends().fullName());
}
processMethodCollectionForSerializableSupport(jclass.methods().iterator(), dataOutputStream);
processMethodCollectionForSerializableSupport(jclass.constructors(), dataOutputStream);
}
use of com.sun.codemodel.JClass in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method addHashCode.
private void addHashCode(JDefinedClass jclass) {
Map<String, JFieldVar> fields = jclass.fields();
JMethod hashCode = jclass.method(JMod.PUBLIC, int.class, "hashCode");
Class<?> hashCodeBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.HashCodeBuilder.class : org.apache.commons.lang.builder.HashCodeBuilder.class;
JBlock body = hashCode.body();
JClass hashCodeBuilderClass = jclass.owner().ref(hashCodeBuilder);
JInvocation hashCodeBuilderInvocation = JExpr._new(hashCodeBuilderClass);
if (!jclass._extends().fullName().equals(Object.class.getName())) {
hashCodeBuilderInvocation = hashCodeBuilderInvocation.invoke("appendSuper").arg(JExpr._super().invoke("hashCode"));
}
for (JFieldVar fieldVar : fields.values()) {
if ((fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
continue;
}
hashCodeBuilderInvocation = hashCodeBuilderInvocation.invoke("append").arg(fieldVar);
}
body._return(hashCodeBuilderInvocation.invoke("toHashCode"));
hashCode.annotate(Override.class);
}
use of com.sun.codemodel.JClass in project jsonschema2pojo by joelittlejohn.
the class ArrayRuleTest method arrayOfPrimitivesProducesCollectionOfWrapperTypes.
@Test
public void arrayOfPrimitivesProducesCollectionOfWrapperTypes() {
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.isUsePrimitives()).thenReturn(true);
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 arrayDefaultsToNonUnique.
@Test
public void arrayDefaultsToNonUnique() {
JCodeModel codeModel = new JCodeModel();
JPackage jpackage = codeModel._package(getClass().getPackage().getName());
ObjectMapper mapper = new ObjectMapper();
ObjectNode itemsNode = mapper.createObjectNode();
itemsNode.put("type", "boolean");
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/defaultArray"));
JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, schema);
assertThat(propertyType.erasure(), is(codeModel.ref(List.class)));
}
use of com.sun.codemodel.JClass in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method addEquals.
private void addEquals(JDefinedClass jclass) {
Map<String, JFieldVar> fields = jclass.fields();
JMethod equals = jclass.method(JMod.PUBLIC, boolean.class, "equals");
JVar otherObject = equals.param(Object.class, "other");
Class<?> equalsBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.EqualsBuilder.class : org.apache.commons.lang.builder.EqualsBuilder.class;
JBlock body = equals.body();
body._if(otherObject.eq(JExpr._this()))._then()._return(JExpr.TRUE);
body._if(otherObject._instanceof(jclass).eq(JExpr.FALSE))._then()._return(JExpr.FALSE);
JVar rhsVar = body.decl(jclass, "rhs").init(JExpr.cast(jclass, otherObject));
JClass equalsBuilderClass = jclass.owner().ref(equalsBuilder);
JInvocation equalsBuilderInvocation = JExpr._new(equalsBuilderClass);
if (!jclass._extends().fullName().equals(Object.class.getName())) {
equalsBuilderInvocation = equalsBuilderInvocation.invoke("appendSuper").arg(JExpr._super().invoke("equals").arg(otherObject));
}
for (JFieldVar fieldVar : fields.values()) {
if ((fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
continue;
}
equalsBuilderInvocation = equalsBuilderInvocation.invoke("append").arg(fieldVar).arg(rhsVar.ref(fieldVar.name()));
}
JInvocation reflectionEquals = jclass.owner().ref(equalsBuilder).staticInvoke("reflectionEquals");
reflectionEquals.arg(JExpr._this());
reflectionEquals.arg(otherObject);
body._return(equalsBuilderInvocation.invoke("isEquals"));
equals.annotate(Override.class);
}
Aggregations