private void addActionInOnHandleIntent( EIntentServiceHolder holder, ExecutableElement executableElement, String methodName, JFieldVar actionKeyField) { // If action match, call the method JInvocation actionCondition = actionKeyField.invoke("equals").arg(holder.getOnHandleIntentIntentAction()); JBlock callActionBlock = holder.getOnHandleIntentBody()._if(actionCondition)._then(); JInvocation callActionInvocation = JExpr._super().invoke(methodName); // For each method params, we get back value from extras and put it // in super calls List<? extends VariableElement> methodParameters = executableElement.getParameters(); if (methodParameters.size() > 0) { // Extras JVar extras = callActionBlock.decl(classes().BUNDLE, "extras"); extras.init(holder.getOnHandleIntentIntent().invoke("getExtras")); JBlock extrasNotNullBlock = callActionBlock._if(extras.ne(_null()))._then(); // Extras params for (VariableElement param : methodParameters) { String paramName = param.getSimpleName().toString(); String extraParamName = paramName + "Extra"; JFieldVar paramVar = getStaticExtraField(holder, paramName); JClass extraParamClass = codeModelHelper.typeMirrorToJClass(param.asType(), holder); BundleHelper bundleHelper = new BundleHelper(annotationHelper, param); JExpression getExtraExpression = JExpr.invoke(extras, bundleHelper.getMethodNameToRestore()).arg(paramVar); if (bundleHelper.restoreCallNeedCastStatement()) { getExtraExpression = JExpr.cast(extraParamClass, getExtraExpression); if (bundleHelper.restoreCallNeedsSuppressWarning()) { JMethod onHandleIntentMethod = holder.getOnHandleIntentMethod(); if (onHandleIntentMethod.annotations().size() == 0) { onHandleIntentMethod.annotate(SuppressWarnings.class).param("value", "unchecked"); } } } JVar extraField = extrasNotNullBlock.decl(extraParamClass, extraParamName, getExtraExpression); callActionInvocation.arg(extraField); } extrasNotNullBlock.add(callActionInvocation); } else { callActionBlock.add(callActionInvocation); } callActionBlock._return(); }
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 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 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")); }
@Override public void buildLayoutCall(JDefinedClass definedClass, JBlock block) { try { Map<InjectionNode, TypedExpression> expressionMap = injectionFragmentGenerator.buildFragment(block, definedClass, layoutHandlerInjectionNode); // LayoutHandlerDelegate.invokeLayout() JExpression layoutHandlerDelegate = expressionMap.get(layoutHandlerInjectionNode).getExpression(); block.add(layoutHandlerDelegate.invoke(LayoutHandlerDelegate.INVOKE_LAYOUT_METHOD)); } catch (ClassNotFoundException e) { logger.error("ClassNotFoundException while trying to generate LayoutHandler", e); } catch (JClassAlreadyExistsException e) { logger.error("JClassAlreadyExistsException while trying to generate LayoutHandler", e); } }
private void buildMethodInterceptor( JDefinedClass definedClass, ConstructorInjectionPoint proxyConstructorInjectionPoint, JMethod constructor, JBlock constructorBody, Map<ASTMethod, Map<InjectionNode, JFieldVar>> interceptorFields, Map.Entry<ASTMethod, Set<InjectionNode>> methodInterceptorEntry) throws ClassNotFoundException { ASTMethod method = methodInterceptorEntry.getKey(); if (method.getAccessModifier().equals(ASTAccessModifier.PRIVATE)) { throw new TransfuseAnalysisException("Unable to provide AOP on private methods"); } if (!interceptorFields.containsKey(methodInterceptorEntry.getKey())) { interceptorFields.put( methodInterceptorEntry.getKey(), new HashMap<InjectionNode, JFieldVar>()); } Map<InjectionNode, JFieldVar> injectionNodeInstanceNameMap = interceptorFields.get(methodInterceptorEntry.getKey()); // setup interceptor fields for (InjectionNode interceptorInjectionNode : methodInterceptorEntry.getValue()) { String interceptorInstanceName = namer.generateName(interceptorInjectionNode); JFieldVar interceptorField = definedClass.field( JMod.PRIVATE, codeModel.ref(interceptorInjectionNode.getClassName()), interceptorInstanceName); injectionNodeInstanceNameMap.put(interceptorInjectionNode, interceptorField); JVar interceptorParam = constructor.param( codeModel.ref(interceptorInjectionNode.getClassName()), namer.generateName(interceptorInjectionNode)); constructorBody.assign(interceptorField, interceptorParam); proxyConstructorInjectionPoint.addInjectionNode(interceptorInjectionNode); } JType returnType = codeModel.parseType(method.getReturnType().getName()); JMethod methodDeclaration = definedClass.method( method.getAccessModifier().getCodeModelJMod(), returnType, method.getName()); JBlock body = methodDeclaration.body(); // define method parameter Map<ASTParameter, JVar> parameterMap = new HashMap<ASTParameter, JVar>(); for (ASTParameter parameter : method.getParameters()) { parameterMap.put( parameter, methodDeclaration.param( JMod.FINAL, codeModel.ref(parameter.getASTType().getName()), namer.generateName(parameter.getASTType()))); } // aop interceptor Map<InjectionNode, JFieldVar> interceptorNameMap = interceptorFields.get(methodInterceptorEntry.getKey()); JArray paramArray = JExpr.newArray(codeModel.ref(Object.class)); for (ASTParameter astParameter : method.getParameters()) { paramArray.add(parameterMap.get(astParameter)); } JInvocation interceptorInvocation = buildInterceptorChain( definedClass, method, parameterMap, methodInterceptorEntry.getValue(), interceptorNameMap) .invoke("invoke"); interceptorInvocation.arg(paramArray); if (method.getReturnType().equals(ASTVoidType.VOID)) { body.add(interceptorInvocation); } else { body._return(JExpr.cast(returnType.boxify(), interceptorInvocation)); } }