Example usage for java.lang Class isInstance

List of usage examples for java.lang Class isInstance

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public native boolean isInstance(Object obj);

Source Link

Document

Determines if the specified Object is assignment-compatible with the object represented by this Class .

Usage

From source file:org.web4thejob.web.util.ZkUtil.java

@SuppressWarnings("rawtypes")
public static Entity getLookupSelectionIfUnique(Command command,
        Class<? extends LookupCommandDecorator> listenerType) {
    if (command == null)
        return null;

    Entity entity = null;/*from  w ww .ja  v a 2 s.  co  m*/
    boolean found = false;
    for (MessageAware listener : command.getListeners()) {
        if (listenerType.isInstance(listener)) {
            if (!found) {
                entity = ((LookupCommandDecorator) listener).getLookupSelection();
                found = true;
            } else {
                throw new IllegalStateException(
                        "more than one command decorators were found for command " + command.getId());
            }
        }
    }

    return entity;
}

From source file:hudson.Util.java

/**
 * Creates a filtered sublist./*from   w  w  w.ja  v a  2s  .  c  o  m*/
 * @since 1.176
 */
public static <T> List<T> filter(Iterable<?> base, Class<T> type) {
    List<T> r = new ArrayList<T>();
    for (Object i : base) {
        if (type.isInstance(i))
            r.add(type.cast(i));
    }
    return r;
}

From source file:hudson.Util.java

/**
 * Create a sub-list by only picking up instances of the specified type.
 *//*from ww  w  .  j  a  v a  2s  .c o m*/
public static <T> List<T> createSubList(Collection<?> source, Class<T> type) {
    List<T> r = new ArrayList<T>();
    for (Object item : source) {
        if (type.isInstance(item))
            r.add(type.cast(item));
    }
    return r;
}

From source file:org.red5.server.util.ScopeUtils.java

public static Object getScopeService(IScope scope, Class<?> intf, Class<?> defaultClass, boolean checkHandler) {
    if (scope == null || intf == null) {
        return null;
    }/*from w w w.  ja  v  a 2  s .c  om*/
    // We expect an interface
    assert intf.isInterface();

    String attr = IPersistable.TRANSIENT_PREFIX + SERVICE_CACHE_PREFIX + intf.getCanonicalName();
    if (scope.hasAttribute(attr)) {
        // return cached service
        return scope.getAttribute(attr);
    }

    Object handler = null;
    if (checkHandler) {
        IScope current = scope;
        while (current != null) {
            IScopeHandler scopeHandler = current.getHandler();
            if (intf.isInstance(scopeHandler)) {
                handler = scopeHandler;
                break;
            }
            if (!current.hasParent()) {
                break;
            }
            current = current.getParent();
        }
    }

    if (handler == null && IScopeService.class.isAssignableFrom(intf)) {
        // we've got an IScopeService, try to lookup bean
        Field key = null;
        Object serviceName = null;
        try {
            key = intf.getField("BEAN_NAME");
            serviceName = key.get(null);
            if (serviceName instanceof String) {
                handler = getScopeService(scope, (String) serviceName, defaultClass);
            }
        } catch (Exception e) {
            log.debug("No string field 'BEAN_NAME' in that interface");
        }
    }
    if (handler == null && defaultClass != null) {
        try {
            handler = defaultClass.newInstance();
        } catch (Exception e) {
            log.error("", e);
        }
    }
    // cache service
    scope.setAttribute(attr, handler);
    return handler;
}

From source file:org.red5.server.api.ScopeUtils.java

public static Object getScopeService(IScope scope, Class intf, Class defaultClass, boolean checkHandler) {
    if (scope == null || intf == null) {
        return null;
    }//from   w  w w  .jav a2 s  .c  o  m
    // We expect an interface
    assert intf.isInterface();

    if (scope.hasAttribute(IPersistable.TRANSIENT_PREFIX + SERVICE_CACHE_PREFIX + intf.getCanonicalName())) {
        // Return cached service
        return scope
                .getAttribute(IPersistable.TRANSIENT_PREFIX + SERVICE_CACHE_PREFIX + intf.getCanonicalName());
    }

    Object handler = null;
    if (checkHandler) {
        IScope current = scope;
        while (current != null) {
            IScopeHandler scopeHandler = current.getHandler();
            if (intf.isInstance(scopeHandler)) {
                handler = scopeHandler;
                break;
            }

            if (!current.hasParent())
                break;

            current = current.getParent();
        }
    }

    if (handler == null && IScopeService.class.isAssignableFrom(intf)) {
        // We got an IScopeService, try to lookup bean
        Field key = null;
        Object serviceName = null;
        try {
            key = intf.getField("BEAN_NAME");
            serviceName = key.get(null);
            if (!(serviceName instanceof String))
                serviceName = null;
        } catch (Exception e) {
            log.debug("No string field 'BEAN_NAME' in that interface");
        }

        if (serviceName != null)
            handler = getScopeService(scope, (String) serviceName, defaultClass);
    }

    if (handler == null && defaultClass != null) {
        try {
            handler = defaultClass.newInstance();
        } catch (Exception e) {
            log.error(e);
        }
    }

    // Cache service
    scope.setAttribute(IPersistable.TRANSIENT_PREFIX + SERVICE_CACHE_PREFIX + intf.getCanonicalName(), handler);
    return handler;
}

From source file:com.fasheng.queue.receiver.AbstractMQReceiverStarter.java

protected boolean satisfiedEventType(ApplicationEvent event) {
    Class<?> clazz = getDesiredType();
    return clazz.isInstance(event);
}

From source file:io.ucoin.ucoinj.web.rest.RestResponseEntityExceptionHandler.java

private Throwable getCause(Throwable e, Class... classes) {
    for (Class clazz : classes) {
        if (clazz.isInstance(e)) {
            return e;
        }/*  w  w  w  .j  av a 2 s. co m*/
    }
    if (e.getCause() != null) {
        return getCause(e.getCause(), classes);
    }
    return null;
}

From source file:dk.clanie.aop.interceptor.ExceptionWrapperInterceptor.java

public Object invoke(MethodInvocation invocation) throws Throwable {
    try {//  w w w . ja v  a2 s .c  o m
        Object returnValue = invocation.proceed();
        return returnValue;
    } catch (Throwable ex) {
        for (Class<? extends Throwable> acceptedClass : accepted) {
            if (acceptedClass.isInstance(ex))
                throw ex;
        }
        throw new WrappedException(ex);
    }
}

From source file:com.github.jknack.css.expression.AbstractExpression.java

@Override
public <E extends Expression> E adapt(final Class<E> expressionType) {
    notNull(expressionType, "The expressionType is required.");
    if (expressionType.isInstance(this)) {
        return expressionType.cast(this);
    }/* w  w  w.j a v  a 2  s . co  m*/
    return null;
}

From source file:org.echocat.jemoni.jmx.support.SpringUtils.java

@Nonnull
public static <T> T getBeanFor(@Nonnull ServletContext servletContext, @Nonnull String beanName,
        @Nonnull Class<T> beanType) throws ServletException {
    final Object applicationContext = getApplicationContext(servletContext);
    final Object plainBean;
    try {/*from  w ww  . ja va 2  s  .  co m*/
        plainBean = beanType.cast(GET_BEAN.invoke(applicationContext, beanName));
    } catch (Exception e) {
        final Throwable target = e instanceof InvocationTargetException
                ? ((InvocationTargetException) e).getTargetException()
                : null;
        if (target != null && target.getClass().getName().endsWith("NoSuchBeanDefinitionException")) {
            throw new ServletException("Could not find bean '" + beanName + "' at " + applicationContext + ".",
                    target);
        } else {
            throw new ServletException(
                    "Could not retrieve bean '" + beanName + "' from " + applicationContext + ".",
                    target != null ? target : e);
        }
    }
    if (!beanType.isInstance(plainBean)) {
        throw new ServletException("Could bean '" + beanName + "' is of type " + plainBean.getClass().getName()
                + " not of expected " + beanType.getName() + ".");
    }
    return beanType.cast(plainBean);
}