List of usage examples for java.lang Class isAssignableFrom
@HotSpotIntrinsicCandidate public native boolean isAssignableFrom(Class<?> cls);
From source file:com.github.helenusdriver.commons.lang3.reflect.ReflectionUtils.java
/** * Gets the serialization constructor for the given class based on the given * base class. The specified base class is expected to have a default constructor * to use for initializing the hierarchy from that point on. The constructor * can have any visibility (i.e. public, protected, package private, or private). * <p>//from ww w . j a v a2 s .c o m * <i>Note:</i> The returned constructor results in having a proper object of * the given class instantiated without calling any of the constructors from * its class or super classes. The fields of all classes in the hierarchy will * not be initialized; thus set using default Java initialization (i.e. * <code>null</code>, 0, 0L, false, ...). This is how Java creates new * serialized objects before calling the <code>readObject()</code> method of * each of the subclasses. * * @author paouelle * * @param <T> the class for which to get the serialization constructor * * @param clazz the class for which to get the serialization constructor * @param bclass the base class to start the initialization from in the * returned serialization constructor * @return the non-<code>null</code> serialization constructor for the given * class * @throws NullPointerException if <code>clazz</code> or <code>bclass</code> * is <code>null</code> * @throws IllegalArgumentException if <code>bclass</code> is not a base class * for <code>clazz</code> * @throws SecurityException if the request is denied */ public static <T> Constructor<T> getSerializationConstructorFromBaseClass(Class<T> clazz, Class<?> bclass) { org.apache.commons.lang3.Validate.notNull(clazz, "invalid null class"); org.apache.commons.lang3.Validate.notNull(bclass, "invalid null base class"); org.apache.commons.lang3.Validate.isTrue(bclass.isAssignableFrom(clazz), bclass.getName() + " is not a superclass of " + clazz.getName()); final Constructor<?> ctor; try { // find the default ctor for the base class ctor = bclass.getDeclaredConstructor(); ctor.setAccessible(true); } catch (NoSuchMethodException e) { throw new IllegalStateException("missing default constructor for base class: " + bclass.getName(), e); } @SuppressWarnings({ "restriction", "unchecked" }) final Constructor<T> nc = (Constructor<T>) ReflectionUtils.reflFactory.newConstructorForSerialization(clazz, ctor); nc.setAccessible(true); return nc; }
From source file:com.github.helenusdriver.driver.impl.DataDecoder.java
/** * Gets a "udt" to {@link Object} decoder based on the given UDT class info. * * @author paouelle//w w w .j a v a 2 s.c o m * * @param cinfo the user-defined class info * @return the non-<code>null</code> decoder for user-defined types represented * by the given class info */ @SuppressWarnings("rawtypes") public final static DataDecoder<Object> udt(final UDTClassInfoImpl<?> cinfo) { return new DataDecoder<Object>(Object.class) { @SuppressWarnings("unchecked") @Override protected Object decodeImpl(Row row, String name, Class clazz) { org.apache.commons.lang3.Validate.isTrue(clazz.isAssignableFrom(cinfo.getObjectClass()), "unsupported class '%s' to decode to", clazz.getName()); final UDTValue uval = row.getUDTValue(name); if (uval == null) { return null; } return cinfo.getObject(uval); } @Override protected Object decodeImpl(UDTValue uval, String name, Class clazz) { // not supported???? throw new IllegalArgumentException( "user-defined types based on other user-defined types are not supported"); } }; }
From source file:com.jeeframework.util.classes.ClassUtils.java
/** * Check if the right-hand side type may be assigned to the left-hand side * type, assuming setting by reflection. Considers primitive wrapper * classes as assignable to the corresponding primitive types. * @param lhsType the target type//from www. j a va2s. c o m * @param rhsType the value type that should be assigned to the target type * @return if the target type is assignable from the value type * @see TypeUtils#isAssignable */ public static boolean isAssignable(Class lhsType, Class rhsType) { Assert.notNull(lhsType, "Left-hand side type must not be null"); Assert.notNull(rhsType, "Right-hand side type must not be null"); return (lhsType.isAssignableFrom(rhsType) || lhsType.equals(primitiveWrapperTypeMap.get(rhsType))); }
From source file:com.flexive.faces.FxJsfUtils.java
/** * Find a parent of the given class. Throws a runtime exception if none is found. * * @param component the (child) component that searches an ancestor * @param cls the class or interface to be searched for in the component's ancestors * @return the parent component//from ww w. jav a 2s .c o m */ public static <T> T findAncestor(UIComponent component, Class<T> cls) { UIComponent current = component; if (current != null) { current = current.getParent(); } while (current != null && !(cls.isAssignableFrom(current.getClass()))) { current = current.getParent(); } if (current == null) { throw new FxNotFoundException(LOG, "ex.jsf.ancestor.notFound").asRuntimeException(); } return cls.cast(current); }
From source file:org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter.java
@Override public boolean canWrite(Class<?> clazz, MediaType mediaType) { return (clazz.isAssignableFrom(Alps.class) || clazz.isAssignableFrom(RootResourceInformation.class)) && super.canWrite(clazz, mediaType); }
From source file:edu.mit.media.funf.config.DefaultRuntimeTypeAdapterFactory.java
@SuppressWarnings("unchecked") public static <T> Class<? extends T> getRuntimeType(JsonElement el, Class<T> baseClass, Class<? extends T> defaultClass) { Class<? extends T> type = defaultClass; String typeString = null;//from ww w.j av a2s . c om if (el != null) { try { if (el.isJsonObject()) { JsonObject jsonObject = el.getAsJsonObject(); if (jsonObject.has(RuntimeTypeAdapterFactory.TYPE)) { typeString = jsonObject.get(RuntimeTypeAdapterFactory.TYPE).getAsString(); } } else if (el.isJsonPrimitive()) { typeString = el.getAsString(); } } catch (ClassCastException e) { } } // TODO: expand string to allow for builtin to be specified as ".SampleProbe" if (typeString != null) { try { Class<?> runtimeClass = Class.forName(typeString); if (baseClass.isAssignableFrom(runtimeClass)) { type = (Class<? extends T>) runtimeClass; } else { Log.w(TAG, "RuntimeTypeAdapter: Runtime class '" + typeString + "' is not assignable from default class '" + defaultClass.getName() + "'."); } } catch (ClassNotFoundException e) { Log.w(TAG, "RuntimeTypeAdapter: Runtime class '" + typeString + "' not found."); } } return type; }
From source file:de.chaosfisch.services.ExportPostProcessor.java
@Override public Upload process(final Upload upload) { if (!config.getBoolean(ExportPostProcessor.JSON_LOGFILES, false)) { return upload; }// www.j a va 2 s . c o m LOGGER.info("Running export postprocessor"); final Gson gson = new GsonBuilder().setPrettyPrinting() .addSerializationExclusionStrategy(new ExclusionStrategy() { @Override public boolean shouldSkipField(final FieldAttributes f) { return f.getDeclaringClass().isAssignableFrom(Account.class); } @Override public boolean shouldSkipClass(final Class<?> clazz) { return clazz.isAssignableFrom(Account.class); } }).registerTypeAdapter(DateTime.class, new DateTimeTypeConverter()).serializeNulls().create(); try { final Upload copy = gson.fromJson(gson.toJson(upload), Upload.class); try { Files.createDirectories(Paths.get(ApplicationData.DATA_DIR + "/uploads/")); Files.write( Paths.get(String.format("%s/uploads/%s.json", ApplicationData.DATA_DIR, copy.getVideoid())), gson.toJson(copy).getBytes(Charsets.UTF_8)); } catch (final IOException e) { LOGGER.warn("Couldn't write json log.", e); } LOGGER.info("Finished export postprocessor"); } catch (final Exception e) { e.printStackTrace(); } return upload; }
From source file:funf.config.DefaultRuntimeTypeAdapterFactory.java
@SuppressWarnings("unchecked") public static <T> Class<? extends T> getRuntimeType(JsonElement el, Class<T> baseClass, Class<? extends T> defaultClass) { Class<? extends T> type = defaultClass; String typeString = null;//from w w w . j a v a 2s . c o m if (el != null) { try { if (el.isJsonObject()) { JsonObject jsonObject = el.getAsJsonObject(); if (jsonObject.has(RuntimeTypeAdapterFactory.TYPE)) { typeString = jsonObject.get(RuntimeTypeAdapterFactory.TYPE).getAsString(); } } else if (el.isJsonPrimitive()) { typeString = el.getAsString(); } } catch (ClassCastException e) { } } // TODO: expand string to allow for builtin to be specified as ".SampleProbe" if (typeString != null) { try { Class<?> runtimeClass = Class.forName(typeString); if (baseClass.isAssignableFrom(runtimeClass)) { type = (Class<? extends T>) runtimeClass; } else { Log.w(LogUtil.TAG, "RuntimeTypeAdapter: Runtime class '" + typeString + "' is not assignable from default class '" + defaultClass.getName() + "'."); } } catch (ClassNotFoundException e) { Log.w(LogUtil.TAG, "RuntimeTypeAdapter: Runtime class '" + typeString + "' not found."); } } return type; }
From source file:com.callidusrobotics.droptables.exception.HtmlBodyErrorWriter.java
@Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return type.isAssignableFrom(ErrorMessage.class); }
From source file:com.khs.sherpa.servlet.RequestMapper.java
private Object mapNonAnnotation(String endpoint, String action, Class<?> type) { if (type.isAssignableFrom(SessionTokenService.class)) { return service.getTokenService(); } else if (type.isAssignableFrom(UserService.class)) { return service.getUserService(); } else if (type.isAssignableFrom(ActivityService.class)) { return service.getActivityService(); } else if (type.isAssignableFrom(ServletRequest.class)) { return request; } else if (type.isAssignableFrom(ServletResponse.class)) { return response; } else {/*from ww w . j a va 2 s. c om*/ String body = UrlUtil.getRequestBody((HttpServletRequest) request); if (StringUtils.isNotEmpty(body)) { return this.parseObject(type, body, null); } } return null; }