Java Code Examples for java.lang.reflect.Modifier#isProtected()

The following examples show how to use java.lang.reflect.Modifier#isProtected() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypesParser.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static int getMethodFlags(Executable method) {
    int flags = FunctionInfo.NONE;
    int modifiers = method.getModifiers();
    if (Modifier.isAbstract(modifiers)) {
        flags |= FunctionInfo.ABSTRACT;
    }
    if (Modifier.isFinal(modifiers)) {
        flags |= FunctionInfo.FINAL;
    }
    if (Modifier.isPublic(modifiers)) {
        flags |= FunctionInfo.PUBLIC;
    } else if (Modifier.isProtected(modifiers)) {
        flags |= FunctionInfo.PROTECTED;
    } else if (Modifier.isPrivate(modifiers)) {
        flags |= FunctionInfo.PRIVATE;
    } else {
        flags |= FunctionInfo.DEFAULT;
    }
    if (Modifier.isStatic(modifiers)) {
        flags |= FunctionInfo.STATIC;
    }
    if (Modifier.isSynchronized(modifiers)) {
        flags |= FunctionInfo.SYNCHRONIZED;
    }
    return flags;
}
 
Example 2
Source File: ApiTestBase.java    From iaf with Apache License 2.0 6 votes vote down vote up
public void register(M jaxRsResource) {
	Method[] classMethods = jaxRsResource.getClass().getDeclaredMethods();
	for(Method classMethod : classMethods) {
		int modifiers = classMethod.getModifiers();
		if (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers)) {
			Path path = AnnotationUtils.findAnnotation(classMethod, Path.class);
			HttpMethod httpMethod = AnnotationUtils.findAnnotation(classMethod, HttpMethod.class);
			if(path != null && httpMethod != null) {
				String rsResourceKey = compileKey(httpMethod.value(), path.value());

				System.out.println("adding new JAX-RS resource key ["+rsResourceKey+"] method ["+classMethod.getName()+"]");
				rsRequests.put(rsResourceKey, classMethod);
			}
			//Ignore security for now
		}
	}
}
 
Example 3
Source File: ReflectionUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressWarnings( "unchecked" )
public static <T> T invokeMethod( Object target, Method method, Object... args )
{
    if ( target == null || method == null )
    {
        return null;
    }

    if ( Modifier.isProtected( method.getModifiers() ) || Modifier.isPrivate( method.getModifiers() ) )
    {
        return null;
    }

    try
    {
        return (T) method.invoke( target, args );
    }
    catch ( InvocationTargetException | IllegalAccessException e )
    {
        throw new RuntimeException( e );
    }
}
 
Example 4
Source File: Injection.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public int compare(Method o1, Method o2)
{
   int m1 = o1.getModifiers();
   int m2 = o2.getModifiers();

   if (Modifier.isPublic(m1))
      return -1;

   if (Modifier.isPublic(m2))
      return 1;

   if (Modifier.isProtected(m1))
      return -1;

   if (Modifier.isProtected(m2))
      return 1;

   if (Modifier.isPrivate(m1))
      return -1;

   if (Modifier.isPrivate(m2))
      return 1;

   return 0;
}
 
Example 5
Source File: ReflectionTypeFactory.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void setVisibility(org.eclipse.xtext.common.types.JvmMember result, int modifiers) {
	if (Modifier.isPrivate(modifiers))
		result.setVisibility(JvmVisibility.PRIVATE);
	else if (Modifier.isProtected(modifiers))
		result.setVisibility(JvmVisibility.PROTECTED);
	else if (Modifier.isPublic(modifiers))
		result.setVisibility(JvmVisibility.PUBLIC);
	else
		result.setVisibility(JvmVisibility.DEFAULT);
}
 
Example 6
Source File: AtomicIntegerFieldUpdater.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
AtomicIntegerFieldUpdaterImpl(final Class<T> tclass,
                              final String fieldName,
                              final Class<?> caller) {
    final Field field;
    final int modifiers;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
          sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    Class<?> fieldt = field.getType();
    if (fieldt != int.class)
        throw new IllegalArgumentException("Must be integer type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    this.cclass = (Modifier.isProtected(modifiers) &&
                   caller != tclass) ? caller : null;
    this.tclass = tclass;
    offset = unsafe.objectFieldOffset(field);
}
 
Example 7
Source File: AstToTextHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static String getModifiersText(int modifiers) {
    StringBuilder result = new StringBuilder();
    if (Modifier.isPrivate(modifiers)) {
        result.append("private ");
    }
    if (Modifier.isProtected(modifiers)) {
        result.append("protected ");
    }
    if (Modifier.isPublic(modifiers)) {
        result.append("public ");
    }
    if (Modifier.isStatic(modifiers)) {
        result.append("static ");
    }
    if (Modifier.isAbstract(modifiers)) {
        result.append("abstract ");
    }
    if (Modifier.isFinal(modifiers)) {
        result.append("final ");
    }
    if (Modifier.isInterface(modifiers)) {
        result.append("interface ");
    }
    if (Modifier.isNative(modifiers)) {
        result.append("native ");
    }
    if (Modifier.isSynchronized(modifiers)) {
        result.append("synchronized ");
    }
    if (Modifier.isTransient(modifiers)) {
        result.append("transient ");
    }
    if (Modifier.isVolatile(modifiers)) {
        result.append("volatile ");
    }
    return result.toString().trim();
}
 
Example 8
Source File: AbstractGenerator.java    From quarkus with Apache License 2.0 5 votes vote down vote up
protected boolean isReflectionFallbackNeeded(FieldInfo field, String targetPackage) {
    // Reflection fallback is needed for private fields and non-public fields declared on superclasses located in a different package
    if (Modifier.isPrivate(field.flags())) {
        return true;
    }
    if (Modifier.isProtected(field.flags()) || isPackagePrivate(field.flags())) {
        return !DotNames.packageName(field.declaringClass().name()).equals(targetPackage);
    }
    return false;
}
 
Example 9
Source File: ClassUtils.java    From dolphin with Apache License 2.0 5 votes vote down vote up
private static boolean isOverridable(Method method, Class<?> targetClass) {
	if (Modifier.isPrivate(method.getModifiers())) {
		return false;
	}
	if (Modifier.isPublic(method.getModifiers()) || Modifier.isProtected(method.getModifiers())) {
		return true;
	}
	return getPackageName(method.getDeclaringClass()).equals(getPackageName(targetClass));
}
 
Example 10
Source File: ReflectionTypeFactory.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void setVisibility(Class<?> clazz, JvmMember result) {
	if (Modifier.isPrivate(clazz.getModifiers()))
		result.setVisibility(JvmVisibility.PRIVATE);
	else if (Modifier.isProtected(clazz.getModifiers()))
		result.setVisibility(JvmVisibility.PROTECTED);
	else if (Modifier.isPublic(clazz.getModifiers()))
		result.setVisibility(JvmVisibility.PUBLIC);
}
 
Example 11
Source File: AtomicIntegerFieldUpdater.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
AtomicIntegerFieldUpdaterImpl(final Class<T> tclass,
                              final String fieldName,
                              final Class<?> caller) {
    final Field field;
    final int modifiers;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
          sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    Class<?> fieldt = field.getType();
    if (fieldt != int.class)
        throw new IllegalArgumentException("Must be integer type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    this.cclass = (Modifier.isProtected(modifiers) &&
                   caller != tclass) ? caller : null;
    this.tclass = tclass;
    offset = unsafe.objectFieldOffset(field);
}
 
Example 12
Source File: AtomicLongFieldUpdater.java    From j2objc with Apache License 2.0 5 votes vote down vote up
CASUpdater(final Class<T> tclass, final String fieldName,
           final Class<?> caller) {
    final Field field;
    final int modifiers;
    try {
        field = tclass.getDeclaredField(fieldName); // android-changed
        modifiers = field.getModifiers();
        // BEGIN Android-removed
        // sun.reflect.misc.ReflectUtil.ensureMemberAccess(
        //     caller, tclass, null, modifiers);
        // ClassLoader cl = tclass.getClassLoader();
        // ClassLoader ccl = caller.getClassLoader();
        // if ((ccl != null) && (ccl != cl) &&
        //     ((cl == null) || !isAncestor(cl, ccl))) {
        //     sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        // }
        // END Android-removed
    // BEGIN Android-removed
    // } catch (PrivilegedActionException pae) {
    //     throw new RuntimeException(pae.getException());
    // END Android-removed
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    if (field.getType() != long.class)
        throw new IllegalArgumentException("Must be long type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    this.cclass = (Modifier.isProtected(modifiers)) ? caller : tclass;
    this.tclass = tclass;
    this.offset = U.objectFieldOffset(field);
}
 
Example 13
Source File: AtomicLongFieldUpdater.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
LockedUpdater(final Class<T> tclass, final String fieldName,
              final Class<?> caller) {
    Field field = null;
    int modifiers = 0;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
            sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    if (field.getType() != long.class)
        throw new IllegalArgumentException("Must be long type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    // Access to protected field members is restricted to receivers only
    // of the accessing class, or one of its subclasses, and the
    // accessing class must in turn be a subclass (or package sibling)
    // of the protected member's defining class.
    // If the updater refers to a protected field of a declaring class
    // outside the current package, the receiver argument will be
    // narrowed to the type of the accessing class.
    this.cclass = (Modifier.isProtected(modifiers) &&
                   tclass.isAssignableFrom(caller) &&
                   !isSamePackage(tclass, caller))
                  ? caller : tclass;
    this.tclass = tclass;
    this.offset = U.objectFieldOffset(field);
}
 
Example 14
Source File: MemberName.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/** Utility method to query the modifier flags of this member. */
public boolean isProtected() {
    return Modifier.isProtected(flags);
}
 
Example 15
Source File: AtomicReferenceFieldUpdater.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
AtomicReferenceFieldUpdaterImpl(final Class<T> tclass,
                                final Class<V> vclass,
                                final String fieldName,
                                final Class<?> caller) {
    final Field field;
    final Class<?> fieldClass;
    final int modifiers;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
            sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
        fieldClass = field.getType();
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    if (vclass != fieldClass)
        throw new ClassCastException();
    if (vclass.isPrimitive())
        throw new IllegalArgumentException("Must be reference type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    // Access to protected field members is restricted to receivers only
    // of the accessing class, or one of its subclasses, and the
    // accessing class must in turn be a subclass (or package sibling)
    // of the protected member's defining class.
    // If the updater refers to a protected field of a declaring class
    // outside the current package, the receiver argument will be
    // narrowed to the type of the accessing class.
    this.cclass = (Modifier.isProtected(modifiers) &&
                   tclass.isAssignableFrom(caller) &&
                   !isSamePackage(tclass, caller))
                  ? caller : tclass;
    this.tclass = tclass;
    this.vclass = vclass;
    this.offset = U.objectFieldOffset(field);
}
 
Example 16
Source File: CustomisePropertyVisibilityStrategyTest.java    From Java-EE-8-Sampler with MIT License 4 votes vote down vote up
@Override
public boolean isVisible(Field field) {
    return Modifier.isProtected(field.getModifiers());
}
 
Example 17
Source File: AtomicReferenceFieldUpdater.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
AtomicReferenceFieldUpdaterImpl(final Class<T> tclass,
                                final Class<V> vclass,
                                final String fieldName,
                                final Class<?> caller) {
    final Field field;
    final Class<?> fieldClass;
    final int modifiers;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
          sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
        fieldClass = field.getType();
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    if (vclass != fieldClass)
        throw new ClassCastException();

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    this.cclass = (Modifier.isProtected(modifiers) &&
                   caller != tclass) ? caller : null;
    this.tclass = tclass;
    if (vclass == Object.class)
        this.vclass = null;
    else
        this.vclass = vclass;
    offset = unsafe.objectFieldOffset(field);
}
 
Example 18
Source File: AtomicIntegerFieldUpdater.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
AtomicIntegerFieldUpdaterImpl(final Class<T> tclass,
                              final String fieldName,
                              final Class<?> caller) {
    final Field field;
    final int modifiers;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
            sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }

    if (field.getType() != int.class)
        throw new IllegalArgumentException("Must be integer type");

    if (!Modifier.isVolatile(modifiers))
        throw new IllegalArgumentException("Must be volatile type");

    // Access to protected field members is restricted to receivers only
    // of the accessing class, or one of its subclasses, and the
    // accessing class must in turn be a subclass (or package sibling)
    // of the protected member's defining class.
    // If the updater refers to a protected field of a declaring class
    // outside the current package, the receiver argument will be
    // narrowed to the type of the accessing class.
    this.cclass = (Modifier.isProtected(modifiers) &&
                   tclass.isAssignableFrom(caller) &&
                   !isSamePackage(tclass, caller))
                  ? caller : tclass;
    this.tclass = tclass;
    this.offset = U.objectFieldOffset(field);
}
 
Example 19
Source File: JConstructorWrapper.java    From baratine with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns true for a protected method.
 */
public boolean isProtected()
{
  return Modifier.isProtected(_method.getModifiers());
}
 
Example 20
Source File: ReflectionPredicates.java    From brooklyn-server with Apache License 2.0 votes vote down vote up
@Override public boolean apply(Integer modifiers) { return Modifier.isProtected(modifiers); }