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:com.gargoylesoftware.htmlunit.SimpleWebTestCase.java

/**
 * From Junit. Test if the method is a junit test.
 * @param method the method//from w  w  w . ja v a2 s  .co  m
 * @return <code>true</code> if this is a junit test
 */
private boolean isPublicTestMethod(final Method method) {
    return method.getParameterTypes().length == 0
            && (method.getName().startsWith("test") || method.getAnnotation(Test.class) != null)
            && method.getReturnType() == Void.TYPE && Modifier.isPublic(method.getModifiers());
}

From source file:com.sun.faces.el.impl.BeanInfoManager.java

/**
 * Returns a publicly-accessible version of the given method, by
 * searching for a public declaring class.
 *///from  w  ww.j a v a2s. c  om
static Method getPublicMethod(Method pMethod) {
    if (pMethod == null) {
        return null;
    }

    // See if the method is already available from a public class
    Class cl = pMethod.getDeclaringClass();
    if (Modifier.isPublic(cl.getModifiers())) {
        return pMethod;
    }

    // Otherwise, try to find a public class that declares the method
    Method ret = getPublicMethod(cl, pMethod);
    if (ret != null) {
        return ret;
    } else {
        return pMethod;
    }
}

From source file:org.apache.sling.scripting.sightly.render.AbstractRuntimeObjectModel.java

protected Method extractMethodInheritanceChain(Class type, Method m) {
    if (m == null || Modifier.isPublic(type.getModifiers())) {
        return m;
    }//from w w w  . j  av a 2 s .c o  m
    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.gradle.api.internal.AbstractClassGenerator.java

private void inspectType(Class<?> type, ClassMetaData classMetaData) {
    ClassDetails classDetails = ClassInspector.inspect(type);
    for (Method method : classDetails.getAllMethods()) {
        if (method.getAnnotation(Inject.class) != null) {
            if (!Modifier.isPublic(method.getModifiers()) && !Modifier.isProtected(method.getModifiers())) {
                throw new UnsupportedOperationException(String.format(
                        "Cannot attach @Inject to method %s.%s() as it is not public or protected.",
                        method.getDeclaringClass().getSimpleName(), method.getName()));
            }/*from   w ww . jav a2s  .  c  o m*/
            if (Modifier.isStatic(method.getModifiers())) {
                throw new UnsupportedOperationException(
                        String.format("Cannot attach @Inject to method %s.%s() as it is static.",
                                method.getDeclaringClass().getSimpleName(), method.getName()));
            }
        }
    }
    for (PropertyDetails property : classDetails.getProperties()) {
        PropertyMetaData propertyMetaData = classMetaData.property(property.getName());
        for (Method method : property.getGetters()) {
            propertyMetaData.addGetter(method);
        }
        for (Method method : property.getSetters()) {
            propertyMetaData.addSetter(method);
        }
    }
    for (Method method : classDetails.getInstanceMethods()) {
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes.length == 1) {
            classMetaData.addCandidateSetMethod(method);
        }
        if (parameterTypes.length > 0 && parameterTypes[parameterTypes.length - 1].equals(Action.class)) {
            classMetaData.addActionMethod(method);
        } else if (parameterTypes.length > 0
                && parameterTypes[parameterTypes.length - 1].equals(Closure.class)) {
            classMetaData.addClosureMethod(method);
        }
    }
}

From source file:org.acoveo.tools.Reflection.java

/**
 * Make the given member accessible if it isn't already.
 *///w w  w .  ja  va  2 s .  co  m
private static void makeAccessible(AccessibleObject ao, int mods) {
    try {
        if (!Modifier.isPublic(mods) && !ao.isAccessible())
            AccessController.doPrivileged(setAccessibleAction(ao, true));
    } catch (SecurityException se) {
        throw new RuntimeException("Reflection security " + ao);
    }
}

From source file:net.servicefixture.util.ReflectionUtils.java

/**
 * Returns the first public method that matches method name.
 *//*from  ww w  . j av  a2 s.com*/
public static Method findMethodByName(Class type, String methodName) {
    Method[] methods = type.getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        if (method.getName().equals(methodName) && Modifier.isPublic(method.getModifiers())) {
            return method;
        }
    }
    return null;
}

From source file:org.apache.openaz.xacml.std.json.JSONRequest.java

/**
 * Use reflection to load the map with all the names of all DataTypes, both the long name and the
 * shorthand, and point each name to the appropriate Identifier. The shorthand map is used differently in
 * JSONRequest than in JSONResponse, so there are similarities and differences in the implementation. This
 * is done once the first time a Request is processed.
 *//*from  w w  w . ja  v a  2 s .  c  o m*/
private static void initShorthandMap() throws JSONStructureException {
    Field[] declaredFields = XACML3.class.getDeclaredFields();
    shorthandMap = new HashMap<String, Identifier>();
    for (Field field : declaredFields) {
        if (Modifier.isStatic(field.getModifiers()) && field.getName().startsWith("ID_DATATYPE")
                && Modifier.isPublic(field.getModifiers())) {
            try {
                Identifier id = (Identifier) field.get(null);
                String longName = id.stringValue();
                // most names start with 'http://www.w3.org/2001/XMLSchema#'
                int sharpIndex = longName.lastIndexOf("#");
                if (sharpIndex <= 0) {
                    // some names start with 'urn:oasis:names:tc:xacml:1.0:data-type:'
                    // or urn:oasis:names:tc:xacml:2.0:data-type:
                    if (longName.contains(":data-type:")) {
                        sharpIndex = longName.lastIndexOf(":");
                    } else {
                        continue;
                    }
                }
                String shortName = longName.substring(sharpIndex + 1);
                // put both the full name and the short name in the table
                shorthandMap.put(longName, id);
                shorthandMap.put(shortName, id);
            } catch (Exception e) {
                throw new JSONStructureException("Error loading ID Table, e=" + e);
            }
        }
    }
}

From source file:com.link_intersystems.lang.reflect.criteria.MemberCriteriaTest.java

@Test
public void privateStaticFields() {
    memberCriteria.membersOfType(Field.class);
    memberCriteria.withAccess(AccessType.PRIVATE);
    memberCriteria.withModifiers(Modifier.STATIC);
    ClassCriteria classCriteria = new ClassCriteria();
    Iterable<Class<?>> classIterable = classCriteria.getIterable(Class.class);
    Iterable<Member> memberIterable = memberCriteria.getIterable(classIterable);
    assertTrue(memberIterable.iterator().hasNext());
    for (Member member : memberIterable) {
        assertTrue("member must be a field", member instanceof Field);
        int modifiers = member.getModifiers();
        assertFalse(Modifier.isPublic(modifiers));
        assertFalse(Modifier.isProtected(modifiers));
        assertTrue(Modifier.isPrivate(modifiers));
        assertTrue(Modifier.isStatic(modifiers));
    }/* w w w  . ja  v a2  s . c  o m*/
}

From source file:com.sun.faces.el.impl.BeanInfoManager.java

/**
 * If the given class is public and has a Method that declares the
 * same name and arguments as the given method, then that method is
 * returned.  Otherwise the superclass and interfaces are searched
 * recursively.//  www  .jav  a 2s.  co m
 */
static Method getPublicMethod(Class pClass, Method pMethod) {
    // See if this is a public class declaring the method
    if (Modifier.isPublic(pClass.getModifiers())) {
        try {
            Method m;
            try {
                m = pClass.getDeclaredMethod(pMethod.getName(), pMethod.getParameterTypes());
            } catch (java.security.AccessControlException ex) {
                // kludge to accommodate J2EE RI's default settings
                // TODO: see if we can simply replace
                //       getDeclaredMethod() with getMethod() ...?
                m = pClass.getMethod(pMethod.getName(), pMethod.getParameterTypes());
            }
            if (Modifier.isPublic(m.getModifiers())) {
                return m;
            }
        } catch (NoSuchMethodException exc) {
        }
    }

    // Search the interfaces
    {
        Class[] interfaces = pClass.getInterfaces();
        if (interfaces != null) {
            for (int i = 0; i < interfaces.length; i++) {
                Method m = getPublicMethod(interfaces[i], pMethod);
                if (m != null) {
                    return m;
                }
            }
        }
    }

    // Search the superclass
    {
        Class superclass = pClass.getSuperclass();
        if (superclass != null) {
            Method m = getPublicMethod(superclass, pMethod);
            if (m != null) {
                return m;
            }
        }
    }

    return null;
}