Search in sources :

Example 6 with CallerSensitive

use of sun.reflect.CallerSensitive in project jdk8u_jdk by JetBrains.

the class Class method getMethod.

/**
     * Returns a {@code Method} object that reflects the specified public
     * member method of the class or interface represented by this
     * {@code Class} object. The {@code name} parameter is a
     * {@code String} specifying the simple name of the desired method. The
     * {@code parameterTypes} parameter is an array of {@code Class}
     * objects that identify the method's formal parameter types, in declared
     * order. If {@code parameterTypes} is {@code null}, it is
     * treated as if it were an empty array.
     *
     * <p> If the {@code name} is "{@code <init>}" or "{@code <clinit>}" a
     * {@code NoSuchMethodException} is raised. Otherwise, the method to
     * be reflected is determined by the algorithm that follows.  Let C be the
     * class or interface represented by this object:
     * <OL>
     * <LI> C is searched for a <I>matching method</I>, as defined below. If a
     *      matching method is found, it is reflected.</LI>
     * <LI> If no matching method is found by step 1 then:
     *   <OL TYPE="a">
     *   <LI> If C is a class other than {@code Object}, then this algorithm is
     *        invoked recursively on the superclass of C.</LI>
     *   <LI> If C is the class {@code Object}, or if C is an interface, then
     *        the superinterfaces of C (if any) are searched for a matching
     *        method. If any such method is found, it is reflected.</LI>
     *   </OL></LI>
     * </OL>
     *
     * <p> To find a matching method in a class or interface C:&nbsp; If C
     * declares exactly one public method with the specified name and exactly
     * the same formal parameter types, that is the method reflected. If more
     * than one such method is found in C, and one of these methods has a
     * return type that is more specific than any of the others, that method is
     * reflected; otherwise one of the methods is chosen arbitrarily.
     *
     * <p>Note that there may be more than one matching method in a
     * class because while the Java language forbids a class to
     * declare multiple methods with the same signature but different
     * return types, the Java virtual machine does not.  This
     * increased flexibility in the virtual machine can be used to
     * implement various language features.  For example, covariant
     * returns can be implemented with {@linkplain
     * java.lang.reflect.Method#isBridge bridge methods}; the bridge
     * method and the method being overridden would have the same
     * signature but different return types.
     *
     * <p> If this {@code Class} object represents an array type, then this
     * method does not find the {@code clone()} method.
     *
     * <p> Static methods declared in superinterfaces of the class or interface
     * represented by this {@code Class} object are not considered members of
     * the class or interface.
     *
     * @param name the name of the method
     * @param parameterTypes the list of parameters
     * @return the {@code Method} object that matches the specified
     *         {@code name} and {@code parameterTypes}
     * @throws NoSuchMethodException if a matching method is not found
     *         or if the name is "&lt;init&gt;"or "&lt;clinit&gt;".
     * @throws NullPointerException if {@code name} is {@code null}
     * @throws SecurityException
     *         If a security manager, <i>s</i>, is present and
     *         the caller's class loader is not the same as or an
     *         ancestor of the class loader for the current class and
     *         invocation of {@link SecurityManager#checkPackageAccess
     *         s.checkPackageAccess()} denies access to the package
     *         of this class.
     *
     * @jls 8.2 Class Members
     * @jls 8.4 Method Declarations
     * @since JDK1.1
     */
@CallerSensitive
public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException {
    checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
    Method method = getMethod0(name, parameterTypes, true);
    if (method == null) {
        throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
    }
    return method;
}
Also used : Method(java.lang.reflect.Method) CallerSensitive(sun.reflect.CallerSensitive)

Example 7 with CallerSensitive

use of sun.reflect.CallerSensitive in project jdk8u_jdk by JetBrains.

the class Class method getDeclaredField.

/**
     * Returns a {@code Field} object that reflects the specified declared
     * field of the class or interface represented by this {@code Class}
     * object. The {@code name} parameter is a {@code String} that specifies
     * the simple name of the desired field.
     *
     * <p> If this {@code Class} object represents an array type, then this
     * method does not find the {@code length} field of the array type.
     *
     * @param name the name of the field
     * @return  the {@code Field} object for the specified field in this
     *          class
     * @throws  NoSuchFieldException if a field with the specified name is
     *          not found.
     * @throws  NullPointerException if {@code name} is {@code null}
     * @throws  SecurityException
     *          If a security manager, <i>s</i>, is present and any of the
     *          following conditions is met:
     *
     *          <ul>
     *
     *          <li> the caller's class loader is not the same as the
     *          class loader of this class and invocation of
     *          {@link SecurityManager#checkPermission
     *          s.checkPermission} method with
     *          {@code RuntimePermission("accessDeclaredMembers")}
     *          denies access to the declared field
     *
     *          <li> the caller's class loader is not the same as or an
     *          ancestor of the class loader for the current class and
     *          invocation of {@link SecurityManager#checkPackageAccess
     *          s.checkPackageAccess()} denies access to the package
     *          of this class
     *
     *          </ul>
     *
     * @since JDK1.1
     * @jls 8.2 Class Members
     * @jls 8.3 Field Declarations
     */
@CallerSensitive
public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException {
    checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
    Field field = searchFields(privateGetDeclaredFields(false), name);
    if (field == null) {
        throw new NoSuchFieldException(name);
    }
    return field;
}
Also used : ObjectStreamField(java.io.ObjectStreamField) Field(java.lang.reflect.Field) CallerSensitive(sun.reflect.CallerSensitive)

Example 8 with CallerSensitive

use of sun.reflect.CallerSensitive in project jdk8u_jdk by JetBrains.

the class Class method getEnclosingMethod.

/**
     * If this {@code Class} object represents a local or anonymous
     * class within a method, returns a {@link
     * java.lang.reflect.Method Method} object representing the
     * immediately enclosing method of the underlying class. Returns
     * {@code null} otherwise.
     *
     * In particular, this method returns {@code null} if the underlying
     * class is a local or anonymous class immediately enclosed by a type
     * declaration, instance initializer or static initializer.
     *
     * @return the immediately enclosing method of the underlying class, if
     *     that class is a local or anonymous class; otherwise {@code null}.
     *
     * @throws SecurityException
     *         If a security manager, <i>s</i>, is present and any of the
     *         following conditions is met:
     *
     *         <ul>
     *
     *         <li> the caller's class loader is not the same as the
     *         class loader of the enclosing class and invocation of
     *         {@link SecurityManager#checkPermission
     *         s.checkPermission} method with
     *         {@code RuntimePermission("accessDeclaredMembers")}
     *         denies access to the methods within the enclosing class
     *
     *         <li> the caller's class loader is not the same as or an
     *         ancestor of the class loader for the enclosing class and
     *         invocation of {@link SecurityManager#checkPackageAccess
     *         s.checkPackageAccess()} denies access to the package
     *         of the enclosing class
     *
     *         </ul>
     * @since 1.5
     */
@CallerSensitive
public Method getEnclosingMethod() throws SecurityException {
    EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();
    if (enclosingInfo == null)
        return null;
    else {
        if (!enclosingInfo.isMethod())
            return null;
        MethodRepository typeInfo = MethodRepository.make(enclosingInfo.getDescriptor(), getFactory());
        Class<?> returnType = toClass(typeInfo.getReturnType());
        Type[] parameterTypes = typeInfo.getParameterTypes();
        Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];
        // don't have generics information
        for (int i = 0; i < parameterClasses.length; i++) parameterClasses[i] = toClass(parameterTypes[i]);
        // Perform access check
        Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass();
        enclosingCandidate.checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
        /*
             * Loop over all declared methods; match method name,
             * number of and type of parameters, *and* return
             * type.  Matching return type is also necessary
             * because of covariant returns, etc.
             */
        for (Method m : enclosingCandidate.getDeclaredMethods()) {
            if (m.getName().equals(enclosingInfo.getName())) {
                Class<?>[] candidateParamClasses = m.getParameterTypes();
                if (candidateParamClasses.length == parameterClasses.length) {
                    boolean matches = true;
                    for (int i = 0; i < candidateParamClasses.length; i++) {
                        if (!candidateParamClasses[i].equals(parameterClasses[i])) {
                            matches = false;
                            break;
                        }
                    }
                    if (matches) {
                        // finally, check return type
                        if (m.getReturnType().equals(returnType))
                            return m;
                    }
                }
            }
        }
        throw new InternalError("Enclosing method not found");
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) AnnotatedType(java.lang.reflect.AnnotatedType) Type(java.lang.reflect.Type) MethodRepository(sun.reflect.generics.repository.MethodRepository) Method(java.lang.reflect.Method) CallerSensitive(sun.reflect.CallerSensitive)

Example 9 with CallerSensitive

use of sun.reflect.CallerSensitive in project jdk8u_jdk by JetBrains.

the class Class method getEnclosingConstructor.

/**
     * If this {@code Class} object represents a local or anonymous
     * class within a constructor, returns a {@link
     * java.lang.reflect.Constructor Constructor} object representing
     * the immediately enclosing constructor of the underlying
     * class. Returns {@code null} otherwise.  In particular, this
     * method returns {@code null} if the underlying class is a local
     * or anonymous class immediately enclosed by a type declaration,
     * instance initializer or static initializer.
     *
     * @return the immediately enclosing constructor of the underlying class, if
     *     that class is a local or anonymous class; otherwise {@code null}.
     * @throws SecurityException
     *         If a security manager, <i>s</i>, is present and any of the
     *         following conditions is met:
     *
     *         <ul>
     *
     *         <li> the caller's class loader is not the same as the
     *         class loader of the enclosing class and invocation of
     *         {@link SecurityManager#checkPermission
     *         s.checkPermission} method with
     *         {@code RuntimePermission("accessDeclaredMembers")}
     *         denies access to the constructors within the enclosing class
     *
     *         <li> the caller's class loader is not the same as or an
     *         ancestor of the class loader for the enclosing class and
     *         invocation of {@link SecurityManager#checkPackageAccess
     *         s.checkPackageAccess()} denies access to the package
     *         of the enclosing class
     *
     *         </ul>
     * @since 1.5
     */
@CallerSensitive
public Constructor<?> getEnclosingConstructor() throws SecurityException {
    EnclosingMethodInfo enclosingInfo = getEnclosingMethodInfo();
    if (enclosingInfo == null)
        return null;
    else {
        if (!enclosingInfo.isConstructor())
            return null;
        ConstructorRepository typeInfo = ConstructorRepository.make(enclosingInfo.getDescriptor(), getFactory());
        Type[] parameterTypes = typeInfo.getParameterTypes();
        Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];
        // don't have generics information
        for (int i = 0; i < parameterClasses.length; i++) parameterClasses[i] = toClass(parameterTypes[i]);
        // Perform access check
        Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass();
        enclosingCandidate.checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
        /*
             * Loop over all declared constructors; match number
             * of and type of parameters.
             */
        for (Constructor<?> c : enclosingCandidate.getDeclaredConstructors()) {
            Class<?>[] candidateParamClasses = c.getParameterTypes();
            if (candidateParamClasses.length == parameterClasses.length) {
                boolean matches = true;
                for (int i = 0; i < candidateParamClasses.length; i++) {
                    if (!candidateParamClasses[i].equals(parameterClasses[i])) {
                        matches = false;
                        break;
                    }
                }
                if (matches)
                    return c;
            }
        }
        throw new InternalError("Enclosing constructor not found");
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) AnnotatedType(java.lang.reflect.AnnotatedType) Type(java.lang.reflect.Type) ConstructorRepository(sun.reflect.generics.repository.ConstructorRepository) CallerSensitive(sun.reflect.CallerSensitive)

Example 10 with CallerSensitive

use of sun.reflect.CallerSensitive in project openj9 by eclipse.

the class MsgHelp method loadMessages.

// Loads properties from the specified resource. The properties are of
// the form <code>key=value</code>, one property per line.
/*[IF]*/
// This is taken from java.util.Properties#load(InputStream).
/*[ENDIF]*/
@CallerSensitive
public static Hashtable loadMessages(String resourceName) throws IOException {
    InputStream resourceStream;
    String language, region, variant;
    String resName;
    Properties props = new Properties();
    Locale defLocale = Locale.getDefault();
    language = defLocale.getLanguage();
    // $NON-NLS-1$
    if (language.length() == 0)
        language = "en";
    region = defLocale.getCountry();
    // $NON-NLS-1$
    if (region.length() == 0)
        region = "US";
    variant = defLocale.getVariant();
    /*[IF !Sidecar19-SE]
	final ClassLoader loader = com.ibm.oti.vm.VM.getStackClassLoader(1);
	if (null == loader) {
		return null;
	}
/*[ENDIF]*/
    /*[IF Sidecar19-SE]*/
    Module module = MsgHelp.class.getModule();
    if (module == null) {
        /* Too early in bootstrap - modules not initialized yet */
        return null;
    }
    if (variant.length() > 0) {
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        resName = resourceName + "_" + language + "_" + region + "_" + variant + ".properties";
        /*[IF Sidecar19-SE]
		resourceStream = module.getResourceAsStream(resName);
/*[ELSE]*/
        resourceStream = loader.getResourceAsStream(resName);
        /*[ENDIF]*/
        if (resourceStream != null) {
            props.load(resourceStream);
            /*[PR CMVC 182098] Perf: zWAS ftprint regressed 6% Java7 vs 626FP1 (MsgHelp) */
            resourceStream.close();
            return props;
        }
    }
    // $NON-NLS-1$  //$NON-NLS-2$ //$NON-NLS-3$
    resName = resourceName + "_" + language + "_" + region + ".properties";
    /*[IF Sidecar19-SE]
	resourceStream = module.getResourceAsStream(resName);
/*[ELSE]*/
    resourceStream = loader.getResourceAsStream(resName);
    /*[ENDIF]*/
    if (resourceStream != null) {
        props.load(resourceStream);
        resourceStream.close();
        return props;
    }
    // $NON-NLS-1$  //$NON-NLS-2$
    resName = resourceName + "_" + language + ".properties";
    /*[IF Sidecar19-SE] 
	resourceStream = module.getResourceAsStream(resName);
/*[ELSE]*/
    resourceStream = loader.getResourceAsStream(resName);
    /*[ENDIF]*/
    if (resourceStream != null) {
        props.load(resourceStream);
        resourceStream.close();
        return props;
    }
    /*[IF Sidecar19-SE] 
	resourceStream = module.getResourceAsStream(resourceName + ".properties"); //$NON-NLS-1$	
/*[ELSE]*/
    // $NON-NLS-1$
    resourceStream = loader.getResourceAsStream(resourceName + ".properties");
    /*[ENDIF]*/
    if (resourceStream != null) {
        props.load(resourceStream);
        resourceStream.close();
        return props;
    } else {
        return null;
    }
}
Also used : InputStream(java.io.InputStream) CallerSensitive(sun.reflect.CallerSensitive)

Aggregations

CallerSensitive (sun.reflect.CallerSensitive)10 Method (java.lang.reflect.Method)3 ObjectStreamField (java.io.ObjectStreamField)2 AnnotatedType (java.lang.reflect.AnnotatedType)2 Field (java.lang.reflect.Field)2 GenericArrayType (java.lang.reflect.GenericArrayType)2 Type (java.lang.reflect.Type)2 MethodAccessor (sun.reflect.MethodAccessor)2 InputStream (java.io.InputStream)1 ConstructorAccessor (sun.reflect.ConstructorAccessor)1 ConstructorRepository (sun.reflect.generics.repository.ConstructorRepository)1 MethodRepository (sun.reflect.generics.repository.MethodRepository)1