Example usage for java.lang.reflect Modifier isProtected

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

Introduction

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

Prototype

public static boolean isProtected(int mod) 

Source Link

Document

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

Usage

From source file:org.apache.openjpa.enhance.PCSubclassValidator.java

private void checkMethodIsSubclassable(Method meth, FieldMetaData fmd) {
    String className = fmd.getDefiningMetaData().getDescribedType().getName();
    if (!(Modifier.isProtected(meth.getModifiers()) || Modifier.isPublic(meth.getModifiers())))
        addError(loc.get("subclasser-private-accessors-unsupported", className, meth.getName()), fmd);
    if (Modifier.isFinal(meth.getModifiers()))
        addError(loc.get("subclasser-final-methods-not-allowed", className, meth.getName()), fmd);
    if (Modifier.isNative(meth.getModifiers()))
        addContractViolation(loc.get("subclasser-native-methods-not-allowed", className, meth.getName()), fmd);
    if (Modifier.isStatic(meth.getModifiers()))
        addError(loc.get("subclasser-static-methods-not-supported", className, meth.getName()), fmd);
}

From source file:org.gradle.build.docs.dsl.SourceMetaDataVisitor.java

private void maybeAddPropertyFromField(GroovySourceAST t) {
    GroovySourceAST parentNode = getParentNode();
    boolean isField = parentNode != null && parentNode.getType() == OBJBLOCK;
    if (!isField) {
        return;//from w  w  w  .  j  av a 2s.c  o m
    }

    int modifiers = extractModifiers(t);
    boolean isConst = getCurrentClass().isInterface()
            || (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers));
    if (isConst) {
        visitConst(t);
        return;
    }

    boolean isProp = groovy && !Modifier.isStatic(modifiers) && !Modifier.isPublic(modifiers)
            && !Modifier.isProtected(modifiers) && !Modifier.isPrivate(modifiers);
    if (!isProp) {
        return;
    }

    ASTIterator children = new ASTIterator(t);
    children.skip(MODIFIERS);

    String propertyName = extractIdent(t);
    TypeMetaData propertyType = extractTypeName(children.current);
    ClassMetaData currentClass = getCurrentClass();

    MethodMetaData getterMethod = currentClass
            .addMethod(String.format("get%s", StringUtils.capitalize(propertyName)), propertyType, "");
    currentClass.addReadableProperty(propertyName, propertyType, getJavaDocCommentsBeforeNode(t), getterMethod);
    if (!Modifier.isFinal(modifiers)) {
        MethodMetaData setterMethod = currentClass
                .addMethod(String.format("set%s", StringUtils.capitalize(propertyName)), TypeMetaData.VOID, "");
        setterMethod.addParameter(propertyName, propertyType);
        currentClass.addWriteableProperty(propertyName, propertyType, getJavaDocCommentsBeforeNode(t),
                setterMethod);
    }
}

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

@Test
public void defaultAccessMethods() {
    memberCriteria.membersOfType(Method.class);
    memberCriteria.withAccess(AccessType.DEFAULT);
    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 method", member instanceof Method);
        int modifiers = member.getModifiers();
        assertFalse(Modifier.isPublic(modifiers));
        assertFalse(Modifier.isProtected(modifiers));
        assertFalse(Modifier.isPrivate(modifiers));
    }/*from w ww .  j ava2s.  c  o m*/
}

From source file:com.link_intersystems.lang.reflect.Member2.java

/**
 * @param referenceMember//from   www . j av a  2 s. com
 * @return true if the access modifier of this {@link Member2} are less
 *         restrictive than the access modifier of the referenceInvokable.
 *         <p>
 *         Logic according to the java language specification:
 *
 *         <pre>
 * The access modifier (6.6) of an overriding or hiding method must provide at least as much access as the overridden or hidden method, or a compile-time error occurs. In more detail:
 *         <ul>
 *         <li>If the overridden or hidden method is <code>public</code>,
 *         then the overriding or hiding method must be <code>public</code>;
 *         otherwise, a compile-time error occurs. <a name="39550"></a></li>
 *         <li>If the overridden or hidden method is <code>protected</code>,
 *         then the overriding or hiding method must be
 *         <code>protected</code> or <code>public</code>; otherwise, a
 *         compile-time error occurs. <a name="39551"></a></li>
 *         <li>If the overridden or hidden method has default (package)
 *         access, then the overriding or hiding method must not be
 *         <code>private</code>; otherwise, a compile-time error occurs.
 *
 *         </li>
 *         </ul>
 * </pre>
 *
 *         <pre>
 *                                 +---------------------------------------------------------------------+
 *                                 |                               this                                  |
 *                                 +----------------+----------------+-------------------+---------------+
 *                                 |     public     |    protected   | package (default) |    private    |
 * +-----------+-------------------+----------------+----------------+-------------------+---------------+
 * |           | public            |      true      |     false      |       false       |     false     |
 * |           +-------------------+----------------+----------------+-------------------+---------------+
 * | reference | protected         |      true      |     true       |       false       |     false     |
 * |           +-------------------+----------------+----------------+-------------------+---------------+
 * | invokable | package (default) |      true      |     true       |       true        |     false     |
 * |           +-------------------+----------------+----------------+-------------------+---------------+
 * |           | private           |      false     |     false      |       false       |     false     |
 * +-----------+-------------------+----------------+----------------+-------------------+---------------+
 * </pre>
 *
 *         </p>
 * @since 1.0.0.0
 */
protected boolean isAccessModifierOverriddingCompatible(Member2<?> referenceMember) {
    int referenceModifiers = referenceMember.getModifiers();
    int invokableModifiers = getModifiers();
    if (Modifier.isPrivate(referenceModifiers) || Modifier.isPrivate(invokableModifiers)) {
        return false;
    }
    if (Modifier.isPublic(referenceModifiers)) {
        return Modifier.isPublic(invokableModifiers);
    }
    if (Modifier.isProtected(referenceModifiers)) {
        return Modifier.isProtected(invokableModifiers) || Modifier.isPublic(invokableModifiers);
    }
    return true;
}

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

@Test
public void defaultAccessMethodsWithStaticNativeOnly() {
    memberCriteria.membersOfType(Method.class);
    memberCriteria.withAccess(AccessType.DEFAULT);
    memberCriteria.withModifiers(Modifier.STATIC | Modifier.NATIVE);
    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 method", member instanceof Method);
        int modifiers = member.getModifiers();
        assertFalse(Modifier.isPublic(modifiers));
        assertFalse(Modifier.isProtected(modifiers));
        assertFalse(Modifier.isPrivate(modifiers));
        assertTrue(Modifier.isStatic(modifiers));
        assertTrue(Modifier.isNative(modifiers));
    }/*from   www.j a  va 2s.  c  om*/
}

From source file:org.eclipse.buildship.docs.source.SourceMetaDataVisitor.java

private void maybeAddPropertyFromField(GroovySourceAST t) {
    GroovySourceAST parentNode = getParentNode();
    boolean isField = parentNode != null && parentNode.getType() == OBJBLOCK;
    if (!isField) {
        return;/*from w  w  w  .j ava2s .  co m*/
    }

    int modifiers = extractModifiers(t);
    boolean isConst = getCurrentClass().isInterface()
            || (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers));
    if (isConst) {
        visitConst(t);
        return;
    }

    boolean isProp = groovy && !Modifier.isStatic(modifiers) && !Modifier.isPublic(modifiers)
            && !Modifier.isProtected(modifiers) && !Modifier.isPrivate(modifiers);
    if (!isProp) {
        return;
    }

    ASTIterator children = new ASTIterator(t);
    children.skip(MODIFIERS);

    String propertyName = extractIdent(t);
    TypeMetaData propertyType = extractTypeName(children.current);
    ClassMetaData currentClass = getCurrentClass();

    MethodMetaData getterMethod = currentClass
            .addMethod(String.format("get%s", StringUtils.capitalize(propertyName)), propertyType, "");
    PropertyMetaData property = currentClass.addReadableProperty(propertyName, propertyType,
            getJavaDocCommentsBeforeNode(t), getterMethod);
    findAnnotations(t, property);
    if (!Modifier.isFinal(modifiers)) {
        MethodMetaData setterMethod = currentClass
                .addMethod(String.format("set%s", StringUtils.capitalize(propertyName)), TypeMetaData.VOID, "");
        setterMethod.addParameter(propertyName, propertyType);
        currentClass.addWriteableProperty(propertyName, propertyType, getJavaDocCommentsBeforeNode(t),
                setterMethod);
    }
}

From source file:cern.c2mon.shared.common.datatag.address.impl.HardwareAddressImpl.java

/**
 * Returns an XML representation of the HardwareAddress object.
 *
 * @throws RuntimeException if Illegal access to fields
 *///from  ww w .  j a  v  a2 s  .com
public final synchronized String toConfigXML() {
    Class handlerClass = this.getClass();
    Field[] fields = handlerClass.getDeclaredFields();

    StringBuilder str = new StringBuilder();

    str.append("        <HardwareAddress class=\"");
    str.append(getClass().getName());
    str.append("\">\n");

    for (int i = 0; i < fields.length; i++) {
        if (Modifier.isProtected(fields[i].getModifiers()) && !Modifier.isFinal(fields[i].getModifiers())) {
            try {
                if (fields[i].get(this) != null) {
                    str.append("          <");
                    String fieldXMLName = encodeFieldName(fields[i].getName());

                    str.append(fieldXMLName);
                    str.append(">");
                    try {
                        str.append(fields[i].get(this));
                    } catch (IllegalAccessException iae) {
                        iae.printStackTrace();
                    }
                    str.append("</");
                    str.append(fieldXMLName);
                    str.append(">\n");
                }
            } catch (IllegalAccessException iae) {
                iae.printStackTrace();
                throw new RuntimeException("Exception caught while converting HardwareAddress to XML.", iae);
            }
        }
    }

    str.append("        </HardwareAddress>\n");
    return str.toString();
}

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 w w. jav a 2 s. c om*/
            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: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 .  j  a  va 2  s . co m*/
}

From source file:org.springframework.aop.framework.CglibAopProxy.java

/**
 * Checks for final methods on the given {@code Class}, as well as package-visible
 * methods across ClassLoaders, and writes warnings to the log for each one found.
 *//*from   w w  w .ja  v a2 s  . c  om*/
private void doValidateClass(Class<?> proxySuperClass, @Nullable ClassLoader proxyClassLoader,
        Set<Class<?>> ifcs) {
    if (proxySuperClass != Object.class) {
        Method[] methods = proxySuperClass.getDeclaredMethods();
        for (Method method : methods) {
            int mod = method.getModifiers();
            if (!Modifier.isStatic(mod) && !Modifier.isPrivate(mod)) {
                if (Modifier.isFinal(mod)) {
                    if (implementsInterface(method, ifcs)) {
                        logger.warn("Unable to proxy interface-implementing method [" + method + "] because "
                                + "it is marked as final: Consider using interface-based JDK proxies instead!");
                    }
                    logger.info("Final method [" + method + "] cannot get proxied via CGLIB: "
                            + "Calls to this method will NOT be routed to the target instance and "
                            + "might lead to NPEs against uninitialized fields in the proxy instance.");
                } else if (!Modifier.isPublic(mod) && !Modifier.isProtected(mod) && proxyClassLoader != null
                        && proxySuperClass.getClassLoader() != proxyClassLoader) {
                    logger.info("Method [" + method + "] is package-visible across different ClassLoaders "
                            + "and cannot get proxied via CGLIB: Declare this method as public or protected "
                            + "if you need to support invocations through the proxy.");
                }
            }
        }
        doValidateClass(proxySuperClass.getSuperclass(), proxyClassLoader, ifcs);
    }
}