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:org.openflamingo.uploader.el.ELService.java

public static Method findMethod(String className, String methodName) throws SystemException {
    Method method = null;
    try {//from w  w  w.  j  av  a  2 s .  co  m
        Class clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
        for (Method m : clazz.getMethods()) {
            if (m.getName().equals(methodName)) {
                method = m;
                break;
            }
        }
        if (method == null) {
            // throw new SystemException(ErrorCode.E0111, className, methodName);
        }
        if ((method.getModifiers() & (Modifier.PUBLIC | Modifier.STATIC)) != (Modifier.PUBLIC
                | Modifier.STATIC)) {
            // throw new SystemException(ErrorCode.E0112, className, methodName);
        }
    } catch (ClassNotFoundException ex) {
        // throw new SystemException(ErrorCode.E0113, className);
    }
    return method;
}

From source file:net.yasion.common.core.bean.wrapper.ExtendedBeanInfo.java

public static boolean isCandidateWriteMethod(Method method) {
    String methodName = method.getName();
    Class<?>[] parameterTypes = method.getParameterTypes();
    int nParams = parameterTypes.length;
    return (methodName.length() > 3 && methodName.startsWith("set") && Modifier.isPublic(method.getModifiers())
            && (!void.class.isAssignableFrom(method.getReturnType())
                    || Modifier.isStatic(method.getModifiers()))
            && (nParams == 1 || (nParams == 2 && parameterTypes[0].equals(int.class))));
}

From source file:br.com.lucasisrael.regra.reflections.TratamentoReflections.java

/**
 * Make the given method accessible, explicitly setting it accessible if
 * necessary. The {@code setAccessible(true)} method is only called
 * when actually necessary, to avoid unnecessary conflicts with a JVM
 * SecurityManager (if active)./* www  .j  a v a  2  s .com*/
 * @param method the method to make accessible
 * @see java.lang.reflect.Method#setAccessible
 */
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.google.code.siren4j.util.ReflectionUtils.java

/**
 * Determine if the method is a setter.//from  w w w . j a  v a 2s . com
 *
 * @param method
 * @return
 */
public static boolean isSetter(Method method) {
    String name = method.getName();
    String[] splitname = StringUtils.splitByCharacterTypeCamelCase(name);
    return method.getReturnType().equals(void.class) && Modifier.isPublic(method.getModifiers())
            && splitname[0].equals(SETTER_PREFIX);
}

From source file:com.google.code.siren4j.util.ReflectionUtils.java

/**
 * Determine if the method is a getter./*  www  .  j  a  v  a2  s .  co  m*/
 *
 * @param method
 * @return
 */
public static boolean isGetter(Method method) {
    String name = method.getName();
    String[] splitname = StringUtils.splitByCharacterTypeCamelCase(name);
    return !method.getReturnType().equals(void.class) && Modifier.isPublic(method.getModifiers())
            && (splitname[0].equals(GETTER_PREFIX_BOOLEAN) || splitname[0].equals(GETTER_PREFIX));
}

From source file:org.jaffa.qm.finder.apis.ExcelExportService.java

/**
 * Invokes the query method on the input service class passing the given criteria.
 *
 * @param criteriaClassName// w w w.  j  av a  2 s. c o  m
 *            the name of the criteria class.
 * @param criteriaObject
 *            criteria as a JSON structure.
 * @param serviceClassName
 *            the name of the service class that should contain the method
 *            'public AGraphDataObject[] query(AGraphCriteria criteria)'.
 * @return the output from the query invocation.
 * @throws ClassNotFoundException
 *             if either the criteria or the service class are invalid.
 * @throws NoSuchMethodException
 *             if the 'public AGraphDataObject[] query(AGraphCriteria
 *             criteria)' is not found on the service class.
 * @throws InstantiationException
 *             if an error occurs in instantiating the service class.
 * @throws IllegalAccessException
 *             if the query method is not accessible.
 * @throws IllegalArgumentException
 *             if the input is invalid.
 * @throws InvocationTargetException
 *             if an error occurs during invocation of the query method.
 */
private static Object[] invokeQueryService(String criteriaClassName, String criteriaObject,
        String serviceClassName, String serviceClassMethodName)
        throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException,
        IllegalArgumentException, InvocationTargetException {
    if (log.isDebugEnabled())
        log.debug("Invoking query service '" + serviceClassName + "' passing an instance of '"
                + criteriaClassName + "' having the criteria '" + criteriaObject + '\'');

    // convert the JSON criteria into a Java object
    Object criteria = jsonToBean(criteriaObject, criteriaClassName);

    // If no method name supplied, use default method "query"
    if (serviceClassMethodName == null || serviceClassMethodName.equals(""))
        serviceClassMethodName = "query";

    // find the 'public AGraphDataObject[] query(AGraphCriteria criteria)'
    // method on the service class
    Class serviceClass = Class.forName(serviceClassName);
    Method m = serviceClass.getMethod(serviceClassMethodName, criteria.getClass());

    // invoke the query method on the service
    Object service = m.getModifiers() == Modifier.STATIC ? null : serviceClass.newInstance();
    Object[] output = null;
    if (m.getReturnType().isArray()) {
        output = (Object[]) m.invoke(service, criteria);
    } else {
        Object gqr = m.invoke(service, criteria);
        output = (Object[]) BeanHelper.getField(gqr, "graphs");
    }
    if (log.isDebugEnabled())
        log.debug("Query output: " + Arrays.toString(output));
    return output;
}

From source file:org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory.java

private static boolean isGetter(Method method) {
    return ((method.getName().startsWith("get") && method.getReturnType() != Void.TYPE)
            || (method.getName().startsWith("is") && method.getReturnType().equals(boolean.class)))
            && method.getParameterTypes().length == 0 && !Modifier.isStatic(method.getModifiers());
}

From source file:org.jgentleframework.utils.control.Delegator.java

/**
 * Utility method to locate a proper Method object.
 * //from   w ww .  j  av  a 2  s . c o m
 * @param targetClass
 *            the target class
 * @param MethodName
 *            the method name
 * @param templ
 *            the templ
 * @return the method
 */
protected static Method findSuitableMethod(Class targetClass, String MethodName, Delegator templ) {

    Class[] args = templ.getArguments();
    Class retClass = templ.getReturn();
    // perfect match
    Method ret;
    try {
        ret = targetClass.getMethod(MethodName, args);
        if (!isValidReturn(ret, retClass))
            throw new IllegalArgumentException("Requested method returns wrong type");
        if (!Modifier.isPublic(ret.getModifiers()))
            throw new IllegalArgumentException("Requested method is not public");
        return (ret);
    } catch (SecurityException e) {
        if (log.isInfoEnabled()) {
            log.info("Could not found the suitable method of target class [" + targetClass + "]");
        }
    } catch (NoSuchMethodException e) {
        if (log.isInfoEnabled()) {
            log.info("Could not found the suitable method of target class [" + targetClass + "]");
        }
    }
    Method[] possibilities = getCandidateMethods(targetClass, MethodName, args.length);
    for (int i = 0; i < possibilities.length; i++) {
        Method possibility = possibilities[i];
        if (isSuitableMethod(possibility, args, retClass))
            return (possibility);
    }
    throw new IllegalArgumentException("No suitable method found");
}

From source file:it.unibo.alchemist.language.protelis.util.ProtelisLoader.java

private static MethodCall parseMethod(final JvmOperation jvmOp, final List<Expression> args,
        final Map<FasterString, FunctionDefinition> nameToFun,
        final Map<FunctionDef, FunctionDefinition> funToFun, final AtomicInteger id) {
    final boolean ztatic = jvmOp.isStatic();
    final List<AnnotatedTree<?>> arguments = parseArgs(args, nameToFun, funToFun, id);
    final String classname = jvmOp.getDeclaringType().getQualifiedName();
    try {//from w  w  w.  j av  a  2  s .  c om
        final Class<?> clazz = Class.forName(classname);
        /*
         * TODO: Check for return type and params: if param is Field and
         * return type is not then L.warn()
         */
        List<Method> tempList = new ArrayList<>();
        for (Method m : clazz.getMethods()) {
            if (ztatic) {
                if (Modifier.isStatic(m.getModifiers())) {
                    tempList.add(m);
                }
            }
        }
        /*
         * Same number of arguments
         */
        final int parameterCount = jvmOp.getParameters().size();
        List<Method> tempList2 = new ArrayList<>();
        for (Method m : tempList) {
            // TODO: Workaround for different Method API
            if (m.getParameterTypes().length == parameterCount) {
                tempList2.add(m);
            }
        }
        /*
         * Same name
         */
        final String methodName = jvmOp.getSimpleName();
        List<Method> res = new ArrayList<>();
        for (Method m : tempList2) {
            if (m.getName().equals(methodName)) {
                res.add(m);
            }
        }
        /*
         * There should be only one left - otherwise we have overloading,
         * and to properly deal with that we need type checking. The
         * following collection operation is for debug and warning purposes,
         * and should be removed as soon as we have a proper way to deal
         * with overloading in place. TODO
         */
        if (res.size() > 1) {
            final StringBuilder sb = new StringBuilder(64);
            sb.append("Method ");
            sb.append(jvmOp.getQualifiedName());
            sb.append('/');
            sb.append(parameterCount);
            sb.append(" is overloaded by:\n");
            for (Method m : res) {
                sb.append(m.toString());
                sb.append('\n'); // NOPMD
            }
            sb.append("Protelis can not (yet) properly deal with that.");
            L.warn(sb.toString());
        }
        if (res.isEmpty()) {
            throw new IllegalStateException("Can not bind any method that satisfies the name "
                    + jvmOp.getQualifiedName() + "/" + parameterCount + ".");
        }
        return new MethodCall(res.get(0), arguments, ztatic);
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException("Class " + classname + " could not be found in classpath.");
    } catch (SecurityException e) {
        throw new IllegalStateException(
                "Class " + classname + " could not be loaded due to security permissions.");
    } catch (Error e) {
        throw new IllegalStateException("An error occured while loading class " + classname + ".");
    }

}

From source file:es.caib.zkib.jxpath.util.ValueUtils.java

/**
 * Return an accessible method (that is, one that can be invoked via
 * reflection) that implements the specified Method.  If no such method
 * can be found, return <code>null</code>.
 *
 * @param method The method that we wish to call
 * @return Method/*from www  . ja v a  2s . com*/
 */
public static Method getAccessibleMethod(Method method) {

    // Make sure we have a method to check
    if (method == null) {
        return (null);
    }

    // If the requested method is not public we cannot call it
    if (!Modifier.isPublic(method.getModifiers())) {
        return (null);
    }

    // If the declaring class is public, we are done
    Class clazz = method.getDeclaringClass();
    if (Modifier.isPublic(clazz.getModifiers())) {
        return (method);
    }

    String name = method.getName();
    Class[] parameterTypes = method.getParameterTypes();
    while (clazz != null) {
        // Check the implemented interfaces and subinterfaces
        Method aMethod = getAccessibleMethodFromInterfaceNest(clazz, name, parameterTypes);
        if (aMethod != null) {
            return aMethod;
        }

        clazz = clazz.getSuperclass();
        if (clazz != null && Modifier.isPublic(clazz.getModifiers())) {
            try {
                return clazz.getDeclaredMethod(name, parameterTypes);
            } catch (NoSuchMethodException e) { //NOPMD
                //ignore
            }
        }
    }
    return null;
}