Example usage for java.lang.reflect Method getModifiers

List of usage examples for java.lang.reflect Method getModifiers

Introduction

In this page you can find the example usage for java.lang.reflect Method getModifiers.

Prototype

@Override
public int getModifiers() 

Source Link

Usage

From source file:$.Reflections.java

/**
     * ?private/protectedpublic?????JDKSecurityManager
     *///w w w  .ja  v a2 s .c o  m
    public static void makeAccessible(Method method) {
        if ((!Modifier.isPublic(method.getModifiers())
                || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) && !method.isAccessible()) {
            method.setAccessible(true);
        }
    }

From source file:com.example.basedemo.view.ViewInjectorImpl.java

@SuppressWarnings("ConstantConditions")
private static void injectObject(Object handler, Class<?> handlerType, ViewFinder finder) {

    if (handlerType == null || IGNORED.contains(handlerType)) {
        return;/*from w ww  .  ja  v  a  2s.  c  o  m*/
    }

    // ?
    injectObject(handler, handlerType.getSuperclass(), finder);

    // inject view
    Field[] fields = handlerType.getDeclaredFields();
    if (fields != null && fields.length > 0) {
        for (Field field : fields) {

            Class<?> fieldType = field.getType();
            if (
            /* ??? */ Modifier.isStatic(field.getModifiers()) ||
            /* ?final */ Modifier.isFinal(field.getModifiers()) ||
            /* ? */ fieldType.isPrimitive() ||
            /* ? */ fieldType.isArray()) {
                continue;
            }

            ViewInject viewInject = field.getAnnotation(ViewInject.class);
            if (viewInject != null) {
                try {
                    View view = finder.findViewById(viewInject.value(), viewInject.parentId());
                    if (view != null) {
                        field.setAccessible(true);
                        field.set(handler, view);
                    } else {
                        throw new RuntimeException("Invalid @ViewInject for " + handlerType.getSimpleName()
                                + "." + field.getName());
                    }
                } catch (Throwable ex) {
                    Log.e(ex.getMessage(), ex.toString());
                }
            }
        }
    } // end inject view

    // inject event
    Method[] methods = handlerType.getDeclaredMethods();
    if (methods != null && methods.length > 0) {
        for (Method method : methods) {

            if (Modifier.isStatic(method.getModifiers()) || !Modifier.isPrivate(method.getModifiers())) {
                continue;
            }

            //??event
            Event event = method.getAnnotation(Event.class);
            if (event != null) {
                try {
                    // id?
                    int[] values = event.value();
                    int[] parentIds = event.parentId();
                    int parentIdsLen = parentIds == null ? 0 : parentIds.length;
                    //id?ViewInfo???
                    for (int i = 0; i < values.length; i++) {
                        int value = values[i];
                        if (value > 0) {
                            ViewInfo info = new ViewInfo();
                            info.value = value;
                            info.parentId = parentIdsLen > i ? parentIds[i] : 0;
                            method.setAccessible(true);
                            EventListenerManager.addEventMethod(finder, info, event, handler, method);
                        }
                    }
                } catch (Throwable ex) {
                    Log.e(ex.getMessage(), ex.toString());
                }
            }
        }
    } // end inject event

}

From source file:com.asuka.android.asukaandroid.view.ViewInjectorImpl.java

@SuppressWarnings("ConstantConditions")
private static void injectObject(Object handler, Class<?> handlerType, ViewFinder finder) {

    if (handlerType == null || IGNORED.contains(handlerType)) {
        return;//from   w ww . j  av a2s  .c o m
    }

    // ?
    injectObject(handler, handlerType.getSuperclass(), finder);

    // inject view
    Field[] fields = handlerType.getDeclaredFields();
    if (fields != null && fields.length > 0) {
        for (Field field : fields) {

            Class<?> fieldType = field.getType();
            if (
            /* ??? */ Modifier.isStatic(field.getModifiers()) ||
            /* ?final */ Modifier.isFinal(field.getModifiers()) ||
            /* ? */ fieldType.isPrimitive() ||
            /* ? */ fieldType.isArray()) {
                continue;
            }

            ViewInject viewInject = field.getAnnotation(ViewInject.class);
            if (viewInject != null) {
                try {
                    View view = finder.findViewById(viewInject.value(), viewInject.parentId());
                    if (view != null) {
                        field.setAccessible(true);
                        field.set(handler, view);
                    } else {
                        throw new RuntimeException("Invalid @ViewInject for " + handlerType.getSimpleName()
                                + "." + field.getName());
                    }
                } catch (Throwable ex) {
                    LogUtil.e(ex.getMessage(), ex);
                }
            }
        }
    } // end inject view

    // inject event
    Method[] methods = handlerType.getDeclaredMethods();
    if (methods != null && methods.length > 0) {
        for (Method method : methods) {

            if (Modifier.isStatic(method.getModifiers()) || !Modifier.isPrivate(method.getModifiers())) {
                continue;
            }

            //??event
            Event event = method.getAnnotation(Event.class);
            if (event != null) {
                try {
                    // id?
                    int[] values = event.value();
                    int[] parentIds = event.parentId();
                    int parentIdsLen = parentIds == null ? 0 : parentIds.length;
                    //id?ViewInfo???
                    for (int i = 0; i < values.length; i++) {
                        int value = values[i];
                        if (value > 0) {
                            ViewInfo info = new ViewInfo();
                            info.value = value;
                            info.parentId = parentIdsLen > i ? parentIds[i] : 0;
                            method.setAccessible(true);
                            EventListenerManager.addEventMethod(finder, info, event, handler, method);
                        }
                    }
                } catch (Throwable ex) {
                    LogUtil.e(ex.getMessage(), ex);
                }
            }
        }
    } // end inject event

}

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

private static StaticMethodInvocationDesc extractStaticMethodInvocation(Method method) {
    Preconditions.checkArgument(Modifier.isPublic(method.getModifiers()));
    Preconditions.checkArgument(Modifier.isStatic(method.getModifiers()));

    String methodName = method.getName();
    Class returnClass = method.getReturnType();
    Class[] paramClasses = method.getParameterTypes();
    return new StaticMethodInvocationDesc(method.getDeclaringClass(), methodName, returnClass, paramClasses);
}

From source file:com.cnksi.core.tools.utils.Reflections.java

/**
 * ?private/protectedpublic?????JDKSecurityManager
 *///from   w w w .java  2 s . c om
public static void makeAccessible(Method method) {

    if ((!Modifier.isPublic(method.getModifiers())
            || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) && !method.isAccessible()) {
        method.setAccessible(true);
    }
}

From source file:com.nridge.core.base.field.data.DataBeanBag.java

/**
 * Accepts a POJO containing one or more public annotated get
 * methods and creates a DataBag from them.  The DataBeanObject2
 * test class provides a reference example.
 *
 * @param anObject POJO instance.// w w  w.j av a2 s .c  om
 *
 * @return Data bag instance populated with field information.
 *
 * @throws NSException Thrown if object is null.
 * @throws IllegalAccessException Thrown if access is illegal.
 * @throws InvocationTargetException Thrown if target execution fails.
 */
public static DataBag fromMethodsToBag(Object anObject)
        throws NSException, IllegalAccessException, InvocationTargetException {
    DataField dataField;
    BeanField beanField;
    boolean isPublicAccess, isAnnotationPresent;

    if (anObject == null)
        throw new NSException("Object is null");

    DataBag dataBag = new DataBag(anObject.toString());
    Class<?> objClass = anObject.getClass();
    Method[] methodArray = objClass.getDeclaredMethods();
    for (Method objMethod : methodArray) {
        isPublicAccess = Modifier.isPublic(objMethod.getModifiers());
        isAnnotationPresent = objMethod.isAnnotationPresent(BeanField.class);
        if ((isAnnotationPresent) && (isPublicAccess)) {
            beanField = objMethod.getAnnotation(BeanField.class);
            dataField = reflectMethod(anObject, beanField, objMethod);
            dataBag.add(dataField);
        }
    }

    return dataBag;
}

From source file:ReflectionTest.java

/**
 * Prints all methods of a class/*from  w w w. jav a 2 s . c o  m*/
 * @param cl a class
 */
public static void printMethods(Class cl) {
    Method[] methods = cl.getDeclaredMethods();

    for (Method m : methods) {
        Class retType = m.getReturnType();
        String name = m.getName();

        System.out.print("   ");
        // print modifiers, return type and method name
        String modifiers = Modifier.toString(m.getModifiers());
        if (modifiers.length() > 0)
            System.out.print(modifiers + " ");
        System.out.print(retType.getName() + " " + name + "(");

        // print parameter types
        Class[] paramTypes = m.getParameterTypes();
        for (int j = 0; j < paramTypes.length; j++) {
            if (j > 0)
                System.out.print(", ");
            System.out.print(paramTypes[j].getName());
        }
        System.out.println(");");
    }
}

From source file:org.evosuite.setup.TestClusterUtils.java

public static void makeAccessible(Method method) {
    if (!Modifier.isPublic(method.getModifiers())
            || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) {
        method.setAccessible(true);/* w w w  .  j ava 2 s  .co  m*/
    }
}

From source file:cop.raml.mocks.MockUtils.java

public static ExecutableElementMock createExecutable(Method method) {
    if (method == null)
        return null;

    boolean isStatic = Modifier.isStatic(method.getModifiers());
    String params = Arrays.stream(method.getParameterTypes()).map(Class::getSimpleName)
            .collect(Collectors.joining(","));
    String name = String.format("(%s)%s", params, method.getReturnType().getSimpleName());

    return new ExecutableElementMock(method.getName() + "()", createMethodElement(name).asType())
            .setStatic(isStatic).setSimpleName(method.getName());
}

From source file:org.apache.tapestry.listener.ListenerMap.java

private static Map buildMethodMap(Class beanClass) {
    if (LOG.isDebugEnabled())
        LOG.debug("Building method map for class " + beanClass.getName());

    Map result = new HashMap();
    Method[] methods = beanClass.getMethods();

    for (int i = 0; i < methods.length; i++) {
        Method m = methods[i];
        int mods = m.getModifiers();

        if (Modifier.isStatic(mods))
            continue;

        // Probably not necessary, getMethods() returns only public
        // methods.

        if (!Modifier.isPublic(mods))
            continue;

        // Must return void

        if (m.getReturnType() != Void.TYPE)
            continue;

        Class[] parmTypes = m.getParameterTypes();

        if (parmTypes.length != 1)
            continue;

        // parm must be IRequestCycle

        if (!parmTypes[0].equals(IRequestCycle.class))
            continue;

        // Ha!  Passed all tests.

        result.put(m.getName(), m);/*from ww  w.  j  av a2s  .co  m*/
    }

    return result;

}