use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.
the class RequiredArrayRule method apply.
@Override
public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass, Schema schema) {
List<String> requiredFieldMethods = new ArrayList<String>();
JsonNode properties = schema.getContent().get("properties");
for (Iterator<JsonNode> iterator = node.elements(); iterator.hasNext(); ) {
String requiredArrayItem = iterator.next().asText();
JsonNode propertyNode = null;
if (properties != null) {
propertyNode = properties.findValue(requiredArrayItem);
}
String fieldName = ruleFactory.getNameHelper().getPropertyName(requiredArrayItem, propertyNode);
JFieldVar field = jclass.fields().get(fieldName);
if (field == null) {
continue;
}
addJavaDoc(field);
if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations()) {
addNotNullAnnotation(field);
}
if (ruleFactory.getGenerationConfig().isIncludeJsr305Annotations()) {
addNonnullAnnotation(field);
}
requiredFieldMethods.add(getGetterName(fieldName, field.type(), node));
requiredFieldMethods.add(getSetterName(fieldName, node));
}
updateGetterSetterJavaDoc(jclass, requiredFieldMethods);
return jclass;
}
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 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.JFieldVar in project jsonschema2pojo by joelittlejohn.
the class SerializableHelper method addSerializableSupport.
public static void addSerializableSupport(JDefinedClass jclass) {
jclass._implements(Serializable.class);
try {
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
processDefinedClassForSerializableSupport(jclass, dataOutputStream);
dataOutputStream.flush();
final MessageDigest digest = MessageDigest.getInstance("SHA");
final byte[] digestBytes = digest.digest(byteArrayOutputStream.toByteArray());
long serialVersionUID = 0L;
for (int i = Math.min(digestBytes.length, 8) - 1; i >= 0; i--) {
serialVersionUID = serialVersionUID << 8 | digestBytes[i] & 0xff;
}
JFieldVar serialUIDField = jclass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, long.class, "serialVersionUID");
serialUIDField.init(JExpr.lit(serialVersionUID));
} catch (IOException exception) {
throw new GenerationException("IOException while generating serialversionUID field while adding serializable support to class: " + jclass.fullName(), exception);
} catch (NoSuchAlgorithmException exception) {
throw new GenerationException("SHA algorithm not found when trying to generate serialversionUID field while adding serializable support to class: " + jclass.fullName(), exception);
}
}
use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.
the class ObjectRule method searchClassAndSuperClassesForField.
private JFieldVar searchClassAndSuperClassesForField(String property, JDefinedClass jclass) {
Map<String, JFieldVar> fields = jclass.fields();
JFieldVar field = fields.get(property);
if (field == null) {
return searchSuperClassesForField(property, jclass);
}
return field;
}
Aggregations