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.jgentleframework.utils.ReflectUtils.java

/**
 * Returns the first <b>abstract</b> super class of the given class.
 * //  www.ja v  a  2 s  . com
 * @param clazz
 *            the given class
 * @return returns the object class of the first abstract super class of the
 *         given class if it exists, if not, reuturns <code>null</code>
 */
public static Class<?> getFirstAbstractSuperClass(Class<?> clazz) {

    Class<?> result = clazz.getSuperclass();
    while (result != null) {
        if (!Modifier.isAbstract(result.getModifiers())) {
            result = result.getSuperclass();
        } else {
            break;
        }
    }
    return result;
}

From source file:edu.psu.citeseerx.utility.ConfigurationManager.java

/**
 * Determines whether to accept the specified key.  Currently, this
 * method makes sure that the key is not a public object.
 * @param key//from   ww w .ja  v  a2 s  .  co  m
 * @return true if the method returns.
 * @throws InvalidAccessKeyException if the key should not be accepted.
 */
private boolean isValidKey(ConfigurationKey key) throws InvalidAccessKeyException {
    Class keyClass = key.getClass();
    int m = keyClass.getModifiers();
    if (Modifier.isPublic(m)) {
        throw new InvalidAccessKeyException("Key has innapropriate scope");
    }
    return true;

}

From source file:br.ufpr.gres.core.classpath.ClassDetails.java

/**
 * Verify if a class is testable// w w w .j a v a 2 s .com
 *
 * @return Return true if a class is testable; otherwise false
 * @throws java.lang.ClassNotFoundException
 * @throws java.io.IOException
 */
public boolean isTestable()
        throws ClassNotFoundException, IOException, InstantiationException, IllegalAccessException {
    Class c = getClassInstance();

    if (c.isInterface()) {
        logger.error("Can't apply mutation because " + className + " is 'interface'");
        return false;
    }

    if (Modifier.isAbstract(c.getModifiers())) {
        logger.error("Can't apply mutation because " + className + " is 'abstract' class");
        return false;
    }

    if (isGUI(c)) {
        logger.error("Can't apply mutation because " + className + " is 'GUI' class");
        return false;
    }
    if (isApplet(c)) {
        logger.error("Can't apply mutation because " + className + " is 'applet' class");
        return false;
    }

    return true;
}

From source file:hu.bme.mit.sette.common.validator.reflection.ClassValidator.java

/**
 * Sets the required modifiers for the Java class.
 *
 * @param modifiers/*from ww w . j a va 2s.  c  o  m*/
 *            the required modifiers for the Java class.
 * @return this object
 */
public ClassValidator withModifiers(final int modifiers) {
    if (getSubject() != null) {
        Class<?> javaClass = getSubject();

        if ((javaClass.getModifiers() & modifiers) != modifiers) {
            this.addException(String.format(
                    "The Java class must have all the " + "specified modifiers\n(modifiers: [%s])",
                    Modifier.toString(modifiers)));
        }
    }

    return this;
}

From source file:hu.bme.mit.sette.common.validator.reflection.ClassValidator.java

/**
 * Sets the prohibited modifiers for the Java class.
 *
 * @param modifiers//from  w ww.j a  va 2s  . co  m
 *            the prohibited modifiers for the Java class.
 * @return this object
 */
public ClassValidator withoutModifiers(final int modifiers) {
    if (getSubject() != null) {
        Class<?> javaClass = getSubject();

        if ((javaClass.getModifiers() & modifiers) != 0) {
            this.addException(String.format(
                    "The Java class must not have " + "any of the specified modifiers\n" + "(modifiers: [%s])",
                    Modifier.toString(modifiers)));
        }
    }

    return this;
}

From source file:org.apache.ddlutils.TestAgainstLiveDatabaseBase.java

/**
 * Creates the test suite for the given test class which must be a sub class of
 * {@link RoundtripTestBase}. If the platform supports it, it will be tested
 * with both delimited and undelimited identifiers.
 * //from w  ww . j ava 2 s . c  o m
 * @param testedClass The tested class
 * @return The tests
 */
protected static TestSuite getTests(Class testedClass) {
    if (!TestAgainstLiveDatabaseBase.class.isAssignableFrom(testedClass)
            || Modifier.isAbstract(testedClass.getModifiers())) {
        throw new DdlUtilsException("Cannot create parameterized tests for class " + testedClass.getName());
    }

    TestSuite suite = new TestSuite();
    Properties props = readTestProperties();

    if (props == null) {
        return suite;
    }

    DataSource dataSource = initDataSourceFromProperties(props);
    String databaseName = determineDatabaseName(props, dataSource);

    try {
        Method[] methods = testedClass.getMethods();
        PlatformInfo info = null;
        TestAgainstLiveDatabaseBase newTest;

        for (int idx = 0; (methods != null) && (idx < methods.length); idx++) {
            if (methods[idx].getName().startsWith("test") && ((methods[idx].getParameterTypes() == null)
                    || (methods[idx].getParameterTypes().length == 0))) {
                newTest = (TestAgainstLiveDatabaseBase) testedClass.newInstance();
                newTest.setName(methods[idx].getName());
                newTest.setTestProperties(props);
                newTest.setDataSource(dataSource);
                newTest.setDatabaseName(databaseName);
                newTest.setUseDelimitedIdentifiers(false);
                suite.addTest(newTest);

                if (info == null) {
                    info = PlatformFactory.createNewPlatformInstance(newTest.getDatabaseName())
                            .getPlatformInfo();
                }
                if (info.isDelimitedIdentifiersSupported()) {
                    newTest = (TestAgainstLiveDatabaseBase) testedClass.newInstance();
                    newTest.setName(methods[idx].getName());
                    newTest.setTestProperties(props);
                    newTest.setDataSource(dataSource);
                    newTest.setDatabaseName(databaseName);
                    newTest.setUseDelimitedIdentifiers(true);
                    suite.addTest(newTest);
                }
            }
        }
    } catch (Exception ex) {
        throw new DdlUtilsException(ex);
    }

    return suite;
}

From source file:org.eclipse.wb.internal.rcp.databinding.xwt.ui.contentproviders.ValidationUiContentProvider.java

private ClassInfo createInfo(String className) {
    ClassInfo info = new ClassInfo();
    info.className = className;/*from w  ww  . j a v  a 2 s . c  om*/
    //
    if (className.length() == 0) {
        info.message = Messages.ValidationUiContentProvider_noClass;
    } else {
        if (className.startsWith("{") && className.endsWith("}")) {
            return info;
        }
        //
        try {
            // check load class
            Class<?> testClass = loadClass(className);
            // check permissions
            int modifiers = testClass.getModifiers();
            if (!Modifier.isPublic(modifiers)) {
                info.message = Messages.ValidationUiContentProvider_notPublicClass;
                return info;
            }
            if (Modifier.isAbstract(modifiers)) {
                info.message = Messages.ValidationUiContentProvider_abstractClass;
                return info;
            }
            // check constructor
            boolean noConstructor = true;
            try {
                testClass.getConstructor(ArrayUtils.EMPTY_CLASS_ARRAY);
                noConstructor = false;
            } catch (SecurityException e) {
            } catch (NoSuchMethodException e) {
            }
            // prepare error message for constructor
            if (noConstructor) {
                info.message = Messages.ValidationUiContentProvider_noPublicConstructor
                        + ClassUtils.getShortClassName(className) + "().";
            }
        } catch (ClassNotFoundException e) {
            info.message = Messages.ValidationUiContentProvider_notExistClass;
        }
    }
    return info;
}

From source file:com.github.helenusdriver.driver.impl.TypeClassInfoImpl.java

/**
 * Instantiates a new <code>TypeClassInfoImpl</code> object.
 *
 * @author paouelle//from  w w w . j  av a 2  s  .  co m
 *
 * @param  mgr the non-<code>null</code> statement manager
 * @param  rclazz the class of POJO for the root entity this POJO is a type
 * @param  clazz the class of POJO for which to get a class info object for
 * @throws NullPointerException if <code>clazz</code> is <code>null</code>
 * @throws IllegalArgumentException if <code>clazz</code> doesn't represent
 *         a valid POJO class
 */
TypeClassInfoImpl(StatementManagerImpl mgr, Class<? super T> rclazz, Class<T> clazz) {
    super(mgr, clazz, RootEntity.class); // search for ctor starting at root
    org.apache.commons.lang3.Validate.isTrue(!Modifier.isAbstract(clazz.getModifiers()),
            "type entity class '%s', cannot be abstract", clazz.getSimpleName());
    this.type = findType();
    this.rclazz = rclazz;
    // validate the type entity POJO class
    validate(rclazz);
}

From source file:org.broadleafcommerce.common.util.dao.DynamicDaoHelperImpl.java

@Override
public boolean isExcludeClassFromPolymorphism(Class<?> clazz) {
    //We filter out abstract classes because they can't be instantiated.
    if (Modifier.isAbstract(clazz.getModifiers())) {
        return true;
    }//w  w  w . j av a2s.c o  m

    //We filter out classes that are marked to exclude from polymorphism
    AdminPresentationClass adminPresentationClass = clazz.getAnnotation(AdminPresentationClass.class);
    if (adminPresentationClass == null) {
        return false;
    } else if (adminPresentationClass.excludeFromPolymorphism()) {
        return true;
    }
    return false;
}

From source file:com.github.helenusdriver.driver.impl.UDTClassInfoImpl.java

/**
 * Instantiates a new <code>TypeClassInfoImpl</code> object.
 *
 * @author paouelle//from  w  w w .  j  av a  2  s  .c  om
 *
 * @param  mgr the non-<code>null</code> statement manager
 * @param  clazz the class of POJO for which to get a class info object for
 * @throws NullPointerException if <code>clazz</code> is <code>null</code>
 * @throws IllegalArgumentException if <code>clazz</code> doesn't represent
 *         a valid POJO class
 */
UDTClassInfoImpl(StatementManagerImpl mgr, Class<T> clazz) {
    super(mgr, clazz, UDTEntity.class);
    org.apache.commons.lang3.Validate.isTrue(!Modifier.isAbstract(clazz.getModifiers()),
            "type entity class '%s', cannot be abstract", clazz.getSimpleName());
    this.name = findName();
}