< prev index next >

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

Print this page




2578          * be initialized, if it has not already been initialized.
2579          * @param c the reflected constructor
2580          * @return a method handle which can invoke the reflected constructor
2581          * @throws IllegalAccessException if access checking fails
2582          *                                or if the method's variable arity modifier bit
2583          *                                is set and {@code asVarargsCollector} fails
2584          * @throws NullPointerException if the argument is null
2585          */
2586         public MethodHandle unreflectConstructor(Constructor<?> c) throws IllegalAccessException {
2587             MemberName ctor = new MemberName(c);
2588             assert(ctor.isObjectConstructorOrStaticInitMethod());
2589             @SuppressWarnings("deprecation")
2590             Lookup lookup = c.isAccessible() ? IMPL_LOOKUP : this;
2591             if (ctor.isObjectConstructor()) {
2592                 assert(ctor.getReturnType() == void.class);
2593                 return lookup.getDirectConstructorNoSecurityManager(ctor.getDeclaringClass(), ctor);
2594             } else {
2595                 // static init factory is a static method
2596                 assert(ctor.isMethod() && ctor.getReturnType() == ctor.getDeclaringClass() && ctor.getReferenceKind() == REF_invokeStatic);
2597                 assert(!MethodHandleNatives.isCallerSensitive(ctor));  // must not be caller-sensitive
2598                 return lookup.getDirectMethodNoSecurityManager(ctor.getReferenceKind(), ctor.getDeclaringClass(), ctor, null);
2599             }
2600         }
2601 
2602         /**
2603          * Produces a method handle giving read access to a reflected field.
2604          * The type of the method handle will have a return type of the field's
2605          * value type.
2606          * If the field is {@code static}, the method handle will take no arguments.
2607          * Otherwise, its single argument will be the instance containing
2608          * the field.
2609          * If the {@code Field} object's {@code accessible} flag is not set,
2610          * access checking is performed immediately on behalf of the lookup class.
2611          * <p>
2612          * If the field is static, and
2613          * if the returned method handle is invoked, the field's class will
2614          * be initialized, if it has not already been initialized.
2615          * @param f the reflected field
2616          * @return a method handle which can load values from the reflected field
2617          * @throws IllegalAccessException if access checking fails
2618          * @throws NullPointerException if the argument is null




2578          * be initialized, if it has not already been initialized.
2579          * @param c the reflected constructor
2580          * @return a method handle which can invoke the reflected constructor
2581          * @throws IllegalAccessException if access checking fails
2582          *                                or if the method's variable arity modifier bit
2583          *                                is set and {@code asVarargsCollector} fails
2584          * @throws NullPointerException if the argument is null
2585          */
2586         public MethodHandle unreflectConstructor(Constructor<?> c) throws IllegalAccessException {
2587             MemberName ctor = new MemberName(c);
2588             assert(ctor.isObjectConstructorOrStaticInitMethod());
2589             @SuppressWarnings("deprecation")
2590             Lookup lookup = c.isAccessible() ? IMPL_LOOKUP : this;
2591             if (ctor.isObjectConstructor()) {
2592                 assert(ctor.getReturnType() == void.class);
2593                 return lookup.getDirectConstructorNoSecurityManager(ctor.getDeclaringClass(), ctor);
2594             } else {
2595                 // static init factory is a static method
2596                 assert(ctor.isMethod() && ctor.getReturnType() == ctor.getDeclaringClass() && ctor.getReferenceKind() == REF_invokeStatic);
2597                 assert(!MethodHandleNatives.isCallerSensitive(ctor));  // must not be caller-sensitive
2598                 return lookup.getDirectMethodNoSecurityManager(ctor.getReferenceKind(), ctor.getDeclaringClass(), ctor, lookup);
2599             }
2600         }
2601 
2602         /**
2603          * Produces a method handle giving read access to a reflected field.
2604          * The type of the method handle will have a return type of the field's
2605          * value type.
2606          * If the field is {@code static}, the method handle will take no arguments.
2607          * Otherwise, its single argument will be the instance containing
2608          * the field.
2609          * If the {@code Field} object's {@code accessible} flag is not set,
2610          * access checking is performed immediately on behalf of the lookup class.
2611          * <p>
2612          * If the field is static, and
2613          * if the returned method handle is invoked, the field's class will
2614          * be initialized, if it has not already been initialized.
2615          * @param f the reflected field
2616          * @return a method handle which can load values from the reflected field
2617          * @throws IllegalAccessException if access checking fails
2618          * @throws NullPointerException if the argument is null


< prev index next >