Example usage for java.lang Class isInterface

List of usage examples for java.lang Class isInterface

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public native boolean isInterface();

Source Link

Document

Determines if the specified Class object represents an interface type.

Usage

From source file:com.alibaba.wasp.ipc.WaspRPC.java

private static synchronized RpcEngine getProtocolEngine(Class protocol, Configuration conf) {
    RpcEngine engine = PROTOCOL_ENGINES.get(protocol);
    if (engine == null) {
        // check for a configured default engine
        Class<?> defaultEngine = conf.getClass(RPC_ENGINE_PROP, ProtobufRpcEngine.class);

        // check for a per interface override
        Class<?> impl = conf.getClass(RPC_ENGINE_PROP + "." + protocol.getName(), defaultEngine);
        LOG.debug("Using " + impl.getName() + " for " + protocol.getName());
        engine = (RpcEngine) ReflectionUtils.newInstance(impl, conf);
        if (protocol.isInterface())
            PROXY_ENGINES.put(Proxy.getProxyClass(protocol.getClassLoader(), protocol), engine);
        PROTOCOL_ENGINES.put(protocol, engine);
    }/* w w  w .  j  a  va2  s .  c  om*/
    return engine;
}

From source file:net.jodah.typetools.TypeResolver.java

/**
 * Returns the generic {@code type} using type variable information from the {@code subType} else {@code null} if the
 * generic type cannot be resolved./*from   w  w  w.j  a va  2 s.c  o m*/
 *
 * @param type to resolve generic type for
 * @param subType to extract type variable information from
 * @return generic {@code type} else {@code null} if it cannot be resolved
 */
public static Type resolveGenericType(Class<?> type, Type subType) {
    Class<?> rawType;
    if (subType instanceof ParameterizedType)
        rawType = (Class<?>) ((ParameterizedType) subType).getRawType();
    else
        rawType = (Class<?>) subType;

    if (type.equals(rawType))
        return subType;

    Type result;
    if (type.isInterface()) {
        for (Type superInterface : rawType.getGenericInterfaces())
            if (superInterface != null && !superInterface.equals(Object.class))
                if ((result = resolveGenericType(type, superInterface)) != null)
                    return result;
    }

    Type superClass = rawType.getGenericSuperclass();
    if (superClass != null && !superClass.equals(Object.class))
        if ((result = resolveGenericType(type, superClass)) != null)
            return result;

    return null;
}

From source file:net.jodah.typetools.TypeResolver.java

/**
 * Returns an array of raw classes representing arguments for the {@code genericType} using type variable information
 * from the {@code subType}. Arguments for {@code genericType} that cannot be resolved are returned as
 * {@code Unknown.class}. If no arguments can be resolved then {@code null} is returned.
 *
 * @param genericType to resolve arguments for
 * @param subType to extract type variable information from
 * @return array of raw classes representing arguments for the {@code genericType} else {@code null} if no type
 *         arguments are declared/*from  ww  w . j  a v a2 s.  co  m*/
 */
public static Class<?>[] resolveRawArguments(Type genericType, Class<?> subType) {
    Class<?>[] result = null;
    Class<?> functionalInterface = null;

    // Handle lambdas
    if (SUPPORTS_LAMBDAS && subType.isSynthetic()) {
        Class<?> fi = genericType instanceof ParameterizedType
                && ((ParameterizedType) genericType).getRawType() instanceof Class
                        ? (Class<?>) ((ParameterizedType) genericType).getRawType()
                        : genericType instanceof Class ? (Class<?>) genericType : null;
        if (fi != null && fi.isInterface())
            functionalInterface = fi;
    }

    if (genericType instanceof ParameterizedType) {
        ParameterizedType paramType = (ParameterizedType) genericType;
        Type[] arguments = paramType.getActualTypeArguments();
        result = new Class[arguments.length];
        for (int i = 0; i < arguments.length; i++)
            result[i] = resolveRawClass(arguments[i], subType, functionalInterface);
    } else if (genericType instanceof TypeVariable) {
        result = new Class[1];
        result[0] = resolveRawClass(genericType, subType, functionalInterface);
    } else if (genericType instanceof Class) {
        TypeVariable<?>[] typeParams = ((Class<?>) genericType).getTypeParameters();
        result = new Class[typeParams.length];
        for (int i = 0; i < typeParams.length; i++)
            result[i] = resolveRawClass(typeParams[i], subType, functionalInterface);
    }

    return result;
}

From source file:com.github.dozermapper.core.util.ReflectionUtils.java

public static PropertyDescriptor[] getPropertyDescriptors(Class<?> objectClass) {
    // If the class is an interface, use custom method to get all prop descriptors in the inheritance hierarchy.
    // PropertyUtils.getPropertyDescriptors() does not work correctly for interface inheritance. It finds props in the
    // actual interface ok, but does not find props in the inheritance hierarchy.
    if (objectClass.isInterface()) {
        return getInterfacePropertyDescriptors(objectClass);
    } else {/*from  w ww .j  a va  2s .com*/
        return PropertyUtils.getPropertyDescriptors(objectClass);
    }
}

From source file:org.gradle.internal.reflect.JavaPropertyReflectionUtil.java

private static <A extends Annotation> A getAnnotation(Class<?> type, Class<A> annotationType,
        boolean checkType) {
    A annotation;/*from w ww.j a v a 2s  .c  om*/
    if (checkType) {
        annotation = type.getAnnotation(annotationType);
        if (annotation != null) {
            return annotation;
        }
    }

    if (annotationType.getAnnotation(Inherited.class) != null) {
        for (Class<?> anInterface : type.getInterfaces()) {
            annotation = getAnnotation(anInterface, annotationType, true);
            if (annotation != null) {
                return annotation;
            }
        }
    }

    if (type.isInterface() || type.equals(Object.class)) {
        return null;
    } else {
        return getAnnotation(type.getSuperclass(), annotationType, false);
    }
}

From source file:br.msf.commons.mocca.ajax.AbstractAjaxService.java

private static void validateTargetClass(final Class<?> targetClass) {
    ArgumentUtils.rejectIfNull(targetClass);
    if (targetClass.isInterface()) {
        throw new IllegalArgumentException("targetClass cannot be an interface.");
    } else if (targetClass.isArray()) {
        throw new IllegalArgumentException("targetClass cannot be an array.");
    } else if (Collection.class.isAssignableFrom(targetClass) || Map.class.isAssignableFrom(targetClass)) {
        throw new IllegalArgumentException("targetClass cannot be a collection.");
    }/*w  w  w  .jav  a 2 s . co  m*/
}

From source file:org.apache.hadoop.hbase.ipc.HBaseClientRPC.java

static synchronized RpcClientEngine getProtocolEngine(Class<? extends IpcProtocol> protocol,
        Configuration conf) {//  w ww. ja v  a  2 s  .  c  om
    RpcClientEngine engine = PROTOCOL_ENGINES.get(protocol);
    if (engine == null) {
        // check for a configured default engine
        Class<?> defaultEngine = conf.getClass(RPC_ENGINE_PROP, ProtobufRpcClientEngine.class);

        // check for a per interface override
        Class<?> impl = conf.getClass(RPC_ENGINE_PROP + "." + protocol.getName(), defaultEngine);
        LOG.debug("Using " + impl.getName() + " for " + protocol.getName());
        engine = (RpcClientEngine) ReflectionUtils.newInstance(impl, conf);
        if (protocol.isInterface()) {
            PROXY_ENGINES.put(Proxy.getProxyClass(protocol.getClassLoader(), protocol), engine);
        }
        PROTOCOL_ENGINES.put(protocol, engine);
    }
    return engine;
}

From source file:adalid.core.XS1.java

private static Class<?> getFieldType(Class<?> fieldType) {
    if (fieldType.isInterface()) {
        if (Expression.class.isAssignableFrom(fieldType)) {
            if (EntityExpression.class.isAssignableFrom(fieldType)) {
                return EntityX.class;
            } else if (BooleanExpression.class.isAssignableFrom(fieldType)) {
                return BooleanX.class;
            } else if (CharacterExpression.class.isAssignableFrom(fieldType)) {
                return CharacterX.class;
            } else if (NumericExpression.class.isAssignableFrom(fieldType)) {
                return NumericX.class;
            } else if (TemporalExpression.class.isAssignableFrom(fieldType)) {
                return TemporalX.class;
            } else {
                return VariantX.class;
            }//  w  ww.  j  a  v  a  2 s  .c  o  m
        }
    }
    return isRestrictedFieldType(fieldType) ? null : fieldType;
}

From source file:com.opensource.frameworks.processframework.utils.MethodInvoker.java

/**
 * Algorithm that judges the match between the declared parameter types of a candidate method
 * and a specific list of arguments that this method is supposed to be invoked with.
 * <p>Determines a weight that represents the class hierarchy difference between types and
 * arguments. A direct match, i.e. type Integer -> arg of class Integer, does not increase
 * the result - all direct matches means weight 0. A match between type Object and arg of
 * class Integer would increase the weight by 2, due to the superclass 2 steps up in the
 * hierarchy (i.e. Object) being the last one that still matches the required type Object.
 * Type Number and class Integer would increase the weight by 1 accordingly, due to the
 * superclass 1 step up the hierarchy (i.e. Number) still matching the required type Number.
 * Therefore, with an arg of type Integer, a constructor (Integer) would be preferred to a
 * constructor (Number) which would in turn be preferred to a constructor (Object).
 * All argument weights get accumulated.
 * @param paramTypes the parameter types to match
 * @param args the arguments to match//from  ww  w  . j  ava2s . c  om
 * @return the accumulated weight for all arguments
 */
public static int getTypeDifferenceWeight(Class[] paramTypes, Object[] args) {
    int result = 0;
    for (int i = 0; i < paramTypes.length; i++) {
        if (!ClassUtils.isAssignableValue(paramTypes[i], args[i])) {
            return Integer.MAX_VALUE;
        }
        if (args[i] != null) {
            Class paramType = paramTypes[i];
            Class superClass = args[i].getClass().getSuperclass();
            while (superClass != null) {
                if (paramType.equals(superClass)) {
                    result = result + 2;
                    superClass = null;
                } else if (ClassUtils.isAssignable(paramType, superClass)) {
                    result = result + 2;
                    superClass = superClass.getSuperclass();
                } else {
                    superClass = null;
                }
            }
            if (paramType.isInterface()) {
                result = result + 1;
            }
        }
    }
    return result;
}

From source file:org.jboss.dashboard.commons.misc.ReflectionUtils.java

public static List<Field> getClassFields(Class clazz, Class type, boolean isStatic, String[] fieldsToIgnore) {
    List<Field> results = new ArrayList<Field>();
    if (clazz == null)
        return results;
    if (clazz.isPrimitive())
        return results;
    if (clazz.isAnnotation())
        return results;
    if (clazz.isInterface())
        return results;
    if (clazz.isEnum())
        return results;

    Collection<String> toIgnore = fieldsToIgnore != null ? Arrays.asList(fieldsToIgnore)
            : Collections.EMPTY_SET;
    Field[] fields = clazz.getDeclaredFields();

    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];//from   w ww  .  j  ava 2s. c  om
        if (toIgnore.contains(field.getName()))
            continue;
        if (isStatic && !Modifier.isStatic(field.getModifiers()))
            continue;
        if (!isStatic && Modifier.isStatic(field.getModifiers()))
            continue;
        if (type != null && !field.getType().equals(type))
            continue;
        results.add(field);
    }
    return results;
}