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:edu.ku.brc.specify.tasks.subpane.wb.wbuploader.UploadTable.java

private Vector<Method> getGetters() {
    Method[] methods = tblClass.getMethods();
    Vector<Method> result = new Vector<Method>();
    for (Method m : methods) {
        if (m.getName().startsWith("get") && m.getParameterTypes().length == 0
                && m.getReturnType() != void.class && Modifier.isPublic(m.getModifiers())
                && !Modifier.isTransient(m.getModifiers()) && !Modifier.isStatic(m.getModifiers())
                && !Modifier.isAbstract(m.getModifiers())) {
            Annotation jc = m.getAnnotation(javax.persistence.Column.class);
            Annotation c = m.getAnnotation(javax.persistence.JoinColumn.class);
            Annotation otm = m.getAnnotation(javax.persistence.OneToMany.class);
            if (otm == null && (jc != null || c != null || m.getName().equalsIgnoreCase("getId"))) {
                result.add(m);//from w w  w . j  av  a2 s .com
            }

        }
    }
    return result;
}

From source file:com.clark.func.Functions.java

/**
 * <p>// w w  w.  j  a  v a 2s. c  o  m
 * Returns the desired Method much like <code>Class.getMethod</code>,
 * however it ensures that the returned Method is from a public class or
 * interface and not from an anonymous inner class. This means that the
 * Method is invokable and doesn't fall foul of Java bug <a
 * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4071957"
 * >4071957</a>).
 * 
 * <code><pre>Set set = Collections.unmodifiableSet(...);
 *  Method method = ClassUtils.getPublicMethod(set.getClass(), "isEmpty",  new Class[0]);
 *  Object result = method.invoke(set, new Object[]);</pre></code>
 * </p>
 * 
 * @param cls
 *            the class to check, not null
 * @param methodName
 *            the name of the method
 * @param parameterTypes
 *            the list of parameters
 * @return the method
 * @throws NullPointerException
 *             if the class is null
 * @throws SecurityException
 *             if a a security violation occured
 * @throws NoSuchMethodException
 *             if the method is not found in the given class or if the
 *             metothod doen't conform with the requirements
 */
public static Method getPublicMethod(Class<?> cls, String methodName, Class<?> parameterTypes[])
        throws SecurityException, NoSuchMethodException {

    Method declaredMethod = cls.getMethod(methodName, parameterTypes);
    if (Modifier.isPublic(declaredMethod.getDeclaringClass().getModifiers())) {
        return declaredMethod;
    }

    List<Class<?>> candidateClasses = new ArrayList<Class<?>>();
    candidateClasses.addAll(getAllInterfaces(cls));
    candidateClasses.addAll(getAllSuperclasses(cls));

    for (Class<?> candidateClass : candidateClasses) {
        if (!Modifier.isPublic(candidateClass.getModifiers())) {
            continue;
        }
        Method candidateMethod;
        try {
            candidateMethod = candidateClass.getMethod(methodName, parameterTypes);
        } catch (NoSuchMethodException ex) {
            continue;
        }
        if (Modifier.isPublic(candidateMethod.getDeclaringClass().getModifiers())) {
            return candidateMethod;
        }
    }

    throw new NoSuchMethodException("Can't find a public method for " + methodName);
}

From source file:com.clark.func.Functions.java

/**
 * Returns accessible version of the given constructor.
 * /*from w  w w  .j  a v  a  2s.  com*/
 * @param ctor
 *            prototype constructor object.
 * @return <code>null</code> if accessible constructor can not be found.
 * @see java.lang.SecurityManager
 */
public static <T> Constructor<T> getAccessibleConstructor(Constructor<T> ctor) {
    return isAccessible(ctor) && Modifier.isPublic(ctor.getDeclaringClass().getModifiers()) ? ctor : null;
}

From source file:com.clark.func.Functions.java

/**
 * <p>//from  ww  w .j  a  va  2  s.com
 * 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 method
 *            The method that we wish to call
 * @return The accessible method
 */
public static Method getAccessibleMethod(Method method) {
    if (!isAccessible(method)) {
        return null;
    }
    // If the declaring class is public, we are done
    Class<?> cls = method.getDeclaringClass();
    if (Modifier.isPublic(cls.getModifiers())) {
        return method;
    }
    String methodName = method.getName();
    Class<?>[] parameterTypes = method.getParameterTypes();

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

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

From source file:com.clark.func.Functions.java

/**
 * <p>//from  www. ja va  2  s .  c o m
 * Return 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(Class<?> cls, String methodName,
        Class<?>... parameterTypes) {
    Class<?> parentClass = cls.getSuperclass();
    while (parentClass != null) {
        if (Modifier.isPublic(parentClass.getModifiers())) {
            try {
                return parentClass.getMethod(methodName, parameterTypes);
            } catch (NoSuchMethodException e) {
                return null;
            }
        }
        parentClass = parentClass.getSuperclass();
    }
    return null;
}

From source file:com.clark.func.Functions.java

/**
 * <p>/*from  w  w  w  .j  ava2  s  .  c  o m*/
 * 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 cls
 *            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
 * @return the accessible method or <code>null</code> if not found
 */
private static Method getAccessibleMethodFromInterfaceNest(Class<?> cls, String methodName,
        Class<?>... parameterTypes) {
    Method method = null;

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

        // Check the implemented interfaces of the parent class
        Class<?>[] interfaces = cls.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) {
                break;
            }
            // Recursively check our parent interfaces
            method = getAccessibleMethodFromInterfaceNest(interfaces[i], methodName, parameterTypes);
            if (method != null) {
                break;
            }
        }
    }
    return method;
}

From source file:com.clark.func.Functions.java

/**
 * XXX Default access superclass workaround
 * // www  .j a  v  a 2  s. co  m
 * When a public class has a default access superclass with public members,
 * these members are accessible. Calling them from compiled code works fine.
 * Unfortunately, on some JVMs, using reflection to invoke these members
 * seems to (wrongly) to prevent access even when the modifer is public.
 * Calling setAccessible(true) solves the problem but will only work from
 * sufficiently privileged code. Better workarounds would be gratefully
 * accepted.
 * 
 * @param o
 *            the AccessibleObject to set as accessible
 */
static void setAccessibleWorkaround(AccessibleObject o) {
    if (o == null || o.isAccessible()) {
        return;
    }
    Member m = (Member) o;
    if (Modifier.isPublic(m.getModifiers()) && isPackageAccess(m.getDeclaringClass().getModifiers())) {
        try {
            o.setAccessible(true);
        } catch (SecurityException e) {
            // ignore in favor of subsequent IllegalAccessException
        }
    }
}

From source file:com.clark.func.Functions.java

/**
 * Check a Member for basic accessibility.
 * //from   w ww  .ja v a2s.  c o m
 * @param m
 *            Member to check
 * @return true if <code>m</code> is accessible
 */
static boolean isAccessible(Member m) {
    return m != null && Modifier.isPublic(m.getModifiers()) && !m.isSynthetic();
}

From source file:com.clark.func.Functions.java

/**
 * Gets an accessible <code>Field</code> by name breaking scope if
 * requested. Superclasses/interfaces will be considered.
 * // w  ww .ja  va  2 s .co m
 * @param cls
 *            the class to reflect, must not be null
 * @param fieldName
 *            the field name to obtain
 * @param forceAccess
 *            whether to break scope restrictions using the
 *            <code>setAccessible</code> method. <code>False</code> will
 *            only match public fields.
 * @return the Field object
 * @throws IllegalArgumentException
 *             if the class or field name is null
 */
public static Field getField(final Class<?> cls, String fieldName, boolean forceAccess) {
    if (cls == null) {
        throw new IllegalArgumentException("The class must not be null");
    }
    if (fieldName == null) {
        throw new IllegalArgumentException("The field name must not be null");
    }
    // Sun Java 1.3 has a bugged implementation of getField hence we write
    // the
    // code ourselves

    // getField() will return the Field object with the declaring class
    // set correctly to the class that declares the field. Thus requesting
    // the
    // field on a subclass will return the field from the superclass.
    //
    // priority order for lookup:
    // searchclass private/protected/package/public
    // superclass protected/package/public
    // private/different package blocks access to further superclasses
    // implementedinterface public

    // check up the superclass hierarchy
    for (Class<?> acls = cls; acls != null; acls = acls.getSuperclass()) {
        try {
            Field field = acls.getDeclaredField(fieldName);
            // getDeclaredField checks for non-public scopes as well
            // and it returns accurate results
            if (!Modifier.isPublic(field.getModifiers())) {
                if (forceAccess) {
                    field.setAccessible(true);
                } else {
                    continue;
                }
            }
            return field;
        } catch (NoSuchFieldException ex) {
            // ignore
        }
    }
    // check the public interface case. This must be manually searched for
    // incase there is a public supersuperclass field hidden by a
    // private/package
    // superclass field.
    Field match = null;
    for (Iterator<Class<?>> intf = getAllInterfaces(cls).iterator(); intf.hasNext();) {
        try {
            Field test = ((Class<?>) intf.next()).getField(fieldName);
            if (match != null) {
                throw new IllegalArgumentException(
                        "Reference to field " + fieldName + " is ambiguous relative to " + cls
                                + "; a matching field exists on two or more implemented interfaces.");
            }
            match = test;
        } catch (NoSuchFieldException ex) {
            // ignore
        }
    }
    return match;
}