@Override public void propertyField(JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) { JClass moshiAnnotation = clazz.owner().directClass("com.squareup.moshi.Json"); field.annotate(moshiAnnotation).param("name", propertyName); }
@Override public void enumConstant(JDefinedClass _enum, JEnumConstant constant, String value) { JClass moshiAnnotation = _enum.owner().directClass("com.squareup.moshi.Json"); constant.annotate(moshiAnnotation).param("name", value); }
private JFieldVar addAdditionalPropertiesField(JDefinedClass jclass, JType propertyType) { JClass propertiesMapType = jclass.owner().ref(Map.class); propertiesMapType = propertiesMapType.narrow(jclass.owner().ref(String.class), propertyType.boxify()); JClass propertiesMapImplType = jclass.owner().ref(HashMap.class); propertiesMapImplType = propertiesMapImplType.narrow(jclass.owner().ref(String.class), propertyType.boxify()); JFieldVar field = jclass.field(JMod.PRIVATE, propertiesMapType, "additionalProperties"); ruleFactory.getAnnotator().additionalPropertiesField(field, jclass, "additionalProperties"); field.init(JExpr._new(propertiesMapImplType)); return field; }
JFieldRef getOrAddNotFoundVar(JDefinedClass jclass) { jclass.field(PROTECTED | STATIC | FINAL, Object.class, NOT_FOUND_VALUE_FIELD, _new(jclass.owner()._ref(Object.class))); return jclass.staticRef(NOT_FOUND_VALUE_FIELD); }
public void addCreator(JDefinedClass jclass) { JClass creatorType = jclass.owner().directClass("android.os.Parcelable.Creator").narrow(jclass); JDefinedClass creatorClass = jclass.owner().anonymousClass(creatorType); addCreateFromParcel(jclass, creatorClass); addNewArray(jclass, creatorClass); JFieldVar creatorField = jclass.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, creatorType, "CREATOR"); creatorField.init(JExpr._new(creatorClass)); }
private JMethod addInternalGetMethodJava7(JDefinedClass jclass, JsonNode propertiesNode) { JMethod method = jclass.method(PROTECTED, jclass.owner()._ref(Object.class), DEFINED_GETTER_NAME); JVar nameParam = method.param(String.class, "name"); JVar notFoundParam = method.param(jclass.owner()._ref(Object.class), "notFoundValue"); 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(); addGetPropertyCase(jclass, propertySwitch, propertyName, propertyType, node); } } JClass extendsType = jclass._extends(); if (extendsType != null && extendsType instanceof JDefinedClass) { JDefinedClass parentClass = (JDefinedClass) extendsType; JMethod parentMethod = parentClass.getMethod(DEFINED_GETTER_NAME, new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) }); propertySwitch._default().body() ._return(_super().invoke(parentMethod).arg(nameParam).arg(notFoundParam)); } else { propertySwitch._default().body() ._return(notFoundParam); } return method; }
private JInvocation illegalArgumentInvocation(JDefinedClass jclass, JVar propertyName) { return _new(jclass.owner()._ref(IllegalArgumentException.class)) .arg(lit("property \"").plus(propertyName).plus(lit("\" is not defined"))); }
private JInvocation illegalArgumentInvocation(JDefinedClass jclass, String propertyName, JType propertyType, JVar valueVar) { return _new(jclass.owner()._ref(IllegalArgumentException.class)) .arg(lit("property \"" + propertyName + "\" is of type \"" + propertyType.fullName() + "\", but got ") .plus(valueVar.invoke("getClass").invoke("toString"))); }
private JType getReturnType(final JDefinedClass c, final JFieldVar field, final boolean required, final boolean usesOptional) { JType returnType = field.type(); if (ruleFactory.getGenerationConfig().isUseOptionalForGetters() || usesOptional) { if (!required && field.type().isReference()) { returnType = c.owner().ref("java.util.Optional").narrow(field.type()); } } return returnType; }
private void addCreateFromParcel(JDefinedClass jclass, JDefinedClass creatorClass) { JMethod createFromParcel = creatorClass.method(JMod.PUBLIC, jclass, "createFromParcel"); JVar in = createFromParcel.param(jclass.owner().directClass("android.os.Parcel"), "in"); suppressWarnings(createFromParcel, "unchecked"); createFromParcel.body()._return(JExpr._new(jclass).arg(in)); }
private void addParcelSupport(JDefinedClass jclass) { jclass._implements(jclass.owner().directClass("android.os.Parcelable")); parcelableHelper.addWriteToParcel(jclass); parcelableHelper.addDescribeContents(jclass); parcelableHelper.addCreator(jclass); parcelableHelper.addConstructorFromParcel(jclass); // #742 : includeConstructors will include the default constructor if (!ruleFactory.getGenerationConfig().isIncludeConstructors()) { // Add empty constructor jclass.constructor(JMod.PUBLIC); } }
private JFieldVar addQuickLookupMap(JDefinedClass _enum, JType backingType) { JClass lookupType = _enum.owner().ref(Map.class).narrow(backingType.boxify(), _enum); JFieldVar lookupMap = _enum.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, lookupType, "CONSTANTS"); JClass lookupImplType = _enum.owner().ref(HashMap.class).narrow(backingType.boxify(), _enum); lookupMap.init(JExpr._new(lookupImplType)); JForEach forEach = _enum.init().forEach(_enum, "c", JExpr.invoke("values")); JInvocation put = forEach.body().invoke(lookupMap, "put"); put.arg(forEach.var().ref("value")); put.arg(forEach.var()); return lookupMap; }
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); } } }
private JMethod addGetter(JDefinedClass c, JFieldVar field, String jsonPropertyName, JsonNode node, boolean isRequired, boolean usesOptional) { JType type = getReturnType(c, field, isRequired, usesOptional); JMethod getter = c.method(JMod.PUBLIC, type, getGetterName(jsonPropertyName, field.type(), node)); JBlock body = getter.body(); if ((ruleFactory.getGenerationConfig().isUseOptionalForGetters() || usesOptional) && !isRequired && field.type().isReference()) { body._return(c.owner().ref("java.util.Optional").staticInvoke("ofNullable").arg(field)); } else { body._return(field); } return getter; }
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; }
private JMethod addPublicSetMethod(JDefinedClass jclass, JMethod internalSetMethod) { JMethod method = jclass.method(PUBLIC, jclass.owner().VOID, SETTER_NAME); JVar nameParam = method.param(String.class, "name"); JVar valueParam = method.param(Object.class, "value"); JBlock body = method.body(); JBlock notFound = body._if(JOp.not(invoke(internalSetMethod).arg(nameParam).arg(valueParam)))._then(); // if we have additional properties, then put value. JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {}); if (getAdditionalProperties != null) { JType additionalPropertiesType = ((JClass) (getAdditionalProperties.type())).getTypeParameters().get(1); notFound.add(invoke(getAdditionalProperties).invoke("put").arg(nameParam) .arg(cast(additionalPropertiesType, valueParam))); } // else throw exception. else { notFound._throw(illegalArgumentInvocation(jclass, nameParam)); } return method; }
public void addConstructorFromParcel(JDefinedClass jclass) { JMethod ctorFromParcel = jclass.constructor(JMod.PROTECTED); JVar in = ctorFromParcel.param(jclass.owner().directClass("android.os.Parcel"), "in"); 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()")) ) ); } } }
private void addFactoryMethod(JDefinedClass _enum, JType backingType) { JFieldVar quickLookupMap = addQuickLookupMap(_enum, backingType); JMethod fromValue = _enum.method(JMod.PUBLIC | JMod.STATIC, _enum, "fromValue"); JVar valueParam = fromValue.param(backingType, "value"); JBlock body = fromValue.body(); JVar constant = body.decl(_enum, "constant"); constant.init(quickLookupMap.invoke("get").arg(valueParam)); JConditional _if = body._if(constant.eq(JExpr._null())); JInvocation illegalArgumentException = JExpr._new(_enum.owner().ref(IllegalArgumentException.class)); JExpression expr = valueParam; // if string no need to add "" if(!isString(backingType)){ expr = expr.plus(JExpr.lit("")); } illegalArgumentException.arg(expr); _if._then()._throw(illegalArgumentException); _if._else()._return(constant); ruleFactory.getAnnotator().enumCreatorMethod(_enum, fromValue); }