下面列出了java.lang.reflect.Member#getDeclaringClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Does a conservative approximation of member access check. Use this if
* you don't have an actual 'userland' caller Class/ClassLoader available.
* This might be more restrictive than a precise member access check where
* you have a caller, but should never allow a member access that is
* forbidden.
*
* @param m the {@code Member} about to be accessed
*/
public static void conservativeCheckMemberAccess(Member m) throws SecurityException{
final SecurityManager sm = System.getSecurityManager();
if (sm == null)
return;
// Check for package access on the declaring class.
//
// In addition, unless the member and the declaring class are both
// public check for access declared member permissions.
//
// This is done regardless of ClassLoader relations between the {@code
// Member m} and any potential caller.
final Class<?> declaringClass = m.getDeclaringClass();
checkPackageAccess(declaringClass);
if (Modifier.isPublic(m.getModifiers()) &&
Modifier.isPublic(declaringClass.getModifiers()))
return;
// Check for declared member access.
sm.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION);
}
/**
* Given a reflective method or a constructor, creates a dynamic method that represents it. This method will
* distinguish between caller sensitive and ordinary methods/constructors, and create appropriate caller sensitive
* dynamic method when needed.
* @param m the reflective member
* @return the single dynamic method representing the reflective member
*/
private static SingleDynamicMethod createDynamicMethod(final AccessibleObject m) {
if(CallerSensitiveDetector.isCallerSensitive(m)) {
// Method has @CallerSensitive annotation
return new CallerSensitiveDynamicMethod(m);
}
// Method has no @CallerSensitive annotation
final MethodHandle mh;
try {
mh = unreflectSafely(m);
} catch (final IllegalAccessError e) {
// java.lang.invoke can in some case conservatively treat as caller sensitive methods that aren't
// marked with the annotation. In this case, we'll fall back to treating it as caller sensitive.
return new CallerSensitiveDynamicMethod(m);
}
// Proceed with non-caller sensitive
final Member member = (Member)m;
return new SimpleDynamicMethod(mh, member.getDeclaringClass(), member.getName(), m instanceof Constructor);
}
/**
* Does a conservative approximation of member access check. Use this if
* you don't have an actual 'userland' caller Class/ClassLoader available.
* This might be more restrictive than a precise member access check where
* you have a caller, but should never allow a member access that is
* forbidden.
*
* @param m the {@code Member} about to be accessed
*/
public static void conservativeCheckMemberAccess(Member m) throws SecurityException{
final SecurityManager sm = System.getSecurityManager();
if (sm == null)
return;
// Check for package access on the declaring class.
//
// In addition, unless the member and the declaring class are both
// public check for access declared member permissions.
//
// This is done regardless of ClassLoader relations between the {@code
// Member m} and any potential caller.
final Class<?> declaringClass = m.getDeclaringClass();
checkPackageAccess(declaringClass);
if (Modifier.isPublic(m.getModifiers()) &&
Modifier.isPublic(declaringClass.getModifiers()))
return;
// Check for declared member access.
sm.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION);
}
/**
* Does a conservative approximation of member access check. Use this if
* you don't have an actual 'userland' caller Class/ClassLoader available.
* This might be more restrictive than a precise member access check where
* you have a caller, but should never allow a member access that is
* forbidden.
*
* @param m the {@code Member} about to be accessed
*/
public static void conservativeCheckMemberAccess(Member m) throws SecurityException{
final SecurityManager sm = System.getSecurityManager();
if (sm == null)
return;
// Check for package access on the declaring class.
//
// In addition, unless the member and the declaring class are both
// public check for access declared member permissions.
//
// This is done regardless of ClassLoader relations between the {@code
// Member m} and any potential caller.
final Class<?> declaringClass = m.getDeclaringClass();
checkPackageAccess(declaringClass);
if (Modifier.isPublic(m.getModifiers()) &&
Modifier.isPublic(declaringClass.getModifiers()))
return;
// Check for declared member access.
sm.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION);
}
/**
* Does a conservative approximation of member access check. Use this if
* you don't have an actual 'userland' caller Class/ClassLoader available.
* This might be more restrictive than a precise member access check where
* you have a caller, but should never allow a member access that is
* forbidden.
*
* @param m the {@code Member} about to be accessed
*/
public static void conservativeCheckMemberAccess(Member m) throws SecurityException{
final SecurityManager sm = System.getSecurityManager();
if (sm == null)
return;
// Check for package access on the declaring class.
//
// In addition, unless the member and the declaring class are both
// public check for access declared member permissions.
//
// This is done regardless of ClassLoader relations between the {@code
// Member m} and any potential caller.
final Class<?> declaringClass = m.getDeclaringClass();
checkPackageAccess(declaringClass);
if (Modifier.isPublic(m.getModifiers()) &&
Modifier.isPublic(declaringClass.getModifiers()))
return;
// Check for declared member access.
sm.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION);
}
public static Object forMember(Member member) {
if (member == null) {
return SourceProvider.UNKNOWN_SOURCE;
}
Class declaringClass = member.getDeclaringClass();
/*if[AOP]*/
LineNumbers lineNumbers = lineNumbersCache.getUnchecked(declaringClass);
String fileName = lineNumbers.getSource();
Integer lineNumberOrNull = lineNumbers.getLineNumber(member);
int lineNumber = lineNumberOrNull == null ? lineNumbers.getFirstLine() : lineNumberOrNull;
/*end[AOP]*/
/*if[NO_AOP]
String fileName = null;
int lineNumber = -1;
end[NO_AOP]*/
Class<? extends Member> memberType = Classes.memberType(member);
String memberName = memberType == Constructor.class ? "<init>" : member.getName();
return new StackTraceElement(declaringClass.getName(), memberName, fileName, lineNumber);
}
/**
* Creates the instance to be injected for the given member.
*
* @param <T> The type of the instance to be injected.
* @param name The name that was used to create the channel.
* @param injectionTarget The target member for the injection.
* @param injectionType The class that should injected.
* @param channel The channel that should be used to create the instance.
* @return The value that matches the type of the given field.
* @throws BeansException If the value of the field could not be created or the type of the field is unsupported.
*/
protected <T> T valueForMember(final String name, final Member injectionTarget,
final Class<T> injectionType,
final Channel channel) throws BeansException {
if (Channel.class.equals(injectionType)) {
return injectionType.cast(channel);
} else if (AbstractStub.class.isAssignableFrom(injectionType)) {
@SuppressWarnings("unchecked") // Eclipse incorrectly marks this as not required
AbstractStub<?> stub = createStub(injectionType.asSubclass(AbstractStub.class), channel);
for (final StubTransformer stubTransformer : getStubTransformers()) {
stub = stubTransformer.transform(name, stub);
}
return injectionType.cast(stub);
} else {
throw new InvalidPropertyException(injectionTarget.getDeclaringClass(), injectionTarget.getName(),
"Unsupported type " + injectionType.getName());
}
}
/**
* Does a conservative approximation of member access check. Use this if
* you don't have an actual 'userland' caller Class/ClassLoader available.
* This might be more restrictive than a precise member access check where
* you have a caller, but should never allow a member access that is
* forbidden.
*
* @param m the {@code Member} about to be accessed
*/
public static void conservativeCheckMemberAccess(Member m) throws SecurityException{
final SecurityManager sm = System.getSecurityManager();
if (sm == null)
return;
// Check for package access on the declaring class.
//
// In addition, unless the member and the declaring class are both
// public check for access declared member permissions.
//
// This is done regardless of ClassLoader relations between the {@code
// Member m} and any potential caller.
final Class<?> declaringClass = m.getDeclaringClass();
checkPackageAccess(declaringClass);
if (Modifier.isPublic(m.getModifiers()) &&
Modifier.isPublic(declaringClass.getModifiers()))
return;
// Check for declared member access.
sm.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION);
}
/**
* Does a conservative approximation of member access check. Use this if
* you don't have an actual 'userland' caller Class/ClassLoader available.
* This might be more restrictive than a precise member access check where
* you have a caller, but should never allow a member access that is
* forbidden.
*
* @param m the {@code Member} about to be accessed
*/
public static void conservativeCheckMemberAccess(Member m) throws SecurityException{
final SecurityManager sm = System.getSecurityManager();
if (sm == null)
return;
// Check for package access on the declaring class.
//
// In addition, unless the member and the declaring class are both
// public check for access declared member permissions.
//
// This is done regardless of ClassLoader relations between the {@code
// Member m} and any potential caller.
final Class<?> declaringClass = m.getDeclaringClass();
privateCheckPackageAccess(sm, declaringClass);
if (Modifier.isPublic(m.getModifiers()) &&
Modifier.isPublic(declaringClass.getModifiers()))
return;
// Check for declared member access.
sm.checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION);
}
boolean isAccessible(final Member m) {
final Class<?> declaring = m.getDeclaringClass();
// (declaring == clazz) is just an optimization - we're calling this only from code that operates on a
// non-restricted class, so if the declaring class is identical to the class being inspected, then forego
// a potentially expensive restricted-package check.
return declaring == clazz || !CheckRestrictedPackage.isRestrictedClass(declaring);
}
boolean isAccessible(Member m) {
final Class<?> declaring = m.getDeclaringClass();
// (declaring == clazz) is just an optimization - we're calling this only from code that operates on a
// non-restriced class, so if the declaring class is identical to the class being inspected, then forego
// a potentially expensive restricted-package check.
return declaring == clazz || !CheckRestrictedPackage.isRestrictedClass(declaring);
}
boolean isAccessible(final Member m) {
final Class<?> declaring = m.getDeclaringClass();
// (declaring == clazz) is just an optimization - we're calling this only from code that operates on a
// non-restricted class, so if the declaring class is identical to the class being inspected, then forego
// a potentially expensive restricted-package check.
return declaring == clazz || !CheckRestrictedPackage.isRestrictedClass(declaring);
}
boolean isAccessible(final Member m) {
final Class<?> declaring = m.getDeclaringClass();
// (declaring == clazz) is just an optimization - we're calling this only from code that operates on a
// non-restricted class, so if the declaring class is identical to the class being inspected, then forego
// a potentially expensive restricted-package check.
return declaring == clazz || !CheckRestrictedPackage.isRestrictedClass(declaring);
}
boolean isAccessible(Member m) {
final Class<?> declaring = m.getDeclaringClass();
// (declaring == clazz) is just an optimization - we're calling this only from code that operates on a
// non-restriced class, so if the declaring class is identical to the class being inspected, then forego
// a potentially expensive restricted-package check.
return declaring == clazz || !CheckRestrictedPackage.isRestrictedClass(declaring);
}
private MemberImpl(Member member) {
this.declaringClass = member.getDeclaringClass();
this.name = member.getName();
this.modifiers = member.getModifiers();
this.synthetic = member.isSynthetic();
this.memberType = memberType(member);
this.memberKey = memberKey(member);
}
public static Object forMember(Member member) {
if (member == null) {
return SourceProvider.UNKNOWN_SOURCE;
}
Class declaringClass = member.getDeclaringClass();
String fileName = null;
int lineNumber = -1;
Class<? extends Member> memberType = MoreTypes.memberType(member);
String memberName = memberType == Constructor.class ? "<init>" : member.getName();
return new StackTraceElement(declaringClass.getName(), memberName, fileName, lineNumber);
}
boolean isAccessible(final Member m) {
final Class<?> declaring = m.getDeclaringClass();
// (declaring == clazz) is just an optimization - we're calling this only from code that operates on a
// non-restricted class, so if the declaring class is identical to the class being inspected, then forego
// a potentially expensive restricted-package check.
return declaring == clazz || !CheckRestrictedPackage.isRestrictedClass(declaring);
}
/**
* Given a reflective method or a constructor, creates a dynamic method that represents it. This method will
* distinguish between caller sensitive and ordinary methods/constructors, and create appropriate caller sensitive
* dynamic method when needed.
* @param m the reflective member
* @return the single dynamic method representing the reflective member
*/
private static SingleDynamicMethod createDynamicMethod(AccessibleObject m) {
if(CallerSensitiveDetector.isCallerSensitive(m)) {
return new CallerSensitiveDynamicMethod(m);
}
final Member member = (Member)m;
return new SimpleDynamicMethod(unreflectSafely(m), member.getDeclaringClass(), member.getName());
}
/**
* Populates the {@code map} with variable/argument pairs for the
* {@code functionalInterface}.
*/
private static void populateLambdaArgs(Class<?> functionalInterface, final Class<?> lambdaType,
Map<TypeVariable<?>, Type> map) {
if (RESOLVES_LAMBDAS) {
// Find SAM
for (Method m : functionalInterface.getMethods()) {
if (!isDefaultMethod(m) && !Modifier.isStatic(m.getModifiers()) && !m.isBridge()) {
// Skip methods that override Object.class
Method objectMethod = OBJECT_METHODS.get(m.getName());
if (objectMethod != null && Arrays.equals(m.getTypeParameters(), objectMethod.getTypeParameters()))
continue;
// Get functional interface's type params
Type returnTypeVar = m.getGenericReturnType();
Type[] paramTypeVars = m.getGenericParameterTypes();
Member member = getMemberRef(lambdaType);
if (member == null)
return;
// Populate return type argument
if (returnTypeVar instanceof TypeVariable) {
Class<?> returnType = member instanceof Method ? ((Method) member).getReturnType()
: ((Constructor<?>) member).getDeclaringClass();
returnType = wrapPrimitives(returnType);
if (!returnType.equals(Void.class))
map.put((TypeVariable<?>) returnTypeVar, returnType);
}
Class<?>[] arguments = member instanceof Method ? ((Method) member).getParameterTypes()
: ((Constructor<?>) member).getParameterTypes();
// Populate object type from arbitrary object method reference
int paramOffset = 0;
if (paramTypeVars.length > 0 && paramTypeVars[0] instanceof TypeVariable
&& paramTypeVars.length == arguments.length + 1) {
Class<?> instanceType = member.getDeclaringClass();
map.put((TypeVariable<?>) paramTypeVars[0], instanceType);
paramOffset = 1;
}
// Handle additional arguments that are captured from the lambda's enclosing
// scope
int argOffset = 0;
if (paramTypeVars.length < arguments.length) {
argOffset = arguments.length - paramTypeVars.length;
}
// Populate type arguments
for (int i = 0; i + argOffset < arguments.length; i++) {
if (paramTypeVars[i] instanceof TypeVariable)
map.put((TypeVariable<?>) paramTypeVars[i + paramOffset],
wrapPrimitives(arguments[i + argOffset]));
}
return;
}
}
}
}
@Override
public boolean isMockedImplementationSupported(BeanManager beanManager, Annotated annotated)
{
if (!isMockSupportEnabled(annotated))
{
return false;
}
Class origin = null;
if (annotated instanceof AnnotatedType)
{
origin = ((AnnotatedType)annotated).getJavaClass();
Set<Annotation> annotations = new HashSet<Annotation>();
annotations.addAll(annotated.getAnnotations());
for (AnnotatedMethod annotatedMethod :
(Set<javax.enterprise.inject.spi.AnnotatedMethod>)((AnnotatedType) annotated).getMethods())
{
annotations.addAll(annotatedMethod.getAnnotations());
}
if (isEjbOrAnnotatedTypeWithInterceptorAnnotation(
beanManager, annotations, origin.getName()))
{
return false;
}
}
else if (annotated instanceof AnnotatedMember)
{
Member member = ((AnnotatedMember)annotated).getJavaMember();
origin = member.getDeclaringClass();
if (isEjbOrAnnotatedTypeWithInterceptorAnnotation(
beanManager, annotated.getAnnotations(), member.toString()))
{
return false;
}
}
if (origin != null && origin.getPackage() == null)
{
LOG.warning("Please don't use the default-package for " + origin.getName());
return true;
}
return origin != null && !isInternalPackage(origin.getPackage().getName());
}