use of com.sun.codemodel.JFieldVar 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.JFieldVar in project Activiti by Activiti.
the class CxfWSDLImporter method _importFields.
protected static void _importFields(final JDefinedClass theClass, final AtomicInteger index, final SimpleStructureDefinition structure) {
final JClass parentClass = theClass._extends();
if (parentClass != null && parentClass instanceof JDefinedClass) {
_importFields((JDefinedClass) parentClass, index, structure);
}
for (Entry<String, JFieldVar> entry : theClass.fields().entrySet()) {
Class<?> fieldClass = ReflectUtil.loadClass(entry.getValue().type().boxify().erasure().fullName());
String fieldName = entry.getKey();
if (fieldName.startsWith("_")) {
if (!JJavaName.isJavaIdentifier(fieldName.substring(1))) {
//it was prefixed with '_' so we should use the original name.
fieldName = fieldName.substring(1);
}
}
structure.setFieldName(index.getAndIncrement(), fieldName, fieldClass);
}
}
use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.
the class AdditionalPropertiesRule method addInnerBuilder.
private JMethod addInnerBuilder(JDefinedClass jclass, JType propertyType, JFieldVar field) {
Optional<JDefinedClass> builderClass = StreamSupport.stream(Spliterators.spliteratorUnknownSize(jclass.classes(), Spliterator.ORDERED), false).filter(definedClass -> definedClass.name().equals(getBuilderClassName(jclass))).findFirst();
JMethod builder = builderClass.get().method(JMod.PUBLIC, builderClass.get(), "withAdditionalProperty");
JVar nameParam = builder.param(String.class, "name");
JVar valueParam = builder.param(propertyType, "value");
JBlock body = builder.body();
JInvocation mapInvocation = body.invoke(JExpr.ref(JExpr.cast(jclass, JExpr._this().ref("instance")), field), "put");
mapInvocation.arg(nameParam);
mapInvocation.arg(valueParam);
body._return(JExpr._this());
return builder;
}
use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.
the class BuilderRule method generateNoArgsBaseBuilderConstructor.
private void generateNoArgsBaseBuilderConstructor(JDefinedClass instanceClass, JDefinedClass builderClass, JDefinedClass concreteBuilderClass) {
JMethod noArgsConstructor = builderClass.constructor(JMod.PUBLIC);
JAnnotationUse warningSuppression = noArgsConstructor.annotate(SuppressWarnings.class);
warningSuppression.param("value", "unchecked");
JBlock constructorBlock = noArgsConstructor.body();
JFieldVar instanceField = reflectionHelper.searchClassAndSuperClassesForField("instance", builderClass);
// Determine if we need to invoke the super() method for our parent builder
JClass parentClass = builderClass._extends();
if (!(parentClass.isPrimitive() || reflectionHelper.isFinal(parentClass) || Objects.equals(parentClass.fullName(), "java.lang.Object"))) {
constructorBlock.invoke("super");
}
// Only initialize the instance if the object being constructed is actually this class
// if it's a subtype then ignore the instance initialization since the subclass will initialize it
constructorBlock.directStatement("// Skip initialization when called from subclass");
JInvocation comparison = JExpr._this().invoke("getClass").invoke("equals").arg(JExpr.dotclass(concreteBuilderClass));
JConditional ifNotSubclass = constructorBlock._if(comparison);
ifNotSubclass._then().assign(JExpr._this().ref(instanceField), JExpr.cast(instanceField.type(), JExpr._new(instanceClass)));
}
use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.
the class ConstructorRule method generateCopyConstructor.
private JMethod generateCopyConstructor(JDefinedClass jclass, Set<String> classProperties, Set<String> combinedSuperProperties) {
// Create the JMethod for the copy constructor
JMethod copyConstructorResult = jclass.constructor(JMod.PUBLIC);
// Add a parameter to the copyConstructor for the actual object being copied
copyConstructorResult.javadoc().addParam("source");
JVar copyConstructorParam = copyConstructorResult.param(jclass, "source");
// Create the method block for the copyConstructor
JBlock constructorBody = copyConstructorResult.body();
// Invoke the super class constructor for this class. We'll include the original object
// being copied if there are any combinedSuperProperties to be set, and if not then we'll
// simply call the empty constructor from the super class
JInvocation superInvocation = constructorBody.invoke("super");
if (!combinedSuperProperties.isEmpty()) {
superInvocation.arg(copyConstructorParam);
}
// For each of the class properties being set we then need to do something like the following
// this.property = source.property
Map<String, JFieldVar> fields = jclass.fields();
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");
}
constructorBody.assign(JExpr._this().ref(field), copyConstructorParam.ref(field));
}
return copyConstructorResult;
}
Aggregations