Example usage for java.lang Class isAssignableFrom

List of usage examples for java.lang Class isAssignableFrom

Introduction

In this page you can find the example usage for java.lang Class isAssignableFrom.

Prototype

@HotSpotIntrinsicCandidate
public native boolean isAssignableFrom(Class<?> cls);

Source Link

Document

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.

Usage

From source file:org.brunocvcunha.instagram4j.requests.InstagramRequest.java

/**
 * Parses Json into type, considering the status code
 * @param statusCode HTTP Status Code/* ww w. ja  va2s . c  om*/
 * @param str Entity content
 * @param clazz Class
 * @return Result
 */
@SneakyThrows
public <U> U parseJson(int statusCode, String str, Class<U> clazz) {

    if (clazz.isAssignableFrom(StatusResult.class)) {

        //TODO: implement a better way to handle exceptions
        if (statusCode == HttpStatus.SC_NOT_FOUND) {
            StatusResult result = (StatusResult) clazz.newInstance();
            result.setStatus("error");
            result.setMessage("SC_NOT_FOUND");
            return (U) result;
        } else if (statusCode == HttpStatus.SC_FORBIDDEN) {
            StatusResult result = (StatusResult) clazz.newInstance();
            result.setStatus("error");
            result.setMessage("SC_FORBIDDEN");
            return (U) result;
        }
    }

    return parseJson(str, clazz);
}

From source file:org.lareferencia.xoai.services.impl.xoai.BaseLRFilterResolver.java

@Override
public Filter getFilter(Class<? extends Filter> filterClass, ParameterMap configuration) {
    if (filterClass.isAssignableFrom(LRAtLeastOneMetadataFilter.class)) {
        return new LRAtLeastOneMetadataFilter(configuration);
    } else if (filterClass.isAssignableFrom(LRAuthorizationFilter.class)) {
        try {/*from   w w  w  .  j  av a 2s  . c  o  m*/
            return new LRAuthorizationFilter(contextService.getContext());
        } catch (ContextServiceException e) {
            LOGGER.error(e.getMessage(), e);
            return null;
        }
    } else if (filterClass.isAssignableFrom(LRMetadataExistsFilter.class)) {
        return new LRMetadataExistsFilter(configuration);
    }
    LOGGER.error("Filter " + filterClass.getName() + " unknown instantiation");
    return null;
}

From source file:br.com.suricattus.surispring.jsf.exception.PrettyExceptionHandler.java

protected boolean handleException(Iterator<ExceptionQueuedEvent> it, Throwable t,
        Class<? extends Throwable> type, String message) {
    if (type.isAssignableFrom(t.getClass())) {
        try {//from   w  w w .  ja v a2  s.  c  om
            if (!catched) {
                FacesContext facesContext = FacesUtils.getContext();

                FacesUtils.addMsgErro(message);
                messagesUtils.saveMessages(facesContext, facesContext.getExternalContext().getSessionMap());

                facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, null,
                        "pretty:error");
                facesContext.renderResponse();

                return true;
            }
        } finally {
            catched = true;
            logger.log(Level.SEVERE, t.getLocalizedMessage(), t);
            it.remove();
        }
    }
    return false;
}

From source file:com.dbs.sdwt.jpa.PropertySelector.java

public boolean isType(Class<?> type) {
    return type.isAssignableFrom(getAttributes().get(getAttributes().size() - 1).getJavaType());
}

From source file:net.sf.morph.integration.commons.collections.DecoratedConverterToTransformerAdapter.java

private Class determineDestinationClass(Object source) {
    if (!ObjectUtils.isEmpty(destinationClassMap)) {
        for (Iterator it = destinationClassMap.keySet().iterator(); it.hasNext();) {
            Class c = (Class) it.next();
            if (c.isAssignableFrom(source.getClass())) {
                return (Class) destinationClassMap.get(c);
            }//from ww w  . j  a  v  a  2  s . c o m
        }
    }
    return destinationClass == null ? Object.class : destinationClass;
}

From source file:TypeConversionHelper.java

/**
 * Convert the value to a instance of the given type. The value converted only
 * if the type can't be assigned from the current type of the value instance. 
 * @param value the value to be converted
 * @param type the type of the expected object returned from the coversion
 * @return the converted object, or null if the object can't be converted
 *///w w w.  j  ava  2s  .co m
public static Object convertTo(Object value, Class type) {
    //check if the id can be assigned for the field type, otherwise convert the id to the appropriate type
    if (!type.isAssignableFrom(value.getClass())) {
        if (type == short.class || type == Short.class) {
            return new Short(value.toString());
        } else if (type == char.class || type == Character.class) {
            return new Character(value.toString().charAt(0));
        } else if (type == int.class || type == Integer.class) {
            return new Integer(value.toString());
        } else if (type == long.class || type == Long.class) {
            return new Long(value.toString());
        } else if (type == boolean.class || type == Boolean.class) {
            return new Boolean(value.toString());
        } else if (type == byte.class || type == Byte.class) {
            return new Byte(value.toString());
        } else if (type == float.class || type == Float.class) {
            return new Float(value.toString());
        } else if (type == double.class || type == Double.class) {
            return new Double(value.toString());
        } else if (type == BigDecimal.class) {
            return new BigDecimal(value.toString());
        } else if (type == BigInteger.class) {
            return new BigInteger(value.toString());
        } else if (type == String.class) {
            return value.toString();
        } else {
            return null;
        }
    }
    return value;
}

From source file:net.kaczmarzyk.spring.data.jpa.web.AnnotatedSpecInterfaceArgumentResolver.java

private Object getAnnotation(Annotation[] parameterAnnotations) {
    for (Annotation annotation : parameterAnnotations) {
        for (Class<? extends Annotation> annotationType : annotationTypes) {
            if (annotationType.isAssignableFrom(annotation.getClass())) {
                return annotation;
            }//from   w w  w . j a v a2s.c o m
        }
    }
    return null;
}

From source file:de.micromata.genome.util.runtime.ClassUtils.java

/**
 * Looks if generic supertype is paramized with given baseRequested.
 *
 * @param <T> the generic type//from   ww w  . j  a v a 2  s. c  o  m
 * @param genericSuperclass the generic superclass
 * @param baseRequested the base requested
 * @return the generic type argument from generic super type. null if not found.
 */
//CHECKSTYLE.OFF FinalParameter Precondition cast
public static <T> Class<T> getGenericTypeArgumentFromGenericSuperType(Type genericSuperclass,
        Class<T> baseRequested) {
    Type loopVar = genericSuperclass;
    while (loopVar != null && !(ParameterizedType.class.isAssignableFrom(loopVar.getClass()))) {
        loopVar = ((Class<?>) loopVar).getGenericSuperclass();
    }
    if (loopVar != null && ParameterizedType.class.isAssignableFrom(loopVar.getClass())) {
        Type[] typeArgs = ((ParameterizedType) loopVar).getActualTypeArguments();
        for (Type typeArg : typeArgs) {
            if (typeArg instanceof ParameterizedType) {
                typeArg = ((ParameterizedType) typeArg).getRawType();
            }
            if ((typeArg instanceof Class) == false) {
                continue;
            }
            if (baseRequested.isAssignableFrom((Class<?>) typeArg) == false) {
                continue;
            }
            return (Class<T>) typeArg;
        }
    }
    return null;
}

From source file:com.reignite.messaging.server.SpringInitializedDestination.java

@Override
public Service getService(String operation, Object[] params) {
    List<Method> methods = methodMap.get(operation);
    int paramLength = params == null ? 0 : params.length;
    Service service = null;/*from   www  . j  ava2s.  c o  m*/
    if (methods != null) {
        for (Method method : methods) {
            if (method.getParameterTypes().length == paramLength) {
                Class<?>[] methodTypes = method.getParameterTypes();
                int matches = 0;
                Object[] newParams = new Object[paramLength];
                for (int i = 0; i < paramLength; i++) {
                    Object param = params[i];
                    Class<?> requiredClass = methodTypes[i];
                    if (param == null || requiredClass.isAssignableFrom(param.getClass())) {
                        matches++;
                        newParams[i] = param;
                    } else {
                        param = convertParam(param, requiredClass);
                        if (param == null || requiredClass.isAssignableFrom(param.getClass())) {
                            matches++;
                            newParams[i] = param;
                        }
                    }
                }
                if (matches == paramLength) {
                    service = new RAMFService(target, method, newParams);
                    break;
                }
            }
        }
    }
    return service;
}

From source file:de.unioninvestment.eai.portal.portlet.crud.scripting.domain.container.rest.ValueConverter.java

private Object convertNumberToNumber(Class<?> targetClass, Number number) {
    if (targetClass.isAssignableFrom(number.getClass())) {
        return number;
    } else if (targetClass == Integer.class) {
        return number.intValue();
    } else if (targetClass == Long.class) {
        return number.longValue();
    } else if (targetClass == Double.class) {
        return number.doubleValue();
    } else if (targetClass == BigDecimal.class) {
        return new BigDecimal(number.toString());
    }/*from  w ww .j  a v a 2 s  .  c  o  m*/
    throw new IllegalArgumentException("Cannot convert to " + targetClass.getSimpleName() + ": " + number
            + " of type " + number.getClass().getName());
}