本文整理汇总了Java中com.google.common.reflect.TypeToken.isPrimitive方法的典型用法代码示例。如果您正苦于以下问题:Java TypeToken.isPrimitive方法的具体用法?Java TypeToken.isPrimitive怎么用?Java TypeToken.isPrimitive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.reflect.TypeToken
的用法示例。
在下文中一共展示了TypeToken.isPrimitive方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isConvertibleForInvocation
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
/**
* Test if an invocation conversion can be applied to the given types.
*
* https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.3
*/
public static boolean isConvertibleForInvocation(TypeToken<?> to, TypeToken<?> from) {
if(to.isPrimitive()) {
// Assigning to a primitive allows for both unboxing and primitive widening
Class<?> fromRaw = from.getRawType();
if(!fromRaw.isPrimitive()) {
fromRaw = BOXINGS.inverse().get(fromRaw);
if(fromRaw == null) return false;
}
return isPromotable(to.getRawType(), fromRaw);
} else if(from.isPrimitive()) {
// Assigning to an object from a primitive allows boxing and reference widening
return to.isAssignableFrom(box(from.getRawType()));
} else {
return to.isAssignableFrom(from);
}
}
示例2: canHandle
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
if (targetTypeToken.isPrimitive()) {
targetTypeToken = PrimitiveTypeUtils.toWrapperTypeToken(targetTypeToken);
}
if (!targetTypeToken.isSubtypeOf(Number.class)) return false;
if (!(source instanceof String)) return false;
try {
NumberUtils.parseNumber((String) source, (Class) targetTypeToken.getType());
return true;
} catch (IllegalArgumentException e){
return false;
}
}
示例3: convert
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public Object convert(Object source, TypeToken<?> targetTypeToken) {
if (!canHandle(source, targetTypeToken)) throw new ConverterException(source, targetTypeToken.getRawType());
if (targetTypeToken.isPrimitive()) {
targetTypeToken = PrimitiveTypeUtils.toWrapperTypeToken(targetTypeToken);
}
return NumberUtils.convertNumberToTargetClass((Number) source, (Class) targetTypeToken.getType());
}
示例4: canHandle
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
if (targetTypeToken.isPrimitive()) {
targetTypeToken = PrimitiveTypeUtils.toWrapperTypeToken(targetTypeToken);
}
return TypeToken.of(source.getClass()).isSubtypeOf(Number.class)
&& targetTypeToken.isSubtypeOf(Number.class);
}
示例5: convert
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
@Override
public Object convert(Object source, TypeToken<?> targetTypeToken) {
if (targetTypeToken.isPrimitive()) {
targetTypeToken = PrimitiveTypeUtils.toWrapperTypeToken(targetTypeToken);
}
if (canHandle(source, targetTypeToken)) {
return NumberUtils.parseNumber((String) source, (Class) targetTypeToken.getType());
} else {
throw new ConverterException("can not convert '" + source + "' to " + targetTypeToken.getRawType());
}
}
示例6: box
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
public static <T> TypeToken<T> box(TypeToken<T> type) {
return type.isPrimitive() ? TypeToken.of(box((Class<T>) type.getRawType())) : type;
}
示例7: convert
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
/**
* Convert the fixtureList to the given TypeToken
* @param fixtureList
* @param typeToken
* @return converted object from fixtureList.
* @throws IllegalAccessException
* @throws InstantiationException
*/
protected Object convert(FixtureList fixtureList, TypeToken<?> typeToken) {
boolean isArray = typeToken.isArray();
boolean isList = typeToken.isSubtypeOf(TypeToken.of(List.class));
if (!isList && !isArray) {
throw new FixtureMappingException("Target setter of '" + fixtureList.getFixtureName() + "' must be List or array.");
}
final List convertedList;
if (isArray || typeToken.getRawType().isInterface()) {
convertedList = new ArrayList();
} else {
try {
convertedList = (List) typeToken.getRawType().newInstance();
} catch (Exception e) {
throw new FixtureMappingException(e);
}
}
TypeToken<?> elementTypeToken = TypeTokenUtils.extractElementTypeToken(typeToken);
for (FixtureTemplate template : fixtureList) {
Object converted = convert(template, elementTypeToken);
if (converted != null) {
convertedList.add(converted);
} else {
logger.warn("Can not find converter for " + fixtureList.getFixtureName());
}
}
// not found converter
if (convertedList.size() == 0) return null;
if(isArray) {
if (elementTypeToken.isPrimitive()) {
return PrimitiveTypeUtils.toWrapperListToPrimitiveArray(convertedList, (Class<?>) elementTypeToken.getType());
} else {
return Arrays.copyOf(convertedList.toArray(), convertedList.size(), (Class) typeToken.getRawType());
}
} else {
return convertedList;
}
}