Refine search
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())); JClass stringBuilderClass = jclass.owner().ref(StringBuilder.class); JVar sb = body.decl(stringBuilderClass, "sb", JExpr._new(stringBuilderClass)); body.add(sb .invoke("append").arg(jclass.dotclass().invoke("getName")) .invoke("append").arg(JExpr.lit('@')) .invoke("append").arg( jclass.owner().ref(Integer.class).staticInvoke("toHexString").arg( jclass.owner().ref(System.class).staticInvoke("identityHashCode").arg(JExpr._this()))) .invoke("append").arg(JExpr.lit('['))); 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")); JVar contentStart = superToStringBlock.decl(jclass.owner().INT, "contentStart", superString.invoke("indexOf").arg(JExpr.lit('['))); JVar contentEnd = superToStringBlock.decl(jclass.owner().INT, "contentEnd", superString.invoke("lastIndexOf").arg(JExpr.lit(']'))); JExpr.refthis(fieldVar.name()).eq(JExpr._null()), JExpr.lit("<null>"), jclass.owner().ref(Arrays.class).staticInvoke("toString") .arg(JExpr.refthis(fieldVar.name())) .invoke("replace").arg(JExpr.lit('[')).arg(JExpr.lit('{'))
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; }
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()); Iterator<JDefinedClass> classes = jclass.classes(); while (classes.hasNext()) { JDefinedClass nestedClass = classes.next(); sortedClasses.put(nestedClass.fullName(), nestedClass); TreeSet<String> fieldNames = new TreeSet<>(jclass.fields().keySet()); for (String fieldName : fieldNames) { JFieldVar fieldVar = jclass.fields().get(fieldName); processFieldVarForSerializableSupport(jclass.fields().get(fieldName), dataOutputStream); Iterator<JClass> interfaces = jclass._implements(); List<JClass> interfacesList = new ArrayList<>(); while (interfaces.hasNext()) { if (jclass._extends() != null) { dataOutputStream.writeUTF(jclass._extends().fullName()); processMethodCollectionForSerializableSupport(jclass.methods().iterator(), dataOutputStream); processMethodCollectionForSerializableSupport(jclass.constructors(), dataOutputStream);
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 JMethod addConstructorFromURI(final JDefinedClass builder, final String baseUriInputParam, final String newClientInputParam) { final JMethod constructor = builder.constructor(JMod.PUBLIC); constructor.param(JMod.FINAL, URI.class, baseUriInputParam); constructor .body() .invoke(THIS_KEYWORD) .arg(JExpr.invoke(JExpr.ref(baseUriInputParam), "toString")) .arg(builder.owner().ref(ClientBuilder.class).staticInvoke(newClientInputParam)) .arg(JExpr._new(builder.owner().ref(RequestParams.class))); // attach the Javadoc constructor.javadoc().append(JavaDocs.DOCS_CONSTRUCTOR_1.fillTemplate(builder.name())); return constructor; }
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(); JVar rhsVar = body.decl(jclass, "rhs").init(JExpr.cast(jclass, otherObject)); JExpression result = JExpr.lit(true); if (!jclass._extends().fullName().equals(Object.class.getName())) { result = result.cand(JExpr._super().invoke("equals").arg(rhsVar)); JClass doubleClass = jclass.owner().ref(Double.class); fieldEquals = doubleClass.staticInvoke("doubleToLongBits").arg(thisFieldRef).eq( doubleClass.staticInvoke("doubleToLongBits").arg(otherFieldRef)); } else if ("float".equals(fieldVar.type().name())) { JClass floatClass = jclass.owner().ref(Float.class); fieldEquals = floatClass.staticInvoke("floatToIntBits").arg(thisFieldRef).eq( floatClass.staticInvoke("floatToIntBits").arg(otherFieldRef)); fieldEquals = jclass.owner().ref(Arrays.class).staticInvoke("equals").arg(thisFieldRef).arg(otherFieldRef); } else { fieldEquals = thisFieldRef.eq(otherFieldRef).cor( body._return(result);
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; }
private JMethod addInternalGetMethodJava6(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(); JConditional propertyConditional = null; JsonNode node = property.getValue(); String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node); JType propertyType = jclass.fields().get(fieldName).type(); JExpression condition = lit(propertyName).invoke("equals").arg(nameParam); if (propertyConditional == null) { propertyConditional = body._if(condition); } else { propertyConditional = propertyConditional._elseif(condition); JMethod propertyGetter = jclass.getMethod(getGetterName(propertyName, propertyType, node), new JType[] {}); propertyConditional._then()._return(invoke(propertyGetter)); JClass extendsType = jclass._extends(); JBlock lastBlock = propertyConditional == null ? body : propertyConditional._else(); 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) }); lastBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(notFoundParam)); } else { lastBlock._return(notFoundParam);
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)); fieldHash = JExpr.cast(jclass.owner().INT, fieldRef.xor(fieldRef.shrz(JExpr.lit(32)))); } else if ("boolean".equals(fieldVar.type().name())) { fieldHash = JOp.cond(fieldRef, JExpr.lit(1), JExpr.lit(0)); fieldHash = fieldRef; } else if ("double".equals(fieldVar.type().name())) { JClass doubleClass = jclass.owner().ref(Double.class); JExpression longField = doubleClass.staticInvoke("doubleToLongBits").arg(fieldRef); fieldHash = JExpr.cast(jclass.owner().INT, longField.xor(longField.shrz(JExpr.lit(32)))); } else if ("float".equals(fieldVar.type().name())) { fieldHash = jclass.owner().ref(Float.class).staticInvoke("floatToIntBits").arg(fieldRef); } else { fieldHash = JExpr.cast(jclass.owner().INT, fieldRef); fieldHash = jclass.owner().ref(Arrays.class).staticInvoke("hashCode").arg(fieldRef); } else { fieldHash = JOp.cond(fieldRef.eq(JExpr._null()), JExpr.lit(0), fieldRef.invoke("hashCode")); if (!jclass._extends().fullName().equals(Object.class.getName())) { body.assign(result, result.mul(JExpr.lit(31)).plus(JExpr._super().invoke("hashCode")));
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 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); }
jClass._extends(ref.fragmentPagerAdapter()); if (hasLocationsField) { locationsField = jClass.field(JMod.PRIVATE, ref.string().array(), "locations"); JMethod constructor = jClass.constructor(JMod.PUBLIC); constructor.param(ref.fragmentManager(), "fm"); if (hasLocationsField) { constructor.param(ref.string().array(), "locations"); JBlock constructorBody = constructor.body(); constructorBody.directStatement("super(fm);"); constructorBody.assign(JExpr._this().ref("locations"), locationsField); JMethod getCountMethod = jClass.method(JMod.PUBLIC, jCodeModel.INT, "getCount"); JBlock getCountMethodBody = getCountMethod.body(); if (hasLocationsField) { JMethod getItemMethod = jClass.method(JMod.PUBLIC, ref.fragment(), "getItem"); getItemMethod.param(jCodeModel.INT, "position"); JBlock getItemMethodBody = getItemMethod.body(); JVar fragmentVar = getItemMethodBody.decl(ref.fragment(), "fragment", JExpr._new(ref.ref(sampleFragmentPackage))); JVar bundleVar = getItemMethodBody.decl(ref.bundle(), "bundle", JExpr._new(ref.bundle()));
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; }
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()); } } }
private JMethod generateLazyProxyInitGetter(final ClassOutline classOutline, final FieldOutline fieldOutline) { final JCodeModel m = classOutline.parent().getCodeModel(); final JDefinedClass definedClass = classOutline.implClass; final String fieldName = fieldOutline.getPropertyInfo().getName(false); final String getterName = "get" + fieldOutline.getPropertyInfo().getName(true); final JFieldVar collectionField = definedClass.fields().get(fieldName); final JClass elementType = ((JClass) collectionField.type()).getTypeParameters().get(0); final JClass proxyFieldType = m.ref(BoundList.class).narrow(elementType); final JFieldRef collectionFieldRef = JExpr._this().ref(collectionField); final JFieldRef proxyField = JExpr._this().ref(collectionField.name() + BoundPropertiesPlugin.PROXY_SUFFIX); final JMethod oldGetter = definedClass.getMethod(getterName, new JType[0]); definedClass.methods().remove(oldGetter); final JMethod newGetter = definedClass.method(JMod.PUBLIC, proxyFieldType, getterName); newGetter.body()._if(collectionFieldRef.eq(JExpr._null()))._then().assign(collectionFieldRef, JExpr._new(m.ref(ArrayList.class).narrow(elementType))); final JBlock ifProxyNull = newGetter.body()._if(proxyField.eq(JExpr._null()))._then(); ifProxyNull.assign(proxyField, JExpr._new(m.ref(BoundListProxy.class).narrow(elementType)).arg(collectionFieldRef)); newGetter.body()._return(proxyField); return newGetter; }
private void addListNavigationConfiguration(JBlock configureActionBarBody) { jClass._implements(ref.sNavigationListener()); JMethod onNavigationItemSelectedMethod = jClass.method(JMod.PUBLIC, jCodeModel.BOOLEAN, "onNavigationItemSelected"); JVar itemPositionParam = onNavigationItemSelectedMethod.param(jCodeModel.INT, "itemPosition"); onNavigationItemSelectedMethod.param(jCodeModel.LONG, "itemId"); onNavigationItemSelectedMethod.annotate(ref.override()); JBlock onNavigationItemSelectedBody = onNavigationItemSelectedMethod.body(); if (appDetails.isViewPager()) { onNavigationItemSelectedBody.invoke(pagerField, "setCurrentItem").arg(itemPositionParam); onNavigationItemSelectedBody._return(JExpr.TRUE); JInvocation getSupportActionbar = JExpr.invoke("getSupportActionBar"); JInvocation getContext = getSupportActionbar.invoke("getThemedContext"); JVar contextVar = configureActionBarBody.decl(ref.context(), "context", getContext); JExpression rLayoutSherlockSpinner = JExpr.direct("android.R.layout.simple_list_item_1"); arg(contextVar). // arg(rArrayLocations). // arg(rLayoutSherlockSpinner); configureActionBarBody.invoke(getSupportActionbar, "setListNavigationCallbacks").// arg(listVar).// arg(JExpr._this());
jClass._extends(ref.ref(appDetails.getRoboSherlockFragmentPackage())); } else if (appDetails.isRoboguice()) { jClass._extends(ref.roboFragment()); } else { jClass._extends(ref.fragment()); JFieldVar labelTextField = jClass.field(appDetails.isAndroidAnnotations() || appDetails.isRoboguice() ? JMod.NONE : JMod.PRIVATE, ref.textView(), "labelText"); jClass.annotate(ref.efragment()).param("value", ref.r().staticRef("layout").ref("fragment_sample")); JMethod onCreateViewMethod = jClass.method(JMod.NONE, jCodeModel.VOID, "afterViews"); onCreateViewMethod.annotate(ref.afterViews()); onCreateViewMethodBody = onCreateViewMethod.body(); JMethod onCreateViewMethod = jClass.method(JMod.PUBLIC, ref.view(), "onCreateView"); onCreateViewMethod.annotate(ref.override()); JVar inflaterParam = onCreateViewMethod.param(ref.layoutInflater(), "inflater"); JVar containerParam = onCreateViewMethod.param(ref.viewGroup(), "container"); JMethod onViewCreatedMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "onViewCreated"); onViewCreatedMethod.annotate(ref.override()); JVar viewParam = onViewCreatedMethod.param(ref.view(), "view"); JMethod onCreateViewMethod = jClass.method(JMod.PUBLIC, ref.view(), "onCreateView"); onCreateViewMethod.annotate(ref.override()); JVar inflaterParam = onCreateViewMethod.param(ref.layoutInflater(), "inflater");
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()")) ) ); } } }