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.norconex.commons.lang.ClassFinder.java

private static String resolveName(ClassLoader loader, String rawName, Class<?> superClass) {
    String className = rawName;//w  ww. jav a2s  .com
    if (!rawName.endsWith(".class") || className.contains("$")) {
        return null;
    }
    className = className.replaceAll("[\\\\/]", ".");
    className = StringUtils.removeStart(className, ".");
    className = StringUtils.removeEnd(className, ".class");

    try {
        Class<?> clazz = loader.loadClass(className);
        // load only concrete implementations
        if (!clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers())
                && superClass.isAssignableFrom(clazz)) {
            return clazz.getName();
        }
    } catch (ClassNotFoundException e) {
        LOG.error("Invalid class: " + className, e);
    } catch (NoClassDefFoundError e) {
        LOG.debug("Invalid class: " + className, e);
    }
    return null;
}

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

/**
 *  <code>ownerClass</code> ??.
 * /*from  w ww . j  a  v  a2s. co  m*/
 * @param ownerClass
 *            class
 * @return true<br>
 *          <code>ownerClass</code> null,false
 * @see java.lang.Class#getModifiers()
 * @see java.lang.reflect.Modifier#isInterface(int)
 */
public static boolean isInterface(Class<?> ownerClass) {
    return null == ownerClass ? false : Modifier.isInterface(ownerClass.getModifiers());// ??
}

From source file:org.springframework.messaging.tcp.reactor.ReactorTcpClient.java

private static void checkReactorVersion() {
    Class<?> type = null;
    try {/* w  w  w  . jav  a 2s  .c om*/
        type = ReactorTcpClient.class.getClassLoader().loadClass("reactor.event.dispatch.BaseDispatcher");
        Assert.isTrue(Modifier.isPublic(type.getModifiers()),
                "Detected older version of reactor-tcp. Switch to 1.0.1.RELEASE or higher.");
    } catch (ClassNotFoundException e) {
        // Ignore, must be 1.1+
    }
}

From source file:org.apache.sling.scripting.sightly.impl.compiler.CompileTimeObjectModel.java

private static Method extractMethodInheritanceChain(Class type, Method m) {
    if (m == null || Modifier.isPublic(type.getModifiers())) {
        return m;
    }/*  w w w .  j  ava  2  s  . c  om*/
    Class[] iFaces = type.getInterfaces();
    Method mp;
    for (Class<?> iFace : iFaces) {
        mp = getClassMethod(iFace, m);
        if (mp != null) {
            return mp;
        }
    }
    return getClassMethod(type.getSuperclass(), m);
}

From source file:org.xulux.utils.ClassLoaderUtils.java

/**
 * Also instantiates static AND non static innerclasses.
 * The parent class needs to have an empty constructor!
 * You can overcome this problem by adding this to the paramlist!
 * @param clazz the class//w w  w  .j ava  2  s  .c  o m
 * @return an object from the specified class or null when errors occur
 */
public static Object getObjectFromClass(Class clazz) {
    if (clazz == null) {
        return null;
    }
    Object object = null;
    try {
        if (isInner(clazz) && !Modifier.isStatic(clazz.getModifiers())) {
            Object parent = getParentObjectForInnerClass(clazz);
            if (parent != null) {
                Constructor constructor = clazz.getConstructor(new Class[] { parent.getClass() });
                object = constructor.newInstance(new Object[] { parent });
            }
        } else {
            // static inner classes can be instantiated without a problem..
            object = clazz.newInstance();
        }
        return object;
    } catch (InvocationTargetException e) {
        log.warn("Cannot invocate target on " + clazz.getName());
    } catch (NoSuchMethodException e) {
        log.warn("Cannot find method on " + clazz.getName());
    } catch (InstantiationException e) {
        log.warn("Cannot instantiate class " + clazz.getName());
    } catch (IllegalAccessException e) {
        log.warn("Cannot access class " + clazz.getName());
    } catch (NoClassDefFoundError e) {
        log.warn("Cannot find class definition for class " + clazz.getName());
    }
    return null;
}

From source file:org.apache.tajo.engine.function.FunctionLoader.java

/**
 * This method finds and build FunctionDesc for the legacy function and UD(A)F system.
 *
 * @return A list of FunctionDescs/*from   w w w .  j  av  a 2s.c  om*/
 */
public static List<FunctionDesc> findLegacyFunctions() {
    List<FunctionDesc> sqlFuncs = new ArrayList<>();

    Set<Class> functionClasses = ClassUtil.findClasses(Function.class, "org.apache.tajo.engine.function");

    for (Class eachClass : functionClasses) {
        if (eachClass.isInterface() || Modifier.isAbstract(eachClass.getModifiers())) {
            continue;
        }
        Function function = null;
        try {
            function = (Function) eachClass.newInstance();
        } catch (Exception e) {
            LOG.warn(eachClass + " cannot instantiate Function class because of " + e.getMessage(), e);
            continue;
        }
        String functionName = function.getClass().getAnnotation(Description.class).functionName();
        String[] synonyms = function.getClass().getAnnotation(Description.class).synonyms();
        String description = function.getClass().getAnnotation(Description.class).description();
        String detail = function.getClass().getAnnotation(Description.class).detail();
        String example = function.getClass().getAnnotation(Description.class).example();
        TajoDataTypes.Type returnType = function.getClass().getAnnotation(Description.class).returnType();
        ParamTypes[] paramArray = function.getClass().getAnnotation(Description.class).paramTypes();

        String[] allFunctionNames = null;
        if (synonyms != null && synonyms.length > 0) {
            allFunctionNames = new String[1 + synonyms.length];
            allFunctionNames[0] = functionName;
            System.arraycopy(synonyms, 0, allFunctionNames, 1, synonyms.length);
        } else {
            allFunctionNames = new String[] { functionName };
        }

        for (String eachFunctionName : allFunctionNames) {
            for (ParamTypes params : paramArray) {
                ParamOptionTypes[] paramOptionArray;
                if (params.paramOptionTypes() == null
                        || params.paramOptionTypes().getClass().getAnnotation(ParamTypes.class) == null) {
                    paramOptionArray = new ParamOptionTypes[0];
                } else {
                    paramOptionArray = params.paramOptionTypes().getClass().getAnnotation(ParamTypes.class)
                            .paramOptionTypes();
                }

                TajoDataTypes.Type[] paramTypes = params.paramTypes();
                if (paramOptionArray.length > 0)
                    paramTypes = params.paramTypes().clone();

                for (int i = 0; i < paramOptionArray.length + 1; i++) {
                    FunctionDesc functionDesc = new FunctionDesc(eachFunctionName, function.getClass(),
                            function.getFunctionType(), CatalogUtil.newSimpleDataType(returnType),
                            paramTypes.length == 0 ? CatalogUtil.newSimpleDataTypeArray()
                                    : CatalogUtil.newSimpleDataTypeArray(paramTypes));

                    functionDesc.setDescription(description);
                    functionDesc.setExample(example);
                    functionDesc.setDetail(detail);
                    sqlFuncs.add(functionDesc);

                    if (i != paramOptionArray.length) {
                        paramTypes = new TajoDataTypes.Type[paramTypes.length
                                + paramOptionArray[i].paramOptionTypes().length];
                        System.arraycopy(params.paramTypes(), 0, paramTypes, 0, paramTypes.length);
                        System.arraycopy(paramOptionArray[i].paramOptionTypes(), 0, paramTypes,
                                paramTypes.length, paramOptionArray[i].paramOptionTypes().length);
                    }
                }
            }
        }
    }

    return sqlFuncs;
}

From source file:org.alfresco.module.org_alfresco_module_rm.api.PublicAPITestUtil.java

/**
 * Get all the classes referenced by the given class, which might be used by an extension. We consider visible
 * methods, constructors, fields and inner classes, as well as superclasses and interfaces extended by the class.
 *
 * @param initialClass The class to analyse.
 * @param consideredClasses Classes that have already been considered, and which should not be considered again. If
 *            the given class has already been considered then an empty set will be returned. This set will be
 *            updated with the given class.
 * @return The set of classes that might be accessible by an extension of this class.
 *//* ww  w . ja v a2  s .co  m*/
private static Set<Class<?>> getReferencedClassesFromClass(Class<?> initialClass,
        Set<Class<?>> consideredClasses) {
    Set<Class<?>> referencedClasses = new HashSet<>();

    if (consideredClasses.add(initialClass)) {
        for (Method method : initialClass.getDeclaredMethods()) {
            if (isVisibleToExtender(method.getModifiers())) {
                referencedClasses.addAll(getClassesFromMethod(method));
            }
        }
        for (Constructor<?> constructor : initialClass.getDeclaredConstructors()) {
            if (isVisibleToExtender(constructor.getModifiers())) {
                referencedClasses.addAll(getClassesFromConstructor(constructor));
            }
        }
        for (Field field : initialClass.getDeclaredFields()) {
            if (isVisibleToExtender(field.getModifiers())) {
                referencedClasses.addAll(getClassesFromField(field));
            }
        }
        for (Class<?> clazz : initialClass.getDeclaredClasses()) {
            if (isVisibleToExtender(clazz.getModifiers())) {
                referencedClasses.addAll(getReferencedClassesFromClass(clazz, consideredClasses));
            }
        }
        if (initialClass.getSuperclass() != null) {
            referencedClasses
                    .addAll(getReferencedClassesFromClass(initialClass.getSuperclass(), consideredClasses));
        }
        for (Class<?> clazz : initialClass.getInterfaces()) {
            referencedClasses.addAll(getReferencedClassesFromClass(clazz, consideredClasses));
        }
    }
    return referencedClasses;
}

From source file:io.github.benas.randombeans.util.ReflectionUtils.java

/**
 * Check if the type is public.//from   w  w w .  j  av  a  2s.  c o m
 *
 * @param type the type to check
 * @param <T>  the actual type to check
 * @return true if the type is public, false otherwise
 */
public static <T> boolean isPublic(final Class<T> type) {
    return Modifier.isPublic(type.getModifiers());
}

From source file:io.github.benas.randombeans.util.ReflectionUtils.java

/**
 * Check if the type is abstract (either an interface or an abstract class).
 *
 * @param type the type to check/* ww w. j a v  a 2s .c o  m*/
 * @param <T>  the actual type to check
 * @return true if the type is abstract, false otherwise
 */
public static <T> boolean isAbstract(final Class<T> type) {
    return Modifier.isAbstract(type.getModifiers());
}

From source file:org.openmrs.module.openhmis.cashier.api.ReceiptNumberGeneratorFactory.java

/**
 * Locates and instantiates all classes that implement {@link IReceiptNumberGenerator} in the current classpath.
 * @return The instantiated receipt number generators.
 * @should Locate all classes that implement IReceiptNumberGenerator
 * @should Not throw an exception if the class instantiation fails
 * @should Use the existing instance for the currently defined generator
 *//*from   w  w w. j av a  2 s. c  o  m*/
public static IReceiptNumberGenerator[] locateGenerators() {
    // Search for any modules that define classes which implement the IReceiptNumberGenerator interface
    Reflections reflections = new Reflections("org.openmrs.module");
    List<Class<? extends IReceiptNumberGenerator>> classes = new ArrayList<Class<? extends IReceiptNumberGenerator>>();
    for (Class<? extends IReceiptNumberGenerator> cls : reflections
            .getSubTypesOf(IReceiptNumberGenerator.class)) {
        // We only care about public instantiable classes so ignore others
        if (!cls.isInterface() && !Modifier.isAbstract(cls.getModifiers())
                && Modifier.isPublic(cls.getModifiers())) {
            classes.add(cls);
        }
    }

    // Now attempt to instantiate each found class
    List<IReceiptNumberGenerator> instances = new ArrayList<IReceiptNumberGenerator>();
    for (Class<? extends IReceiptNumberGenerator> cls : classes) {
        if (generator != null && cls.equals(generator.getClass())) {
            instances.add(generator);
        } else {
            try {
                instances.add(cls.newInstance());
            } catch (Exception ex) {
                // We don't care about specific exceptions here.  Just log and ignore the class
                LOG.warn("Could not instantiate the '" + cls.getName() + "' class.  It will be ignored.");
            }
        }
    }

    // Finally, copy the instances to an array
    IReceiptNumberGenerator[] results = new IReceiptNumberGenerator[instances.size()];
    instances.toArray(results);

    return results;
}