private void getIndex(ClassGenerator<StreamingAggregator> g) { switch (incoming.getSchema().getSelectionVectorMode()) { case FOUR_BYTE: { JVar var = g.declareClassField("sv4_", g.getModel()._ref(SelectionVector4.class)); g.getBlock("setupInterior") .assign(var, JExpr.direct("incoming").invoke("getSelectionVector4")); g.getBlock("getVectorIndex")._return(var.invoke("get").arg(JExpr.direct("recordIndex"))); ; return; } case NONE: { g.getBlock("getVectorIndex")._return(JExpr.direct("recordIndex")); ; return; } case TWO_BYTE: { JVar var = g.declareClassField("sv2_", g.getModel()._ref(SelectionVector2.class)); g.getBlock("setupInterior") .assign(var, JExpr.direct("incoming").invoke("getSelectionVector2")); g.getBlock("getVectorIndex") ._return(var.invoke("getIndex").arg(JExpr.direct("recordIndex"))); ; return; } default: throw new IllegalStateException(); } }
private void addCreateFromParcel(JDefinedClass jclass, JDefinedClass creatorClass) { JMethod createFromParcel = creatorClass.method(JMod.PUBLIC, jclass, "createFromParcel"); JVar in = createFromParcel.param(Parcel.class, "in"); JVar instance = createFromParcel.body().decl(jclass, "instance", JExpr._new(jclass)); suppressWarnings(createFromParcel, "unchecked"); for (JFieldVar f : jclass.fields().values()) { if ((f.mods().getValue() & JMod.STATIC) == JMod.STATIC) { continue; } if (f.type().erasure().name().equals("List")) { createFromParcel .body() .invoke(in, "readList") .arg(instance.ref(f)) .arg(JExpr.direct(getGenericType(f.type()) + ".class.getClassLoader()")); } else { createFromParcel .body() .assign( instance.ref(f), JExpr.cast( f.type(), in.invoke("readValue") .arg(JExpr.direct(f.type().erasure().name() + ".class.getClassLoader()")))); } } createFromParcel.body()._return(instance); }
private JDefinedClass addBuilderCls(Feed feed, JDefinedClass cls) throws JClassAlreadyExistsException, ClassNotFoundException { JDefinedClass bldrCls = cls._class(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, "Builder"); JVar paramBuilder = bldrCls .field(JMod.PRIVATE | JMod.FINAL, immutableMapBldr, "params") .init(immutableMap.staticInvoke("builder")); for (Filter filter : feed.getFilters().getFilter()) { if (!Boolean.TRUE.equals(filter.isDeprecated())) { addWithersFor(filter, bldrCls, paramBuilder); } } if (feed.getMixins() != null && feed.getMixins().getMixin() != null) { JDefinedClass mixinEnum = getMixinEnum(feed); for (Mixin mixin : feed.getMixins().getMixin()) { String mixinName = mixin.getName().toUpperCase().replace(' ', '_'); JEnumConstant mixinCnst = mixinEnum.enumConstant(mixinName); mixinCnst.arg(JExpr.lit(mixin.getName().replace(' ', '+'))); } JFieldVar field = cls.field(privateStaticFinal, String.class, "MIXIN"); field.init(JExpr.lit("mixin")); JMethod iterWither = bldrCls.method(JMod.PUBLIC, bldrCls, "withMixins"); JVar param = iterWither.param(iterable(mixinEnum), "mixins"); JBlock mthdBody = iterWither.body(); mthdBody.add( paramBuilder .invoke("put") .arg(field) .arg(immutableList.staticInvoke("copyOf").arg(param))); mthdBody._return(JExpr._this()); JMethod varArgWither = bldrCls.method(JMod.PUBLIC, bldrCls, "withMixins"); param = varArgWither.varParam(mixinEnum, "mixins"); varArgWither .body() ._return(JExpr.invoke(iterWither).arg(immutableList.staticInvoke("copyOf").arg(param))); } JMethod bldMthd = bldrCls.method(JMod.PUBLIC, cls, "build"); bldMthd.body()._return(JExpr._new(cls).arg(paramBuilder.invoke("build"))); // TODO: add sorts return bldrCls; }
private static void processToString(JDefinedClass packetClass, JCodeModel codeModel) { JClass string = codeModel.ref(String.class); JClass stringBuilder = codeModel.ref(StringBuilder.class); JClass arrays = codeModel.ref(Arrays.class); JMethod toStringMeth = packetClass.method(JMod.PUBLIC, String.class, "toString"); toStringMeth.annotate(Override.class); JBlock body = toStringMeth.body(); JVar stringBuilderVar = body.decl(stringBuilder, "sb"); stringBuilderVar = stringBuilderVar.init(JExpr._new(stringBuilder).arg(packetClass.name() + "[")); JInvocation appendChain = null; for (JFieldVar fieldVar : packetClass.fields().values()) { if (appendChain != null) { // a comma is needed appendChain = appendChain.invoke("append").arg("," + fieldVar.name() + "="); } else { appendChain = stringBuilderVar.invoke("append").arg(fieldVar.name() + "="); } // now add the field to the toString output JExpression expression = fieldVar.type().isArray() ? arrays.staticInvoke("toString").arg(JExpr._this().ref(fieldVar.name())) : fieldVar.type().isReference() ? JExpr._this().ref(fieldVar.name()).invoke("toString") : JExpr._this().ref(fieldVar.name()); appendChain = appendChain.invoke("append").arg(expression); } if (appendChain != null) { appendChain = appendChain.invoke("append").arg("]"); } else { appendChain = stringBuilderVar.invoke("append").arg("]"); } body.add(appendChain); body._return(stringBuilderVar.invoke("toString")); }
private JMethod addIterableWither( Filter filter, JDefinedClass bldrCls, JVar paramBuilder, JFieldVar field, JClass paramType) throws JClassAlreadyExistsException { JMethod method = createWitherMethod(filter, bldrCls); if (filter.getTitle() != null) { method.javadoc().add(String.format("<p>%s</p>", filter.getTitle())); } JVar param = addParam(filter, method, iterable(paramType)); JBlock mthdBody = method.body(); mthdBody.add( paramBuilder.invoke("put").arg(field).arg(immutableList.staticInvoke("copyOf").arg(param))); mthdBody._return(JExpr._this()); return method; }
private void genInitFields( Refs r, Map<Ref, JFieldVar> fieldVarMap, JVar viewVar, Collection<Ref> refs, JBlock body) { for (Ref ref : refs) { JFieldVar fieldVar = fieldVarMap.get(ref); JFieldRef idVar = (ref.isAndroidId ? r.androidRClass : r.rClass).staticRef("id").ref(ref.id); if (ref instanceof View) { JClass viewType = r.ref(((View) ref).type); body.assign(fieldVar, cast(viewType, viewVar.invoke("findViewById").arg(idVar))); } else if (ref instanceof Include) { JClass includeType = r.ref(getClassName(((Include) ref).layout)); body.assign(fieldVar, _new(includeType).arg(viewVar)); } } }
public void run(Set<ClassOutline> sorted, JDefinedClass transformer) { for (ClassOutline classOutline : sorted) { // skip over abstract classes if (!classOutline.target.isAbstract()) { // add the accept method to the bean JDefinedClass beanImpl = classOutline.implClass; JMethod acceptMethod = beanImpl.method(JMod.PUBLIC, Object.class, "accept"); JTypeVar genericType = acceptMethod.generify("T"); acceptMethod.type(genericType); JVar vizParam = acceptMethod.param(transformer.narrow(genericType), "aTransformer"); JBlock block = acceptMethod.body(); block._return(vizParam.invoke("transform").arg(JExpr._this())); } } }
private void addWither( Filter filter, JDefinedClass bldrCls, JVar paramBuilder, JFieldVar field, JClass paramType) throws JClassAlreadyExistsException { JMethod method = createWitherMethod(filter, bldrCls); if (filter.getTitle() != null) { method.javadoc().add(String.format("<p>%s</p>", filter.getTitle())); } JVar param = addParam(filter, method, paramType); JBlock mthdBody = method.body(); boolean needsNullCheck = true; if (filter.getMinValue() != null) { mthdBody.add(precs.staticInvoke("checkNotNull").arg(param)); needsNullCheck = false; int min = filter.getMinValue().intValue(); mthdBody.add( precs .staticInvoke("checkArgument") .arg(JExpr.lit(min).lte(param)) .arg(JExpr.lit(param.name() + ": %s < " + min)) .arg(param)); } if (filter.getMaxValue() != null) { if (needsNullCheck) { mthdBody.add(precs.staticInvoke("checkNotNull").arg(param)); needsNullCheck = false; } int max = filter.getMaxValue().intValue(); mthdBody.add( precs .staticInvoke("checkArgument") .arg(JExpr.lit(max).gte(param)) .arg(JExpr.lit(param.name() + ": %s > " + max)) .arg(param)); } JInvocation putIntoMap = paramBuilder.invoke("put").arg(field); if (needsNullCheck) { putIntoMap.arg(precs.staticInvoke("checkNotNull").arg(param)); } else { putIntoMap.arg(param); } mthdBody.add(putIntoMap); mthdBody._return(JExpr._this()); }
private void createResponseBuilderInResourceMethodReturnType( final JDefinedClass responseClass, final int statusCode, final Response response, final MimeType responseMimeType) throws Exception { final String responseBuilderMethodName = Names.buildResponseMethodName(statusCode, responseMimeType); final JMethod responseBuilderMethod = responseClass.method(PUBLIC + STATIC, responseClass, responseBuilderMethodName); final JDocComment javadoc = responseBuilderMethod.javadoc(); if (isNotBlank(response.getDescription())) { javadoc.add(response.getDescription()); } if ((responseMimeType != null) && (isNotBlank(responseMimeType.getExample()))) { javadoc.add(EXAMPLE_PREFIX + responseMimeType.getExample()); } JInvocation builderArgument = types .getGeneratorClass(javax.ws.rs.core.Response.class) .staticInvoke("status") .arg(JExpr.lit(statusCode)); if (responseMimeType != null) { builderArgument = builderArgument .invoke("header") .arg(HttpHeaders.CONTENT_TYPE) .arg(responseMimeType.getType()); } final StringBuilder freeFormHeadersDescription = new StringBuilder(); for (final Entry<String, Header> namedHeaderParameter : response.getHeaders().entrySet()) { final String headerName = namedHeaderParameter.getKey(); final Header header = namedHeaderParameter.getValue(); if (headerName.contains(RESPONSE_HEADER_WILDCARD_SYMBOL)) { appendParameterJavadocDescription(header, freeFormHeadersDescription); continue; } final String argumentName = Names.buildVariableName(headerName); builderArgument = builderArgument.invoke("header").arg(headerName).arg(JExpr.ref(argumentName)); addParameterJavaDoc(header, argumentName, javadoc); responseBuilderMethod.param(types.buildParameterType(header, argumentName), argumentName); } final JBlock responseBuilderMethodBody = responseBuilderMethod.body(); final JVar builderVariable = responseBuilderMethodBody.decl( types.getGeneratorType(ResponseBuilder.class), "responseBuilder", builderArgument); if (freeFormHeadersDescription.length() > 0) { // generate a Map<String, List<Object>> argument for {?} headers final JClass listOfObjectsClass = types.getGeneratorClass(List.class).narrow(Object.class); final JClass headersArgument = types .getGeneratorClass(Map.class) .narrow(types.getGeneratorClass(String.class), listOfObjectsClass); builderArgument = responseBuilderMethodBody .invoke("headers") .arg(JExpr.ref(MULTIPLE_RESPONSE_HEADERS_ARGUMENT_NAME)) .arg(builderVariable); final JVar param = responseBuilderMethod.param(headersArgument, MULTIPLE_RESPONSE_HEADERS_ARGUMENT_NAME); javadoc.addParam(param).add(freeFormHeadersDescription.toString()); } if (responseMimeType != null) { responseBuilderMethodBody .invoke(builderVariable, "entity") .arg(JExpr.ref(GENERIC_PAYLOAD_ARGUMENT_NAME)); responseBuilderMethod.param( types.getResponseEntityClass(responseMimeType), GENERIC_PAYLOAD_ARGUMENT_NAME); javadoc .addParam(GENERIC_PAYLOAD_ARGUMENT_NAME) .add(defaultString(responseMimeType.getExample())); } responseBuilderMethodBody._return( JExpr._new(responseClass).arg(builderVariable.invoke("build"))); }
public List<JVar> readIntent( List<? extends VariableElement> parameters, JBlock body, JVar intentParam, BaseAnnotationHandler handler) throws Exception { List<JVar> vars = new ArrayList<JVar>(); for (VariableElement variableElement : parameters) { String type = variableElement.asType().toString(); LOGGER.debug("var type:{} {}", variableElement.getSimpleName().toString(), type); JVar var = null; if (type.equals("java.lang.String")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getStringExtra") .arg(variableElement.getSimpleName().toString())); } else if (type.equals("int")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getIntExtra") .arg(variableElement.getSimpleName().toString()) .arg(JExpr.lit(0))); } else if (type.equals("float")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getFloatExtra") .arg(variableElement.getSimpleName().toString()) .arg(JExpr.lit(0))); } if (type.equals("double")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getDoubleExtra") .arg(variableElement.getSimpleName().toString()) .arg(JExpr.lit(0))); } if (type.equals("char")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getCharExtra") .arg(variableElement.getSimpleName().toString()) .arg(JExpr.lit((char) 0))); } else if (type.equals("java.util.ArrayList<java.lang.Integer>")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getIntegerArrayListExtra") .arg(variableElement.getSimpleName().toString())); } else if (type.equals("java.util.ArrayList<java.lang.String>")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getStringArrayListExtra") .arg(variableElement.getSimpleName().toString())); } else if (type.equals("float[]")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getFloatArrayExtra") .arg(variableElement.getSimpleName().toString())); } else if (type.equals("double[]")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getDoubleArrayExtra") .arg(variableElement.getSimpleName().toString())); } else if (type.equals("char[]")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getCharArrayExtra") .arg(variableElement.getSimpleName().toString())); } else if (type.equals("java.lang.String[]")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getStringArrayExtra") .arg(variableElement.getSimpleName().toString())); } else if (type.equals("boolean[]")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getBooleanArrayExtra") .arg(variableElement.getSimpleName().toString())); } else if (type.equals("boolean")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getBooleanExtra") .arg(variableElement.getSimpleName().toString()) .arg(JExpr.lit(false))); } else if (type.equals("int[]")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getIntArrayExtra") .arg(variableElement.getSimpleName().toString())); } else if (type.equals("long[]")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getLongArrayExtra") .arg(variableElement.getSimpleName().toString())); } else if (type.equals("long")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getLongExtra") .arg(variableElement.getSimpleName().toString()) .arg(JExpr.lit(0))); } else if (type.equals("byte[]")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getByteArrayExtra") .arg(variableElement.getSimpleName().toString())); } else if (type.equals("byte")) { var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), intentParam .invoke("getByteExtra") .arg(variableElement.getSimpleName().toString()) .arg(JExpr.lit(0))); } else if (type.endsWith("[]")) { if (EntityHandler.isInterface( type.substring(0, type.length() - 2), "android.os.Parcelable")) { var = body.decl( handler.refClass(type), variableElement.getSimpleName().toString(), JExpr.cast( handler.refClass(type), intentParam .invoke("getParcelableArrayExtra") .arg(variableElement.getSimpleName().toString()))); } else { var = body.decl( handler.refClass(type), variableElement.getSimpleName().toString(), JExpr.cast( handler.refClass(type), intentParam .invoke("getSerializableExtra") .arg(variableElement.getSimpleName().toString()))); } // JConditional // parcel=body._if(JExpr._new(handler.refClass(type.substring(0,type.length()-2)))._instanceof(handler.refClass("android.os.Parcelable"))); // parcel._then().assign(var,JExpr.cast(handler.refClass(type), // // intentParam.invoke("getParcelableArrayExtra").arg(variableElement.getSimpleName().toString()))); // JConditional serializable= // parcel._else()._if(JExpr._new(handler.refClass(type.substring(0,type.length()-2)))._instanceof(handler.refClass("java.io.Serializable"))); // serializable._then().assign(var, // JExpr.cast(handler.refClass(variableElement.asType().toString()), // // intentParam.invoke("getSerializableExtra").arg(variableElement.getSimpleName().toString()))); } else if (type.startsWith("java.util.ArrayList")) { String clz = type.substring(type.indexOf('<') + 1, type.lastIndexOf('>')); var = body.decl( handler.codeModel().parseType(variableElement.asType().toString()), variableElement.getSimpleName().toString(), JExpr.cast( handler.refClass("java.util.ArrayList").narrow(handler.refClass(clz)), intentParam .invoke("getSerializableExtra") .arg(variableElement.getSimpleName().toString()))); } if (var == null) { if (EntityHandler.isInterface(type, "android.os.Parcelable")) { var = body.decl( handler.refClass(type), variableElement.getSimpleName().toString(), JExpr.cast( handler.refClass(type), intentParam .invoke("getParcelableExtra") .arg(variableElement.getSimpleName().toString()))); } else { var = body.decl( handler.refClass(type), variableElement.getSimpleName().toString(), JExpr.cast( handler.refClass(type), intentParam .invoke("getSerializableExtra") .arg(variableElement.getSimpleName().toString()))); } // var=body.decl(handler.refClass(type),variableElement.getSimpleName().toString(),JExpr._new(handler.refClass(type))); // JConditional parcel=body._if(var._instanceof(handler.refClass("android.os.Parcelable"))); // parcel._then().assign(var,JExpr.cast(handler.refClass(variableElement.asType().toString()), // // intentParam.invoke("getParcelableExtra").arg(variableElement.getSimpleName().toString()))); // JConditional serializable= // parcel._else()._if(var._instanceof(handler.refClass("java.io.Serializable"))); // serializable._then().assign(var, // JExpr.cast(handler.refClass(variableElement.asType().toString()), // // intentParam.invoke("getSerializableExtra").arg(variableElement.getSimpleName().toString()))); } vars.add(var); } return vars; }