本文整理汇总了Java中com.google.common.reflect.TypeToken.isAssignableFrom方法的典型用法代码示例。如果您正苦于以下问题:Java TypeToken.isAssignableFrom方法的具体用法?Java TypeToken.isAssignableFrom怎么用?Java TypeToken.isAssignableFrom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.reflect.TypeToken
的用法示例。
在下文中一共展示了TypeToken.isAssignableFrom方法的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: getMessageType
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
private TypeToken<? extends Message> getMessageType(TypeToken decl, Method method) {
if(method.getParameterTypes().length < 1 || method.getParameterTypes().length > 3) {
throw new IllegalStateException("Message handler method must take 1 to 3 parameters");
}
final TypeToken<Message> base = new TypeToken<Message>(){};
for(Type param : method.getGenericParameterTypes()) {
final TypeToken paramToken = decl.resolveType(param);
Types.assertFullySpecified(paramToken);
if(base.isAssignableFrom(paramToken)) {
messageRegistry.typeName(paramToken.getRawType()); // Verify message type is registered
return paramToken;
}
}
throw new IllegalStateException("Message handler has no message parameter");
}
示例3: prune
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
@Override
protected boolean prune(TypeToken<? super T> adding) {
for(Iterator<TypeToken<? super T>> iterator = iterator(); iterator.hasNext(); ) {
final TypeToken<? super T> existing = iterator.next();
if(adding.isAssignableFrom(existing)) return false;
if(existing.isAssignableFrom(adding)) iterator.remove();
}
return true;
}
示例4: assignableTo
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
public static Predicate<? super Type> assignableTo(final Type to) {
final TypeToken<?> toToken = TypeToken.of(to);
return from -> from != null && toToken.isAssignableFrom(from);
}
示例5: assertAssignableTo
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
private static Field assertAssignableTo(Field field, TypeToken<?> type) {
if(!type.isAssignableFrom(TypeToken.of(field.getGenericType()))) {
throw new NoSuchFieldError("Field " + field.getName() + " is not of type " + type);
}
return field;
}
示例6: assignableTo
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
public static Predicate<Field> assignableTo(TypeToken<?> type) {
return field -> type.isAssignableFrom(field.getGenericType());
}
示例7: Transaction
import com.google.common.reflect.TypeToken; //导入方法依赖的package包/类
private Transaction(Exchange.Direct exchange,
final Queue replyQueue,
Message request,
@Nullable Metadata requestProps,
@Nullable Publish publish,
TypeToken<T> replyType,
@Nullable Duration timeout) {
super(timeout != null ? timeout : DEFAULT_TIMEOUT);
checkNotNull(request, "request");
checkNotNull(replyType, "replyType");
this.callSite = new Exception().getStackTrace();
this.replyQueue = replyQueue;
final Metadata finalRequestProps = requestProps = replyQueue.client().getProperties(request, requestProps);
finalRequestProps.setReplyTo(this.replyQueue.name());
this.requestId = checkNotNull(finalRequestProps.getMessageId());
this.requestType = request.getClass();
this.messageHandler = new MessageHandler<Message>() {
@Override public void handleDelivery(Message message, TypeToken<? extends Message> type, Metadata replyProps, Delivery delivery) {
if(requestId.equals(replyProps.getCorrelationId())) {
Transaction.this.replyQueue.unsubscribe(messageHandler);
if(replyProps.protocolVersion() != finalRequestProps.protocolVersion()) {
setException(new ApiException("Received a protocol " + replyProps.protocolVersion() +
" reply to a protocol " + finalRequestProps.protocolVersion() + " request",
callSite));
} else {
final Reply reply = message instanceof Reply ? (Reply) message : null;
if(reply != null && !reply.success()) {
setException(new ApiException(reply.error() != null ? reply.error()
: "Generic failure reply to request " + requestId,
callSite));
} else if(!replyType.isAssignableFrom(type)) {
setException(new ApiException("Wrong reply type to request " + requestId +
", expected a " + replyType +
", received a " + type,
callSite));
} else {
set((T) message);
}
}
}
}
};
this.replyQueue.subscribe(Message.class, messageHandler, null);
exchange.publishAsync(request, finalRequestProps, publish);
}