Search in sources :

Example 6 with JFieldVar

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;
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 7 with JFieldVar

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);
    }
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JFieldVar(com.sun.codemodel.JFieldVar) JClass(com.sun.codemodel.JClass)

Example 8 with JFieldVar

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);
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) JTypeVar(com.sun.codemodel.JTypeVar) JFieldVar(com.sun.codemodel.JFieldVar) TreeSet(java.util.TreeSet)

Example 9 with JFieldVar

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);
    }
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) DataOutputStream(java.io.DataOutputStream) GenerationException(org.jsonschema2pojo.exception.GenerationException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 10 with JFieldVar

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;
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar)

Aggregations

JFieldVar (com.sun.codemodel.JFieldVar)24 JMethod (com.sun.codemodel.JMethod)11 JInvocation (com.sun.codemodel.JInvocation)9 JVar (com.sun.codemodel.JVar)9 JBlock (com.sun.codemodel.JBlock)8 JClass (com.sun.codemodel.JClass)8 JDefinedClass (com.sun.codemodel.JDefinedClass)6 JExpression (com.sun.codemodel.JExpression)4 ArrayList (java.util.ArrayList)4 ByteString (com.linkedin.data.ByteString)3 JType (com.sun.codemodel.JType)3 HashMap (java.util.HashMap)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)2 DataSchema (com.linkedin.data.schema.DataSchema)2 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)2 MapDataSchema (com.linkedin.data.schema.MapDataSchema)2 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)2 JConditional (com.sun.codemodel.JConditional)2 Map (java.util.Map)2