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.qmetry.qaf.automation.step.JavaStepFinder.java

private static Set<Method> getAllMethodsWithAnnotation(Collection<Class<?>> classes,
        Class<? extends Annotation> annotation) {

    Set<Method> methods = new HashSet<Method>();
    for (Class<?> cls : classes) {

        if (cls.isInterface() || Modifier.isAbstract(cls.getModifiers()))
            continue;

        boolean isStepProvider = cls.isAnnotationPresent(QAFTestStepProvider.class);

        for (Method method : cls.getMethods()) {
            if (isStepProvider || ClassUtil.hasAnnotation(method, annotation)) {
                methods.add(method);// ww  w .  ja v  a  2  s  .  co m
            }
        }

    }

    return methods;
}

From source file:gov.nih.nci.caarray.external.v1_0.AbstractCaArrayEntityTest.java

public static <T> List<Class<? extends T>> findLocalSubclasses(Class<T> c)
        throws ClassNotFoundException, URISyntaxException {
    List<Class<? extends T>> l = new ArrayList<Class<? extends T>>();
    File dir = new File(c.getProtectionDomain().getCodeSource().getLocation().toURI());
    Iterator<File> fi = FileUtils.iterateFiles(dir, new String[] { "class" }, true);
    int prefix = dir.getAbsolutePath().length() + 1;
    int suffix = ".class".length();
    while (fi.hasNext()) {
        File cf = fi.next();//from w w  w.ja v  a  2s.co m
        String fn = cf.getAbsolutePath();
        try {
            String cn = fn.substring(prefix, fn.length() - suffix);
            if (cn.endsWith("<error>")) {
                continue;
            }
            cn = cn.replace('/', '.');
            System.out.println("cn = " + cn);
            Class tmp = c.getClassLoader().loadClass(cn);
            if ((tmp.getModifiers() & Modifier.ABSTRACT) != 0) {
                continue;
            }
            if (c.isAssignableFrom(tmp)) {
                l.add(tmp);
                System.out.println("added " + cf.getAbsolutePath() + " as " + cn);
            }
        } catch (Exception e) {
            System.err.println(fn);
            e.printStackTrace();
        }
    }
    return l;
}

From source file:org.lunarray.model.descriptor.creational.util.CreationalScanUtil.java

/**
 * Find a no-args public constructor./*from  w  ww.  j a va  2  s .c  om*/
 * 
 * @param source
 *            The source class. May not be null.
 * @return The constructor, or null.
 * @param <E>
 *            The entity type.
 */
@SuppressWarnings("unchecked")
public static <E> Constructor<E> findConstructor(final Class<E> source) {
    Validate.notNull(source, "Source may not be null.");
    Constructor<E> result = null;
    if (!Modifier.isAbstract(source.getModifiers()) && !source.isSynthetic()) {
        for (final Constructor<E> constructor : (Constructor<E>[]) source.getConstructors()) {
            if (!constructor.isSynthetic() && (constructor.getParameterTypes().length == 0)) {
                result = constructor;
            }
        }
    }
    return result;
}

From source file:io.github.pellse.decorator.util.reflection.ReflectionUtils.java

public static boolean isAbstract(Class<?> clazz) {
    return clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers());
}

From source file:com.dianping.squirrel.client.util.ClassUtils.java

public static <T> Constructor<T> getDeclaredConstructor(Constructor<T> ctor) {
    if (ctor == null) {
        return (null);
    }//  w  ww .j a va  2s  .  c  o  m
    Class<T> clazz = ctor.getDeclaringClass();
    if (Modifier.isPublic(clazz.getModifiers())) {
        return (ctor);
    }
    return null;

}

From source file:org.lanternpowered.server.util.ReflectionHelper.java

public static <T> T createInstance(final Class<T> objectClass, @Nullable Object... args) {
    checkArgument(!Modifier.isAbstract(objectClass.getModifiers()),
            "Cannot construct an instance of an abstract class!");
    checkArgument(!Modifier.isInterface(objectClass.getModifiers()),
            "Cannot construct an instance of an interface!");
    if (args == null) {
        args = new Object[] { null };
    }//from   w ww .  ja  v  a2s  . c o m
    final Constructor<T> ctor = findConstructor(objectClass, args);
    try {
        return ctor.newInstance(args);
    } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
        Lantern.getLogger().error("Couldn't find an appropriate constructor for "
                + objectClass.getCanonicalName() + "with the args: " + Arrays.toString(args), e);
    }
    throw new IllegalArgumentException("Couldn't find an appropriate constructor for "
            + objectClass.getCanonicalName() + "the args: " + Arrays.toString(args));
}

From source file:MyClass.java

public static String getClassDescription(Class c) {
    StringBuilder classDesc = new StringBuilder();
    int modifierBits = 0;
    String keyword = "";
    if (c.isInterface()) {
        modifierBits = c.getModifiers() & Modifier.interfaceModifiers();
        if (c.isAnnotation()) {
            keyword = "@interface";
        } else {/*www .  ja v  a  2s.  com*/
            keyword = "interface";
        }
    } else if (c.isEnum()) {
        modifierBits = c.getModifiers() & Modifier.classModifiers();
        keyword = "enum";
    }
    modifierBits = c.getModifiers() & Modifier.classModifiers();
    keyword = "class";

    String modifiers = Modifier.toString(modifierBits);
    classDesc.append(modifiers);
    classDesc.append(" " + keyword);
    String simpleName = c.getSimpleName();
    classDesc.append(" " + simpleName);

    String genericParms = getGenericTypeParams(c);
    classDesc.append(genericParms);

    Class superClass = c.getSuperclass();
    if (superClass != null) {
        String superClassSimpleName = superClass.getSimpleName();
        classDesc.append("  extends " + superClassSimpleName);
    }
    String interfaces = Main.getClassInterfaces(c);
    if (interfaces != null) {
        classDesc.append("  implements " + interfaces);
    }
    return classDesc.toString();
}

From source file:game.utils.Utils.java

public static boolean isConcrete(Class type) {
    return type.isPrimitive()
            || (!Modifier.isAbstract(type.getModifiers()) && !Modifier.isInterface(type.getModifiers()));
}

From source file:com.linecorp.armeria.common.thrift.text.StructContext.java

private static boolean isAbstract(Class clazz) {
    return Modifier.isAbstract(clazz.getModifiers());
}

From source file:Main.java

/**
 * Converts the given array to a set with the given type. Works like
 * {@link Arrays#asList(Object...)}.//from   www. j  a  v a 2 s  .c  o  m
 *
 * @param <T>
 *            The type of the set values.
 * @param setType
 *            The set type to return.
 * @param arr
 *            The array to convert into a set.
 * @return An object of the given set or, if, and only if, an error
 *         occurred, <code>null</code>.
 */
@SuppressWarnings("unchecked")
public static <T> Set<T> toSet(@SuppressWarnings("rawtypes") final Class<? extends Set> setType,
        final T... arr) {
    assert setType != null : "SetType cannot be null!";
    assert !Modifier.isAbstract(setType.getModifiers()) : "SetType cannot be abstract!";
    assert !setType.isInterface() : "SetType cannot be an interface!";
    assert arr != null : "Arr cannot be null!";

    Set<T> result = null;
    try {
        result = setType.newInstance();

        for (final T t : arr) {
            result.add(t);
        }
    } catch (final InstantiationException ex) {
        ex.printStackTrace();
    } catch (final IllegalAccessException ex) {
        ex.printStackTrace();
    }

    return result;
}