JDK-8232227 : Lookup::in should not allow target class be primitive or array class
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.lang.invoke
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 14
  • Submitted: 2019-10-14
  • Updated: 2019-10-29
  • Resolved: 2019-10-29
Related Reports
CSR :  
Description
Summary
-------

`Lookup::in` throws `IllegalArgumentException` if the target class is a primitive type, void or an array class.

Problem
-------

The current spec of `Lookup::in` allows to produce a Lookup object on a primitive type,
void or an array class which is against the original design that assumes the lookup class 
of a Lookup object is never non-array and non-primitive class (method handle
lookup is as if a specified member is referenced via bytecode)

The static factory methods `MethodHandles::lookup`, `MethodHandles::privateLookupIn` 
never produce a Lookup object on primitive type, void or array class.

Solution
--------

Change the spec of `Lookup::in` to throw `IllegalArgumentException`
if `requestedLookupClass` is a primitive type, void or array type.

An alternative would be that `Lookup::in` returns a Lookup object 
successfully but drop all lookup modes if `requestedLookupClass` is 
a primitive type, void or array type.   This delays the failure point to method handle
lookup time.  It makes it harder to diagnose if any existing code is depending
on the existing behavior. 

It is expected that it's rare for existing code to call `Lookup::in` on a
primitive type, void or array type.  It'd be more helpful to  throw IAE when a 
bad `requestLookupClass` argument is passed to `Lookup::in`.

Specification
-------------

The spec of `Lookup::in` method has the following new `@throws IAE`:

```
+         * @throws IllegalArgumentException if {@code requestedLookupClass} is a primitive type or void or array class
```

`@throw IAE` in the spec of `MethodHandles::privateLookupIn` method is updated to explicitly call out `void`:

```
-     * @throws IllegalArgumentException if {@code targetClass} is a primitive type or array class
+     * @throws IllegalArgumentException if {@code targetClass} is a primitive type or void or array class
```
Comments
Moving to Approved.
29-10-2019

[~darcy] the `@throw IAE` spec is updated to call out void explicitly. This CSR also fixes the existing `@throw IAE` of `MethodHandles::privateLookupIn` to call out void.
29-10-2019

Catching up on CSR reviews, I suggest explicitly calling out void for the presumed IAE behavior here. (java.lang.Class.isPrimative treats Void.TYPE/void.class as primitive, but this is not a natural categorization so if worth noting void handling explicitly.)
29-10-2019