List of usage examples for com.google.common.reflect TypeToken isAssignableFrom
@Deprecated public final boolean isAssignableFrom(Type type)
From source file:org.asoem.greyfish.core.test.GreyfishMatchers.java
public static <T> Matcher<T> isA(final TypeToken<T> typeToken) { return new BaseMatcher<T>() { @Override//from w w w. j ava 2s . c o m public boolean matches(final Object item) { return item != null && typeToken.isAssignableFrom(item.getClass()); } @Override public void describeTo(final Description description) { description.appendText("an instance of ").appendValue(typeToken.getType()); } }; }
From source file:eu.stratosphere.sopremo.type.AbstractTypeMapper.java
protected static Type findRegisteredSubclass(final Set<? extends Type> map, final Class<?> targetType) { final TypeToken<?> token = TypeToken.of(targetType); for (final Type type : map) if (token.isAssignableFrom(type)) return type; return null;/*from w w w . j a v a 2s . c om*/ }
From source file:edu.washington.cs.cupid.TypeManager.java
/** * Returns <code>true</code> iff <code>lhs</code> can be assigned to <code>rhs</code> according to * Java's standard typing rules. Uses {@link TypeToken#isAssignableFrom(TypeToken)}. * @param lhs the left-hand side /*from ww w. ja va 2 s. co m*/ * @param rhs the right-hand side * @return <code>true</code> iff <code>lhs</code> can be assigned to <code>rhs</code> modulo * Java's standard typing. */ public static boolean isJavaCompatible(final TypeToken<?> lhs, final TypeToken<?> rhs) { return lhs.isAssignableFrom(rhs); }
From source file:com.microsoft.rest.Validator.java
/** * Validates a user provided required parameter to be not null. * An {@link IllegalArgumentException} is thrown if a property fails the validation. * * @param parameter the parameter to validate * @throws IllegalArgumentException thrown when the Validator determines the argument is invalid *///from w w w . jav a2s . co m public static void validate(Object parameter) throws IllegalArgumentException { // Validation of top level payload is done outside if (parameter == null) { return; } Class parameterType = parameter.getClass(); TypeToken<?> parameterToken = TypeToken.of(parameterType); if (Primitives.isWrapperType(parameterType)) { parameterToken = parameterToken.unwrap(); } if (parameterToken.isPrimitive() || parameterType.isEnum() || parameterToken.isAssignableFrom(LocalDate.class) || parameterToken.isAssignableFrom(DateTime.class) || parameterToken.isAssignableFrom(String.class) || parameterToken.isAssignableFrom(DateTimeRfc1123.class) || parameterToken.isAssignableFrom(Period.class)) { return; } for (Class<?> c : parameterToken.getTypes().classes().rawTypes()) { // Ignore checks for Object type. if (c.isAssignableFrom(Object.class)) { continue; } for (Field field : c.getDeclaredFields()) { field.setAccessible(true); JsonProperty annotation = field.getAnnotation(JsonProperty.class); Object property; try { property = field.get(parameter); } catch (IllegalAccessException e) { throw new IllegalArgumentException(e.getMessage(), e); } if (property == null) { if (annotation != null && annotation.required()) { throw new IllegalArgumentException(field.getName() + " is required and cannot be null."); } } else { try { Class<?> propertyType = property.getClass(); if (TypeToken.of(List.class).isAssignableFrom(propertyType)) { List<?> items = (List<?>) property; for (Object item : items) { Validator.validate(item); } } else if (TypeToken.of(Map.class).isAssignableFrom(propertyType)) { Map<?, ?> entries = (Map<?, ?>) property; for (Map.Entry<?, ?> entry : entries.entrySet()) { Validator.validate(entry.getKey()); Validator.validate(entry.getValue()); } } else if (parameterType != propertyType) { Validator.validate(property); } } catch (IllegalArgumentException ex) { if (ex.getCause() == null) { // Build property chain throw new IllegalArgumentException(field.getName() + "." + ex.getMessage()); } else { throw ex; } } } } } }
From source file:net.derquinse.common.meta.InterfaceMetaBuilderFactory.java
private static <T extends WithMetaClass> MetaField<? super T, ?> checkReturnType(Method method, MetaField<? super T, ?> field, TypeToken<?> fieldType) { TypeToken<?> returnType = Invokable.from(method).getReturnType(); // Check for primitive types if (Primitives.allPrimitiveTypes().contains(returnType.getRawType())) { returnType = TypeToken.of(Primitives.wrap(returnType.getRawType())); }//from www. jav a2s . co m if (returnType.isAssignableFrom(fieldType)) { return field; } return null; }
From source file:ratpack.guice.internal.GuiceUtil.java
public static <T> void search(Injector injector, TypeToken<T> type, Function<Provider<? extends T>, Boolean> transformer) { Map<Key<?>, Binding<?>> allBindings = injector.getAllBindings(); for (Map.Entry<Key<?>, Binding<?>> keyBindingEntry : allBindings.entrySet()) { TypeLiteral<?> bindingType = keyBindingEntry.getKey().getTypeLiteral(); if (type.isAssignableFrom(toTypeToken(bindingType))) { @SuppressWarnings("unchecked") Provider<? extends T> provider = (Provider<? extends T>) keyBindingEntry.getValue().getProvider(); try { if (!transformer.apply(provider)) { return; }/* w w w. j a v a 2 s . c o m*/ } catch (Exception e) { throw uncheck(e); } } } Injector parent = injector.getParent(); if (parent != null) { search(parent, type, transformer); } }
From source file:org.jclouds.karaf.cache.utils.CacheUtils.java
public static <T> ServiceTracker createServiceCacheTracker(final BundleContext context, final TypeToken<T> serviceToken, final CacheManager<T> cacheManager) { return new ServiceTracker(context, serviceToken.getRawType().getName(), null) { @Override/*from w w w . j ava2 s . co m*/ public Object addingService(ServiceReference reference) { Object service = super.addingService(reference); if (serviceToken.isAssignableFrom(service.getClass())) { cacheManager.bindService((T) service); } return service; } @Override public void removedService(ServiceReference reference, Object service) { if (serviceToken.isAssignableFrom(service.getClass())) { cacheManager.unbindService((T) service); } super.removedService(reference, service); } }; }
From source file:org.jclouds.apis.ApiPredicates.java
/** * Returns all apis who's contexts are assignable from the parameter * /*from ww w . ja v a 2 s .co m*/ * @param type * the type of the context to search for * * @return the apis with contexts assignable from given type */ public static Predicate<ApiMetadata> contextAssignableFrom(final TypeToken<?> type) { checkNotNull(type, "context must be defined"); return new Predicate<ApiMetadata>() { /** * {@inheritDoc} */ @Override public boolean apply(ApiMetadata apiMetadata) { return type.isAssignableFrom(apiMetadata.getContext()); } /** * {@inheritDoc} */ @Override public String toString() { return "contextAssignableFrom(" + type + ")"; } }; }
From source file:org.jclouds.apis.ApiPredicates.java
/** * Returns all apis with the given type. * /*from w w w.j a v a 2s. c o m*/ * @param type * the type of the api to return * * @return the apis with the given type */ public static Predicate<RestApiMetadata> apiAssignableFrom(final TypeToken<?> type) { checkNotNull(type, "type must be defined"); return new Predicate<RestApiMetadata>() { /** * {@inheritDoc} */ @Override public boolean apply(RestApiMetadata apiMetadata) { return type.isAssignableFrom(apiMetadata.getApi()); } /** * {@inheritDoc} */ @Override public String toString() { return "contextAssignableFrom(" + type + ")"; } }; }
From source file:org.jclouds.apis.Apis.java
/** * Returns the type of context//w ww.j a v a2s.c o m * * @param type * the type of the context to search for * * @return the apis with contexts transformable to the given type */ public static TypeToken<?> findView(final ApiMetadata apiMetadata, final TypeToken<?> view) throws NoSuchElementException { checkNotNull(apiMetadata, "apiMetadata must be defined"); checkNotNull(view, "context must be defined"); return Iterables.find(apiMetadata.getViews(), new Predicate<TypeToken<?>>() { @Override public boolean apply(TypeToken<?> input) { return view.isAssignableFrom(input); } }); }