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

The following examples show how to use com.squareup.javapoet.TypeName#isBoxedPrimitive() . 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: 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 2
Source File: FitProcessor.java    From fit with Apache License 2.0 5 votes vote down vote up
private MethodSpec.Builder genSaveCode(MethodSpec.Builder builder, TypeName typeName,
    String putMethod, String valueL, String propertyName) {
  TypeName unboxFieldTypeName = unbox(typeName);
  if (TypeName.DOUBLE.equals(typeName)) {
    valueL = "Double.doubleToLongBits( obj." + valueL + ")";
    builder.addStatement("editor.$L($S, " + valueL + ")", putMethod, propertyName);
    return builder;
  } else if (setOfHoverboards.equals(unboxFieldTypeName) || hashSetOfHoverboards.equals(
      unboxFieldTypeName)) {
    builder.addStatement("$T.$L($L, $S, obj." + valueL + ")", UTILS, putMethod, "editor",
        propertyName);
    return builder;
  }

  if (typeName.isBoxedPrimitive()) {
    if (TypeName.DOUBLE.equals(unboxFieldTypeName)) {
      builder.addStatement(
          "editor.$L($S, Double.doubleToLongBits($T.checkNonNull(obj.$L) ?  obj.$L : 0))",
          putMethod, propertyName, UTILS, propertyName, propertyName);
    } else if (TypeName.CHAR.equals(unboxFieldTypeName) || TypeName.BYTE.equals(
        unboxFieldTypeName) || TypeName.SHORT.equals(unboxFieldTypeName) || TypeName.INT.equals(
        unboxFieldTypeName) || TypeName.LONG.equals(unboxFieldTypeName) || TypeName.FLOAT.equals(
        unboxFieldTypeName)) {
      builder.addStatement(
          "editor.$L($S, $T.checkNonNull( obj." + valueL + ") ? obj." + valueL + " : 0)",
          putMethod, propertyName, UTILS);
    } else if (TypeName.BOOLEAN.equals(unboxFieldTypeName)) {
      builder.addStatement("editor.$L($S, $T.checkNonNull(obj.$L) ? obj.$L : false)", putMethod,
          propertyName, UTILS, propertyName, propertyName);
    }
  } else {
    if (stringTypeName.equals(unboxFieldTypeName) || typeName.isPrimitive()) {
      builder.addStatement("editor.$L($S, obj." + valueL + ")", putMethod, propertyName);
    } else {
      builder.addStatement("$T.writeObject(context, name + $S, obj.$L)", FILE_OBJECT_UTIL,
          "." + propertyName, valueL);
    }
  }
  return builder;
}
 
Example 3
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 4
Source File: FitProcessor.java    From fit with Apache License 2.0 5 votes vote down vote up
private MethodSpec.Builder genSaveCode(MethodSpec.Builder builder, TypeName typeName,
    String putMethod, String valueL, String propertyName) {
  TypeName unboxFieldTypeName = unbox(typeName);
  if (TypeName.DOUBLE.equals(typeName)) {
    valueL = "Double.doubleToLongBits( obj." + valueL + ")";
    builder.addStatement("editor.$L($S, " + valueL + ")", putMethod, propertyName);
    return builder;
  } else if (setOfHoverboards.equals(unboxFieldTypeName) || hashSetOfHoverboards.equals(
      unboxFieldTypeName)) {
    builder.addStatement("$T.$L($L, $S, obj." + valueL + ")", UTILS, putMethod, "editor",
        propertyName);
    return builder;
  }

  if (typeName.isBoxedPrimitive()) {
    if (TypeName.DOUBLE.equals(unboxFieldTypeName)) {
      builder.addStatement(
          "editor.$L($S, Double.doubleToLongBits($T.checkNonNull(obj.$L) ?  obj.$L : 0))",
          putMethod, propertyName, UTILS, propertyName, propertyName);
    } else if (TypeName.CHAR.equals(unboxFieldTypeName) || TypeName.BYTE.equals(
        unboxFieldTypeName) || TypeName.SHORT.equals(unboxFieldTypeName) || TypeName.INT.equals(
        unboxFieldTypeName) || TypeName.LONG.equals(unboxFieldTypeName) || TypeName.FLOAT.equals(
        unboxFieldTypeName)) {
      builder.addStatement(
          "editor.$L($S, $T.checkNonNull( obj." + valueL + ") ? obj." + valueL + " : 0)",
          putMethod, propertyName, UTILS);
    } else if (TypeName.BOOLEAN.equals(unboxFieldTypeName)) {
      builder.addStatement("editor.$L($S, $T.checkNonNull(obj.$L) ? obj.$L : false)", putMethod,
          propertyName, UTILS, propertyName, propertyName);
    }
  } else {
    if (stringTypeName.equals(unboxFieldTypeName) || typeName.isPrimitive()) {
      builder.addStatement("editor.$L($S, obj." + valueL + ")", putMethod, propertyName);
    } else {
      builder.addStatement("$T.writeObject(context, name + $S, obj.$L)", FILE_OBJECT_UTIL,
          "." + propertyName, valueL);
    }
  }
  return builder;
}
 
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: AbstractConfigElement.java    From aircon with MIT License 4 votes vote down vote up
protected static TypeName unbox(TypeName typeName) {
	return typeName.isBoxedPrimitive() ? typeName.unbox() : typeName;
}
 
Example 7
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 8
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 9
Source File: Parcelables.java    From auto-parcel with Apache License 2.0 4 votes vote down vote up
public static boolean isValidType(TypeName typeName) {
    return typeName.isPrimitive() || typeName.isBoxedPrimitive() || VALID_TYPES.contains(typeName);
}
 
Example 10
Source File: FitProcessor.java    From fit with Apache License 2.0 4 votes vote down vote up
private TypeName unbox(TypeName typeName) {
  if (typeName.isBoxedPrimitive()) {
    return typeName.unbox();
  }
  return typeName;
}
 
Example 11
Source File: FitProcessor.java    From fit with Apache License 2.0 4 votes vote down vote up
private TypeName unbox(TypeName typeName) {
  if (typeName.isBoxedPrimitive()) {
    return typeName.unbox();
  }
  return typeName;
}
 
Example 12
Source File: BindingFieldHelper.java    From AutoBundle with Apache License 2.0 4 votes vote down vote up
static String getOperationName(TypeName target, Elements elements, Types types) {
    if (fieldTypes.containsKey(target)) {
        return fieldTypes.get(target);
    }
    if (target.isBoxedPrimitive()) {
        TypeName unboxed = target.unbox();
        if (fieldTypes.containsKey(unboxed)) {
            return fieldTypes.get(unboxed);
        }
    }

    // Array
    TypeMirror parcelable = elements.getTypeElement("android.os.Parcelable").asType();
    if (target.toString().endsWith("[]")) {
        String removed = target.toString().substring(0, target.toString().length() - 2);
        for (TypeName arrayType : fieldArrayTypes.keySet()) {
            if (removed.equals(arrayType.toString())) {
                return fieldArrayTypes.get(arrayType);
            }
        }
        // Parcelable[]
        TypeElement element = elements.getTypeElement(removed);
        if (element != null && types.isAssignable(element.asType(), parcelable)) {
            return "ParcelableArray";
        }
    }

    String[] splits = detectTypeArgument(target.toString());
    TypeMirror targetType;
    if (splits.length == 1) {
        targetType = elements.getTypeElement(target.toString()).asType();
    } else {
        TypeElement genericType = elements.getTypeElement(splits[0]);
        TypeMirror argType = elements.getTypeElement(splits[1]).asType();
        targetType = types.getDeclaredType(genericType, argType);
    }

    // Parcelable
    if (types.isAssignable(targetType, parcelable)) {
        return "Parcelable";
    }

    // ArrayList<? extend Parcelable>
    TypeElement arrayList = elements.getTypeElement(ArrayList.class.getName());
    TypeMirror wildCardParcelable = types.getWildcardType(parcelable, null);
    TypeMirror parcelableArrayList = types.getDeclaredType(arrayList, wildCardParcelable);
    if (types.isAssignable(targetType, parcelableArrayList)) {
        return "ParcelableArrayList";
    }

    // SparseArray<? extend Parcelable>
    TypeElement sparseArray = elements.getTypeElement("android.util.SparseArray");
    TypeMirror sparceParcelableArray = types.getDeclaredType(sparseArray, wildCardParcelable);
    if (types.isAssignable(targetType, sparceParcelableArray)) {
        return "SparseParcelableArray";
    }

    // IBinder
    TypeMirror iBinder = elements.getTypeElement("android.os.IBinder").asType();
    if (types.isAssignable(targetType, iBinder)) {
        return "Binder";
    }

    // Serializable
    TypeMirror serializable = elements.getTypeElement(Serializable.class.getName()).asType();
    if (types.isAssignable(targetType, serializable)) {
        return "Serializable";
    }

    return null;
}
 
Example 13
Source File: TypeVariableResolver.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * Resolve.
 *
 * @param inputType the input type
 * @return the type name
 */
public TypeName resolve(TypeName inputType) {
	if (!hasTypeVariables())
		return inputType;
	
	if (inputType.isPrimitive() || inputType.isBoxedPrimitive() || inputType.equals(TypeName.get(String.class))) {
		return inputType;
	}
	
	if (TypeName.VOID.equals(inputType)) return inputType;
	
	TypeName resolved=null;
	if (inputType instanceof ParameterizedTypeName)
	{
		ParameterizedTypeName inputParametrizedTypeName=(ParameterizedTypeName)inputType;
		
		TypeName[] typeArguments=new TypeName[inputParametrizedTypeName.typeArguments.size()];
		int i=0;
		for (TypeName item: inputParametrizedTypeName.typeArguments)
		{
			typeArguments[i]=resolve(item);
			i++;
		}
		
		resolved=ParameterizedTypeName.get((ClassName)resolve(inputParametrizedTypeName.rawType), typeArguments);
		
		return resolved;
	} else if (inputType instanceof ArrayTypeName)
	{
		ArrayTypeName inputTypeName=(ArrayTypeName)inputType;
		
		resolved=ArrayTypeName.of(resolve(inputTypeName.componentType));
		
		return resolved;
	} else {
		// it's not a type variable
		if (inputType.toString().contains(".")) {
			return inputType;
		}
		// ASSERT: simple typeName 
		if (typeVariableMap.containsKey(inputType.toString())) {
			TypeName type = typeVariableMap.get(inputType.toString());
			return type;
		} else if (declaredTypeArgumentList.size()==1){
			resolved = declaredTypeArgumentList.get(0);
			// if we found an unique type variable, we use it.
			//typeVariableMap = new HashMap<>();
			//typeVariableMap.put(inputTypeName.toString(), resolved);

			return resolved;
		}
		
		AssertKripton.assertTrue(false, "In type hierarchy of %s '%s' there is a unresolved type variable named '%s'. Define it with @BindTypeVariables", element.getKind()==ElementKind.CLASS ? "class" :"interface", element.getQualifiedName(),
				inputType.toString());
		
		return resolved;
	}
	
}