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:Main.java

private final static Type resolveTypeVariable(TypeVariable<? extends GenericDeclaration> type,
        Class<?> declaringClass, Class<?> inClass) {

    if (inClass == null)
        return null;

    Class<?> superClass = null;
    Type resolvedType = null;/* w  w  w  .  ja v a 2  s.c  om*/
    Type genericSuperClass = null;

    if (!declaringClass.equals(inClass)) {
        if (declaringClass.isInterface()) {
            // the declaringClass is an interface
            Class<?>[] interfaces = inClass.getInterfaces();
            for (int i = 0; i < interfaces.length && resolvedType == null; i++) {
                superClass = interfaces[i];
                resolvedType = resolveTypeVariable(type, declaringClass, superClass);
                genericSuperClass = inClass.getGenericInterfaces()[i];
            }
        }

        if (resolvedType == null) {
            superClass = inClass.getSuperclass();
            resolvedType = resolveTypeVariable(type, declaringClass, superClass);
            genericSuperClass = inClass.getGenericSuperclass();
        }
    } else {
        resolvedType = type;
        genericSuperClass = superClass = inClass;

    }

    if (resolvedType != null) {
        // if its another type this means we have finished
        if (resolvedType instanceof TypeVariable<?>) {
            type = (TypeVariable<?>) resolvedType;
            TypeVariable<?>[] parameters = superClass.getTypeParameters();
            int positionInClass = 0;
            for (; positionInClass < parameters.length
                    && !type.equals(parameters[positionInClass]); positionInClass++) {
            }

            // we located the position of the typevariable in the superclass
            if (positionInClass < parameters.length) {
                // let's look if we have type specialization information in the current class
                if (genericSuperClass instanceof ParameterizedType) {
                    ParameterizedType pGenericType = (ParameterizedType) genericSuperClass;
                    Type[] args = pGenericType.getActualTypeArguments();
                    return positionInClass < args.length ? args[positionInClass] : null;
                }
            }

            // we didnt find typevariable specialization in the class, so it's the best we can
            // do, lets return the resolvedType...
        }
    }

    return resolvedType;
}

From source file:name.ikysil.beanpathdsl.dynamic.DynamicBeanPath.java

/**
 * Continue bean path construction from a bean of specified class.
 *
 * @param <T> type of the bean//  ww  w.j  av  a  2s .c o  m
 * @param clazz class of the bean
 * @return instance of the bean class instrumented to capture getters and setters invocations
 */
@SuppressWarnings("unchecked")
public static <T> T expr(Class<T> clazz) {
    try {
        T result;
        if (Modifier.isFinal(clazz.getModifiers())) {
            result = clazz.newInstance();
        } else {
            ProxyFactory pf = new ProxyFactory();
            if (clazz.isInterface()) {
                pf.setSuperclass(Object.class);
                pf.setInterfaces(new Class<?>[] { clazz });
            } else {
                pf.setSuperclass(clazz);
            }
            pf.setFilter(new DefaultMethodFilter());
            result = (T) pf.create(new Class<?>[0], new Object[0], new DefaultMethodHandler(clazz));
        }
        return result;
    } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | IllegalArgumentException
            | InvocationTargetException ex) {
        throw new IllegalStateException(String.format("Can't instantiate %s", clazz.getName()), ex);
    }
}

From source file:ClassFinder.java

/**
 * /*from w w  w  .  j  av a  2  s.  c om*/
 * @param parentClasses
 *            list of classes to check for
 * @param strClassName
 *            name of class to be checked
 * @param contextClassLoader
 *            the classloader to use
 * @return
 */
private static boolean isChildOf(Class[] parentClasses, String strClassName, ClassLoader contextClassLoader) {
    // might throw an exception, assume this is ignorable
    try {
        Class c = Class.forName(strClassName, false, contextClassLoader);

        if (!c.isInterface() && !Modifier.isAbstract(c.getModifiers())) {
            for (Class parentClass : parentClasses) {
                if (parentClass.isAssignableFrom(c)) {
                    return true;
                }
            }
        }
    } catch (Throwable ignored) {

    }
    return false;
}

From source file:org.jdto.util.MemberUtils.java

/**
 * Gets the number of steps required needed to turn the source class into
 * the destination class. This represents the number of steps in the object
 * hierarchy graph./*  w  w w .jav  a 2  s.c om*/
 *
 * @param srcClass The source class
 * @param destClass The destination class
 * @return The cost of transforming an object
 */
private static float getObjectTransformationCost(Class srcClass, Class destClass) {
    if (destClass.isPrimitive()) {
        return getPrimitivePromotionCost(srcClass, destClass);
    }
    float cost = 0.0f;
    while (srcClass != null && !destClass.equals(srcClass)) {
        if (destClass.isInterface() && ClassUtils.isAssignable(srcClass, destClass)) {
            // slight penalty for interface match.
            // we still want an exact match to override an interface match,
            // but
            // an interface match should override anything where we have to
            // get a superclass.
            cost += 0.25f;
            break;
        }
        cost++;
        srcClass = srcClass.getSuperclass();
    }
    /*
     * If the destination class is null, we've travelled all the way up to
     * an Object match. We'll penalize this by adding 1.5 to the cost.
     */
    if (srcClass == null) {
        cost += 1.5f;
    }
    return cost;
}

From source file:Main.java

public static ParameterizedType resolveParameterizedType(Type t, Class<?> baseClass) {
    Class<?> raw = getRawType(t);

    if (t instanceof ParameterizedType && baseClass.isAssignableFrom(raw)) {
        return (ParameterizedType) t;
    }/*from w w w  .j a  va  2s.  co m*/

    ParameterizedType pt = null;
    if (raw.getSuperclass() != null && raw.getSuperclass() != Object.class) {
        pt = resolveParameterizedType(raw.getGenericSuperclass(), baseClass);
        if (pt != null)
            return pt;
    }
    if (!raw.isInterface()) {
        for (Type ifs : raw.getGenericInterfaces()) {
            pt = resolveParameterizedType(ifs, baseClass);
            if (pt != null)
                return pt;
        }
    }
    return null;
}

From source file:com.iisigroup.cap.utils.CapBeanUtil.java

public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes) {
    Assert.notNull(clazz, "Class must not be null");
    Assert.notNull(name, "Method name must not be null");
    Class<?> searchType = clazz;
    while (searchType != null) {
        Method[] methods = (searchType.isInterface() ? searchType.getMethods()
                : searchType.getDeclaredMethods());
        for (Method method : methods) {
            if (name.equals(method.getName()) && (paramTypes == null || paramTypes.length == 0
                    || paramTypes[0] == null || Arrays.equals(paramTypes, method.getParameterTypes()))) {
                return method;
            }/*ww  w.  j  a va 2 s .  c om*/
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

From source file:com.springframework.beans.BeanUtils.java

/**
 * Instantiate a class using its no-arg constructor.
 * As this method doesn't try to load classes by name, it should avoid
 * class-loading issues.//from  w w  w.  j ava2 s.c  o  m
 * <p>Note that this method tries to set the constructor accessible
 * if given a non-accessible (that is, non-public) constructor.
 * @param clazz class to instantiate
 * @return the new instance
 * @throws BeanInstantiationException if the bean cannot be instantiated
 */
public static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationException {
    Assert.notNull(clazz, "Class must not be null");
    if (clazz.isInterface()) {
        throw new BeanInstantiationException(clazz, "Specified class is an interface");
    }
    try {
        return instantiateClass(clazz.getDeclaredConstructor());
    } catch (NoSuchMethodException ex) {
        throw new BeanInstantiationException(clazz, "No default constructor found", ex);
    }
}

From source file:com.springframework.beans.BeanUtils.java

/**
 * Convenience method to instantiate a class using its no-arg constructor.
 * As this method doesn't try to load classes by name, it should avoid
 * class-loading issues./*  w ww.  j  a  v a  2  s  .  c  o m*/
 * @param clazz class to instantiate
 * @return the new instance
 * @throws BeanInstantiationException if the bean cannot be instantiated
 */
public static <T> T instantiate(Class<T> clazz) throws BeanInstantiationException {
    Assert.notNull(clazz, "Class must not be null");
    if (clazz.isInterface()) {
        throw new BeanInstantiationException(clazz, "Specified class is an interface");
    }
    try {
        return clazz.newInstance();
    } catch (InstantiationException ex) {
        throw new BeanInstantiationException(clazz, "Is it an abstract class?", ex);
    } catch (IllegalAccessException ex) {
        throw new BeanInstantiationException(clazz, "Is the constructor accessible?", ex);
    }
}

From source file:org.eiichiro.bootleg.Types.java

/**
 * Returns the default implementation type of the specified collection 
 * interface type./*from  w ww .  j  a v a  2 s .com*/
 * The specified type must be an supported collection. To make it sure, use 
 * {@code #isSupportedCollection(Type)}. If the specified collection type is 
 * <b>not</b> an interface, this method returns the specified implementation 
 * type directly.<br>
 * <br>
 * The default implementations are: 
 * <pre>
 * java.util.Collection -> java.util.ArrayList
 * java.util.List -> java.util.ArrayList
 * java.util.Set -> java.util.HashSet
 * java.util.SortedSet -> java.util.TreeSet
 * java.util.NavigableSet -> java.util.TreeSet
 * java.util.Queue -> java.util.PriorityQueue
 * java.util.Deque -> java.util.ArrayDeque
 * </pre>
 * 
 * @param type Collection type (must be supported type).
 * @return The default implementation type of the specified collection 
 * interface type.
 */
public static Class<?> getDefaultImplementationType(Type type) {
    Class<?> clazz = getRawType(type);
    return (clazz.isInterface()) ? implementations.get(clazz) : clazz;
}

From source file:org.hibernate.internal.util.ReflectHelper.java

/**
 * Determine if the given class implements the given interface.
 *
 * @param clazz The class to check/*from  w ww  .java2s. c om*/
 * @param intf The interface to check it against.
 * @return True if the class does implement the interface, false otherwise.
 */
public static boolean implementsInterface(Class clazz, Class intf) {
    assert intf.isInterface() : "Interface to check was not an interface";
    return intf.isAssignableFrom(clazz);
}