Example usage for java.lang.reflect Modifier isPublic

List of usage examples for java.lang.reflect Modifier isPublic

Introduction

In this page you can find the example usage for java.lang.reflect Modifier isPublic.

Prototype

public static boolean isPublic(int mod) 

Source Link

Document

Return true if the integer argument includes the public modifier, false otherwise.

Usage

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

protected static void makeAccessible(Method method) {
    if (!Modifier.isPublic(method.getModifiers())
            || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) {
        method.setAccessible(true);//  w w  w  . ja  va 2s.c o m
    }
}

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

protected static void makeAccessible(Constructor<?> constructor) {
    if (!Modifier.isPublic(constructor.getModifiers())
            || !Modifier.isPublic(constructor.getDeclaringClass().getModifiers())) {
        constructor.setAccessible(true);
    }/*from   www  . ja  v  a  2s.c om*/
}

From source file:javadz.beanutils.MethodUtils.java

/**
 * <p>Return an accessible method (that is, one that can be invoked via
 * reflection) that implements the specified method, by scanning through
 * all implemented interfaces and subinterfaces.  If no such method
 * can be found, return <code>null</code>.</p>
 *
 * <p> There isn't any good reason why this method must be private.
 * It is because there doesn't seem any reason why other classes should
 * call this rather than the higher level methods.</p>
 *
 * @param clazz Parent class for the interfaces to be checked
 * @param methodName Method name of the method we wish to call
 * @param parameterTypes The parameter type signatures
 *///from   www.  j ava  2  s  .c  om
private static Method getAccessibleMethodFromInterfaceNest(Class clazz, String methodName,
        Class[] parameterTypes) {

    Method method = null;

    // Search up the superclass chain
    for (; clazz != null; clazz = clazz.getSuperclass()) {

        // Check the implemented interfaces of the parent class
        Class[] interfaces = clazz.getInterfaces();
        for (int i = 0; i < interfaces.length; i++) {

            // Is this interface public?
            if (!Modifier.isPublic(interfaces[i].getModifiers())) {
                continue;
            }

            // Does the method exist on this interface?
            try {
                method = interfaces[i].getDeclaredMethod(methodName, parameterTypes);
            } catch (NoSuchMethodException e) {
                /* Swallow, if no method is found after the loop then this
                 * method returns null.
                 */
            }
            if (method != null) {
                return method;
            }

            // Recursively check our parent interfaces
            method = getAccessibleMethodFromInterfaceNest(interfaces[i], methodName, parameterTypes);
            if (method != null) {
                return method;
            }

        }

    }

    // We did not find anything
    return (null);

}

From source file:net.sf.juffrou.reflect.JuffrouTypeConverterDelegate.java

private boolean canCreateCopy(Class requiredType) {
    return (!requiredType.isInterface() && !Modifier.isAbstract(requiredType.getModifiers())
            && Modifier.isPublic(requiredType.getModifiers()) && ClassUtils.hasConstructor(requiredType));
}

From source file:com.dianping.resource.io.util.ClassUtils.java

/**
 * Given a method, which may come from an interface, and a target class used
 * in the current reflective invocation, find the corresponding target method
 * if there is one. E.g. the method may be {@code IFoo.bar()} and the
 * target class may be {@code DefaultFoo}. In this case, the method may be
 * {@code DefaultFoo.bar()}. This enables attributes on that method to be found.
 * <p><b>NOTE:</b> In contrast to {@link org.springframework.aop.support.AopUtils#getMostSpecificMethod},
 * this method does <i>not</i> resolve Java 5 bridge methods automatically.
 * Call {@link org.springframework.core.BridgeMethodResolver#findBridgedMethod}
 * if bridge method resolution is desirable (e.g. for obtaining metadata from
 * the original method definition).//from  w  w  w .  j a va  2s .  co  m
 * <p><b>NOTE:</b> Since Spring 3.1.1, if Java security settings disallow reflective
 * access (e.g. calls to {@code Class#getDeclaredMethods} etc, this implementation
 * will fall back to returning the originally provided method.
 * @param method the method to be invoked, which may come from an interface
 * @param targetClass the target class for the current invocation.
 * May be {@code null} or may not even implement the method.
 * @return the specific target method, or the original method if the
 * {@code targetClass} doesn't implement it or is {@code null}
 */
public static Method getMostSpecificMethod(Method method, Class<?> targetClass) {
    if (method != null && isOverridable(method, targetClass) && targetClass != null
            && !targetClass.equals(method.getDeclaringClass())) {
        try {
            if (Modifier.isPublic(method.getModifiers())) {
                try {
                    return targetClass.getMethod(method.getName(), method.getParameterTypes());
                } catch (NoSuchMethodException ex) {
                    return method;
                }
            } else {
                Method specificMethod = ReflectionUtils.findMethod(targetClass, method.getName(),
                        method.getParameterTypes());
                return (specificMethod != null ? specificMethod : method);
            }
        } catch (AccessControlException ex) {
            // Security settings are disallowing reflective access; fall back to 'method' below.
        }
    }
    return method;
}

From source file:org.codehaus.groovy.grails.compiler.injection.GrailsASTUtils.java

public static boolean isCandidateMethod(MethodNode declaredMethod) {
    return !declaredMethod.isSynthetic() && !declaredMethod.getName().contains("$")
            && Modifier.isPublic(declaredMethod.getModifiers())
            && !Modifier.isAbstract(declaredMethod.getModifiers());
}

From source file:com.github.jinahya.sql.database.metadata.bind.MetadataContext.java

private Set<String> getMethodNames() {

    if (methodNames == null) {
        methodNames = new HashSet<String>();
        for (final Method method : DatabaseMetaData.class.getMethods()) {
            if (method.getDeclaringClass() != DatabaseMetaData.class) {
                continue;
            }//from ww w  .java  2s .c  om
            final int modifier = method.getModifiers();
            if (Modifier.isStatic(modifier)) {
                continue;
            }
            if (!Modifier.isPublic(modifier)) {
                continue;
            }
            methodNames.add(method.getName());
        }
    }

    return methodNames;
}

From source file:org.apache.poi.util.MethodUtils.java

/**
 * <p>Find an accessible method that matches the given name and has compatible parameters.
 * Compatible parameters mean that every method parameter is assignable from 
 * the given parameters./*  ww w  .  ja  v  a 2  s .co  m*/
 * In other words, it finds a method with the given name 
 * that will take the parameters given.<p>
 *
 * <p>This method is slightly undeterminstic since it loops 
 * through methods names and return the first matching method.</p>
 * 
 * <p>This method is used by 
 * {@link 
 * #invokeMethod(Object object,String methodName,Object [] args,Class[] parameterTypes)}.
 *
 * <p>This method can match primitive parameter by passing in wrapper classes.
 * For example, a <code>Boolean</code> will match a primitive <code>boolean</code>
 * parameter.
 *
 * @param clazz find method in this class
 * @param methodName find method with this name
 * @param parameterTypes find method with compatible parameters 
 * @return The accessible method
 */
public static Method getMatchingAccessibleMethod(Class clazz, String methodName, Class[] parameterTypes) {
    // trace logging
    Log log = LogFactory.getLog(MethodUtils.class);
    if (log.isTraceEnabled()) {
        log.trace("Matching name=" + methodName + " on " + clazz);
    }
    MethodDescriptor md = new MethodDescriptor(clazz, methodName, parameterTypes, false);

    // see if we can find the method directly
    // most of the time this works and it's much faster
    try {
        Method method = clazz.getMethod(methodName, parameterTypes);
        if (log.isTraceEnabled()) {
            log.trace("Found straight match: " + method);
            log.trace("isPublic:" + Modifier.isPublic(method.getModifiers()));
        }

        setMethodAccessible(method); // Default access superclass workaround

        return method;

    } catch (NoSuchMethodException e) {
        /* SWALLOW */ }

    // search through all methods 
    int paramSize = parameterTypes.length;
    Method bestMatch = null;
    Method[] methods = clazz.getMethods();
    float bestMatchCost = Float.MAX_VALUE;
    float myCost = Float.MAX_VALUE;
    for (int i = 0, size = methods.length; i < size; i++) {
        if (methods[i].getName().equals(methodName)) {
            // log some trace information
            if (log.isTraceEnabled()) {
                log.trace("Found matching name:");
                log.trace(methods[i]);
            }

            // compare parameters
            Class[] methodsParams = methods[i].getParameterTypes();
            int methodParamSize = methodsParams.length;
            if (methodParamSize == paramSize) {
                boolean match = true;
                for (int n = 0; n < methodParamSize; n++) {
                    if (log.isTraceEnabled()) {
                        log.trace("Param=" + parameterTypes[n].getName());
                        log.trace("Method=" + methodsParams[n].getName());
                    }
                    if (!isAssignmentCompatible(methodsParams[n], parameterTypes[n])) {
                        if (log.isTraceEnabled()) {
                            log.trace(methodsParams[n] + " is not assignable from " + parameterTypes[n]);
                        }
                        match = false;
                        break;
                    }
                }

                if (match) {
                    // get accessible version of method
                    Method method = getAccessibleMethod(clazz, methods[i]);
                    if (method != null) {
                        if (log.isTraceEnabled()) {
                            log.trace(method + " accessible version of " + methods[i]);
                        }
                        setMethodAccessible(method); // Default access superclass workaround
                        myCost = getTotalTransformationCost(parameterTypes, method.getParameterTypes());
                        if (myCost < bestMatchCost) {
                            bestMatch = method;
                            bestMatchCost = myCost;
                        }
                    }

                    log.trace("Couldn't find accessible method.");
                }
            }
        }
    }
    if (bestMatch == null) {
        // didn't find a match
        log.trace("No match found.");
    }

    return bestMatch;
}

From source file:eu.qualityontime.commons.MethodUtils.java

/**
 * <p>/*from w  ww . j av  a  2s  .  c  om*/
 * 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>.
 * </p>
 *
 * @param clazz
 *            The class of the object
 * @param method
 *            The method that we wish to call
 * @return The accessible method
 * @since 1.8.0
 */
public static Method getAccessibleMethod(Class<?> clazz, 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;
    }

    boolean sameClass = true;
    if (clazz == null) {
        clazz = method.getDeclaringClass();
    } else {
        sameClass = clazz.equals(method.getDeclaringClass());
        if (!method.getDeclaringClass().isAssignableFrom(clazz)) {
            throw new IllegalArgumentException(
                    clazz.getName() + " is not assignable from " + method.getDeclaringClass().getName());
        }
    }

    // If the class is public, we are done
    if (Modifier.isPublic(clazz.getModifiers())) {
        if (!sameClass && !Modifier.isPublic(method.getDeclaringClass().getModifiers())) {
            setMethodAccessible(method); // Default access superclass
            // workaround
        }
        return method;
    }

    final String methodName = method.getName();
    final Class<?>[] parameterTypes = method.getParameterTypes();

    // Check the implemented interfaces and subinterfaces
    method = getAccessibleMethodFromInterfaceNest(clazz, methodName, parameterTypes);

    // Check the superclass chain
    if (method == null) {
        method = getAccessibleMethodFromSuperclass(clazz, methodName, parameterTypes);
    }

    return method;
}

From source file:com.freetmp.common.util.ClassUtils.java

public static Method getMostSpecificMethod(Method method, Class<?> targetClass) {
    if (method != null && isOverridable(method, targetClass) && targetClass != null
            && !targetClass.equals(method.getDeclaringClass())) {
        try {//from  w w  w. j  a v a 2s.  c  o  m
            if (Modifier.isPublic(method.getModifiers())) {
                try {
                    return targetClass.getMethod(method.getName(), method.getParameterTypes());
                } catch (NoSuchMethodException ex) {
                    return method;
                }
            } else {
                Method specificMethod = ReflectionUtils.findMethod(targetClass, method.getName(),
                        method.getParameterTypes());
                return (specificMethod != null ? specificMethod : method);
            }
        } catch (SecurityException ex) {
            // Security settings are disallowing reflective access; fall back to 'method' below.
        }
    }
    return method;
}