Java Code Examples for java.lang.reflect.Member#getDeclaringClass()

The following examples show how to use java.lang.reflect.Member#getDeclaringClass() . 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: ReflectUtil.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 2
Source File: AbstractJavaLinker.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 3
Source File: ReflectUtil.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 4
Source File: ReflectUtil.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 5
Source File: ReflectUtil.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 6
Source File: ReflectUtil.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 7
Source File: StackTraceElements.java    From businessworks with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 8
Source File: GrpcClientBeanPostProcessor.java    From grpc-spring-boot-starter with MIT License 6 votes vote down vote up
/**
 * 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());
    }
}
 
Example 9
Source File: ReflectUtil.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 10
Source File: AbstractJavaLinker.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example 11
Source File: FacetIntrospector.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 12
Source File: StackTraceElements.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 13
Source File: MoreTypes.java    From crate with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 14
Source File: FacetIntrospector.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 15
Source File: FacetIntrospector.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 16
Source File: FacetIntrospector.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 17
Source File: FacetIntrospector.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 18
Source File: FacetIntrospector.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 19
Source File: DefaultMockFilter.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@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());
}
 
Example 20
Source File: TypeResolver.java    From spring-fabric-gateway with MIT License 4 votes vote down vote up
/**
 * 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;
			}
		}
	}
}