本文整理汇总了Java中com.google.common.reflect.TypeToken.getRawType方法的典型用法代码示例。如果您正苦于以下问题:Java TypeToken.getRawType方法的具体用法?Java TypeToken.getRawType怎么用?Java TypeToken.getRawType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.reflect.TypeToken
的用法示例。
在下文中一共展示了TypeToken.getRawType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: init
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
@SuppressWarnings ("unchecked")
private D init (final URL url, final Capabilities capability) {
log.trace ("Initializing driver...");
final TypeToken <D> token = new TypeToken <D> (getClass ()) {
private static final long serialVersionUID = 1562415938665085306L;
};
final Class <D> cls = (Class <D>) token.getRawType ();
final Class <?> [] argTypes = new Class <?> [] { URL.class, Capabilities.class };
try {
final Constructor <D> ctor = cls.getDeclaredConstructor (argTypes);
return ctor.newInstance (url, capability);
}
catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException e) {
fail (DeviceDriverInitializationFailedError.class, "Error occured while initializing device driver.", e);
}
return null;
}
示例2: getDummyValue
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
private <T> T getDummyValue(TypeToken<T> type) {
Class<? super T> rawType = type.getRawType();
@SuppressWarnings("unchecked") // Assume all default values are generics safe.
T defaultValue = (T) defaultValues.getInstance(rawType);
if (defaultValue != null) {
return defaultValue;
}
@SuppressWarnings("unchecked") // ArbitraryInstances always returns generics-safe dummies.
T value = (T) ArbitraryInstances.get(rawType);
if (value != null) {
return value;
}
if (rawType.isInterface()) {
return new SerializableDummyProxy(this).newProxy(type);
}
return null;
}
示例3: invocationFailureReason
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
private static @Nullable String invocationFailureReason(Invokable<?, ?> to, Invokable<?, ?> from) {
final String reason = invocationFailureReason(methodType(to), methodType(from));
if(reason != null) return reason;
thrownLoop: for(TypeToken<? extends Throwable> thrown : from.getExceptionTypes()) {
final Class<?> thrownRaw = thrown.getRawType();
if(Error.class.isAssignableFrom(thrownRaw)) continue;
if(RuntimeException.class.isAssignableFrom(thrownRaw)) continue ;
for(TypeToken<? extends Throwable> caught : to.getExceptionTypes()) {
if(caught.getRawType().isAssignableFrom(thrownRaw)) continue thrownLoop;
}
return "unhandled exception " + thrown.getRawType().getName();
}
return null;
}
示例4: 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);
}
}
示例5: convert
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
/**
* Convert for a target of Optional
*/
@Override
public Object convert(FixtureTemplate fixtureTemplate, TypeToken typeToken) {
if (typeToken.getRawType() != Optional.class) {
return fixtureConverter.convert(fixtureTemplate, typeToken);
}
List<TypeToken<?>> types = TypeTokenUtils.extractGenericTypeTokens(typeToken);
if (types.isEmpty()) {
if (fixtureTemplate instanceof FixtureValue) {
Object value = ((FixtureValue) fixtureTemplate).getValue();
return Optional.of(value);
}
return null;
} else {
return Optional.of(fixtureConverter.convert(fixtureTemplate, types.get(0)));
}
}
示例6: convert
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
/**
* Convert for a target of Optional
*/
@Override
public Object convert(FixtureTemplate fixtureTemplate, TypeToken typeToken) {
if (typeToken.getRawType() != Optional.class) {
return fixtureConverter.convert(fixtureTemplate, typeToken);
}
List<TypeToken<?>> types = TypeTokenUtils.extractGenericTypeTokens(typeToken);
if (types.isEmpty()) {
if (fixtureTemplate instanceof FixtureValue) {
Object value = ((FixtureValue) fixtureTemplate).getValue();
return Optional.of(value);
}
return null;
} else {
return Optional.of(fixtureConverter.convert(fixtureTemplate, types.get(0)));
}
}
示例7: 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());
Class enumClass = targetTypeToken.getRawType();
for (Object enumConstant : enumClass.getEnumConstants()) {
String enumStr = enumConstant.toString().replaceAll("\\_", "");
String sourceStr = ((String) source).replaceAll("\\-", "").replaceAll("\\_", "").replaceAll("\\s", "");
if (enumStr.equalsIgnoreCase(sourceStr)) {
return Enum.valueOf(enumClass, enumConstant.toString());
}
}
throw new ConverterException(source, targetTypeToken.getRawType(), "can not find enum constants");
}
示例8: NotificationInvoker
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
private NotificationInvoker(final NotificationListener listener) {
delegate = listener;
final Map<Class<? extends Notification>, InvokerContext> builder = new HashMap<>();
for(final TypeToken<?> ifaceToken : TypeToken.of(listener.getClass()).getTypes().interfaces()) {
final Class<?> iface = ifaceToken.getRawType();
if(NotificationListener.class.isAssignableFrom(iface) && BindingReflections.isBindingClass(iface)) {
@SuppressWarnings("unchecked")
final Class<? extends NotificationListener> listenerType = (Class<? extends NotificationListener>) iface;
final NotificationListenerInvoker invoker = NotificationListenerInvoker.from(listenerType);
for(final Class<? extends Notification> type : getNotificationTypes(listenerType)) {
builder.put(type, new InvokerContext(BindingReflections.findQName(type) , invoker));
}
}
}
invokers = ImmutableMap.copyOf(builder);
}
示例9: getDefaultValue
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
private <T> T getDefaultValue(TypeToken<T> type) {
// We assume that all defaults are generics-safe, even if they aren't,
// we take the risk.
@SuppressWarnings("unchecked")
T defaultValue = (T) defaults.getInstance(type.getRawType());
if (defaultValue != null) {
return defaultValue;
}
@SuppressWarnings("unchecked") // All arbitrary instances are generics-safe
T arbitrary = (T) ArbitraryInstances.get(type.getRawType());
if (arbitrary != null) {
return arbitrary;
}
if (type.getRawType() == Class.class) {
// If parameter is Class<? extends Foo>, we return Foo.class
@SuppressWarnings("unchecked")
T defaultClass = (T) getFirstTypeParameter(type.getType()).getRawType();
return defaultClass;
}
if (type.getRawType() == TypeToken.class) {
// If parameter is TypeToken<? extends Foo>, we return TypeToken<Foo>.
@SuppressWarnings("unchecked")
T defaultType = (T) getFirstTypeParameter(type.getType());
return defaultType;
}
if (type.getRawType() == Converter.class) {
TypeToken<?> convertFromType = type.resolveType(
Converter.class.getTypeParameters()[0]);
TypeToken<?> convertToType = type.resolveType(
Converter.class.getTypeParameters()[1]);
@SuppressWarnings("unchecked") // returns default for both F and T
T defaultConverter = (T) defaultConverter(convertFromType, convertToType);
return defaultConverter;
}
if (type.getRawType().isInterface()) {
return newDefaultReturningProxy(type);
}
return null;
}
示例10: convert
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public Object convert(Object input, ParameterContext context) throws ArgumentConversionException {
TypeToken<?> parameterType = TypeToken.of(context.getParameter().getParameterizedType());
if (parameterType.getRawType() != Class.class) {
throw new ArgumentConversionException(
String.format("Could not convert: %s. Invalid parameter type.", input));
}
return convert(input.toString(), (TypeToken<? extends Class<?>>) parameterType);
}
示例11: deserialize
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
@Override
public Trail deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
Class<?> clazz = type.getRawType();
return Sponge.getDataManager()
.deserialize(Trail.class, DataTranslators.CONFIGURATION_NODE.translate(value))
.orElseThrow(() -> new ObjectMappingException("Could not translate DataSerializable of type: " + clazz.getName()));
}
示例12: convert
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
@Override
public Object convert(Object source, TypeToken<?> targetTypeToken) {
if (!canHandle(source, targetTypeToken)) {
throw new ConverterException(source, targetTypeToken.getRawType());
}
Integer period = null;
if (source instanceof Number) {
period = (Integer) numberToNumberConverter.convert(source, TypeToken.of(Integer.class));
} else if (source instanceof String) {
period = (Integer) stringToNumberConverter.convert(source, TypeToken.of(Integer.class));
}
Type targetType = targetTypeToken.getType();
if (targetType.equals(Seconds.class)) {
return Seconds.seconds(period);
} else if (targetType.equals(Minutes.class)) {
return Minutes.minutes(period);
} else if (targetType.equals(Hours.class)) {
return Hours.hours(period);
} else if (targetType.equals(Days.class)) {
return Days.days(period);
} else if (targetType.equals(Weeks.class)) {
return Weeks.weeks(period);
} else if (targetType.equals(Months.class)) {
return Months.months(period);
} else if (targetType.equals(Years.class)) {
return Years.years(period);
}
throw new ConverterException(source, targetTypeToken.getRawType());
}
示例13: convert
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
@Override
public Object convert(Object source, TypeToken<?> targetTypeToken) {
if (!canHandle(source, targetTypeToken)) {
throw new ConverterException(source, targetTypeToken.getRawType());
}
Date date = stringToDateConverter.convert(String.valueOf(source));
return dateToJodaTimeBaseLocalConverter.convert(date, targetTypeToken);
}
示例14: 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());
}
示例15: 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());
}
}