Java Code Examples for com.squareup.javapoet.MethodSpec.Builder#nextControlFlow()
The following examples show how to use
com.squareup.javapoet.MethodSpec.Builder#nextControlFlow() .
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: 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 2
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 3
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 4
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 5
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 6
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(); }
Example 7
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 }