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.datalorax.populace.core.populate.GraphPopulator.java

private static boolean isNotInnerClass(final Class<?> type) {
    return type.getEnclosingClass() == null || Modifier.isStatic(type.getModifiers());
}

From source file:org.grouplens.grapht.util.Types.java

/**
 * Return true if the type is not abstract and not an interface, and has
 * a constructor annotated with {@link Inject} or its only constructor
 * is the default constructor.//from w w w  . ja  va 2  s .co m
 * 
 * @param type A class type
 * @return True if the class type is instantiable
 */
public static boolean isInstantiable(Class<?> type) {
    if (!Modifier.isAbstract(type.getModifiers()) && !type.isInterface()) {
        // first check for a constructor annotated with @Inject, 
        //  - this doesn't care how many we'll let the injector complain
        //    if there are more than one
        for (Constructor<?> c : type.getDeclaredConstructors()) {
            if (c.getAnnotation(Inject.class) != null) {
                return true;
            }
        }

        // check if we only have the public default constructor
        if (type.getConstructors().length == 1 && type.getConstructors()[0].getParameterTypes().length == 0) {
            return true;
        }
    }

    // no constructor available
    return false;
}

From source file:org.robobinding.util.MethodUtils.java

/**
 * <p>/*from ww  w . j ava2s .com*/
 * Returns an accessible method (that is, one that can be invoked via
 * reflection) by scanning through the superclasses. If no such method can
 * be found, return <code>null</code>.
 * </p>
 * 
 * @param cls
 *            Class to be checked
 * @param methodName
 *            Method name of the method we wish to call
 * @param parameterTypes
 *            The parameter type signatures
 * @return the accessible method or <code>null</code> if not found
 */
private static Method getAccessibleMethodFromSuperclass(final Class<?> cls, final String methodName,
        final Class<?>... parameterTypes) {
    Class<?> parentClass = cls.getSuperclass();
    while (parentClass != null) {
        if (Modifier.isPublic(parentClass.getModifiers())) {
            try {
                return parentClass.getMethod(methodName, parameterTypes);
            } catch (final NoSuchMethodException e) {
                return null;
            }
        }
        parentClass = parentClass.getSuperclass();
    }
    return null;
}

From source file:com.alta189.bukkit.script.event.EventScanner.java

public static void scanPlugin(Plugin plugin) {
    if (pluginEvents.containsKey(plugin)) {
        return;/*w  w  w  . ja  va  2s  .c  o  m*/
    }
    BScript.getInstance().debug("Scanning plugin " + plugin.getName());
    if (plugin instanceof JavaPlugin) {
        ClassLoader loader = ReflectionUtil.getFieldValue(JavaPlugin.class, plugin, "classLoader");
        Reflections reflections = new Reflections(new ConfigurationBuilder()
                .filterInputsBy(new FilterBuilder().exclude(FilterBuilder.prefix("org.bukkit")))
                .setUrls(ClasspathHelper.forClassLoader(loader)));

        Set<Class<? extends Event>> classes = reflections.getSubTypesOf(Event.class);

        BScript.getInstance().info("Found " + classes.size() + " classes extending "
                + Event.class.getCanonicalName() + " in " + plugin.getName());
        for (Class<? extends Event> clazz : classes) {
            if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) {
                continue;
            }
            BScript.getInstance().debug(clazz.getCanonicalName());

            String className = clazz.getCanonicalName();
            if (className == null) {
                className = clazz.getName();
            }
            BScript.getInstance().debug(className);
            events.put(className, clazz);
            String simpleName = clazz.getSimpleName();
            if (simpleNameEvents.get(simpleName) != null) {
                simpleNameEvents.remove(simpleName);
            } else {
                simpleNameEvents.put(simpleName, clazz);
            }
        }
        if (classes.size() > 0) {
            pluginEvents.put(plugin, classes);
        }
    } else {
        BScript.getInstance().debug("Plugin is not JavaPlugin " + plugin.getName());
    }
}

From source file:ShowClass.java

/**
 * Display the modifiers, name, superclass and interfaces of a class or
 * interface. Then go and list all constructors, fields, and methods.
 *//* w ww.  j a  va  2s.  c om*/
public static void print_class(Class c) {
    // Print modifiers, type (class or interface), name and superclass.
    if (c.isInterface()) {
        // The modifiers will include the "interface" keyword here...
        System.out.print(Modifier.toString(c.getModifiers()) + " " + typename(c));
    } else if (c.getSuperclass() != null) {
        System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c) + " extends "
                + typename(c.getSuperclass()));
    } else {
        System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c));
    }

    // Print interfaces or super-interfaces of the class or interface.
    Class[] interfaces = c.getInterfaces();
    if ((interfaces != null) && (interfaces.length > 0)) {
        if (c.isInterface())
            System.out.print(" extends ");
        else
            System.out.print(" implements ");
        for (int i = 0; i < interfaces.length; i++) {
            if (i > 0)
                System.out.print(", ");
            System.out.print(typename(interfaces[i]));
        }
    }

    System.out.println(" {"); // Begin class member listing.

    // Now look up and display the members of the class.
    System.out.println("  // Constructors");
    Constructor[] constructors = c.getDeclaredConstructors();
    for (int i = 0; i < constructors.length; i++)
        // Display constructors.
        print_method_or_constructor(constructors[i]);

    System.out.println("  // Fields");
    Field[] fields = c.getDeclaredFields(); // Look up fields.
    for (int i = 0; i < fields.length; i++)
        // Display them.
        print_field(fields[i]);

    System.out.println("  // Methods");
    Method[] methods = c.getDeclaredMethods(); // Look up methods.
    for (int i = 0; i < methods.length; i++)
        // Display them.
        print_method_or_constructor(methods[i]);

    System.out.println("}"); // End class member listing.
}

From source file:therian.Operation.java

private static boolean init(Class<?> type) {
    final boolean valid;
    synchronized (type) {
        if (VALID_INFO.containsKey(type)) {
            valid = VALID_INFO.get(type).booleanValue();
        } else if (Modifier.isAbstract(type.getModifiers())) {
            valid = true;/*  ww  w  .  j ava2 s. co m*/
        } else {
            final Type resultType = TypeUtils.unrollVariables(TypeUtils.getTypeArguments(type, Operation.class),
                    TYPE_VARIABLE_RESULT);
            valid = !TypeUtils.containsTypeVariables(resultType);
            Validate.isTrue(valid, "%s does not fully bind type parameter %s from %s", type,
                    TYPE_VARIABLE_RESULT.getName(), Operation.class);
            VALID_INFO.put(type, Boolean.valueOf(valid));
        }
    }

    final Class<?> parent = type.getSuperclass();
    if (!Operation.class.equals(parent)) {
        init(parent.asSubclass(Operation.class));
    }
    return valid;
}

From source file:com.sunchenbin.store.feilong.core.lang.ClassUtil.java

/**
 * ??.//from   ww w. j a v a2 s . co  m
 * 
 * @param ownerClass
 *            class
 * @return true
 * @see java.lang.Class#getModifiers()
 * @see java.lang.reflect.Modifier#isInterface(int)
 */
public static boolean isInterface(Class<?> ownerClass) {
    int flag = ownerClass.getModifiers();// ?? Java 
    return Modifier.isInterface(flag);// ??
}

From source file:nl.strohalm.cyclos.utils.ClassHelper.java

/**
 * If the class is concrete, return it. Else, try to find a well known subclass
 * @param <T> The specified class type
 * @param <C> The resulting class type
 * @param clazz The specified class//from   www.  ja v a2 s . c  o  m
 * @return The class if it is concrete, or a well-known subclass.
 * @throws IllegalArgumentException The specified class is abstract (or interface) and not a well-known one
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T, C extends T> Class<C> concreteClass(final Class<T> clazz) {
    if (clazz == null) {
        throw new NullPointerException("null.clazz");
    }
    Class<C> ret = null;
    final int modifiers = clazz.getModifiers();
    if (Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers)
            || Collection.class.isAssignableFrom(clazz)) {
        if (Calendar.class.isAssignableFrom(clazz)) {
            ret = (Class) GregorianCalendar.class;
        } else if (SortedSet.class.isAssignableFrom(clazz)) {
            ret = (Class) TreeSet.class;
        } else if (Set.class.isAssignableFrom(clazz)) {
            ret = (Class) LinkedHashSet.class;
        } else if (Collection.class.isAssignableFrom(clazz)) {
            ret = (Class) ArrayList.class;
        } else if (SortedMap.class.isAssignableFrom(clazz)) {
            ret = (Class) TreeMap.class;
        } else if (Map.class.isAssignableFrom(clazz)) {
            ret = (Class) LinkedHashMap.class;
        } else {
            throw new IllegalArgumentException("Unknown concrete class for " + clazz.getName());
        }
    } else {
        ret = (Class) clazz;
    }
    return ret;
}

From source file:net.servicefixture.util.ReflectionUtils.java

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

From source file:org.cruxframework.crux.core.ioc.IocContainerManager.java

public synchronized static void initialize() {
    if (!initialized) {
        initialized = true;//from  ww w  .ja  v a  2  s.c  o m
        try {
            Set<String> configurations = ClassScanner.searchClassesByInterface(IocConfiguration.class);
            if (configurations != null) {
                for (String configurationClassName : configurations) {
                    Class<?> configurationClass = Class.forName(configurationClassName);
                    if (!Modifier.isAbstract(configurationClass.getModifiers())
                            && IocContainerConfigurations.class.isAssignableFrom(configurationClass)) {
                        IocContainerConfigurations configuration = (IocContainerConfigurations) configurationClass
                                .newInstance();
                        if (configuration.isEnabled()) {
                            if (logger.isInfoEnabled()) {
                                logger.info("Configuring new ioc module [" + configurationClassName + "]...");
                            }
                            configuration.configure();
                        }
                    }
                }
            }

            configureAnnotatedClasses();
        } catch (Exception e) {
            logger.error("Error initializing ioc container.", e);
        }
    }
}