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:com.curl.orb.context.Spring2_5ApplicationContext.java

/** 
 * NOTE://from   ww  w.java  2 s. c o  m
 *  Spring framework would wrap org.springframework.aop.framework.Advised class as naming of "$ProxyNN"
 *  in the case of implementing interface and session object.
 *  The following method is to fix this spring framework issue.
 */
@Override
public Class<?> getProperType(Object obj) throws ApplicationContextException {
    // if (obj instanceof Advised) return ((Advised) obj).getTargetSource().getTargetClass();
    // NOTE: Instead of above remarked codes. (No dependency code on spring framework)
    try {
        Class<?> cls = Class.forName("org.springframework.aop.framework.Advised");
        if (cls.isAssignableFrom(obj.getClass())) {
            Object targetSource = MethodUtils.invokeMethod(obj, "getTargetSource", null);
            return (Class<?>) MethodUtils.invokeMethod(targetSource, "getTargetClass", null);
        }
    } catch (ClassNotFoundException ex) {
        throw new ApplicationContextException(ex);
    } catch (NoSuchMethodException ex) {
        throw new ApplicationContextException(ex);
    } catch (IllegalAccessException ex) {
        throw new ApplicationContextException(ex);
    } catch (InvocationTargetException ex) {
        throw new ApplicationContextException(ex);
    }
    return super.getProperType(obj);
}

From source file:org.kantega.dogmaticmvc.web.DefaultMethodParameterFactory.java

private <T extends Annotation> T getParamAnnotation(Method method, int i, Class<T> annotation) {
    for (Annotation a : method.getParameterAnnotations()[i]) {
        if (annotation.isAssignableFrom(a.getClass())) {
            return (T) a;
        }//from www.  jav a 2  s . co  m
    }
    return null;
}

From source file:net.centro.rtb.monitoringcenter.MonitoringCenter.java

@SuppressWarnings("unchecked")
private static <T extends Metric> SortedMap<String, T> getMetricsByNames(boolean appendPrefix,
        String[] startsWithFilters, Class<T> metricClass) {
    if (!configured.get()) {
        return (SortedMap<String, T>) EMPTY_METRIC_MAP;
    }//www.  java  2s .co  m

    SortedMap<String, T> metricsByNames = new TreeMap<>();
    for (Map.Entry<String, Metric> entry : metricRegistry.getMetrics().entrySet()) {
        if (metricClass == null || metricClass.isAssignableFrom(entry.getValue().getClass())) {
            if (startsWithFilters == null || matchesStartsWithFilters(entry.getKey(), startsWithFilters)) {
                String metricName = (appendPrefix ? prefix + MetricNamingUtil.SEPARATOR : StringUtils.EMPTY)
                        + entry.getKey();
                metricsByNames.put(metricName, (T) entry.getValue());
            }
        }
    }
    return metricsByNames;
}

From source file:org.nabucco.alfresco.enhScriptEnv.common.script.converter.rhino.ScriptableFacadeMapConverter.java

/**
 * {@inheritDoc}// w  w  w .j  a va2  s  .  c  om
 */
@Override
public boolean canConvertValueForScript(final Object value, final ValueConverter globalDelegate,
        final Class<?> expectedClass) {
    final boolean canConvert = value instanceof Map<?, ?> && expectedClass.isAssignableFrom(Scriptable.class);
    return canConvert;
}

From source file:kenh.expl.impl.BaseFunction.java

/**
 * Find the method with name <code>process</code> or <code>@Processing</code>
 *///w  w  w.j  a v a2  s .  c om
@Override
public Object invoke(Object... params) throws UnsupportedExpressionException {
    Method[] methods = this.getClass().getMethods();

    for (Method method : methods) {
        String name = method.getName();
        Class[] classes = method.getParameterTypes();
        Annotation a = method.getAnnotation(Processing.class);

        if ((name.equals(METHOD) || a != null) && params.length == classes.length) {

            logger.trace("Method: " + method.toGenericString());
            boolean find = true;
            Object[] objs = new Object[params.length];
            for (int i = 0; i < params.length; i++) {
                Class class1 = params[i].getClass();
                Class class2 = classes[i];

                if (class2.isAssignableFrom(class1) || class2 == Object.class) {
                    objs[i] = params[i];

                } else if (class1 == String.class) {
                    try {
                        Object obj = Environment.convert((String) params[i], class2);
                        if (obj == null) {
                            logger.trace("Failure(Convert failure[" + (i + 1) + "-" + class1 + "," + class2
                                    + "]): " + method.toGenericString());
                            find = false;
                            break;
                        } else {
                            objs[i] = obj;
                        }
                    } catch (Exception e) {
                        logger.trace("Failure(Convert exception[" + (i + 1) + "-" + e.getMessage() + "]): "
                                + method.toGenericString());
                        find = false;
                        break;
                        //UnsupportedExpressionException ex = new UnsupportedExpressionException(e);
                        //throw ex;
                    }
                } else {
                    logger.trace("Failure(Class unmatched[" + (i + 1) + "]): " + method.toGenericString());
                    find = false;
                    break;
                }
            }
            if (find) {
                try {
                    return method.invoke(this, objs);
                } catch (Exception e) {
                    if (e instanceof UnsupportedExpressionException)
                        throw (UnsupportedExpressionException) e;
                    else
                        throw new UnsupportedExpressionException(e);
                }
            }
        }
    }

    String paramStr = "";
    for (Object param : params) {
        paramStr += param.getClass().getCanonicalName() + ", ";
    }
    paramStr = StringUtils.defaultIfBlank(StringUtils.chop(StringUtils.trimToEmpty(paramStr)), "<NO PARAM>");

    UnsupportedExpressionException e = new UnsupportedExpressionException(
            "Can't find the method to process.[" + paramStr + "]");
    throw e;
}

From source file:com.github.xbn.array.helper.NewPrimitiveArrayHelper.java

public static final PrimitiveArrayHelper<?> forUnknownType(Object obj_thatIsPrimArr) {
    Class<?> cls = null;
    try {/*  w  w w  .  j  a va 2s .c o  m*/
        cls = obj_thatIsPrimArr.getClass();
    } catch (RuntimeException rx) {
        throw CrashIfObject.nullOrReturnCause(obj_thatIsPrimArr, "obj_thatIsPrimArr", null, rx);
    }
    /*
    ---nwrqm input---
    boolean Bool
    double Double
    float Float
    long Long
    int Int
    short Short
    byte Byte
    char Char
     */
    if (cls.isAssignableFrom(boolean[].class)) {
        return forBoolean();
    }
    if (cls.isAssignableFrom(char[].class)) {
        return forCharacter();
    }
    if (cls.isAssignableFrom(byte[].class)) {
        return forByte();
    }
    if (cls.isAssignableFrom(short[].class)) {
        return forShort();
    }
    if (cls.isAssignableFrom(int[].class)) {
        return forInteger();
    }
    if (cls.isAssignableFrom(long[].class)) {
        return forLong();
    }
    if (cls.isAssignableFrom(float[].class)) {
        return forFloat();
    }
    if (cls.isAssignableFrom(double[].class)) {
        return forDouble();
    }

    throw new IllegalArgumentException(
            "obj_thatIsPrimArr is not a primitive array. obj_thatIsPrimArr.getClass().getName()="
                    + obj_thatIsPrimArr.getClass().getName());
}

From source file:info.archinnov.achilles.internals.metamodel.ComputedProperty.java

@Override
VALUEFROM decodeFromRawInternal(Object o) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(format("Decode computed property %s from raw data %s", this.toString(), o));
    }/*from  w  ww . j  a  v  a 2s.  co  m*/
    final Class<?> cqlClass = computedColumnInfo.cqlClass;
    Validator.validateTrue(cqlClass.isAssignableFrom(o.getClass()),
            "The class of object %s to decode should be %s", o, cqlClass.getCanonicalName());
    return valueCodec.decode((VALUETO) o);
}

From source file:org.springmodules.validation.bean.conf.loader.xml.SimpleValidationRuleElementHandlerRegistryTests.java

protected boolean containsHandlerOfType(ClassValidationElementHandler[] handlers, Class handlerType) {
    for (int i = 0; i < handlers.length; i++) {
        if (handlerType.isAssignableFrom(handlerType)) {
            return true;
        }//www. ja va2  s .c o m
    }
    return false;
}

From source file:org.springmodules.validation.bean.conf.loader.xml.SimpleValidationRuleElementHandlerRegistryTests.java

protected boolean containsHandlerOfType(PropertyValidationElementHandler[] handlers, Class handlerType) {
    for (int i = 0; i < handlers.length; i++) {
        if (handlerType.isAssignableFrom(handlerType)) {
            return true;
        }/* w w w.j a  v  a 2  s .c  om*/
    }
    return false;
}

From source file:org.nabucco.alfresco.enhScriptEnv.repo.script.converter.rhino.QNameStoreRefConverter.java

/**
 * {@inheritDoc}/*from  w w w  .  j ava  2 s  . c om*/
 */
@Override
public boolean canConvertValueForScript(final Object value, final ValueConverter globalDelegate,
        final Class<?> expectedClass) {
    final boolean canConvert = (value instanceof StoreRef || value instanceof QName)
            && expectedClass.isAssignableFrom(String.class);
    return canConvert;
}