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:com.github.venkateshamurthy.designpatterns.builders.FluentBuilders.java

/**
 * Return true if class passed is <b>not</b> a public class
 * /*w  w w. ja  va  2 s  .  c  o m*/
 * @param thisPojoClass
 *            to be verified if its public
 * @return true if thisPojoClass is <b>not</b> public
 */
private boolean isNotAPublicClass(final Class<?> thisPojoClass) {
    return thisPojoClass != null && (Modifier.PUBLIC & thisPojoClass.getModifiers()) != Modifier.PUBLIC;
}

From source file:org.apache.maven.plugin.javadoc.JavadocUtil.java

/**
 * Auto-detect the class names of the implementation of <code>com.sun.tools.doclets.Taglet</code> class from a
 * given jar file./*from w w  w .j a v a2s.  c o  m*/
 * <br/>
 * <b>Note</b>: <code>JAVA_HOME/lib/tools.jar</code> is a requirement to find
 * <code>com.sun.tools.doclets.Taglet</code> class.
 *
 * @param jarFile not null
 * @return the list of <code>com.sun.tools.doclets.Taglet</code> class names from a given jarFile.
 * @throws IOException if jarFile is invalid or not found, or if the <code>JAVA_HOME/lib/tools.jar</code>
 * is not found.
 * @throws ClassNotFoundException if any
 * @throws NoClassDefFoundError if any
 */
protected static List<String> getTagletClassNames(File jarFile)
        throws IOException, ClassNotFoundException, NoClassDefFoundError {
    List<String> classes = getClassNamesFromJar(jarFile);
    ClassLoader cl;

    // Needed to find com.sun.tools.doclets.Taglet class
    File tools = new File(System.getProperty("java.home"), "../lib/tools.jar");
    if (tools.exists() && tools.isFile()) {
        cl = new URLClassLoader(new URL[] { jarFile.toURI().toURL(), tools.toURI().toURL() }, null);
    } else {
        cl = new URLClassLoader(new URL[] { jarFile.toURI().toURL() }, null);
    }

    List<String> tagletClasses = new ArrayList<String>();

    Class<?> tagletClass = cl.loadClass("com.sun.tools.doclets.Taglet");
    for (String s : classes) {
        Class<?> c = cl.loadClass(s);

        if (tagletClass.isAssignableFrom(c) && !Modifier.isAbstract(c.getModifiers())) {
            tagletClasses.add(c.getName());
        }
    }

    return tagletClasses;
}

From source file:ClassTree.java

public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
        boolean leaf, int row, boolean hasFocus) {
    super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
    // get the user object
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
    Class<?> c = (Class<?>) node.getUserObject();

    // the first time, derive italic font from plain font
    if (plainFont == null) {
        plainFont = getFont();/*from  ww w.j av a 2s. co m*/
        // the tree cell renderer is sometimes called with a label that has a null font
        if (plainFont != null)
            italicFont = plainFont.deriveFont(Font.ITALIC);
    }

    // set font to italic if the class is abstract, plain otherwise
    if ((c.getModifiers() & Modifier.ABSTRACT) == 0)
        setFont(plainFont);
    else
        setFont(italicFont);
    return this;
}

From source file:org.zanata.seam.SeamAutowire.java

/**
 * Registers an implementation to use for beans. This method is
 * provided for beans which are injected by interface rather than name.
 *
 * @param cls//  w w w .  j  a  v  a  2 s .  c om
 *            The class to register.
 */
public SeamAutowire useImpl(Class<?> cls) {
    if (Modifier.isAbstract(cls.getModifiers())) {
        throw new AutowireException("Class " + cls.getName() + " is abstract.");
    }
    this.registerInterfaces(cls);

    return this;
}

From source file:org.zanata.seam.SeamAutowire.java

private void registerInterfaces(Class<?> cls) {
    assert !Modifier.isAbstract(cls.getModifiers());
    // register all interfaces registered by this bean
    for (Class<?> iface : getAllInterfaces(cls)) {
        this.beanImpls.put(iface, cls);
    }//from   ww  w.  jav  a2  s  .  c  o m
}

From source file:net.firejack.platform.core.utils.Factory.java

private <T> T convertFrom0(Class<?> clazz, Object dto) {
    if (dto == null)
        return null;
    Object bean = null;//from   w w w .  j  a v  a 2 s  .  c o  m
    try {
        bean = clazz.newInstance();
        List<FieldInfo> infos = fields.get(dto.getClass());
        if (infos == null) {
            infos = getAllField(dto.getClass(), dto.getClass(), new ArrayList<FieldInfo>());
            fields.put(dto.getClass(), infos);
        }
        for (FieldInfo info : infos) {
            if (info.readonly()) {
                continue;
            }

            String name = info.name();
            Object entity = bean;
            clazz = entity.getClass();
            String[] lookup = name.split("\\.");

            if (lookup.length > 1) {
                if (isNullValue(dto, info.getField().getName()))
                    continue;

                for (int i = 0; i < lookup.length - 1; i++) {
                    Object instance = get(entity, lookup[i], null);
                    if (instance == null) {
                        FieldInfo fieldInfo = getField(clazz, lookup[i]);
                        Class<?> type = fieldInfo.getType();
                        if (!type.isInterface() && !Modifier.isAbstract(type.getModifiers())) {
                            instance = type.newInstance();
                            set(entity, lookup[i], instance);

                            entity = instance;
                            clazz = type;
                        }
                    } else {
                        entity = instance;
                        clazz = instance.getClass();
                    }
                }
                name = lookup[lookup.length - 1];
            }

            FieldInfo distField = getField(clazz, name);
            if (distField != null) {
                Class<?> type = distField.getType();
                Object value = get(dto, info.getField().getName(), type);
                if (value != null) {
                    if (value instanceof AbstractDTO) {
                        if (contains(value)) {
                            Object convert = get(value);
                            set(entity, name, convert);
                        } else {
                            add(value, null);
                            Object convert = convertFrom0(type, value);
                            add(value, convert);
                            set(entity, name, convert);
                        }
                    } else if (value instanceof Collection) {
                        Collection result = (Collection) value;
                        Class<?> arrayType = distField.getGenericType();
                        if (AbstractModel.class.isAssignableFrom(arrayType)
                                || arrayType.isAnnotationPresent(XmlAccessorType.class)) {
                            try {
                                result = (Collection) value.getClass().newInstance();
                            } catch (InstantiationException e) {
                                result = new ArrayList();
                            }

                            for (Object o : (Collection) value) {
                                Object convert = convertFrom0(arrayType, o);
                                result.add(convert);
                            }
                        }
                        set(entity, name, result);
                    } else {
                        set(entity, name, value);
                    }
                }
            }
        }
    } catch (Exception e) {
        logger.warn(e.getMessage());
    }
    return (T) bean;
}

From source file:org.topazproject.otm.mapping.java.ClassBinder.java

/**
 * Creates a new ClassBinder object./*from www.j  av  a  2 s. co m*/
 *
 * @param clazz the java class to bind this entity to
 * @param suppressAlias to dis-allow registration of the class name as an alias. 
 *                      This allows binding the same class to multiple entities.
 */
public ClassBinder(Class<T> clazz, boolean suppressAlias) {
    this.clazz = clazz;
    this.suppressAlias = suppressAlias;

    int mod = clazz.getModifiers();
    instantiable = !Modifier.isAbstract(mod) && !Modifier.isInterface(mod) && Modifier.isPublic(mod);
}

From source file:org.jsonschema2pojo.integration.EnumIT.java

@Test
@SuppressWarnings("unchecked")
public void doubleEnumAtRootCreatesIntBackedEnum() throws ClassNotFoundException, NoSuchMethodException,
        IllegalAccessException, InvocationTargetException {

    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/enum/doubleEnumAsRoot.json",
            "com.example");

    Class<Enum> rootEnumClass = (Class<Enum>) resultsClassLoader
            .loadClass("com.example.enums.DoubleEnumAsRoot");

    assertThat(rootEnumClass.isEnum(), is(true));
    assertThat(rootEnumClass.getDeclaredMethod("fromValue", Double.class), is(notNullValue()));
    assertThat(isPublic(rootEnumClass.getModifiers()), is(true));
}

From source file:io.konik.utils.RandomInvoiceGenerator.java

public Object populteData(Class<?> root, String name) throws InstantiationException, IllegalAccessException,
        NoSuchMethodException, InvocationTargetException {
    Object rootObj;/*  w w  w  .jav  a2  s . c  o  m*/
    if (isLeafType(root)) {//final type
        return generatePrimitveValue(root, name);
    }
    rootObj = createNewInstance(root);

    // get method and populate each of them
    Method[] methods = root.getMethods();
    for (Method method : methods) {
        int methodModifiers = method.getModifiers();
        Class<?> methodParameter = null;
        if (Modifier.isAbstract(methodModifiers) || method.isSynthetic())
            continue;
        if (method.getName().startsWith("add")) {
            methodParameter = method.getParameterTypes()[0];
            if (methodParameter != null && !methodParameter.isArray()
                    && (methodParameter.isInterface() || Modifier.isAbstract(methodParameter.getModifiers()))) {
                continue;
            }
        }
        //getter
        else if (method.getName().startsWith("get")
                && !Collection.class.isAssignableFrom(method.getReturnType())
                && !method.getName().equals("getClass") && !Modifier.isAbstract(methodModifiers)) {
            methodParameter = method.getReturnType();
        } else {
            continue;// next on setter
        }
        if (methodParameter == null || methodParameter.isInterface()) {
            continue;
        }
        Object popultedData = populteData(methodParameter, method.getName());
        setValue(rootObj, method, popultedData);
    }
    return rootObj;
}

From source file:org.jsonschema2pojo.integration.EnumIT.java

@Test
@SuppressWarnings("unchecked")
public void intEnumAtRootCreatesIntBackedEnum() throws ClassNotFoundException, NoSuchMethodException,
        IllegalAccessException, InvocationTargetException {

    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/enum/integerEnumAsRoot.json",
            "com.example");

    Class<Enum> rootEnumClass = (Class<Enum>) resultsClassLoader
            .loadClass("com.example.enums.IntegerEnumAsRoot");

    assertThat(rootEnumClass.isEnum(), is(true));
    assertThat(rootEnumClass.getDeclaredMethod("fromValue", Integer.class), is(notNullValue()));
    assertThat(isPublic(rootEnumClass.getModifiers()), is(true));
}