Refine search
private void addEquals(JDefinedClass jclass, JsonNode node) { Map<String, JFieldVar> fields = removeFieldsExcludedFromEqualsAndHashCode(jclass.fields(), node); JMethod equals = jclass.method(JMod.PUBLIC, boolean.class, "equals"); JVar otherObject = equals.param(Object.class, "other"); JBlock body = equals.body(); body._if(otherObject.eq(JExpr._this()))._then()._return(JExpr.TRUE); body._if(otherObject._instanceof(jclass).eq(JExpr.FALSE))._then()._return(JExpr.FALSE); JVar rhsVar = body.decl(jclass, "rhs").init(JExpr.cast(jclass, otherObject)); if (!jclass._extends().fullName().equals(Object.class.getName())) { result = result.cand(JExpr._super().invoke("equals").arg(rhsVar)); if ((fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) { continue; JFieldRef thisFieldRef = JExpr.refthis(fieldVar.name()); JFieldRef otherFieldRef = JExpr.ref(rhsVar, fieldVar.name()); JExpression fieldEquals; body._return(result); equals.annotate(Override.class);
private void addToString(JDefinedClass jclass) { Map<String, JFieldVar> fields = jclass.fields(); JMethod toString = jclass.method(JMod.PUBLIC, String.class, "toString"); Set<String> excludes = new HashSet<>(Arrays.asList(ruleFactory.getGenerationConfig().getToStringExcludes())); JBlock body = toString.body(); JClass stringBuilderClass = jclass.owner().ref(StringBuilder.class); JVar sb = body.decl(stringBuilderClass, "sb", JExpr._new(stringBuilderClass)); if (!jclass._extends().fullName().equals(Object.class.getName())) { JVar baseLength = body.decl(jclass.owner().INT, "baseLength", sb.invoke("length")); JVar superString = body.decl(jclass.owner().ref(String.class), "superString", JExpr._super().invoke("toString")); JBlock superToStringBlock = body._if(superString.ne(JExpr._null()))._then(); if (excludes.contains(fieldVar.name()) || (fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) { continue; body.add(sb.invoke("append").arg(fieldVar.name())); body.add(sb.invoke("append").arg(JExpr.lit('='))); body._return(sb.invoke("toString")); toString.annotate(Override.class);
String name = listGetterMethod.name(); int mods = JMod.PUBLIC | listGetterMethod.mods().getValue() & JMod.FINAL; JMethod fluentMethod = implClass.method(mods, implClass, FLUENT_SETTER_METHOD_PREFIX + name.substring(GETTER_METHOD_PREFIX_LEN)); if (fluentMethodInfo.isOverride()) fluentMethod.annotate(Override.class); JType returnJType = listGetterMethod.type(); JClass typeParam = typeParams.get(0); JClass narrowedCollectionJClass = implClass.owner().ref(Collection.class).narrow(typeParam); JVar jvarParam = fluentMethod.param(narrowedCollectionJClass, VALUES); JBlock body = fluentMethod.body(); JConditional cond = body._if( jvarParam.ne( JExpr._null())); JInvocation addInvocation = cond._then() .invoke( JExpr.invoke(listGetterMethod), "addAll"); addInvocation.arg(jvarParam); body._return( JExpr._this()); return;
private void addHashCode(JDefinedClass jclass, JsonNode node) { Map<String, JFieldVar> fields = removeFieldsExcludedFromEqualsAndHashCode(jclass.fields(), node); JMethod hashCode = jclass.method(JMod.PUBLIC, int.class, "hashCode"); JBlock body = hashCode.body(); JVar result = body.decl(jclass.owner().INT, "result", JExpr.lit(1)); if ((fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) { continue; JFieldRef fieldRef = JExpr.refthis(fieldVar.name()); if (fieldVar.type().isPrimitive()) { if ("long".equals(fieldVar.type().name())) { fieldHash = JExpr.cast(jclass.owner().INT, fieldRef.xor(fieldRef.shrz(JExpr.lit(32)))); body.assign(result, result.mul(JExpr.lit(31)).plus(fieldHash)); if (!jclass._extends().fullName().equals(Object.class.getName())) { body.assign(result, result.mul(JExpr.lit(31)).plus(JExpr._super().invoke("hashCode"))); body._return(result); hashCode.annotate(Override.class);
String name = setterMethod.name(); int mods = JMod.PUBLIC | setterMethod.mods().getValue() & JMod.FINAL; JMethod fluentMethod = implClass.method(mods, implClass, FLUENT_SETTER_METHOD_PREFIX + name.substring(SETTER_METHOD_PREFIX_LEN)); if (fluentMethodInfo.isOverride()) fluentMethod.annotate(Override.class); JVar[] jvars = setterMethod.listParams(); fluentMethod.param(jvar.mods().getValue(), jvar.type(), jvar.name()); JBlock jblock = fluentMethod.body(); JInvocation jinvocation = jblock.invoke(setterMethod); jblock._return(JExpr._this()); return;
private void addOverrideBuilder(JDefinedClass thisJDefinedClass, JMethod parentBuilder, JVar parentParam) { if (thisJDefinedClass.getMethod(parentBuilder.name(), new JType[] {parentParam.type()}) == null) { JMethod builder = thisJDefinedClass.method(parentBuilder.mods().getValue(), thisJDefinedClass, parentBuilder.name()); builder.annotate(Override.class); JVar param = builder.param(parentParam.type(), parentParam.name()); JBlock body = builder.body(); body.invoke(JExpr._super(), parentBuilder).arg(param); body._return(JExpr._this()); } } }
String name = listGetterMethod.name(); int mods = JMod.PUBLIC | listGetterMethod.mods().getValue() & JMod.FINAL; JMethod fluentMethod = implClass.method(mods, implClass, FLUENT_SETTER_METHOD_PREFIX + name.substring(GETTER_METHOD_PREFIX_LEN)); if (fluentMethodInfo.isOverride()) fluentMethod.annotate(Override.class); JType returnJType = listGetterMethod.type(); JVar jvarParam = fluentMethod.varParam(typeParam, VALUES); JBlock body = fluentMethod.body(); JConditional cond = body._if( jvarParam.ne( JExpr._null())); JForEach forEach = cond._then() .forEach( typeParam, VALUE, JExpr.ref(VALUES)); JInvocation addInvocation = forEach.body() .invoke( JExpr.invoke(listGetterMethod), "add"); addInvocation.arg( JExpr.ref(VALUE));
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()")) ) ); } } }
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 void replaceCollectionGetter(FieldOutline field, final JMethod getter) { JDefinedClass clazz = field.parent().implClass; // remove the old getter clazz.methods().remove(getter); // and create a new one JMethod newGetter = field.parent().implClass.method(getter.mods().getValue(), getter.type(), getter.name()); JBlock block = newGetter.body(); JVar ret = block.decl(getJavaType(field), "ret"); JCodeModel codeModel = field.parent().implClass.owner(); JVar param = generateMethodParameter(getter, field); JConditional conditional = block._if(param.eq(JExpr._null())); conditional._then().assign(ret, getEmptyCollectionExpression(codeModel, param)); conditional._else().assign(ret, getUnmodifiableWrappedExpression(codeModel, param)); block._return(ret); getter.javadoc().append("Returns unmodifiable collection."); }
JMethod method = dc.getMethod(getterName, new JType[0]); JType mtype = method.type(); String setterName = "set" + fo.getPropertyInfo().getName(true); method = dc.getMethod(setterName, new JType[] {mtype}); if (LOG.isLoggable(Level.FINE)) { LOG.fine("Updating setter: " + setterName); JDocComment doc = method.javadoc(); dc.methods().remove(method); int mods = method.mods().getValue(); mtype = mtype.unboxify(); method = dc.method(mods, method.type(), setterName); method.body().assign(fr, JExpr.ref("value")); method = dc.method(mods, method.type(), "unset" + fo.getPropertyInfo().getName(true)); method.body().assign(fr, JExpr._null()); method = dc.getMethod("isSet" + fo.getPropertyInfo().getName(true), new JType[0]);
+ fo.getPropertyInfo().getName(true); JMethod method = dc.getMethod(getterName, new JType[0]); JDocComment doc = method.javadoc(); int mods = method.mods().getValue(); JType mtype = method.type(); if (remapRet) { mtype = mtype.unboxify(); dc.methods().remove(method); method = dc.method(mods, mtype, getterName); method.javadoc().append(doc); if (dvExpr != null) { JExpression test = JOp.eq(JExpr._null(), fr); JConditional jc = method.body()._if(test); jc._then()._return(dvExpr); jc._else()._return(fr); } else { method.body()._return(fr);
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()); JFieldVar fieldVar = jclass.fields().get(fieldName); if ((fieldVar.mods().getValue() & JMod.PRIVATE) != JMod.PRIVATE) { processFieldVarForSerializableSupport(jclass.fields().get(fieldName), dataOutputStream);
public static JMethod recreateMethod(JMethod method, JDefinedClass definedClass, JType overrideReturnType) { Iterator<JMethod> methods = definedClass.methods().iterator(); while (methods.hasNext()) { if (method.equals(methods.next())) { methods.remove(); break; } } JMods mods = method.mods(); JType newReturnType = overrideReturnType != null ? overrideReturnType : method.type(); JMethod newMethod = definedClass.method(mods.getValue(), newReturnType, method.name()); JVar[] params = method.listParams(); if (params != null) { for (JVar param : params) { newMethod.param(param.type(), param.name()); } } return newMethod; }
private static void processMethodCollectionForSerializableSupport(Iterator<JMethod> methods, DataOutputStream dataOutputStream) throws IOException { TreeMap<String, JMethod> sortedMethods = new TreeMap<>(); while (methods.hasNext()) { JMethod method = methods.next(); //Collect non-private methods if ((method.mods().getValue() & JMod.PRIVATE) != JMod.PRIVATE) { sortedMethods.put(method.name(), method); } } for (JMethod method : sortedMethods.values()) { dataOutputStream.writeUTF(method.name()); dataOutputStream.writeInt(method.mods().getValue()); if (method.type() != null) { dataOutputStream.writeUTF(method.type().fullName()); } for (JVar param : method.params()) { dataOutputStream.writeUTF(param.type().fullName()); } } }
private static void processFieldVarForSerializableSupport(JFieldVar fieldVar, DataOutputStream dataOutputStream) throws IOException { dataOutputStream.writeUTF(fieldVar.name()); dataOutputStream.writeInt(fieldVar.mods().getValue()); JType type = fieldVar.type(); dataOutputStream.writeUTF(type.fullName()); }
private void removePrivateStaticCopyMethods(ClassOutline classOutline) { JDefinedClass impl = classOutline.implClass; Iterator<JMethod> methods = impl.methods().iterator(); while (methods.hasNext()) { JMethod method = methods.next(); if ((method.mods().getValue() & (JMod.PRIVATE | JMod.STATIC)) == 0) { continue; } if (method.name().startsWith("copy")) { methods.remove(); } } }
private Map<String, JFieldVar> getNonTransientAndNonStaticFields() { Map<String, JFieldVar> nonStaticNonTransientFields = new LinkedHashMap<>(); if (pojo instanceof JDefinedClass) { Map<String, JFieldVar> fields = ((JDefinedClass) pojo).fields(); Iterator<Map.Entry<String, JFieldVar>> iterator = fields.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, JFieldVar> pair = iterator.next(); // If a field is not static or transient if ((pair.getValue().mods().getValue() & (JMod.STATIC | JMod.TRANSIENT)) == 0) { nonStaticNonTransientFields.put(pair.getKey(), pair.getValue()); } } } return nonStaticNonTransientFields; }