下面列出了java.lang.reflect.Modifier#isProtected ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
@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 );
}
}
/**
* {@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;
}
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
}
}
}
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);
}
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);
}
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);
}
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);
}
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));
}
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;
}
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();
}
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);
}
/**
* Returns true for a protected method.
*/
public boolean isProtected()
{
return Modifier.isProtected(_method.getModifiers());
}
@Override
public boolean isVisible(Field field) {
return Modifier.isProtected(field.getModifiers());
}
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);
}
/** Utility method to query the modifier flags of this member. */
public boolean isProtected() {
return Modifier.isProtected(flags);
}
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);
}
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);
}
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);
}
@Override public boolean apply(Integer modifiers) { return Modifier.isProtected(modifiers); }