Example usage for java.lang.reflect Modifier isInterface

List of usage examples for java.lang.reflect Modifier isInterface

Introduction

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

Prototype

public static boolean isInterface(int mod) 

Source Link

Document

Return true if the integer argument includes the interface modifier, false otherwise.

Usage

From source file:org.evosuite.setup.TestClusterGenerator.java

public static Set<Class<?>> getConcreteClasses(Class<?> clazz, InheritanceTree inheritanceTree) {

    // Some special cases
    if (clazz.equals(java.util.Map.class))
        return getConcreteClassesMap();
    else if (clazz.equals(java.util.List.class))
        return getConcreteClassesList();
    else if (clazz.equals(java.util.Set.class))
        return getConcreteClassesSet();
    else if (clazz.equals(java.util.Collection.class))
        return getConcreteClassesList();
    else if (clazz.equals(java.util.Iterator.class))
        // We don't want to explicitly create iterators
        // This would only pull in java.util.Scanner, the only
        // concrete subclass
        return new LinkedHashSet<Class<?>>();
    else if (clazz.equals(java.util.ListIterator.class))
        // We don't want to explicitly create iterators
        return new LinkedHashSet<Class<?>>();
    else if (clazz.equals(java.io.Serializable.class))
        return new LinkedHashSet<Class<?>>();
    else if (clazz.equals(java.lang.Comparable.class))
        return getConcreteClassesComparable();
    else if (clazz.equals(java.util.Comparator.class))
        return new LinkedHashSet<Class<?>>();

    Set<Class<?>> actualClasses = new LinkedHashSet<Class<?>>();
    if (Modifier.isAbstract(clazz.getModifiers()) || Modifier.isInterface(clazz.getModifiers())
            || clazz.equals(Enum.class)) {
        Set<String> subClasses = inheritanceTree.getSubclasses(clazz.getName());
        logger.debug("Subclasses of {}: {}", clazz.getName(), subClasses);
        Map<String, Integer> classDistance = new HashMap<String, Integer>();
        int maxDistance = -1;
        String name = clazz.getName();
        if (clazz.equals(Enum.class)) {
            name = Properties.TARGET_CLASS;
        }//from  ww  w  .  j a  v  a  2 s .c om
        for (String subClass : subClasses) {
            int distance = getPackageDistance(subClass, name);
            classDistance.put(subClass, distance);
            maxDistance = Math.max(distance, maxDistance);
        }
        int distance = 0;
        while (actualClasses.isEmpty() && distance <= maxDistance) {
            logger.debug(" Current distance: {}", distance);
            for (String subClass : subClasses) {
                if (classDistance.get(subClass) == distance) {
                    try {
                        Class<?> subClazz = Class.forName(subClass, false,
                                TestGenerationContext.getInstance().getClassLoaderForSUT());
                        if (!canUse(subClazz))
                            continue;
                        if (subClazz.isInterface())
                            continue;
                        if (Modifier.isAbstract(subClazz.getModifiers())) {
                            if (!hasStaticGenerator(subClazz))
                                continue;
                        }
                        Class<?> mock = MockList.getMockClass(subClazz.getCanonicalName());
                        if (mock != null) {
                            /*
                             * If we are mocking this class, then such class should not be used
                             * in the generated JUnit test cases, but rather its mock.
                             */
                            logger.debug("Adding mock {} instead of {}", mock, clazz);
                            subClazz = mock;
                        } else {

                            if (!checkIfCanUse(subClazz.getCanonicalName())) {
                                continue;
                            }
                        }

                        actualClasses.add(subClazz);

                    } catch (ClassNotFoundException e) {
                        logger.error("Problem for {}. Class not found: {}", Properties.TARGET_CLASS, subClass,
                                e);
                        logger.error("Removing class from inheritance tree");
                        inheritanceTree.removeClass(subClass);
                    }
                }
            }
            distance++;
        }
        if (hasStaticGenerator(clazz)) {
            actualClasses.add(clazz);
        }
        if (actualClasses.isEmpty()) {
            logger.info("Don't know how to instantiate abstract class {}", clazz.getName());
        }
    } else {
        actualClasses.add(clazz);
    }

    logger.debug("Subclasses of {}: {}", clazz.getName(), actualClasses);
    return actualClasses;
}

From source file:org.apache.flink.api.java.typeutils.TypeExtractor.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private <OUT, IN1, IN2> TypeInformation<OUT> privateGetForClass(Class<OUT> clazz, ArrayList<Type> typeHierarchy,
        ParameterizedType parameterizedType, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type) {
    checkNotNull(clazz);//from  w  w w. jav a2  s . com

    // check if type information can be produced using a factory
    final TypeInformation<OUT> typeFromFactory = createTypeInfoFromFactory(clazz, typeHierarchy, in1Type,
            in2Type);
    if (typeFromFactory != null) {
        return typeFromFactory;
    }

    // Object is handled as generic type info
    if (clazz.equals(Object.class)) {
        return new GenericTypeInfo<>(clazz);
    }

    // Class is handled as generic type info
    if (clazz.equals(Class.class)) {
        return new GenericTypeInfo<OUT>(clazz);
    }

    // recursive types are handled as generic type info
    if (countTypeInHierarchy(typeHierarchy, clazz) > 1) {
        return new GenericTypeInfo<>(clazz);
    }

    // check for arrays
    if (clazz.isArray()) {

        // primitive arrays: int[], byte[], ...
        PrimitiveArrayTypeInfo<OUT> primitiveArrayInfo = PrimitiveArrayTypeInfo.getInfoFor(clazz);
        if (primitiveArrayInfo != null) {
            return primitiveArrayInfo;
        }

        // basic type arrays: String[], Integer[], Double[]
        BasicArrayTypeInfo<OUT, ?> basicArrayInfo = BasicArrayTypeInfo.getInfoFor(clazz);
        if (basicArrayInfo != null) {
            return basicArrayInfo;
        }

        // object arrays
        else {
            TypeInformation<?> componentTypeInfo = createTypeInfoWithTypeHierarchy(typeHierarchy,
                    clazz.getComponentType(), in1Type, in2Type);

            return ObjectArrayTypeInfo.getInfoFor(clazz, componentTypeInfo);
        }
    }

    // check for writable types
    if (isHadoopWritable(clazz)) {
        return createHadoopWritableTypeInfo(clazz);
    }

    // check for basic types
    TypeInformation<OUT> basicTypeInfo = BasicTypeInfo.getInfoFor(clazz);
    if (basicTypeInfo != null) {
        return basicTypeInfo;
    }

    // check for SQL time types
    TypeInformation<OUT> timeTypeInfo = SqlTimeTypeInfo.getInfoFor(clazz);
    if (timeTypeInfo != null) {
        return timeTypeInfo;
    }

    // check for subclasses of Value
    if (Value.class.isAssignableFrom(clazz)) {
        Class<? extends Value> valueClass = clazz.asSubclass(Value.class);
        return (TypeInformation<OUT>) ValueTypeInfo.getValueTypeInfo(valueClass);
    }

    // check for subclasses of Tuple
    if (Tuple.class.isAssignableFrom(clazz)) {
        if (clazz == Tuple0.class) {
            return new TupleTypeInfo(Tuple0.class);
        }
        throw new InvalidTypesException(
                "Type information extraction for tuples (except Tuple0) cannot be done based on the class.");
    }

    // check for Enums
    if (Enum.class.isAssignableFrom(clazz)) {
        return new EnumTypeInfo(clazz);
    }

    // special case for POJOs generated by Avro.
    if (SpecificRecordBase.class.isAssignableFrom(clazz)) {
        return new AvroTypeInfo(clazz);
    }

    if (Modifier.isInterface(clazz.getModifiers())) {
        // Interface has no members and is therefore not handled as POJO
        return new GenericTypeInfo<OUT>(clazz);
    }

    try {
        TypeInformation<OUT> pojoType = analyzePojo(clazz, new ArrayList<Type>(typeHierarchy),
                parameterizedType, in1Type, in2Type);
        if (pojoType != null) {
            return pojoType;
        }
    } catch (InvalidTypesException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Unable to handle type " + clazz + " as POJO. Message: " + e.getMessage(), e);
        }
        // ignore and create generic type info
    }

    // return a generic type
    return new GenericTypeInfo<OUT>(clazz);
}

From source file:eu.crisis_economics.abm.dashboard.Page_Parameters.java

ClassElement[] fetchPossibleTypes(final SubmodelInfo parameterInfo) throws ComboBoxIsTooFullException {
    List<Class<?>> possibleTypes = possibleTypesCache.get(parameterInfo.getName());

    if (possibleTypes == null)
        possibleTypes = parameterInfo.getPossibleTypes();
    if (possibleTypes == null || possibleTypes.isEmpty()) {
        possibleTypes = new ArrayList<Class<?>>();
        possibleTypes.add(parameterInfo.getJavaType());

        possibleTypes.addAll(getSubtypes(parameterInfo.getJavaType()));

        // delete all interfaces and abstract classes
        for (final Iterator<Class<?>> iter = possibleTypes.iterator(); iter.hasNext();) {
            final Class<?> clazz = iter.next();
            if (Modifier.isAbstract(clazz.getModifiers()) || Modifier.isInterface(clazz.getModifiers()))
                iter.remove();//from  w w w . j av a 2s  .  c  om
        }
    }

    possibleTypesCache.put(parameterInfo.getName(), possibleTypes);
    Collections.sort(possibleTypes, new Comparator<Class<?>>() {
        public int compare(final Class<?> class1, final Class<?> class2) {
            return class1.getName().compareTo(class2.getName());
        }

    });

    final ClassElement[] result = new ClassElement[possibleTypes.size() + 1];
    result[0] = new ClassElement(null, null);

    for (int i = 0; i < possibleTypes.size(); ++i) {
        if (possibleTypes.get(i).equals(parameterInfo.getActualType())) {
            result[i + 1] = new ClassElement(possibleTypes.get(i), parameterInfo.getInstance());
        } else {
            result[i + 1] = new ClassElement(possibleTypes.get(i), null);
        }
    }

    return result;
}