Java Code Examples for com.squareup.javapoet.TypeName#box()

The following examples show how to use com.squareup.javapoet.TypeName#box() . 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: BoxWhenNotRequired.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public TypeName typeName(ReferencePluginContext referencePluginContext, TypeDeclaration ramlType, TypeName currentSuggestion) {

    if (! ramlType.required()) {
        return currentSuggestion.box();
    } else {
        return currentSuggestion;
    }
}
 
Example 2
Source File: PsiTypeConverter.java    From json2java4idea with Apache License 2.0 5 votes vote down vote up
@Nonnull
@CheckReturnValue
static TypeName box(@Nonnull TypeName typeName) {
    try {
        return typeName.box();
    } catch (Exception e) {
        return typeName;
    }
}
 
Example 3
Source File: ControllerProcessor.java    From AndServer with Apache License 2.0 5 votes vote down vote up
private void assignmentBasicArrayParameter(CodeBlock.Builder builder, TypeName type, int index) {
    builder.beginControlFlow("try");
    TypeName component = ((ArrayTypeName) type).componentType;
    TypeName box = component.isBoxedPrimitive() ? component : component.box();
    builder.beginControlFlow("for(int i = 0; i < param$LList.size(); i++)", index)
        .addStatement("param$L[i] = $T.valueOf(param$LList.get(i))", index, box, index)
        .endControlFlow();
    builder.nextControlFlow("catch (Throwable e)").addStatement("throw new $T(e)", mParamError).endControlFlow();
}
 
Example 4
Source File: StateProperty.java    From reductor with Apache License 2.0 5 votes vote down vote up
public TypeName getReducerInterfaceTypeName() {
    TypeName stateType = TypeName.get(this.stateType);
    if (stateType.isPrimitive()) {
        stateType = stateType.box();
    }
    return ParameterizedTypeName.get(ClassName.get(Reducer.class), stateType);
}
 
Example 5
Source File: FitProcessor.java    From fit with Apache License 2.0 5 votes vote down vote up
/**
 * @param typeName {@link Type}
 * @return object is true,otherwise false
 * @since 1.0.1
 */
private boolean isObject(TypeName typeName) {
  typeName = typeName.box();
  return !(typeName.isBoxedPrimitive()
      || stringTypeName.equals(typeName)
      || setOfHoverboards.equals(typeName)
      || hashSetOfHoverboards.equals(typeName));
}
 
Example 6
Source File: FitProcessor.java    From fit with Apache License 2.0 5 votes vote down vote up
/**
 * @param typeName {@link Type}
 * @return object is true,otherwise false
 * @since 1.0.1
 */
private boolean isObject(TypeName typeName) {
  typeName = typeName.box();
  return !(typeName.isBoxedPrimitive()
      || stringTypeName.equals(typeName)
      || setOfHoverboards.equals(typeName)
      || hashSetOfHoverboards.equals(typeName));
}
 
Example 7
Source File: BoxType.java    From raml-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public TypeName typeName(ReferencePluginContext referencePluginContext, TypeDeclaration ramlType, TypeName currentSuggestion) {

    return currentSuggestion.box();
}
 
Example 8
Source File: ControllerProcessor.java    From AndServer with Apache License 2.0 4 votes vote down vote up
private void createBasicParameter(CodeBlock.Builder builder, TypeName type, String name, int index) {
    TypeName box = type.isBoxedPrimitive() ? type : type.box();
    builder.addStatement("$T $L$L = null", box, name, index);
}
 
Example 9
Source File: ControllerProcessor.java    From AndServer with Apache License 2.0 4 votes vote down vote up
private void assignmentBasicParameter(CodeBlock.Builder builder, TypeName type, String name, int index) {
    builder.beginControlFlow("try");
    TypeName box = type.isBoxedPrimitive() ? type : type.box();
    builder.addStatement("$L$L = $T.valueOf($L$LStr)", name, index, box, name, index);
    builder.nextControlFlow("catch (Throwable e)").addStatement("throw new $T(e)", mParamError).endControlFlow();
}
 
Example 10
Source File: WriterUtil.java    From sqlitemagic with Apache License 2.0 4 votes vote down vote up
public static TypeName typeNameForGenerics(@NonNull ExtendedTypeElement type) {
  final TypeName typeName = TypeName.get(type.getTypeMirror());
  return type.isPrimitiveElement() ? typeName.box() : typeName;
}
 
Example 11
Source File: BindSharedPreferencesBuilder.java    From kripton with Apache License 2.0 4 votes vote down vote up
private static void generateRxSupport(String sharedPreferenceName, String beanClassName, boolean generateRx, PrefsEntity entity) {
	if (!generateRx)
		return;
	Converter<String, String> converter = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_CAMEL);

	com.squareup.javapoet.MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("onSharedPreferenceChanged").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC)
			.addParameter(SharedPreferences.class, "sharedPreferences").addParameter(String.class, "key");

	methodBuilder.beginControlFlow("switch (key)");
	String subjectFieldName;
	PrefsTransform transform;
	TypeName typeName;
	for (PrefsProperty item : entity.getCollection()) {
		if (!item.isGenerateRx())
			continue;

		if (item.hasTypeAdapter()) {
			transform = PrefsTransformer.lookup(item.typeAdapter.getDataTypeTypename());
		} else {
			transform = PrefsTransformer.lookup(item);
		}

		typeName = item.getPropertyType().getTypeName();
		if (TypeUtility.isTypePrimitive(typeName)) {
			typeName = typeName.box();
		}

		// add subject fields
		subjectFieldName = item.getName() + "Subject";
		FieldSpec fs = FieldSpec.builder(ParameterizedTypeName.get(ClassName.get(Subject.class), typeName), subjectFieldName, Modifier.PRIVATE)
				.addJavadoc("subject for field $L - shared pref $L\n", item.getName(), item.getPreferenceKey()).initializer(CodeBlock.of("$T.create()", BehaviorSubject.class)).build();

		MethodSpec ms = MethodSpec.methodBuilder("get" + converter.convert(item.getName()) + "AsObservable").addModifiers(Modifier.PUBLIC)
				.returns(ParameterizedTypeName.get(ClassName.get(Subject.class), typeName)).addJavadoc("Obtains an observable to <code>$L</code> property\n\n", item.getName())
				.addJavadoc("@return\nan observable to <code>$L</code> property\n", item.getName()).addStatement("return $L", subjectFieldName).build();

		methodBuilder.addComment("$L - $L", item.getPreferenceKey(), item.getName());
		methodBuilder.addCode("case $S: {\n", item.getPreferenceKey());

		transform.generateReadProperty(methodBuilder, "sharedPreferences", typeName(item.getElement().asType()), "defaultBean", item, false, ReadType.VALUE);
		methodBuilder.addCode("\n");

		if (!TypeUtility.isTypePrimitive(item.getPropertyType().getTypeName())) {
			methodBuilder.beginControlFlow("if (_value!=null)");
		}
		methodBuilder.addStatement("$L.onNext(_value)", subjectFieldName);
		if (!TypeUtility.isTypePrimitive(item.getPropertyType().getTypeName())) {
			methodBuilder.endControlFlow();
		}
		methodBuilder.addStatement("return");
		methodBuilder.addCode("}\n", subjectFieldName);

		builder.addField(fs);
		builder.addMethod(ms);

	}

	methodBuilder.addStatement("default: return");
	methodBuilder.endControlFlow();

	TypeSpec innerBuilder = TypeSpec.anonymousClassBuilder("").addSuperinterface(OnSharedPreferenceChangeListener.class).addMethod(methodBuilder.build()).build();

	FieldSpec.Builder f = FieldSpec.builder(ClassName.get(OnSharedPreferenceChangeListener.class), "rxListener", Modifier.PRIVATE)
			.addJavadoc("Listener used to propagate shared prefs changes through RX\n").initializer("$L", innerBuilder);

	builder.addField(f.build());
}