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.tynamo.model.elasticsearch.util.ReflectionUtil.java

/**
 * Checks if is abstract./*  w w  w  .  ja  v  a2 s. c o  m*/
 * 
 * @param clazz
 *          the clazz
 * @return true, if is abstract
 */
public static boolean isAbstract(Class<?> clazz) {
    int modifiers = clazz.getModifiers();
    return (modifiers & Modifier.ABSTRACT) > 0;
}

From source file:org.tynamo.model.elasticsearch.util.ReflectionUtil.java

/**
 * Checks if is interface.//  www .j  a  v a2s . co  m
 * 
 * @param clazz
 *          the clazz
 * @return true, if is interface
 */
public static boolean isInterface(Class<?> clazz) {
    int modifiers = clazz.getModifiers();
    return (modifiers & Modifier.INTERFACE) > 0;
}

From source file:org.openspaces.test.client.executor.ExecutorUtils.java

/**
 * Ensure that the class is valid, that is, that it has appropriate access: The class is public
 * and has public constructor with no-args.
 *//*from ww w.jav a 2 s. c  om*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void ensureValidClass(Class claz) {
    boolean ctorOK = false;

    try {
        // check if defined public constructor
        if (!Modifier.isPublic(claz.getModifiers())) {
            throw new IllegalArgumentException("Class [" + claz.getName() + "] not public");
        }

        Constructor ctor = claz.getConstructor(new Class[0]);
        ctorOK = Modifier.isPublic(ctor.getModifiers());
    } catch (NoSuchMethodException e) {
        ctorOK = false;
    } catch (SecurityException e) {
        ctorOK = false;
    }

    if (!ctorOK) {
        throw new IllegalArgumentException("Class [" + claz.getName() + "] needs public no-arg constructor");
    }
}

From source file:org.bremersee.common.exception.ExceptionRegistry.java

/**
 * Find an instance of the given throwable class.
 *
 * @param clazz   the throwable class/*  w  w w .  j  a  v a2 s  .c o  m*/
 * @param message an optional message
 * @param cause   an optional cause
 * @return the throwable instance
 */
private static Throwable findThrowable(final Class<? extends Throwable> clazz, final String message,
        final Throwable cause) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Finding throwable of class [" + clazz + "] ...");
    }
    Class<? extends Throwable> cls = clazz;
    while (Modifier.isAbstract(cls.getModifiers())) {
        //noinspection unchecked
        cls = (Class<? extends Throwable>) cls.getSuperclass();
    }
    Throwable t = findThrowableByMessageAndCause(cls, message, cause);
    if (t == null) {
        t = findThrowableByMessage(cls, message, cause);
    }
    if (t == null) {
        t = findThrowableByCause(cls, message, cause);
    }
    if (t == null) {
        t = findThrowableByDefaultConstructor(cls, message, cause);
    }
    if (t == null) {
        Class<?> superCls = cls.getSuperclass();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finding throwable of class [" + cls + "] failed. Trying super class [" + superCls
                    + "] ...");
        }
        if (Throwable.class.isAssignableFrom(superCls)) {
            //noinspection unchecked
            return findThrowable((Class<? extends Throwable>) superCls, message, cause);
        } else {
            InternalServerError ise = new InternalServerError();
            LOG.error("Finding throwable for class [" + clazz.getName() + "] failed.", ise); // NOSONAR
            throw ise;
        }
    }
    return t;
}

From source file:org.pantsbuild.tools.junit.impl.ConsoleRunnerImpl.java

private static boolean isTest(final Class<?> clazz) {
    // Must be a public concrete class to be a runnable junit Test.
    if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())
            || !Modifier.isPublic(clazz.getModifiers())) {
        return false;
    }/* ww w .  j  a va2s. co m*/

    // The class must have some public constructor to be instantiated by the runner being used
    if (!Iterables.any(Arrays.asList(clazz.getConstructors()), IS_PUBLIC_CONSTRUCTOR)) {
        return false;
    }

    // Support junit 3.x Test hierarchy.
    if (junit.framework.Test.class.isAssignableFrom(clazz)) {
        return true;
    }

    // Support classes using junit 4.x custom runners.
    if (clazz.isAnnotationPresent(RunWith.class)) {
        return true;
    }

    // Support junit 4.x @Test annotated methods.
    return Iterables.any(Arrays.asList(clazz.getMethods()), IS_ANNOTATED_TEST_METHOD);
}

From source file:org.apache.usergrid.services.DBServiceManager.java

@SuppressWarnings("unchecked")
private static Class<Service> findClass(String classname) {

    Class<Service> cls;
    try {//  ww w.  ja  va 2 s.co  m
        if (logger.isTraceEnabled()) {
            logger.trace("Attempting to instantiate service class {}", classname);
        }
        cls = (Class<Service>) Class.forName(classname);
        if (cls.isInterface()) {
            cls = (Class<Service>) Class.forName(classname.concat(IMPL));
        }
        if ((cls != null) && !Modifier.isAbstract(cls.getModifiers())) {
            return cls;
        }
    } catch (ClassNotFoundException e1) {
        if (logger.isTraceEnabled()) {
            logger.trace("Could not find class", e1);
        }
    }
    return null;
}

From source file:Main.java

public static String isLocalType(Class<?> type, boolean allowNonStatic) {
    /* As per [JACKSON-187], GAE seems to throw SecurityExceptions
     * here and there... and GAE itself has a bug, too
     * (see []). Bah. So we need to catch some wayward exceptions on GAE
     *//* w w w .  ja v  a 2 s  .c  om*/
    try {
        // one more: method locals, anonymous, are not good:
        if (type.getEnclosingMethod() != null) {
            return "local/anonymous";
        }

        /* But how about non-static inner classes? Can't construct
         * easily (theoretically, we could try to check if parent
         * happens to be enclosing... but that gets convoluted)
         */
        if (!allowNonStatic) {
            if (type.getEnclosingClass() != null) {
                if (!Modifier.isStatic(type.getModifiers())) {
                    return "non-static member class";
                }
            }
        }
    } catch (SecurityException e) {
    } catch (NullPointerException e) {
    }
    return null;
}

From source file:org.codehaus.enunciate.modules.rest.RESTOperation.java

/**
 * Create a new instance of something of the specified collection type.
 *
 * @param collectionType The collection type.
 * @return the new instance./*from  www.  jav a  2s. c  o  m*/
 */
public static Collection newCollectionInstance(Class collectionType) {
    if (Collection.class.isAssignableFrom(collectionType)) {
        Collection collection;
        if ((collectionType.isInterface()) || (Modifier.isAbstract(collectionType.getModifiers()))) {
            if (Set.class.isAssignableFrom(collectionType)) {
                if (SortedSet.class.isAssignableFrom(collectionType)) {
                    collection = new TreeSet();
                } else {
                    collection = new HashSet();
                }
            } else {
                collection = new ArrayList();
            }
        } else {
            try {
                collection = (Collection) collectionType.newInstance();
            } catch (Exception e) {
                throw new IllegalArgumentException(
                        "Unable to create an instance of " + collectionType.getName() + ".", e);
            }
        }
        return collection;
    } else {
        throw new IllegalArgumentException("Invalid list type: " + collectionType.getName());
    }
}

From source file:Main.java

/**
 * @since 1.9/*  w  w  w.j av  a 2s.com*/
 */
public static String isLocalType(Class<?> type, boolean allowNonStatic) {
    /* As per [JACKSON-187], GAE seems to throw SecurityExceptions
     * here and there... and GAE itself has a bug, too
     * (see []). Bah. So we need to catch some wayward exceptions on GAE
     */
    try {
        // one more: method locals, anonymous, are not good:
        if (type.getEnclosingMethod() != null) {
            return "local/anonymous";
        }

        /* But how about non-static inner classes? Can't construct
         * easily (theoretically, we could try to check if parent
         * happens to be enclosing... but that gets convoluted)
         */
        if (!allowNonStatic) {
            if (type.getEnclosingClass() != null) {
                if (!Modifier.isStatic(type.getModifiers())) {
                    return "non-static member class";
                }
            }
        }
    } catch (SecurityException e) {
    } catch (NullPointerException e) {
    }
    return null;
}

From source file:org.mule.module.extension.internal.util.IntrospectionUtils.java

public static void checkInstantiable(Class<?> declaringClass, boolean requireDefaultConstructor) {
    checkArgument(declaringClass != null, "declaringClass cannot be null");

    if (requireDefaultConstructor) {
        checkArgument(hasDefaultConstructor(declaringClass),
                String.format("Class %s cannot be instantiated since it doesn't have a default constructor",
                        declaringClass.getName()));
    }/*from   www  . j a va  2  s  .com*/

    checkArgument(!declaringClass.isInterface(),
            String.format("Class %s cannot be instantiated since it's an interface", declaringClass.getName()));
    checkArgument(!Modifier.isAbstract(declaringClass.getModifiers()),
            String.format("Class %s cannot be instantiated since it's abstract", declaringClass.getName()));
}