Example usage for java.lang Class getModifiers

List of usage examples for java.lang Class getModifiers

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public native int getModifiers();

Source Link

Document

Returns the Java language modifiers for this class or interface, encoded in an integer.

Usage

From source file:org.evosuite.regression.RegressionAssertionCounter.java

private static Class<?> getExceptionClassToUse(Throwable exception) {
    /*//from ww w  .  j  av a 2s  .c  om
    we can only catch a public class.
    for "readability" of tests, it shouldn't be a mock one either
      */
    Class<?> ex = exception.getClass();
    while (!Modifier.isPublic(ex.getModifiers()) || EvoSuiteMock.class.isAssignableFrom(ex)
            || ex.getCanonicalName().startsWith("com.sun.")) {
        ex = ex.getSuperclass();
    }
    return ex;
}

From source file:net.radai.beanz.util.ReflectionUtil.java

public static Map<?, ?> instantiateMap(Class<?> mapClass) {
    if (!Map.class.isAssignableFrom(mapClass)) {
        throw new IllegalArgumentException();
    }/*  w  ww . ja  v a2  s . co m*/
    if (!Modifier.isAbstract(mapClass.getModifiers())) {
        try {
            //noinspection unchecked
            return (Map<?, ?>) mapClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new IllegalStateException(e);
        }
    }
    return new HashMap<>();
}

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

/**
 * Is this member publicly accessible./* w  w  w  .  java2  s.  c  o m*/
 *
 * @param clazz The class which defines the member
 * @param member The memeber.
 * @return True if the member is publicly accessible, false otherwise.
 */
public static boolean isPublic(Class clazz, Member member) {
    return Modifier.isPublic(member.getModifiers()) && Modifier.isPublic(clazz.getModifiers());
}

From source file:org.eclipse.wb.internal.rcp.databinding.model.beans.bindables.BeanSupport.java

/**
 * @return {@link PropertyDescriptor} properties for given bean {@link Class}.
 *//*from   w ww .j  a  v  a  2s. c  o m*/
public static List<PropertyDescriptor> getPropertyDescriptors(Class<?> beanClass) throws Exception {
    List<PropertyDescriptor> descriptors = Lists.newArrayList();
    // handle interfaces
    if (beanClass.isInterface() || Modifier.isAbstract(beanClass.getModifiers())) {
        List<Class<?>> interfaces = CoreUtils.cast(ClassUtils.getAllInterfaces(beanClass));
        for (Class<?> i : interfaces) {
            BeanInfo beanInfo = Introspector.getBeanInfo(i);
            addDescriptors(descriptors, beanInfo.getPropertyDescriptors());
        }
    }
    // handle bean
    BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
    addDescriptors(descriptors, beanInfo.getPropertyDescriptors());
    //
    return descriptors;
}

From source file:io.coala.json.JsonUtil.java

/**
 * @param om the {@link ObjectMapper} used to parse/deserialize/unmarshal
 * @param tree the partially parsed JSON {@link TreeNode}
 * @param resultType the type of result {@link Object}
 * @param imports the {@link Properties} instances for default values, etc.
 * @return the parsed/deserialized/unmarshalled {@link Object}
 *///w  ww  . j a  va 2  s .co  m
public static <T> T valueOf(final ObjectMapper om, final TreeNode tree, final Class<T> resultType,
        final Properties... imports) {
    if (tree == null)
        return null;
    // TODO add work-around for Wrapper sub-types?
    if (resultType.isMemberClass() && !Modifier.isStatic(resultType.getModifiers()))
        return Thrower.throwNew(IllegalArgumentException.class, "Unable to deserialize non-static member: {}",
                resultType);

    try {
        return (T) om.treeToValue(tree, checkRegistered(om, resultType, imports));
    } catch (final Exception e) {
        return Thrower.rethrowUnchecked(e);
    }
}

From source file:net.radai.beanz.util.ReflectionUtil.java

public static Collection<?> instantiateCollection(Class<?> collectionClass) {
    if (!Collection.class.isAssignableFrom(collectionClass)) {
        throw new IllegalArgumentException();
    }//from   ww  w .jav a 2 s. c o  m
    if (!Modifier.isAbstract(collectionClass.getModifiers())) {
        try {
            //noinspection unchecked
            return (Collection<?>) collectionClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new IllegalStateException(e);
        }
    }
    if (List.class.isAssignableFrom(collectionClass)) {
        return new ArrayList<>();
    }
    if (Set.class.isAssignableFrom(collectionClass)) {
        return new HashSet<>();
    }
    throw new UnsupportedOperationException();
}

From source file:one.nio.serial.Repository.java

private static synchronized Serializer generateFor(Class<?> cls) {
    Serializer serializer = classMap.get(cls);
    if (serializer == null) {
        if (cls.isArray()) {
            get(cls.getComponentType());
            serializer = new ObjectArraySerializer(cls);
        } else if ((cls.getModifiers() & ENUM) != 0) {
            if (cls.getSuperclass() != Enum.class) {
                serializer = get(cls.getSuperclass());
                classMap.put(cls, serializer);
                return serializer;
            }//w  w  w  .  j  a  v  a 2s .  c om
            serializer = new EnumSerializer(cls);
        } else if (Externalizable.class.isAssignableFrom(cls)) {
            serializer = new ExternalizableSerializer(cls);
        } else if (Collection.class.isAssignableFrom(cls) && !hasOptions(cls, FIELD_SERIALIZATION)) {
            serializer = new CollectionSerializer(cls);
        } else if (Map.class.isAssignableFrom(cls) && !hasOptions(cls, FIELD_SERIALIZATION)) {
            serializer = new MapSerializer(cls);
        } else if (Serializable.class.isAssignableFrom(cls)) {
            serializer = new GeneratedSerializer(cls);
        } else {
            serializer = new InvalidSerializer(cls);
        }

        serializer.generateUid();
        provideSerializer(serializer);

        if (cls.isAnonymousClass()) {
            log.warn("Trying to serialize anonymous class: " + cls.getName());
            anonymousClasses.incrementAndGet();
        }

        Renamed renamed = cls.getAnnotation(Renamed.class);
        if (renamed != null) {
            renamedClasses.put(renamed.from(), cls);
        }
    }
    return serializer;
}

From source file:org.eclipse.e4.emf.internal.xpath.helper.ValueUtils.java

public static int getCollectionHint(Class<?> clazz) {
    if (clazz.isArray()) {
        return 1;
    }//from  ww w. jav  a 2 s. com

    if (Collection.class.isAssignableFrom(clazz)) {
        return 1;
    }

    if (clazz.isPrimitive()) {
        return -1;
    }

    if (clazz.isInterface()) {
        return 0;
    }

    if (Modifier.isFinal(clazz.getModifiers())) {
        return -1;
    }

    return 0;
}

From source file:com.lucidtechnics.blackboard.TargetConstructor.java

public static final boolean canConstructTarget(Class _class) {
    boolean isFinal = Modifier.isFinal(_class.getModifiers());
    boolean isArray = _class.isArray();
    boolean isEnum = _class.isEnum();

    return (isFinal == false && isArray == false && isEnum == false);
}

From source file:es.caib.zkib.jxpath.util.ValueUtils.java

/**
 * Return an accessible method (that is, one that can be invoked via
 * reflection) that implements the specified Method.  If no such method
 * can be found, return <code>null</code>.
 *
 * @param method The method that we wish to call
 * @return Method//www .j  a  va2 s.c  om
 */
public static Method getAccessibleMethod(Method method) {

    // Make sure we have a method to check
    if (method == null) {
        return (null);
    }

    // If the requested method is not public we cannot call it
    if (!Modifier.isPublic(method.getModifiers())) {
        return (null);
    }

    // If the declaring class is public, we are done
    Class clazz = method.getDeclaringClass();
    if (Modifier.isPublic(clazz.getModifiers())) {
        return (method);
    }

    String name = method.getName();
    Class[] parameterTypes = method.getParameterTypes();
    while (clazz != null) {
        // Check the implemented interfaces and subinterfaces
        Method aMethod = getAccessibleMethodFromInterfaceNest(clazz, name, parameterTypes);
        if (aMethod != null) {
            return aMethod;
        }

        clazz = clazz.getSuperclass();
        if (clazz != null && Modifier.isPublic(clazz.getModifiers())) {
            try {
                return clazz.getDeclaredMethod(name, parameterTypes);
            } catch (NoSuchMethodException e) { //NOPMD
                //ignore
            }
        }
    }
    return null;
}