< prev index next >

src/java.base/share/classes/java/lang/invoke/MethodHandles.java

Print this page




1910          * @param type the type of the method, with the receiver argument omitted, and a void return type
1911          * @return the desired method handle
1912          * @throws NoSuchMethodException if the constructor does not exist
1913          * @throws IllegalAccessException if access checking fails
1914          *                                or if the method's variable arity modifier bit
1915          *                                is set and {@code asVarargsCollector} fails
1916          * @exception SecurityException if a security manager is present and it
1917          *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
1918          * @throws NullPointerException if any argument is null
1919          */
1920         public MethodHandle findConstructor(Class<?> refc, MethodType type) throws NoSuchMethodException, IllegalAccessException {
1921             if (refc.isArray()) {
1922                 throw new NoSuchMethodException("no constructor for array class: " + refc.getName());
1923             }
1924             String name = "<init>";
1925             MemberName ctor = resolveOrFail(REF_newInvokeSpecial, refc, name, type);
1926             return getDirectConstructor(refc, ctor);
1927         }
1928 
1929         /**
1930          * Looks up a class by name from the lookup context defined by this {@code Lookup} object. The static

1931          * initializer of the class is not run.
1932          * <p>
1933          * The lookup context here is determined by the {@linkplain #lookupClass() lookup class}, its class
1934          * loader, and the {@linkplain #lookupModes() lookup modes}. In particular, the method first attempts to
1935          * load the requested class, and then determines whether the class is accessible to this lookup object.




1936          *
1937          * @param targetName the fully qualified name of the class to be looked up.
1938          * @return the requested class.
1939          * @exception SecurityException if a security manager is present and it
1940          *            <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
1941          * @throws LinkageError if the linkage fails
1942          * @throws ClassNotFoundException if the class cannot be loaded by the lookup class' loader.
1943          * @throws IllegalAccessException if the class is not accessible, using the allowed access
1944          * modes.
1945          * @exception SecurityException if a security manager is present and it
1946          *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>



1947          * @since 9
1948          */
1949         public Class<?> findClass(String targetName) throws ClassNotFoundException, IllegalAccessException {
1950             Class<?> targetClass = Class.forName(targetName, false, lookupClass.getClassLoader());
1951             return accessClass(targetClass);
1952         }
1953 
1954         /**
1955          * Determines if a class can be accessed from the lookup context defined by
1956          * this {@code Lookup} object. The static initializer of the class is not run.
1957          * <p>
1958          * If the {@code targetClass} is in the same module as the lookup class,
1959          * the lookup class is {@code LC} in module {@code M1} and
1960          * the previous lookup class is in module {@code M0} or
1961          * {@code null} if not present,
1962          * {@code targetClass} is accessible if and only if one of the following is true:
1963          * <ul>
1964          * <li>If this lookup has {@link #PRIVATE} access, {@code targetClass} is
1965          *     {@code LC} or other class in the same nest of {@code LC}.</li>
1966          * <li>If this lookup has {@link #PACKAGE} access, {@code targetClass} is




1910          * @param type the type of the method, with the receiver argument omitted, and a void return type
1911          * @return the desired method handle
1912          * @throws NoSuchMethodException if the constructor does not exist
1913          * @throws IllegalAccessException if access checking fails
1914          *                                or if the method's variable arity modifier bit
1915          *                                is set and {@code asVarargsCollector} fails
1916          * @exception SecurityException if a security manager is present and it
1917          *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
1918          * @throws NullPointerException if any argument is null
1919          */
1920         public MethodHandle findConstructor(Class<?> refc, MethodType type) throws NoSuchMethodException, IllegalAccessException {
1921             if (refc.isArray()) {
1922                 throw new NoSuchMethodException("no constructor for array class: " + refc.getName());
1923             }
1924             String name = "<init>";
1925             MemberName ctor = resolveOrFail(REF_newInvokeSpecial, refc, name, type);
1926             return getDirectConstructor(refc, ctor);
1927         }
1928 
1929         /**
1930          * Looks up a class by name from the lookup context defined by this {@code Lookup} object.
1931          * This method attempts to locate, load, and link the class.  The static
1932          * initializer of the class is not run.
1933          * <p>
1934          * The lookup context here is determined by the {@linkplain #lookupClass() lookup class}, its class
1935          * loader, and the {@linkplain #lookupModes() lookup modes}. In particular, the method first attempts to
1936          * load the requested class, and then determines whether the class is accessible to this lookup object.
1937          * <p>
1938          * Note that this method throws errors related to loading and linking as
1939          * specified in Sections 12.2 and 12.3 of <em>The Java Language
1940          * Specification</em>.
1941          *
1942          * @param targetName the fully qualified name of the class to be looked up.
1943          * @return the requested class.
1944          * @exception SecurityException if a security manager is present and it
1945          *            <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
1946          * @throws LinkageError if the linkage fails
1947          * @throws ClassNotFoundException if the class cannot be loaded by the lookup class' loader.
1948          * @throws IllegalAccessException if the class is not accessible, using the allowed access
1949          * modes.
1950          * @exception SecurityException if a security manager is present and it
1951          *                              <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
1952          *
1953          * @jls 12.2 Loading of Classes and Interfaces
1954          * @jls 12.3 Linking of Classes and Interfaces
1955          * @since 9
1956          */
1957         public Class<?> findClass(String targetName) throws ClassNotFoundException, IllegalAccessException {
1958             Class<?> targetClass = Class.forName(targetName, false, lookupClass.getClassLoader());
1959             return accessClass(targetClass);
1960         }
1961 
1962         /**
1963          * Determines if a class can be accessed from the lookup context defined by
1964          * this {@code Lookup} object. The static initializer of the class is not run.
1965          * <p>
1966          * If the {@code targetClass} is in the same module as the lookup class,
1967          * the lookup class is {@code LC} in module {@code M1} and
1968          * the previous lookup class is in module {@code M0} or
1969          * {@code null} if not present,
1970          * {@code targetClass} is accessible if and only if one of the following is true:
1971          * <ul>
1972          * <li>If this lookup has {@link #PRIVATE} access, {@code targetClass} is
1973          *     {@code LC} or other class in the same nest of {@code LC}.</li>
1974          * <li>If this lookup has {@link #PACKAGE} access, {@code targetClass} is


< prev index next >