Search in sources :

Example 56 with JFieldVar

use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.

the class MinimumMaximumRuleTest method jsrDisable.

@Test
public void jsrDisable() {
    when(config.isIncludeJsr303Annotations()).thenReturn(false);
    JFieldVar result = rule.apply("node", node, null, fieldVar, null);
    assertSame(fieldVar, result);
    verify(fieldVar, never()).annotate(decimalMinClass);
    verify(annotationMin, never()).param(anyString(), anyString());
    verify(fieldVar, never()).annotate(decimalMaxClass);
    verify(annotationMax, never()).param(anyString(), anyString());
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) Test(org.junit.Test)

Example 57 with JFieldVar

use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.

the class MinimumMaximumRuleTest method testMinimum.

@Test
public void testMinimum() {
    when(config.isIncludeJsr303Annotations()).thenReturn(true);
    final String minValue = Integer.toString(new Random().nextInt());
    when(subNode.asText()).thenReturn(minValue);
    when(node.get("minimum")).thenReturn(subNode);
    when(fieldVar.annotate(decimalMinClass)).thenReturn(annotationMin);
    when(node.has("minimum")).thenReturn(true);
    when(fieldVar.type().boxify().fullName()).thenReturn(fieldClass.getTypeName());
    JFieldVar result = rule.apply("node", node, null, fieldVar, null);
    assertSame(fieldVar, result);
    verify(fieldVar, times(isApplicable ? 1 : 0)).annotate(decimalMinClass);
    verify(annotationMin, times(isApplicable ? 1 : 0)).param("value", minValue);
    verify(fieldVar, never()).annotate(decimalMaxClass);
    verify(annotationMax, never()).param(eq("value"), anyString());
}
Also used : Random(java.util.Random) JFieldVar(com.sun.codemodel.JFieldVar) Test(org.junit.Test)

Example 58 with JFieldVar

use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.

the class ParcelableHelper method addWriteToParcel.

public void addWriteToParcel(JDefinedClass jclass) {
    JMethod method = jclass.method(JMod.PUBLIC, void.class, "writeToParcel");
    JVar dest = method.param(jclass.owner().directClass("android.os.Parcel"), "dest");
    method.param(int.class, "flags");
    // Call super.writeToParcel
    if (extendsParcelable(jclass)) {
        method.body().directStatement("super.writeToParcel(dest, flags);");
    }
    for (JFieldVar f : jclass.fields().values()) {
        if ((f.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
            continue;
        }
        if (f.type().erasure().name().equals("List")) {
            method.body().invoke(dest, "writeList").arg(f);
        } else {
            method.body().invoke(dest, "writeValue").arg(f);
        }
    }
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 59 with JFieldVar

use of com.sun.codemodel.JFieldVar in project jsonschema2pojo by joelittlejohn.

the class ParcelableHelper method addConstructorFromParcel.

public void addConstructorFromParcel(JDefinedClass jclass) {
    JMethod ctorFromParcel = jclass.constructor(JMod.PROTECTED);
    JVar in = ctorFromParcel.param(jclass.owner().directClass("android.os.Parcel"), "in");
    suppressWarnings(ctorFromParcel, "unchecked");
    if (extendsParcelable(jclass)) {
        ctorFromParcel.body().directStatement("super(in);");
    }
    for (JFieldVar f : jclass.fields().values()) {
        if ((f.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
            continue;
        }
        if (f.type().erasure().name().equals("List")) {
            ctorFromParcel.body().invoke(in, "readList").arg(JExpr._this().ref(f)).arg(JExpr.direct(getListType(f.type()) + ".class.getClassLoader()"));
        } else {
            ctorFromParcel.body().assign(JExpr._this().ref(f), JExpr.cast(f.type(), in.invoke("readValue").arg(JExpr.direct(f.type().erasure().name() + ".class.getClassLoader()"))));
        }
    }
}
Also used : JFieldVar(com.sun.codemodel.JFieldVar) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 60 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<>();
    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<>(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<>();
    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)

Aggregations

JFieldVar (com.sun.codemodel.JFieldVar)74 JMethod (com.sun.codemodel.JMethod)33 JVar (com.sun.codemodel.JVar)28 JClass (com.sun.codemodel.JClass)24 Test (org.junit.Test)24 JBlock (com.sun.codemodel.JBlock)21 JInvocation (com.sun.codemodel.JInvocation)19 JDefinedClass (com.sun.codemodel.JDefinedClass)16 Random (java.util.Random)12 ByteString (com.linkedin.data.ByteString)10 HashMap (java.util.HashMap)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)9 JExpression (com.sun.codemodel.JExpression)9 JConditional (com.sun.codemodel.JConditional)8 JType (com.sun.codemodel.JType)8 ArrayList (java.util.ArrayList)7 Map (java.util.Map)6 JFieldRef (com.sun.codemodel.JFieldRef)5 JCodeModel (com.sun.codemodel.JCodeModel)3 JPackage (com.sun.codemodel.JPackage)3