Java Code Examples for com.squareup.javapoet.MethodSpec.Builder#addComment()

The following examples show how to use com.squareup.javapoet.MethodSpec.Builder#addComment() . 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: SetPrefsTransformation.java    From kripton with Apache License 2.0 5 votes vote down vote up
@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 2
Source File: SelectPaginatedResultHelper.java    From kripton with Apache License 2.0 5 votes vote down vote up
private void generateTotalCountUsage(SQLiteModelMethod method, Builder methodBuilder, String paginatedResultName, String selectTotalCountMethodName) {
	methodBuilder.addComment("total count - BEGIN");
	methodBuilder.addCode(paginatedResultName + ".setTotalElements(this." + selectTotalCountMethodName + "(");

	method.getParameters().forEach(p -> {
		methodBuilder.addCode(p.value0 + ", ");
	});
	methodBuilder.addCode(paginatedResultName + "));\n");

	methodBuilder.addComment("total count - END");

}
 
Example 3
Source File: ImmutableUtility.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param entity
 * @param methodBuilder
 * @param entityName
 */
public static void generateImmutableVariableCopyFromEntity(ModelClass<?> entity, Builder methodBuilder,
		String entityName) {
	methodBuilder.addComment(
			"immutable object: initialize temporary variables for properties with entity propertiy values");
	for (Pair<String, TypeName> property : entity.getImmutableConstructors()) {
		ModelProperty item = entity.findImmutablePropertyByName(property.value0);
		AssertKripton.assertTrue(item != null, "Can not found property '%s' (type %s) in class '%s'",
				property.value0, property.value1, entity.getName());

		methodBuilder.addCode("$L$L=$L.$L;\n", IMMUTABLE_PREFIX, property.value0, entityName,
				PropertyUtility.getter(item));
	}
}
 
Example 4
Source File: ImmutableUtility.java    From kripton with Apache License 2.0 5 votes vote down vote up
private static void generateImmutableVariableInternal(ModelClass<?> entity, Builder methodBuilder,
		boolean declare) {
	methodBuilder.addComment("immutable object: initialize temporary variables for properties");
	for (Pair<String, TypeName> property : entity.getImmutableConstructors()) {
		if (declare) {
			methodBuilder.addCode("$T ", property.value1);
		}
		methodBuilder.addCode("$L$L=$L;\n", IMMUTABLE_PREFIX, property.value0,
				TypeUtility.getDefaultValue(property.value1));
	}
}
 
Example 5
Source File: ImmutableUtility.java    From kripton with Apache License 2.0 5 votes vote down vote up
public static void generateImmutableEntityCreation(ModelClass<?> entity, Builder methodBuilder, String instanceName,
		boolean createInstance) {
	String separator = "";
	methodBuilder.addComment("immutable object: inizialize object");
	if (createInstance) {
		methodBuilder.addCode("$T ", entity.getElement());
	}

	methodBuilder.addCode("$L=new $T(", instanceName, entity.getElement());
	for (Pair<String, TypeName> property : entity.getImmutableConstructors()) {
		if (TypeUtility.isList(property.value1)
				&& ((ParameterizedTypeName) property.value1).rawType.equals(ClassName.get(List.class))) {
			methodBuilder.addCode(separator + "($L==null ? null : $T.unmodifiableList($L))",
					IMMUTABLE_PREFIX + property.value0, Collections.class, IMMUTABLE_PREFIX + property.value0);
		} else if (TypeUtility.isSet(property.value1)
				&& ((ParameterizedTypeName) property.value1).rawType.equals(ClassName.get(SortedSet.class))) {
			methodBuilder.addCode(separator + "($L==null ? null : $T.unmodifiableSortedSet($L))",
					IMMUTABLE_PREFIX + property.value0, Collections.class, IMMUTABLE_PREFIX + property.value0);
		} else if (TypeUtility.isSet(property.value1)
				&& ((ParameterizedTypeName) property.value1).rawType.equals(ClassName.get(Set.class))) {
			methodBuilder.addCode(separator + "($L==null ? null : $T.unmodifiableSet($L))",
					IMMUTABLE_PREFIX + property.value0, Collections.class, IMMUTABLE_PREFIX + property.value0);
		} else if (TypeUtility.isMap(property.value1)
				&& ((ParameterizedTypeName) property.value1).rawType.equals(ClassName.get(SortedMap.class))) {
			methodBuilder.addCode(separator + "($L==null ? null : $T.unmodifiableSortedMap($L))",
					IMMUTABLE_PREFIX + property.value0, Collections.class, IMMUTABLE_PREFIX + property.value0);
		} else if (TypeUtility.isMap(property.value1)
				&& ((ParameterizedTypeName) property.value1).rawType.equals(ClassName.get(Map.class))) {
			methodBuilder.addCode(separator + "($L==null ? null : $T.unmodifiableMap($L))",
					IMMUTABLE_PREFIX + property.value0, Collections.class, IMMUTABLE_PREFIX + property.value0);
		} else {
			methodBuilder.addCode(separator + IMMUTABLE_PREFIX + property.value0);
		}
		separator = ",";
	}
	methodBuilder.addCode(");\n");

}
 
Example 6
Source File: SetPrefsTransformation.java    From kripton with Apache License 2.0 4 votes vote down vote up
@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();
	}
}