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:org.jbpm.instance.migration.MigrationUtils.java

private static boolean isConcreteClass(Class type) {
    return !type.isInterface() && !Modifier.isAbstract(type.getModifiers());
}

From source file:org.apache.tajo.util.ClassUtil.java

private static boolean isMatched(Class clazz, Class targetClass, Predicate predicate) {
    return !clazz.isInterface() && (targetClass == null || isClassMatched(targetClass, clazz))
            && (predicate == null || predicate.evaluate(clazz));
}

From source file:jp.furplag.util.commons.ObjectUtils.java

/**
 * substitute for {@link java.lang.Class#newInstance()}.
 *
 * @param typeRef {@link com.fasterxml.jackson.core.type.TypeReference}.
 * @return empty instance of specified {@link java.lang.Class}.
 * @throws InstantiationException/*from  ww  w .  ja  v  a  2 s.c  o m*/
 * @throws IllegalAccessException
 * @throws NegativeArraySizeException
 * @throws ClassNotFoundException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 * @throws IllegalArgumentException
 * @throws SecurityException
 */
@SuppressWarnings("unchecked")
public static <T> T newInstance(TypeReference<T> typeRef) throws InstantiationException {
    if (typeRef == null)
        return null;
    Type type = typeRef.getType();
    if (type instanceof GenericArrayType) {
        Type componentType = ((GenericArrayType) type).getGenericComponentType();
        return (T) Array.newInstance((Class<?>) componentType, 0);
    }
    if (type instanceof ParameterizedType) {
        Type rawType = ((ParameterizedType) type).getRawType();
        Class<?> clazz = (Class<?>) rawType;
        if (clazz.isInterface()) {
            if (List.class.isAssignableFrom(clazz))
                return (T) Lists.newArrayList();
            if (Map.class.isAssignableFrom(clazz))
                return (T) Maps.newHashMap();
            if (Set.class.isAssignableFrom(clazz))
                return (T) Sets.newHashSet();
        }
        try {
            return ((Class<T>) rawType).newInstance();
        } catch (IllegalAccessException e) {
        }

        throw new InstantiationException("could not create instance, the default constructor of \""
                + ((Class<T>) rawType).getName() + "()\" is not accessible ( or undefined ).");
    }
    if (type instanceof Class)
        return newInstance((Class<T>) type);

    return null;
}

From source file:eu.leads.processor.planner.ClassUtil.java

private static void findClasses(Set<Class> matchedClassSet, File root, File file, boolean includeJars,
        Class type, String packageFilter) {
    if (file.isDirectory()) {
        for (File child : file.listFiles()) {
            findClasses(matchedClassSet, root, child, includeJars, type, packageFilter);
        }// w w  w.j  ava2  s . co  m
    } else {
        if (file.getName().toLowerCase().endsWith(".jar") && includeJars) {
            JarFile jar = null;
            try {
                jar = new JarFile(file);
            } catch (Exception ex) {
                LOG.error(ex.getMessage(), ex);
                return;
            }
            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                String name = entry.getName();
                int extIndex = name.lastIndexOf(".class");
                if (extIndex > 0) {
                    String qualifiedClassName = name.substring(0, extIndex).replace("/", ".");
                    if (qualifiedClassName.indexOf(packageFilter) >= 0 && !isTestClass(qualifiedClassName)) {
                        try {
                            Class clazz = Class.forName(qualifiedClassName);

                            if (!clazz.isInterface() && isMatch(type, clazz)) {
                                matchedClassSet.add(clazz);
                            }
                        } catch (ClassNotFoundException e) {
                            LOG.error(e.getMessage(), e);
                        }
                    }
                }
            }
        } else if (file.getName().toLowerCase().endsWith(".class")) {
            String qualifiedClassName = createClassName(root, file);
            // if (qualifiedClassName.indexOf(packageFilter) >= 0 && !isTestClass(qualifiedClassName)) {
            try {
                Class clazz = Class.forName(qualifiedClassName);
                if (!clazz.isInterface() && isMatch(type, clazz)) {
                    matchedClassSet.add(clazz);
                }
            } catch (ClassNotFoundException e) {
                LOG.error(e.getMessage(), e);
            }
            // }
        }
    }
}

From source file:MBeanTyper.java

/**
 * create a typed object from an mbean/*from  w w w . j  a va 2  s  .c  o  m*/
 */
public static final Object typeMBean(MBeanServer server, ObjectName mbean, Class mainInterface)
        throws Exception {
    List interfaces = new ArrayList();
    if (mainInterface.isInterface()) {
        interfaces.add(mainInterface);
    }
    addInterfaces(mainInterface.getInterfaces(), interfaces);
    Class cl[] = (Class[]) interfaces.toArray(new Class[interfaces.size()]);
    if (DEBUG) {
        System.err
                .println("typeMean->server=" + server + ",mbean=" + mbean + ",mainInterface=" + mainInterface);
        for (int c = 0; c < cl.length; c++) {
            System.err.println("     :" + cl[c]);
        }
    }

    return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), cl,
            new MBeanTyperInvoker(server, mbean));
}

From source file:Main.java

/**
 * Searches for ofClass in the inherited classes and interfaces of inClass. If ofClass has been found in the super
 * classes/interfaces of inClass, then the generic type corresponding to inClass and its TypeVariables is returned,
 * otherwise null. For example :/*  w  w w .  j  a  v a  2 s  .  c o  m*/
 * 
 * <pre>
 * abstract class MyClass implements Serializer&lt;Number&gt; {
 * 
 * }
 * 
 * // type value will be the parameterized type Serializer&lt;Number&gt;
 * Type type = lookupGenericType(Serializer.class, MyClass.class);
 * </pre>
 */
public final static Type lookupGenericType(Class<?> ofClass, Class<?> inClass) {
    if (ofClass == null || inClass == null || !ofClass.isAssignableFrom(inClass))
        return null;
    if (ofClass.equals(inClass))
        return inClass;

    if (ofClass.isInterface()) {
        // lets look if the interface is directly implemented by fromClass
        Class<?>[] interfaces = inClass.getInterfaces();

        for (int i = 0; i < interfaces.length; i++) {
            // do they match?
            if (ofClass.equals(interfaces[i])) {
                return inClass.getGenericInterfaces()[i];
            } else {
                Type superType = lookupGenericType(ofClass, interfaces[i]);
                if (superType != null)
                    return superType;
            }
        }
    }

    // ok it's not one of the directly implemented interfaces, lets try extended class
    Class<?> superClass = inClass.getSuperclass();
    if (ofClass.equals(superClass))
        return inClass.getGenericSuperclass();
    return lookupGenericType(ofClass, inClass.getSuperclass());
}

From source file:com.alta189.bukkit.script.event.EventScanner.java

public static void scanBukkit() {
    Reflections reflections = new Reflections(new ConfigurationBuilder()
            .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("org.bukkit")))
            .setUrls(ClasspathHelper.forClassLoader(ClasspathHelper.contextClassLoader(),
                    ClasspathHelper.staticClassLoader())));

    Set<Class<? extends Event>> classes = reflections.getSubTypesOf(Event.class);

    BScript plugin = BScript.getInstance();
    plugin.info(// w  w  w  .j  a va2s.c  o  m
            "Found " + classes.size() + " classes extending " + Event.class.getCanonicalName() + " in Bukkit");
    for (Class<? extends Event> clazz : classes) {
        if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) {
            continue;
        }
        bukkitEvent.put(clazz.getSimpleName(), clazz);

        String className = clazz.getCanonicalName();
        if (className == null) {
            className = clazz.getName();
        }
        events.put(className, clazz);
        simpleNameEvents.put(clazz.getSimpleName(), clazz);
        plugin.debug(className);
    }
}

From source file:net.femtoparsec.jnlmin.utils.ReflectUtils.java

private static int findCCDistance(Class<?> lower, Class<?> upper) {
    if (upper.isInterface() || lower.isInterface()) {
        throw new IllegalArgumentException(
                String.format("Invalid input class : cannot be interfaces. upper=%s lower=%s", upper, lower));
    }//  w  ww.  j  av  a  2s. c o m

    if (lower == upper) {
        return 0;
    }
    if (!upper.isAssignableFrom(lower)) {
        return -1;
    }

    int distance = 0;
    while (lower != null && lower != upper) {
        lower = lower.getSuperclass();
        distance++;
    }

    if (lower == null) {
        throw new RuntimeException("BUG");
    }

    return distance;

}

From source file:net.femtoparsec.jnlmin.utils.ReflectUtils.java

private static int findCIOneLevelDistance(Class<?> lower, Class<?> upper) {
    if (!upper.isInterface() || lower.isInterface()) {
        throw new IllegalArgumentException(
                String.format("Invalid input class : upper=%s lower=%s", upper, lower));
    }//from   w  ww.  j  ava  2s  .c  o m

    if (lower == upper) {
        return 0;
    }
    if (!upper.isAssignableFrom(lower)) {
        return -1;
    }

    int distance = -1;
    Class<?>[] interfaces = lower.getInterfaces();
    for (Class<?> anInterface : interfaces) {
        int tmp = findIIDistance(anInterface, upper);
        if (tmp >= 0) {
            distance = distance < 0 ? tmp : Math.min(distance, tmp);
        }

        if (distance == 0) {
            break;
        }
    }

    return distance;
}

From source file:net.femtoparsec.jnlmin.utils.ReflectUtils.java

private static int findIIDistance(Class<?> lower, Class<?> upper) {
    if (!upper.isInterface() || !lower.isInterface()) {
        throw new IllegalArgumentException(
                String.format("Invalid input class : cannot be interfaces. upper=%s lower=%s", upper, lower));
    }//from  w w  w .j  a  v  a2s. c o m

    if (lower == upper) {
        return 0;
    }
    if (!upper.isAssignableFrom(lower)) {
        return -1;
    }

    int distance = -1;
    Class<?>[] interfaces = lower.getInterfaces();
    for (Class<?> anInterface : interfaces) {
        int tmp = findIIDistance(anInterface, upper);
        if (tmp >= 0) {
            tmp++;
            distance = distance < 0 ? tmp : Math.min(distance, tmp);
        }

        if (distance == 0) {
            //cannot do better
            break;
        }
    }

    return distance;
}