Search in sources :

Example 6 with JMethod

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

the class DynamicPropertiesRule method addGetMethods.

void addGetMethods(JDefinedClass jclass) {
    JFieldRef notFoundVar = getOrAddNotFoundVar(jclass);
    JMethod internalGetMethod = this.getInternalGetMethod(jclass);
    addPublicGetMethod(jclass, internalGetMethod, notFoundVar);
}
Also used : JFieldRef(com.sun.codemodel.JFieldRef) JMethod(com.sun.codemodel.JMethod)

Example 7 with JMethod

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

the class DynamicPropertiesRule method addSetMethods.

private void addSetMethods(JDefinedClass jclass) {
    JMethod internalSetMethod = getInternalSetMethod(jclass);
    addPublicSetMethod(jclass, internalSetMethod);
}
Also used : JMethod(com.sun.codemodel.JMethod)

Example 8 with JMethod

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

the class DynamicPropertiesRule method addGetPropertyCase.

private void addGetPropertyCase(JDefinedClass jclass, JSwitch propertySwitch, String propertyName, JType propertyType, JsonNode node) {
    JMethod propertyGetter = jclass.getMethod(getGetterName(propertyName, propertyType, node), new JType[] {});
    propertySwitch._case(lit(propertyName)).body()._return(invoke(propertyGetter));
}
Also used : JMethod(com.sun.codemodel.JMethod)

Example 9 with JMethod

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

the class DynamicPropertiesRule method addPublicGetMethod.

private JMethod addPublicGetMethod(JDefinedClass jclass, JMethod internalGetMethod, JFieldRef notFoundValue) {
    JMethod method = jclass.method(PUBLIC, jclass.owner()._ref(Object.class), GETTER_NAME);
    JTypeVar returnType = method.generify("T");
    method.type(returnType);
    Models.suppressWarnings(method, "unchecked");
    JVar nameParam = method.param(String.class, "name");
    JBlock body = method.body();
    JVar valueVar = body.decl(jclass.owner()._ref(Object.class), "value", invoke(internalGetMethod).arg(nameParam).arg(notFoundValue));
    JConditional found = method.body()._if(notFoundValue.ne(valueVar));
    found._then()._return(cast(returnType, valueVar));
    JBlock notFound = found._else();
    JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
    if (getAdditionalProperties != null) {
        notFound._return(cast(returnType, invoke(getAdditionalProperties).invoke("get").arg(nameParam)));
    } else {
        notFound._throw(illegalArgumentInvocation(jclass, nameParam));
    }
    return method;
}
Also used : JTypeVar(com.sun.codemodel.JTypeVar) JBlock(com.sun.codemodel.JBlock) JConditional(com.sun.codemodel.JConditional) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 10 with JMethod

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

the class DynamicPropertiesRule method addInternalSetMethodJava7.

private JMethod addInternalSetMethodJava7(JDefinedClass jclass, JsonNode propertiesNode) {
    JMethod method = jclass.method(PROTECTED, jclass.owner().BOOLEAN, DEFINED_SETTER_NAME);
    JVar nameParam = method.param(String.class, "name");
    JVar valueParam = method.param(Object.class, "value");
    JBlock body = method.body();
    JSwitch propertySwitch = body._switch(nameParam);
    if (propertiesNode != null) {
        for (Iterator<Map.Entry<String, JsonNode>> properties = propertiesNode.fields(); properties.hasNext(); ) {
            Map.Entry<String, JsonNode> property = properties.next();
            String propertyName = property.getKey();
            JsonNode node = property.getValue();
            String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node);
            JType propertyType = jclass.fields().get(fieldName).type();
            addSetPropertyCase(jclass, propertySwitch, propertyName, propertyType, valueParam, node);
        }
    }
    JBlock defaultBlock = propertySwitch._default().body();
    JClass extendsType = jclass._extends();
    if (extendsType != null && extendsType instanceof JDefinedClass) {
        JDefinedClass parentClass = (JDefinedClass) extendsType;
        JMethod parentMethod = parentClass.getMethod(DEFINED_SETTER_NAME, new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
        defaultBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(valueParam));
    } else {
        defaultBlock._return(FALSE);
    }
    return method;
}
Also used : JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) JsonNode(com.fasterxml.jackson.databind.JsonNode) JSwitch(com.sun.codemodel.JSwitch) JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod) Map(java.util.Map) JType(com.sun.codemodel.JType) JVar(com.sun.codemodel.JVar)

Aggregations

JMethod (com.sun.codemodel.JMethod)62 JVar (com.sun.codemodel.JVar)40 JBlock (com.sun.codemodel.JBlock)26 JClass (com.sun.codemodel.JClass)21 JDefinedClass (com.sun.codemodel.JDefinedClass)17 JInvocation (com.sun.codemodel.JInvocation)15 JFieldVar (com.sun.codemodel.JFieldVar)11 JType (com.sun.codemodel.JType)10 JExpression (com.sun.codemodel.JExpression)9 Map (java.util.Map)7 JConditional (com.sun.codemodel.JConditional)6 HashMap (java.util.HashMap)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ArrayList (java.util.ArrayList)4 ByteString (com.linkedin.data.ByteString)2 DataList (com.linkedin.data.DataList)2 DataMap (com.linkedin.data.DataMap)2 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)2 DataSchema (com.linkedin.data.schema.DataSchema)2 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)2