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

The following examples show how to use com.squareup.javapoet.TypeName#unbox() . 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: ModelProperty.java    From kripton with Apache License 2.0 6 votes vote down vote up
/**
 * Check type adapter.
 *
 * @param entity the entity
 * @param propertyType the property type
 * @param typeAdapter the type adapter
 * @param annotation the annotation
 */
protected void checkTypeAdapter(@SuppressWarnings("rawtypes") ModelEntity entity, TypeMirror propertyType, TypeAdapter typeAdapter, ModelAnnotation annotation) {
	TypeName sourceType = TypeUtility.typeName(TypeAdapterHelper.detectSourceType(entity.getElement(), typeAdapter.adapterClazz));
	TypeName uboxSourceType=sourceType;
	
	if (TypeUtility.isTypeWrappedPrimitive(sourceType)) {
		uboxSourceType=sourceType.unbox();
	}
	
	boolean expr=uboxSourceType.toString().equals(propertyType.toString()) || sourceType.toString().equals(propertyType.toString());
	
	
	AssertKripton.fail(!expr,
			"In class '%s', property '%s' uses @%s that manages type '%s' instead of '%s'", entity.getElement().asType(), getName(),
			annotation.getSimpleName(), element.asType().toString(), TypeAdapterHelper.detectSourceType(entity.getElement(), typeAdapter.adapterClazz), getPropertyType().getTypeName());

}
 
Example 2
Source File: PsiTypeConverter.java    From json2java4idea with Apache License 2.0 5 votes vote down vote up
@Nonnull
@CheckReturnValue
static TypeName unbox(@Nonnull TypeName typeName) {
    try {
        return typeName.unbox();
    } catch (Exception e) {
        return typeName;
    }
}
 
Example 3
Source File: ProcessUtils.java    From RxAndroidOrm with Apache License 2.0 5 votes vote down vote up
public static TypeName unbox(TypeName typeName) {
    try {
        return typeName.unbox();
    } catch (Exception e) {
        return typeName;
    }
}
 
Example 4
Source File: ProcessUtils.java    From Freezer with Apache License 2.0 5 votes vote down vote up
public static TypeName unbox(TypeName typeName) {
    try {
        return typeName.unbox();
    } catch (Exception e) {
        return typeName;
    }
}
 
Example 5
Source File: AnnotatedDurableEntityClass.java    From mnemonic with Apache License 2.0 5 votes vote down vote up
private boolean isUnboxPrimitive(TypeName tn) {
  TypeName n = tn;
  try {
    n = tn.unbox();
  } catch (UnsupportedOperationException ex) {
  }
  return n.isPrimitive();
}
 
Example 6
Source File: AnnotatedDurableEntityClass.java    From mnemonic with Apache License 2.0 5 votes vote down vote up
private TypeName unboxTypeName(TypeName tn) {
  TypeName n = tn;
  try {
    n = tn.unbox();
  } catch (UnsupportedOperationException ex) {
  }
  return n;
}
 
Example 7
Source File: AutoBundleBindingField.java    From AutoBundle with Apache License 2.0 5 votes vote down vote up
private static TypeName detectConvertedTypeByTypeElement(TypeElement element) {
    TypeMirror typeMirror = getConverterGenericsTypesByTypeElement(element).get(1);
    TypeName typeName = TypeName.get(typeMirror);
    try {
        typeName = typeName.unbox();
    } catch (UnsupportedOperationException ignore) {}
    return typeName;
}
 
Example 8
Source File: AutoBundleBindingField.java    From AutoBundle with Apache License 2.0 5 votes vote down vote up
private static TypeName detectConvertedTypeNameByClass(Class clazz) {
    Type converted = getConverterGenericsTypesByClass(clazz)[1];
    TypeName typeName = TypeName.get(converted);
    try {
        typeName = typeName.unbox();
    } catch (UnsupportedOperationException ignore) {}
    return typeName;
}
 
Example 9
Source File: SQLTransformer.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * Check type adapter.
 * @param annotation 
 *
 * @param entity the entity
 * @param propertyType the property type
 * @param typeAdapter the type adapter
 * @param annotation the annotation
 */
private static void checkTypeAdapterForParam( SQLiteModelMethod method,
		String methodParamName, Class<? extends Annotation> annotation) {
	
	TypeName paramType=method.findParameterType(methodParamName);
	if (method.isSpreadParameter(methodParamName)) {
		if (paramType instanceof ArrayTypeName) {				
			paramType = ((ArrayTypeName) paramType).componentType;
		} else if (paramType instanceof ParameterizedTypeName) {				
			paramType = ((ParameterizedTypeName) paramType).typeArguments.get(0);
		} 
	}
	
	TypeName adapterType = method.getAdapterForParam(methodParamName);
	
	
	TypeName sourceType = TypeUtility.typeName(TypeAdapterHelper.detectSourceType(method, adapterType));
	TypeName uboxSourceType=sourceType;
	
	if (TypeUtility.isTypeWrappedPrimitive(sourceType)) {
		uboxSourceType=sourceType.unbox();
	}
	
	boolean expr=uboxSourceType.toString().equals(paramType.toString()) || sourceType.toString().equals(paramType.toString());
	
	
	AssertKripton.fail(!expr,
			"In DAO '%s', method '%s' has parameter '%s' uses @%s that manages type '%s' instead of '%s'", method.getParent().getName(), method.getName(),
			methodParamName, annotation.getSimpleName(),sourceType, paramType);

}
 
Example 10
Source File: TypeUtils.java    From kvs-schema with MIT License 5 votes vote down vote up
public static TypeName unbox(TypeName typeName) {
    try {
        return typeName.unbox();
    } catch (UnsupportedOperationException ignore) {
        return typeName;
    }
}
 
Example 11
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 12
Source File: ReactPropertyProcessor.java    From react-native-GPay with MIT License 4 votes vote down vote up
private static CodeBlock.Builder getPropertyExtractor(
    PropertyInfo info,
    CodeBlock.Builder builder) {
  TypeName propertyType = info.propertyType;
  if (propertyType.equals(STRING_TYPE)) {
    return builder.add("props.getString(name)");
  } else if (propertyType.equals(READABLE_ARRAY_TYPE)) {
    return builder.add("props.getArray(name)");
  } else if (propertyType.equals(READABLE_MAP_TYPE)) {
    return builder.add("props.getMap(name)");
  } else if (propertyType.equals(DYNAMIC_TYPE)) {
    return builder.add("props.getDynamic(name)");
  }

  if (BOXED_PRIMITIVES.contains(propertyType)) {
    propertyType = propertyType.unbox();
  }

  if (propertyType.equals(TypeName.BOOLEAN)) {
    return builder.add("props.getBoolean(name, $L)", info.mProperty.defaultBoolean());
  } if (propertyType.equals(TypeName.DOUBLE)) {
    double defaultDouble = info.mProperty.defaultDouble();
    if (Double.isNaN(defaultDouble)) {
      return builder.add("props.getDouble(name, $T.NaN)", Double.class);
    } else {
      return builder.add("props.getDouble(name, $Lf)", defaultDouble);
    }
  }
  if (propertyType.equals(TypeName.FLOAT)) {
    float defaultFloat = info.mProperty.defaultFloat();
    if (Float.isNaN(defaultFloat)) {
      return builder.add("props.getFloat(name, $T.NaN)", Float.class);
    } else {
      return builder.add("props.getFloat(name, $Lf)", defaultFloat);
    }
  }
  if (propertyType.equals(TypeName.INT)) {
    return builder.add("props.getInt(name, $L)", info.mProperty.defaultInt());
  }

  throw new IllegalArgumentException();
}
 
Example 13
Source File: ColumnElement.java    From sqlitemagic with Apache License 2.0 4 votes vote down vote up
public TypeName getDeserializedTypeName() {
  final ExtendedTypeElement deserializedType = getDeserializedType();
  final TypeName typeName = TypeName.get(deserializedType.getTypeMirror());
  return deserializedType.isPrimitiveElement() ? typeName.unbox() : typeName;
}
 
Example 14
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 15
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 16
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;
}