codeBlockBuilder.nextControlFlow("catch($L ex)", ClassName.get(Throwable.class)); codeBlockBuilder.addStatement("throw new $L(ex)", ClassName.get(RuntimeException.class)); codeBlockBuilder.endControlFlow();
private MethodSpec geneOnNewIntentQueryMethod(TypeElement targetElement, List<Element> fieldElements) { MethodSpec.Builder injectMethodBuilder = MethodSpec.methodBuilder("onNewIntent") .addModifiers(PUBLIC) .addException(Throwable.class) .addAnnotation(AnnotationSpec.builder(Around.class).addMember("value", "$S", "execution(* " + targetElement.getQualifiedName() + ".onNewIntent(..))").build()) .addParameter(ProceedingJoinPoint.class, "joinPoint"); CodeBlock.Builder injectQueryCodeBuilder = geneOnNewIntentCodeBuilder(targetElement); List<String> queryKeys = new ArrayList<>(); for (Element queryElement : fieldElements) { if (queryElement.getAnnotation(Query.class) != null) { String queryKey = queryElement.getAnnotation(Query.class).value(); if (queryKeys.contains(queryKey)) { logger.error("The inject query key cannot be Duplicate" + Query.class.getCanonicalName(), queryElement); } queryKeys.add(queryKey); injectQueryCodeBuilder .beginControlFlow("try") .add("target.$L= $T.getValue(dataBundle,$S,$T.class);\n", queryElement, BUNDLE_COMPACT, queryKey, queryElement) .nextControlFlow("catch ($T e)", Exception.class) .addStatement("e.printStackTrace()") .endControlFlow(); } } injectQueryCodeBuilder.add("joinPoint.proceed();\n"); injectMethodBuilder.addCode(injectQueryCodeBuilder.build()); return injectMethodBuilder.build(); }
private MethodSpec geneOnCreateQueryMethod(TypeElement targetElement, List<Element> fieldElements) { MethodSpec.Builder injectMethodBuilder = MethodSpec.methodBuilder("onCreate") .addModifiers(PUBLIC) .addException(Throwable.class) .addAnnotation(AnnotationSpec.builder(Around.class).addMember("value", "$S", "execution(* " + targetElement.getQualifiedName() + ".onCreate(..))").build()) .addParameter(ProceedingJoinPoint.class, "joinPoint"); CodeBlock.Builder injectQueryCodeBuilder = geneOnCreateCodeBuilder(targetElement); List<String> queryKeys = new ArrayList<>(); for (Element queryElement : fieldElements) { if (queryElement.getAnnotation(Query.class) != null) { String queryKey = queryElement.getAnnotation(Query.class).value(); if (queryKeys.contains(queryKey)) { logger.error("The inject query key cannot be Duplicate" + Query.class.getCanonicalName(), queryElement); } queryKeys.add(queryKey); injectQueryCodeBuilder .beginControlFlow("try") .add("target.$L= $T.getValue(dataBundle,$S,$T.class);\n", queryElement, BUNDLE_COMPACT, queryKey, queryElement) .nextControlFlow("catch ($T e)", Exception.class) .addStatement("e.printStackTrace()") .endControlFlow(); } } injectQueryCodeBuilder.add("joinPoint.proceed();\n"); injectMethodBuilder.addCode(injectQueryCodeBuilder.build()); return injectMethodBuilder.build(); }
} else { String returnType = m.returnsBoolean() ? "false" : m.returnsPrimitive() ? "0" : "null"; code.nextControlFlow("else"); code.addStatement("return $L", returnType); code.endControlFlow();
.addStatement("return $N.validator(method.getGenericReturnType(), annotations)", inspector) .nextControlFlow("catch ($T e)", NoSuchMethodException.class) .addStatement("throw new RuntimeException(\"No method named \" + $N, e)", methodName) .endControlFlow()
private MethodSpec generateToMap(Collection<MapElement> valueMethods) { MethodSpec.Builder builder = MethodSpec.methodBuilder("toMap") .addModifiers(Modifier.PUBLIC) .returns(RETURN_MAP_TYPE); CodeBlock.Builder codeBuilder = CodeBlock.builder(); codeBuilder.addStatement("$T map = new $T()", PARAM_MAP_TYPE, ACTUAL_MAP_TYPE); for (MapElement element : valueMethods) { if (element.getPropertyType() == HIDDEN) { //do nothing } else if (element.getPropertyType() == ADAPTER) { codeBuilder.beginControlFlow("try"); codeBuilder.addStatement("$T adapter = ($T) $T.class.newInstance()", MapAdapter.class, MapAdapter.class, element.getAdapter()); codeBuilder.addStatement("map.put($S, ($T) adapter.toMap($S, $N()))", element.getMapItemName(), element.getAdapterType(), element.getMethodName(), element.getMethodName()); codeBuilder.nextControlFlow("catch (InstantiationException | IllegalAccessException e) "); codeBuilder.addStatement("throw new IllegalStateException(\"Cannot instantiate adapter for $L because \"+e.getMessage())", element.getMethodName()); codeBuilder.endControlFlow(); } else if (element.getPropertyType() == ENUM) { codeBuilder.beginControlFlow("if ($N() != null)", element.getMethodName()); codeBuilder.addStatement("map.put($S, $N().name())", element.getMapItemName(), element.getMethodName()); codeBuilder.endControlFlow(); } else { codeBuilder.addStatement("map.put($S, $N())", element.getMapItemName(), element.getMethodName()); } } codeBuilder.addStatement("return map"); builder.addCode(codeBuilder.build()); return builder.build(); }
private MethodSpec getTypeMethod(Data.MethodData m) { buffer.setLength(0); buffer.append(decoratorSimpleClassName); buffer.append("."); buffer.append(getInterfaceName(m)); String instigatorName = buffer.toString(); addToNonComposableList(instigatorName); // add this interface to our non-composable list buffer.append(".class"); String instigatorClass = buffer.toString(); String returnType = m.returnsPrimitive() ? "0" : "null"; CodeBlock.Builder code = CodeBlock.builder() .addStatement("$L deco = getInstigator($L)", instigatorName, instigatorClass) .beginControlFlow("if (deco != null)") .addStatement( "return deco.$L($L)", m._method.getSimpleName(), getCommaSeparatedParams(m, buffer)) .nextControlFlow("else"); if (m.returnsPrimitive()) { code.addStatement("throw new $T()", TypeName.get(DecoratorNotFoundException.class)); } else { code.addStatement("return $L", returnType); } code.endControlFlow(); return buildEmptyMethod(m, PUBLIC_FINAL, false).addCode(code.build()).build(); }
private MethodSpec getTypeMethod(Data.MethodData m) { CodeBlock.Builder code = CodeBlock.builder() .addStatement("$T obj = decorators.$L($L)", TypeName.get(m._method.getReturnType()), m._method.getSimpleName(), getCommaSeparatedParams(m, buffer)) .beginControlFlow("if (obj != null)") .addStatement("return obj") .nextControlFlow("else") .add("//region user inputed code\n") .add(MethodBodyReader.getMethodBody(processingEnv, m)) .add("//endregion\n") .endControlFlow(); return buildEmptyMethod(m, true) .addCode(code.build()) .build(); }
private MethodSpec getPrimitiveMethod(Data.MethodData m) { CodeBlock.Builder code = CodeBlock.builder() .beginControlFlow("try") .addStatement("return decorators.$L($L)", m._method.getSimpleName(), getCommaSeparatedParams(m, buffer)) .nextControlFlow("catch($T decoratorNotFoundException)", TypeName.get(DecoratorNotFoundException.class)) .add("//region user inputed code\n") .add(MethodBodyReader.getMethodBody(processingEnv, m)) .add("//endregion\n") .endControlFlow(); return buildEmptyMethod(m, true) .addCode(code.build()) .build(); }
public static CodeBlock writeValueWithTypeAdapter(FieldSpec adapter, AutoParcelProcessor.Property p, ParameterSpec out) { CodeBlock.Builder block = CodeBlock.builder(); if (p.isNullable()) { block.beginControlFlow("if ($N == null)", p.fieldName); block.addStatement("$N.writeInt(1)", out); block.nextControlFlow("else"); block.addStatement("$N.writeInt(0)", out); } block.addStatement("$N.toParcel($N, $N)", adapter, p.fieldName, out); if (p.isNullable()) { block.endControlFlow(); } return block.build(); }
public CodeBlock get() { OrmInject inject = pojoInfo.inject(); return CodeBlock.builder() .beginControlFlow("try ($T trx = $L.startTransaction())", Transaction.class, inject.name()) .add(selectExpression.get()) .add(whereExpression.get()) .add(orderByExpression.get()) .add(collectExpression.get()) .nextControlFlow("catch ($T e)", Exception.class) .addStatement("throw new $T(e)", SqlRuntimeException.class) .endControlFlow() .build(); }
/** * @param controlFlow the control flow construct and its code, such as "else if (foo == 10)". * Shouldn't contain braces or newline characters. */ public Builder nextControlFlow(String controlFlow, Object... args) { code.nextControlFlow(controlFlow, args); return this; }
void closeConstructorsAndInitializationBlocks() { if (staticBlockBuilder != null) { staticBlockBuilder.nextControlFlow("catch ($T e)", IllegalAccessException.class); staticBlockBuilder.addStatement("throw new $T(e)", AssertionError.class); staticBlockBuilder.endControlFlow(); typeBuilder.addStaticBlock(staticBlockBuilder.build()); } if (defaultConstructorBuilder != null) { typeBuilder.addMethod(defaultConstructorBuilder.build()); } }
private void addControlFlow(CodeBlock.Builder block, CodeBlock typeBlock, TypeName elementTypeName, int pos) { if (pos == 0) { block.beginControlFlow("if ($L.equals($T.class))", typeBlock, elementTypeName); } else { block.nextControlFlow("else if ($L.equals($T.class))", typeBlock, elementTypeName); } }
void closeConstructorsAndInitializationBlocks() { if (staticBlockBuilder != null) { staticBlockBuilder.nextControlFlow("catch ($T e)", IllegalAccessException.class); staticBlockBuilder.addStatement("throw new $T(e)", AssertionError.class); staticBlockBuilder.endControlFlow(); typeBuilder.addStaticBlock(staticBlockBuilder.build()); } if (defaultConstructorBuilder != null) { typeBuilder.addMethod(defaultConstructorBuilder.build()); } }
/** * @param controlFlow the control flow construct and its code, such as "else if (foo == 10)". * Shouldn't contain braces or newline characters. */ public Builder nextControlFlow(String controlFlow, Object... args) { code.nextControlFlow(controlFlow, args); return this; }
.nextControlFlow("else") .addStatement("this.$L.$L.addAll($L)", componentMemberInstanceName, propName, propName) .endControlFlow();
firstComparator, secondComparator)) .nextControlFlow("else if ($L != null)", secondComparator) .addStatement("return false") .endControlFlow();
.beginControlFlow("if (classLoader == null)") .add(directDelegation) .nextControlFlow("else") .addStatement("$T specClass", ClassNames.CLASS) .beginControlFlow("try") "specClass = classLoader.loadClass(\"$L\")", getTypeNameString(specModel.getSpecTypeName())) .nextControlFlow("catch (ClassNotFoundException _e)") .addStatement("throw new RuntimeException(_e)") .endControlFlow() .nextControlFlow("catch (Exception _e)") .addStatement("throw new RuntimeException(_e)") .endControlFlow()
/** * @param controlFlow the control flow construct and its code, such as "else if (foo == 10)". * Shouldn't contain braces or newline characters. */ public Builder nextControlFlow(String controlFlow, Object... args) { code.nextControlFlow(controlFlow, args); return this; }