Java Code Examples for com.squareup.javapoet.MethodSpec.Builder#addStatement()
The following examples show how to use
com.squareup.javapoet.MethodSpec.Builder#addStatement() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SensorAnnotationsFileBuilder.java From SensorAnnotations with Apache License 2.0 | 6 votes |
/** * Creates the implementation of {@code SensorEventListener#onAccuracyChanged(Sensor, int)} * which calls the annotated method on our target class. * * @param annotatedMethod Method annotated with {@link OnAccuracyChanged}. * @return {@link MethodSpec} of {@code SensorEventListener#onAccuracyChanged(Sensor, int)}. */ @NonNull private static MethodSpec createOnAccuracyChangedListenerMethod( @Nullable AnnotatedMethod annotatedMethod) { ParameterSpec sensorParameter = ParameterSpec.builder(SENSOR, "sensor").build(); ParameterSpec accuracyParameter = ParameterSpec.builder(TypeName.INT, "accuracy").build(); Builder methodBuilder = getBaseMethodBuilder("onAccuracyChanged").addParameter(sensorParameter) .addParameter(accuracyParameter); if (annotatedMethod != null) { ExecutableElement accuracyChangedExecutableElement = annotatedMethod.getExecutableElement(); methodBuilder.addStatement("target.$L($N, $N)", accuracyChangedExecutableElement.getSimpleName(), sensorParameter, accuracyParameter); } return methodBuilder.build(); }
Example 2
Source File: StringBindTransform.java From kripton with Apache License 2.0 | 6 votes |
@Override public void generateParseOnJackson(BindTypeContext context, Builder methodBuilder, String parserName, TypeName beanClass, String beanName, BindProperty property) { if (property.isNullable()) { methodBuilder.beginControlFlow("if ($L.currentToken()!=$T.VALUE_NULL)", parserName, JsonToken.class); } if (property.hasTypeAdapter()) { // there's an type adapter methodBuilder.addCode("// using type adapter $L\n", property.typeAdapter.adapterClazz); methodBuilder.addStatement(setter(beanClass, beanName, property, PRE_TYPE_ADAPTER_TO_JAVA+"$L.getText()"+POST_TYPE_ADAPTER), TypeAdapterUtils.class, typeName(property.typeAdapter.adapterClazz),parserName); } else { // there's no type adapter methodBuilder.addStatement(setter(beanClass, beanName, property, "$L.getText()"), parserName); } if (property.isNullable()) { methodBuilder.endControlFlow(); } }
Example 3
Source File: WrappedBindTransform.java From kripton with Apache License 2.0 | 6 votes |
@Override public void generateParseOnJackson(BindTypeContext context, Builder methodBuilder, String parserName, TypeName beanClass, String beanName, BindProperty property) { if (property.isNullable()) { methodBuilder.beginControlFlow("if ($L.currentToken()!=$T.VALUE_NULL)", parserName, JsonToken.class); } if (property.hasTypeAdapter()) { methodBuilder.addStatement(setter(beanClass, beanName, property, PRE_TYPE_ADAPTER_TO_JAVA + "$T.read($L.getText())" + POST_TYPE_ADAPTER), TypeAdapterUtils.class, TypeUtility.typeName(property.typeAdapter.adapterClazz), utilClazz, parserName); } else { methodBuilder.addStatement(setter(beanClass, beanName, property, "$T.read($L.getText())"), utilClazz, parserName); } if (property.isNullable()) { methodBuilder.endControlFlow(); } }
Example 4
Source File: AbstractNumberBindTransform.java From kripton with Apache License 2.0 | 6 votes |
@Override public void generateParseOnJackson(BindTypeContext context, Builder methodBuilder, String parserName, TypeName beanClass, String beanName, BindProperty property) { if (property.isNullable()) { methodBuilder.beginControlFlow("if ($L.currentToken()!=$T.VALUE_NULL)", parserName, JsonToken.class); } if (property.hasTypeAdapter()) { methodBuilder.addStatement(setter(beanClass, beanName, property, PRE_TYPE_ADAPTER_TO_JAVA + "$T.read($L.getText())" + POST_TYPE_ADAPTER), TypeAdapterUtils.class, TypeUtility.typeName(property.typeAdapter.adapterClazz), NUMBER_UTIL_CLAZZ, parserName); } else { methodBuilder.addStatement(setter(beanClass, beanName, property, "$T.read($L.getText())"), NUMBER_UTIL_CLAZZ, parserName); } if (property.isNullable()) { methodBuilder.endControlFlow(); } }
Example 5
Source File: ObjectBindTransform.java From kripton with Apache License 2.0 | 6 votes |
@Override public void generateParseOnJackson(BindTypeContext context, Builder methodBuilder, String parserName, TypeName beanClass, String beanName, BindProperty property) { // TODO QUA // TypeName typeName = resolveTypeName(property.getParent(), // property.getPropertyType().getTypeName()); TypeName typeName = property.getPropertyType().getTypeName(); String bindName = context.getBindMapperName(context, typeName); if (property.isNullable()) { methodBuilder.beginControlFlow("if ($L.currentToken()==$T.START_OBJECT)", parserName, JsonToken.class); } methodBuilder.addStatement(setter(beanClass, beanName, property, "$L.parseOnJackson(jacksonParser)"), bindName); if (property.isNullable()) { methodBuilder.endControlFlow(); } }
Example 6
Source File: WrappedPrefsTransform.java From kripton with Apache License 2.0 | 5 votes |
@Override public void generateWriteProperty(Builder methodBuilder, String editorName, TypeName beanClass, String beanName, PrefsProperty property) { if (nullable) { methodBuilder.beginControlFlow("if ($L!=null) ", getter(beanName, beanClass, property)); } methodBuilder.addStatement("$L.putString($S,$T.write($L))", editorName, property.getPreferenceKey(), utilClazz, getter(beanName, beanClass, property)); if (nullable) { methodBuilder.nextControlFlow("else"); methodBuilder.addStatement("$L.remove($S)", editorName, property.getName()); methodBuilder.endControlFlow(); } }
Example 7
Source File: DoublePrefsTransform.java From kripton with Apache License 2.0 | 5 votes |
@Override public void generateReadProperty(Builder methodBuilder, String preferenceName, TypeName beanClass, String beanName, PrefsProperty property, boolean readAll, ReadType readType) { if (readAll) { methodBuilder.beginControlFlow(""); } methodBuilder.addStatement("String temp=$L.getString($S, null)", preferenceName, property.getPreferenceKey()); if (readAll) { methodBuilder.addCode(setter(beanClass, beanName, property) + (!property.isPublicField() && beanName!=null ? "(" : "=")); } switch (readType) { case NONE: break; case RETURN: methodBuilder.addCode("return "); break; case VALUE: methodBuilder.addCode("$T _value=", property.getPropertyType().getTypeName()); break; } methodBuilder.addCode("($T.hasText(temp)) ? ", StringUtils.class); methodBuilder.addCode("$T.valueOf(temp)", Double.class); methodBuilder.addCode(": $L", getter(DEFAULT_BEAN_NAME, beanClass, property)); if (readAll) { methodBuilder.addCode(!property.isPublicField() && beanName!=null ? ")" : ""); } methodBuilder.addCode(";\n"); if (readAll) { methodBuilder.endControlFlow(); } }
Example 8
Source File: DoublePrefsTransform.java From kripton with Apache License 2.0 | 5 votes |
@Override public void generateWriteProperty(Builder methodBuilder, String editorName, TypeName beanClass, String beanName, PrefsProperty property) { if (nullable) { methodBuilder.beginControlFlow("if ($L!=null) ", getter(beanName, beanClass, property)); } methodBuilder.addStatement("$L.putString($S,String.valueOf($L))", editorName, property.getPreferenceKey(), getter(beanName, beanClass, property)); if (nullable) { methodBuilder.nextControlFlow("else"); methodBuilder.addStatement("$L.remove($S)", editorName, property.getName()); methodBuilder.endControlFlow(); } }
Example 9
Source File: EnumPrefsTransform.java From kripton with Apache License 2.0 | 5 votes |
@Override public void generateReadProperty(Builder methodBuilder, String preferenceName, TypeName beanClass, String beanName, PrefsProperty property, boolean readAll, ReadType readType) { if (readAll) { methodBuilder.beginControlFlow(""); } methodBuilder.addStatement("String temp=$L.getString($S, null)", preferenceName, property.getPreferenceKey()); if (readAll) { methodBuilder.addCode(setter(beanClass, beanName, property) + (!property.isPublicField() && beanName!=null ? "(" : "=")); } switch (readType) { case NONE: break; case RETURN: methodBuilder.addCode("return "); break; case VALUE: methodBuilder.addCode("$T _value=", property.getPropertyType().getTypeName()); break; } methodBuilder.addCode("($T.hasText(temp)) ? ", StringUtils.class); methodBuilder.addCode("$T.valueOf(temp)", typeName); methodBuilder.addCode(": $L", getter(DEFAULT_BEAN_NAME, beanClass, property)); if (readAll) { methodBuilder.addCode(!property.isPublicField() && beanName!=null ? ")" : ""); } methodBuilder.addCode(";\n"); if (readAll) { methodBuilder.endControlFlow(); } }
Example 10
Source File: EnumPrefsTransform.java From kripton with Apache License 2.0 | 5 votes |
@Override public void generateWriteProperty(Builder methodBuilder, String editorName, TypeName beanClass, String beanName, PrefsProperty property) { methodBuilder.beginControlFlow("if ($L!=null) ", getter(beanName, beanClass, property)); methodBuilder.addStatement("$L.putString($S,$L.toString() )", editorName, property.getPreferenceKey(), getter(beanName, beanClass, property)); methodBuilder.nextControlFlow("else"); methodBuilder.addStatement("$L.remove($S)", editorName, property.getName()); methodBuilder.endControlFlow(); }
Example 11
Source File: AbstractGeneratedPrefsTransform.java From kripton with Apache License 2.0 | 5 votes |
@Override public void generateReadProperty(Builder methodBuilder, String preferenceName, TypeName beanClass, String beanName, PrefsProperty property, boolean readAll, ReadType readType) { if (readAll) { methodBuilder.beginControlFlow(""); } methodBuilder.addStatement("String temp=$L.getString($S, null)", preferenceName, property.getPreferenceKey()); if (readAll) { methodBuilder.addCode(setter(beanClass, beanName, property) + (!property.isPublicField() && beanName!=null ? "(" : "=")); } switch (readType) { case NONE: break; case RETURN: methodBuilder.addCode("return "); break; case VALUE: methodBuilder.addCode("$T _value=", property.getPropertyType().getTypeName()); break; } methodBuilder.addCode("$T.hasText(temp) ? ", StringUtils.class); methodBuilder.addCode("parse$L(temp)", formatter.convert(property.getName())); methodBuilder.addCode(": $L", getter(DEFAULT_BEAN_NAME, beanClass, property)); if (readAll) { methodBuilder.addCode((!property.isPublicField() && beanName!=null ? ")" : "")); } methodBuilder.addCode(";\n"); if (readAll) { methodBuilder.endControlFlow(); } }
Example 12
Source File: AbstractGeneratedPrefsTransform.java From kripton with Apache License 2.0 | 5 votes |
@Override public void generateWriteProperty(Builder methodBuilder, String editorName, TypeName beanClass, String beanName, PrefsProperty property) { Converter<String, String> formatter = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_CAMEL); methodBuilder.beginControlFlow("if ($L!=null) ", getter(beanName, beanClass, property)); methodBuilder.addStatement("String temp=serialize$L($L)", formatter.convert(property.getName()), getter(beanName, beanClass, property)); methodBuilder.addStatement("$L.putString($S,temp)", editorName, property.getPreferenceKey()); methodBuilder.nextControlFlow(" else "); methodBuilder.addStatement("$L.remove($S)", editorName, property.getPreferenceKey()); methodBuilder.endControlFlow(); }
Example 13
Source File: AbstractNumberPrefsTransform.java From kripton with Apache License 2.0 | 5 votes |
@Override public void generateReadProperty(Builder methodBuilder, String preferenceName, TypeName beanClass, String beanName, PrefsProperty property, boolean readAll, ReadType readType) { if (readAll) { methodBuilder.beginControlFlow(""); } methodBuilder.addStatement("String temp=$L.getString($S, $S)", preferenceName, property.getPreferenceKey(), defaultValue); if (readAll) { methodBuilder.addCode(setter(beanClass, beanName, property) + (!property.isPublicField() && beanName!=null ? "(" : "=")); } switch (readType) { case NONE: break; case RETURN: methodBuilder.addCode("return "); break; case VALUE: methodBuilder.addCode("$T _value=", property.getPropertyType().getTypeName()); break; } methodBuilder.addCode("($T.hasText(temp)) ? ", StringUtils.class); methodBuilder.addCode("new $T(temp)", clazz); methodBuilder.addCode(": $L", getter(DEFAULT_BEAN_NAME, beanClass, property)); if (readAll) { methodBuilder.addCode(!property.isPublicField() && beanName!=null ? ")" : ""); } methodBuilder.addCode(";\n"); if (readAll) { methodBuilder.endControlFlow(); } }
Example 14
Source File: SetPrefsTransformation.java From kripton with Apache License 2.0 | 5 votes |
@Override public void generateWriteProperty(Builder methodBuilder, String editorName, TypeName beanClass, String beanName, PrefsProperty property) { Converter<String, String> formatter = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_CAMEL); String tempPre = ""; String tempPost = ""; if (property.hasTypeAdapter()) { // this comment is needed to include $T for PrefsTypeAdapterUtils methodBuilder.addComment("Use $T to convert objects", PrefsTypeAdapterUtils.class); tempPre = String.format("%s.getAdapter(%s.class).toData(", PrefsTypeAdapterUtils.class.getSimpleName(), TypeUtility.className(property.typeAdapter.adapterClazz).simpleName()); tempPost = ")"; } boolean isStringSet = isStringSet(property); if (!isStringSet) { methodBuilder.beginControlFlow("if ($L!=null) ", getter(beanName, beanClass, property)); methodBuilder.addStatement("String temp=serialize$L($L)", formatter.convert(property.getName()), getter(beanName, beanClass, property)); methodBuilder.addStatement("$L.putString($S,temp)", editorName, property.getPreferenceKey()); methodBuilder.nextControlFlow(" else "); methodBuilder.addStatement("$L.remove($S)", editorName, property.getPreferenceKey()); methodBuilder.endControlFlow(); } else { methodBuilder.addStatement("$L.putStringSet($S," + tempPre + "$L" + tempPost + ")", editorName, property.getPreferenceKey(), getter(beanName, beanClass, property)); } }
Example 15
Source File: EnumBindTransform.java From kripton with Apache License 2.0 | 5 votes |
@Override public void generateParseOnJackson(BindTypeContext context, Builder methodBuilder, String parserName, TypeName beanClass, String beanName, BindProperty property) { if (property.isNullable()) { methodBuilder.beginControlFlow("if ($L.currentToken()!=$T.VALUE_NULL)", parserName, JsonToken.class); } else { methodBuilder.beginControlFlow(""); } methodBuilder.addStatement("String tempEnum=$L.getText()", parserName); methodBuilder.addStatement(setter(beanClass, beanName, property, "$T.hasText(tempEnum)?$T.valueOf(tempEnum):null"), StringUtils.class, typeName); methodBuilder.endControlFlow(); }
Example 16
Source File: ByteArrayBindTransform.java From kripton with Apache License 2.0 | 5 votes |
/** * Generate parse on jackson internal. * * @param context the context * @param methodBuilder the method builder * @param parserName the parser name * @param beanClass the bean class * @param beanName the bean name * @param property the property * @param onString the on string */ public void generateParseOnJacksonInternal(BindTypeContext context, Builder methodBuilder, String parserName, TypeName beanClass, String beanName, BindProperty property, boolean onString) { if (property.isNullable()) { methodBuilder.beginControlFlow("if ($L.currentToken()!=$T.VALUE_NULL)", parserName, JsonToken.class); } if (property.hasTypeAdapter()) { if (onString) { methodBuilder.addStatement(setter(beanClass, beanName, property, PRE_TYPE_ADAPTER_TO_JAVA + "$T.decode($L.getValueAsString())" + POST_TYPE_ADAPTER), TypeAdapterUtils.class, TypeUtility.typeName(property.typeAdapter.adapterClazz), Base64Utils.class, parserName); } else { methodBuilder.addStatement(setter(beanClass, beanName, property, PRE_TYPE_ADAPTER_TO_JAVA + "$L.getBinaryValue()" + POST_TYPE_ADAPTER), TypeAdapterUtils.class, TypeUtility.typeName(property.typeAdapter.adapterClazz), parserName); } } else { if (onString) { methodBuilder.addStatement(setter(beanClass, beanName, property, "$T.decode($L.getValueAsString())"), Base64Utils.class, parserName); } else { methodBuilder.addStatement(setter(beanClass, beanName, property, "$L.getBinaryValue()"), parserName); } } if (property.isNullable()) { methodBuilder.endControlFlow(); } }
Example 17
Source File: WrappedPrefsTransform.java From kripton with Apache License 2.0 | 5 votes |
@Override public void generateReadProperty(Builder methodBuilder, String preferenceName, TypeName beanClass, String beanName, PrefsProperty property, boolean readAll, ReadType readType) { if (readAll) { methodBuilder.beginControlFlow(""); } methodBuilder.addStatement("String temp=$L.getString($S, null)", preferenceName, property.getPreferenceKey()); if (readAll) { methodBuilder.addCode(setter(beanClass, beanName, property) + (!property.isPublicField() && beanName!=null ? "(" : "=")); } switch (readType) { case NONE: break; case RETURN: methodBuilder.addCode("return "); break; case VALUE: methodBuilder.addCode("$T _value=",property.getPropertyType().getTypeName()); break; } methodBuilder.addCode("($T.hasText(temp)) ? ", StringUtils.class); methodBuilder.addCode("$T.read(temp)", utilClazz); methodBuilder.addCode(": $L", getter(DEFAULT_BEAN_NAME, beanClass, property)); if (readAll) { methodBuilder.addCode(!property.isPublicField() && beanName!=null ? ")" : ""); } methodBuilder.addCode(";"); if (readAll) { methodBuilder.endControlFlow(); } }
Example 18
Source File: SetPrefsTransformation.java From kripton with Apache License 2.0 | 4 votes |
@Override public void generateReadProperty(Builder methodBuilder, String preferenceName, TypeName beanClass, String beanName, PrefsProperty property, boolean readAll, ReadType readType) { boolean isStringSet = isStringSet(property); String tempPre = ""; String tempPost = ""; String tempPreDefaultValue = ""; String tempPostDefaultValue = ""; if (readAll) { methodBuilder.beginControlFlow(""); } if (property.hasTypeAdapter()) { // this comment is needed to include $T for PrefsTypeAdapterUtils methodBuilder.addComment("Use $T to convert objects", PrefsTypeAdapterUtils.class); tempPre = String.format("%s.getAdapter(%s.class).toJava(", PrefsTypeAdapterUtils.class.getSimpleName(), TypeUtility.className(property.typeAdapter.adapterClazz).simpleName()); tempPost = ")"; tempPreDefaultValue = String.format("%s.getAdapter(%s.class).toData(", PrefsTypeAdapterUtils.class.getSimpleName(), TypeUtility.className(property.typeAdapter.adapterClazz).simpleName()); tempPostDefaultValue = ")"; } if (isStringSet) { methodBuilder.addStatement("$T<String> temp=$L.getStringSet($S, "+tempPreDefaultValue+DEFAULT_BEAN_NAME+"."+getter(property)+tempPostDefaultValue+")", Set.class, preferenceName, property.getPreferenceKey()); if (readAll) { methodBuilder.addCode( setter(beanClass, beanName, property) + (!property.isPublicField() && beanName!=null ? "(" : "=")); } switch (readType) { case NONE: break; case RETURN: methodBuilder.addCode("return "); break; case VALUE: methodBuilder.addCode("$T _value=", property.getPropertyType().getTypeName()); break; } methodBuilder.addCode(tempPre); ParameterizedTypeName typeName; if (property.hasTypeAdapter()) { typeName = (ParameterizedTypeName) TypeUtility.typeName(property.typeAdapter.dataType); } else { typeName = (ParameterizedTypeName) TypeUtility.typeName(property.getElement().asType()); } if (TypeUtility.isEquals(typeName.rawType, Set.class.getCanonicalName())) { methodBuilder.addCode("temp"); } else { methodBuilder.addCode("new $T<String>(temp)", typeName.rawType); } methodBuilder.addCode(tempPost); if (readAll) { methodBuilder.addCode((!property.isPublicField() ? ")" : "")); } methodBuilder.addCode(";\n"); } else { methodBuilder.addStatement("String temp=$L.getString($S, null)", preferenceName, property.getPreferenceKey()); if (readAll) { methodBuilder.addCode( setter(beanClass, beanName, property) + (!property.isPublicField() ? "(" : "=") + ""); } switch (readType) { case NONE: break; case RETURN: methodBuilder.addCode("return "); break; case VALUE: methodBuilder.addCode("$T _value=", property.getPropertyType().getTypeName()); break; } methodBuilder.addCode("$T.hasText(temp) ? ", StringUtils.class); methodBuilder.addCode("parse$L(temp)", formatter.convert(property.getName())); methodBuilder.addCode(": $L",getter(DEFAULT_BEAN_NAME, beanClass, property)); if (readAll) { methodBuilder.addCode(!property.isPublicField() && beanName!=null ? ")" : ""); } methodBuilder.addCode(";\n"); } if (readAll) { methodBuilder.endControlFlow(); } }
Example 19
Source File: MapBindTransformation.java From kripton with Apache License 2.0 | 4 votes |
/** * Generate parse on jackson internal. * * @param context the context * @param methodBuilder the method builder * @param parserName the parser name * @param beanClass the bean class * @param beanName the bean name * @param property the property * @param onString the on string */ public void generateParseOnJacksonInternal(BindTypeContext context, Builder methodBuilder, String parserName, TypeName beanClass, String beanName, BindProperty property, boolean onString) { // define key and value type ParameterizedTypeName mapTypeName=(ParameterizedTypeName) property.getPropertyType().getTypeName(); TypeName keyTypeName = mapTypeName.typeArguments.get(0); TypeName valueTypeName = mapTypeName.typeArguments.get(1); //@formatter:off methodBuilder.beginControlFlow("if ($L.currentToken()==$T.START_ARRAY)", parserName, JsonToken.class); methodBuilder.addStatement("$T<$T, $T> collection=new $T<>()", defineMapClass(mapTypeName), keyTypeName, valueTypeName, defineMapClass(mapTypeName)); BindTransform transformKey=BindTransformer.lookup(keyTypeName); BindProperty elementKeyProperty=BindProperty.builder(keyTypeName, property).inCollection(false).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapKeyName).nullable(false).build(); BindTransform transformValue=BindTransformer.lookup(valueTypeName); BindProperty elementValueProperty=BindProperty.builder(valueTypeName, property).inCollection(false).xmlType(property.xmlInfo.mapEntryType.toXmlType()).elementName(property.mapValueName).nullable(true).build(); methodBuilder.addStatement("$T key=$L", elementKeyProperty.getPropertyType().getTypeName(), DEFAULT_VALUE); methodBuilder.addStatement("$T value=$L", elementValueProperty.getPropertyType().getTypeName(), DEFAULT_VALUE); if (onString) { methodBuilder.addStatement("$T current", JsonToken.class); methodBuilder.addStatement("String tempValue=null"); } methodBuilder.beginControlFlow("while ($L.nextToken() != $T.END_ARRAY)", parserName, JsonToken.class); if (onString) { // on string methodBuilder.addStatement("current=$L.currentToken()", parserName); methodBuilder.beginControlFlow("for (int i=0; i<2 ;i++)"); methodBuilder.beginControlFlow("while (current != $T.FIELD_NAME)",JsonToken.class); methodBuilder.addStatement("current=$L.nextToken()", parserName); methodBuilder.endControlFlow(); methodBuilder.addStatement("$L.nextValue()", parserName); // methodBuilder.addCode("switch(jacksonParser.getCurrentName()) {\n"); methodBuilder.addCode("case $S:$>\n", property.mapKeyName); transformKey.generateParseOnJacksonAsString(context, methodBuilder, parserName, null, "key", elementKeyProperty); methodBuilder.addStatement("$<break"); methodBuilder.addCode("case $S:$>\n", property.mapValueName); methodBuilder.addStatement("tempValue=$L.getValueAsString()", parserName); methodBuilder.beginControlFlow("if ($L.currentToken()==$T.VALUE_STRING && \"null\".equals(tempValue))", parserName, JsonToken.class); methodBuilder.addStatement("value=$L", DEFAULT_VALUE); methodBuilder.nextControlFlow("else"); transformValue.generateParseOnJacksonAsString(context, methodBuilder, parserName, null, "value", elementValueProperty); methodBuilder.endControlFlow(); methodBuilder.addStatement("$<break"); methodBuilder.addCode("}\n"); // methodBuilder.endControlFlow(); } else { // key methodBuilder.addStatement("$L.nextValue()", parserName); transformKey.generateParseOnJackson(context, methodBuilder, parserName, null, "key", elementKeyProperty); // value methodBuilder.addStatement("$L.nextValue()", parserName); transformValue.generateParseOnJackson(context, methodBuilder, parserName, null, "value", elementValueProperty); } methodBuilder.addStatement("collection.put(key, value)"); methodBuilder.addStatement("key=$L", DEFAULT_VALUE); methodBuilder.addStatement("value=$L" , DEFAULT_VALUE); methodBuilder.addStatement("$L.nextToken()", parserName); methodBuilder.endControlFlow(); methodBuilder.addStatement(setter(beanClass, beanName, property, "collection")); methodBuilder.endControlFlow(); //@formatter:on }
Example 20
Source File: SensorAnnotationsFileBuilder.java From SensorAnnotations with Apache License 2.0 | 4 votes |
/** * Create the bind method for our generated class. * * @param targetParameter The target class that has annotated methods. * @param annotatedMethodsPerClass The annotated methods that are in a given class. * @return {@link MethodSpec} of the generated class bind method. */ @NonNull private static MethodSpec createBindMethod(@NonNull ParameterSpec targetParameter, @NonNull AnnotatedMethodsPerClass annotatedMethodsPerClass) throws ProcessingException { Map<Integer, Map<Class, AnnotatedMethod>> itemsMap = annotatedMethodsPerClass.getItemsMap(); Builder bindMethodBuilder = getBaseMethodBuilder("bind").addParameter(targetParameter) .addStatement("int sensorType") .addStatement("$T sensor", SENSOR) .beginControlFlow("for ($T wrapper : $N)", LISTENER_WRAPPER, LISTENER_WRAPPERS_FIELD) .addStatement("sensorType = wrapper.getSensorType()") .addStatement("sensor = wrapper.getSensor($N)", SENSOR_MANAGER_FIELD); if (annotatedMethodsPerClass.hasAnnotationsOfType(OnSensorNotAvailable.class)) { bindMethodBuilder.beginControlFlow("if (sensor == null)"); // Iterate through our map of sensor types and check whether an OnSensorNotAvailable // annotation exists, if so and the sensor is unavailable call the method. List<Integer> sensorTypes = new ArrayList<>(); sensorTypes.addAll(itemsMap.keySet()); for (int i = 0; i < sensorTypes.size(); i++) { Integer sensorType = sensorTypes.get(i); Map<Class, AnnotatedMethod> annotationMap = itemsMap.get(sensorType); AnnotatedMethod annotatedMethod = annotationMap.get(OnSensorNotAvailable.class); if (annotatedMethod != null) { checkAnnotatedMethodForErrors(annotatedMethod.getExecutableElement(), OnSensorNotAvailable.class); if (i == 0) { bindMethodBuilder.beginControlFlow("if (sensorType == $L)", sensorType); } else { bindMethodBuilder.nextControlFlow("else if (sensorType == $L)", sensorType); } bindMethodBuilder.addStatement("$N.$L()", targetParameter, annotatedMethod.getExecutableElement().getSimpleName()); } } bindMethodBuilder.endControlFlow().addStatement("continue").endControlFlow(); } return bindMethodBuilder.addStatement("wrapper.registerListener($N)", SENSOR_MANAGER_FIELD) .endControlFlow() .build(); }