本文整理了Java中java.lang.Class.cast()
方法的一些代码示例,展示了Class.cast()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.cast()
方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:cast
[英]Casts the given object to the type represented by this Class. If the object is null then the result is also null.
[中]将给定对象强制转换为此类表示的类型。如果对象为null,则结果也为null。
代码示例来源:origin: ReactiveX/RxJava
@Override
public U apply(T t) throws Exception {
return clazz.cast(t);
}
}
代码示例来源:origin: google/guava
static void checkCast(Class<?> clazz, Object obj) {
clazz.cast(obj);
}
代码示例来源:origin: greenrobot/EventBus
/**
* Gets the most recent sticky event for the given type.
*
* @see #postSticky(Object)
*/
public <T> T getStickyEvent(Class<T> eventType) {
synchronized (stickyEvents) {
return eventType.cast(stickyEvents.get(eventType));
}
}
代码示例来源:origin: greenrobot/EventBus
/**
* Remove and gets the recent sticky event for the given event type.
*
* @see #postSticky(Object)
*/
public <T> T removeStickyEvent(Class<T> eventType) {
synchronized (stickyEvents) {
return eventType.cast(stickyEvents.remove(eventType));
}
}
代码示例来源:origin: google/guava
private static <T> T newProxy(Class<T> interfaceType, InvocationHandler handler) {
Object object =
Proxy.newProxyInstance(
interfaceType.getClassLoader(), new Class<?>[] {interfaceType}, handler);
return interfaceType.cast(object);
}
代码示例来源:origin: square/okhttp
/**
* Returns the tag attached with {@code type} as a key, or null if no tag is attached with that
* key.
*/
public @Nullable <T> T tag(Class<? extends T> type) {
return type.cast(tags.get(type));
}
代码示例来源:origin: google/guava
private static <T> T expectArgument(Class<T> type, Object arg) {
try {
return type.cast(arg);
} catch (ClassCastException e) {
throw new IllegalArgumentException(arg + " is not a " + type.getSimpleName());
}
}
代码示例来源:origin: google/guava
private static <T> T createEmptyArray(Class<T> arrayType) {
return arrayType.cast(Array.newInstance(arrayType.getComponentType(), 0));
}
代码示例来源:origin: spring-projects/spring-framework
private static List<TypeElement> staticTypesIn(Iterable<? extends Element> elements) {
List<TypeElement> list = new ArrayList<>();
for (Element element : elements) {
if (TYPE_KINDS.contains(element.getKind()) && element.getModifiers().contains(Modifier.STATIC)) {
list.add(TypeElement.class.cast(element));
}
}
return list;
}
代码示例来源:origin: google/guava
@Override
public <A extends Annotation> @Nullable A getAnnotation(Class<A> annotationType) {
checkNotNull(annotationType);
for (Annotation annotation : annotations) {
if (annotationType.isInstance(annotation)) {
return annotationType.cast(annotation);
}
}
return null;
}
代码示例来源:origin: spring-projects/spring-framework
@Override
protected <T> T lookup(String jndiName, Class<T> requiredType) throws NamingException {
++this.callCount;
return requiredType.cast(DESTINATION);
}
}
代码示例来源:origin: google/guava
static <T extends Enum<T>> Optional<T> getEnumIfPresent(Class<T> enumClass, String value) {
WeakReference<? extends Enum<?>> ref = Enums.getEnumConstants(enumClass).get(value);
return ref == null ? Optional.<T>absent() : Optional.of(enumClass.cast(ref.get()));
}
代码示例来源:origin: google/guava
@Override
public sun.misc.Unsafe run() throws Exception {
Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
for (java.lang.reflect.Field f : k.getDeclaredFields()) {
f.setAccessible(true);
Object x = f.get(null);
if (k.isInstance(x)) {
return k.cast(x);
}
}
throw new NoSuchFieldError("the Unsafe");
}
});
代码示例来源:origin: google/guava
private static <B, T extends B> T cast(Class<T> type, B value) {
return Primitives.wrap(type).cast(value);
}
代码示例来源:origin: google/guava
@CanIgnoreReturnValue
private static <B, T extends B> T cast(Class<T> type, B value) {
return Primitives.wrap(type).cast(value);
}
代码示例来源:origin: google/guava
@Nullable
final <T> T generateFresh(Class<T> type) {
return Primitives.wrap(type).cast(generateFresh(TypeToken.of(type)));
}
代码示例来源:origin: spring-projects/spring-framework
@Override
protected <T> T lookup(String jndiName, Class<T> requiredType) {
assertEquals(DATA_SOURCE_NAME, jndiName);
return requiredType.cast(expectedDataSource);
}
};
代码示例来源:origin: spring-projects/spring-framework
@Override
protected <T> T lookup(String jndiName, Class<T> requiredType) throws NamingException {
if (called) {
fail("Must not be delegating to lookup(..), must be resolving from cache.");
}
assertEquals(DESTINATION_NAME, jndiName);
called = true;
return requiredType.cast(DESTINATION);
}
}
代码示例来源:origin: spring-projects/spring-framework
protected <T extends JCacheOperation<?>> T getCacheOperation(
Class<T> operationType, Class<?> targetType, String methodName, Class<?>... parameterTypes) {
JCacheOperation<?> result = getCacheOperation(targetType, methodName, parameterTypes);
assertNotNull(result);
assertEquals(operationType, result.getClass());
return operationType.cast(result);
}
代码示例来源:origin: spring-projects/spring-framework
protected TextMessage createTextMessage(MessageCreator creator) throws JMSException {
Session mock = mock(Session.class);
given(mock.createTextMessage(BDDMockito.any())).willAnswer(
(Answer<TextMessage>) invocation ->
new StubTextMessage((String) invocation.getArguments()[0]));
javax.jms.Message message = creator.createMessage(mock);
verify(mock).createTextMessage(BDDMockito.any());
return TextMessage.class.cast(message);
}
内容来源于网络,如有侵权,请联系作者删除!