use of com.sun.codemodel.JMethod in project jsonschema2pojo by joelittlejohn.
the class SerializableHelper method processMethodCollectionForSerializableSupport.
private static void processMethodCollectionForSerializableSupport(Iterator<JMethod> methods, DataOutputStream dataOutputStream) throws IOException {
TreeMap<String, JMethod> sortedMethods = new TreeMap<String, JMethod>();
while (methods.hasNext()) {
JMethod method = methods.next();
//Collect non-private methods
if ((method.mods().getValue() & JMod.PRIVATE) != JMod.PRIVATE) {
sortedMethods.put(method.name(), method);
}
}
for (JMethod method : sortedMethods.values()) {
dataOutputStream.writeUTF(method.name());
dataOutputStream.writeInt(method.mods().getValue());
if (method.type() != null) {
dataOutputStream.writeUTF(method.type().fullName());
}
for (JVar param : method.params()) {
dataOutputStream.writeUTF(param.type().fullName());
}
}
}
use of com.sun.codemodel.JMethod in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method addToString.
private void addToString(JDefinedClass jclass) {
JMethod toString = jclass.method(JMod.PUBLIC, String.class, "toString");
Class<?> toStringBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.ToStringBuilder.class : org.apache.commons.lang.builder.ToStringBuilder.class;
JBlock body = toString.body();
JInvocation reflectionToString = jclass.owner().ref(toStringBuilder).staticInvoke("reflectionToString");
reflectionToString.arg(JExpr._this());
body._return(reflectionToString);
toString.annotate(Override.class);
}
use of com.sun.codemodel.JMethod in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method addConstructors.
private void addConstructors(JDefinedClass jclass, JsonNode node, Schema schema, boolean onlyRequired) {
LinkedHashSet<String> classProperties = getConstructorProperties(node, onlyRequired);
LinkedHashSet<String> combinedSuperProperties = getSuperTypeConstructorPropertiesRecursive(node, schema, onlyRequired);
// no properties to put in the constructor => default constructor is good enough.
if (classProperties.isEmpty() && combinedSuperProperties.isEmpty()) {
return;
}
// add a no-args constructor for serialization purposes
JMethod noargsConstructor = jclass.constructor(JMod.PUBLIC);
noargsConstructor.javadoc().add("No args constructor for use in serialization");
// add the public constructor with property parameters
JMethod fieldsConstructor = jclass.constructor(JMod.PUBLIC);
JBlock constructorBody = fieldsConstructor.body();
JInvocation superInvocation = constructorBody.invoke("super");
Map<String, JFieldVar> fields = jclass.fields();
Map<String, JVar> classFieldParams = new HashMap<String, JVar>();
for (String property : classProperties) {
JFieldVar field = fields.get(property);
if (field == null) {
throw new IllegalStateException("Property " + property + " hasn't been added to JDefinedClass before calling addConstructors");
}
fieldsConstructor.javadoc().addParam(property);
JVar param = fieldsConstructor.param(field.type(), field.name());
constructorBody.assign(JExpr._this().ref(field), param);
classFieldParams.put(property, param);
}
List<JVar> superConstructorParams = new ArrayList<JVar>();
for (String property : combinedSuperProperties) {
JFieldVar field = searchSuperClassesForField(property, jclass);
if (field == null) {
throw new IllegalStateException("Property " + property + " hasn't been added to JDefinedClass before calling addConstructors");
}
JVar param = classFieldParams.get(property);
if (param == null) {
param = fieldsConstructor.param(field.type(), field.name());
}
fieldsConstructor.javadoc().addParam(property);
superConstructorParams.add(param);
}
for (JVar param : superConstructorParams) {
superInvocation.arg(param);
}
}
use of com.sun.codemodel.JMethod in project jsonschema2pojo by joelittlejohn.
the class AdditionalPropertiesRule method addBuilder.
private void addBuilder(JDefinedClass jclass, JType propertyType, JFieldVar field) {
JMethod builder = jclass.method(JMod.PUBLIC, jclass, "withAdditionalProperty");
JVar nameParam = builder.param(String.class, "name");
JVar valueParam = builder.param(propertyType, "value");
JBlock body = builder.body();
JInvocation mapInvocation = body.invoke(JExpr._this().ref(field), "put");
mapInvocation.arg(nameParam);
mapInvocation.arg(valueParam);
body._return(JExpr._this());
}
use of com.sun.codemodel.JMethod in project jsonschema2pojo by joelittlejohn.
the class DynamicPropertiesRule method addPublicSetMethod.
private JMethod addPublicSetMethod(JDefinedClass jclass, JMethod internalSetMethod) {
JMethod method = jclass.method(PUBLIC, jclass.owner().VOID, SETTER_NAME);
JVar nameParam = method.param(String.class, "name");
JVar valueParam = method.param(Object.class, "value");
JBlock body = method.body();
JBlock notFound = body._if(JOp.not(invoke(internalSetMethod).arg(nameParam).arg(valueParam)))._then();
// if we have additional properties, then put value.
JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
if (getAdditionalProperties != null) {
JType additionalPropertiesType = ((JClass) (getAdditionalProperties.type())).getTypeParameters().get(1);
notFound.add(invoke(getAdditionalProperties).invoke("put").arg(nameParam).arg(cast(additionalPropertiesType, valueParam)));
} else // else throw exception.
{
notFound._throw(illegalArgumentInvocation(jclass, nameParam));
}
return method;
}
Aggregations