RFR: 8332586: Avoid cloning empty arrays in java.lang.reflect.{Method,Constructor}

Chen Liang liach at openjdk.org
Thu May 30 19:30:14 UTC 2024


On Wed, 22 May 2024 14:33:31 GMT, Aleksey Shipilev <shade at openjdk.org> wrote:

>> Why can't you do something like this:
>> 
>> return cloneArray ? ReflectionFactory.copyClasses(interfaces) : interfaces;
>> 
>> 
>> Alternatively, your proposal is a good one too; we can rename this `getInterfaces(boolean)` to `getInterfacesShared()` (like `getSharedEnumConstants()`) and move this copy operation to `getInterfaces()` as you suggest.
>
> I really see no reason to try and save on re-use of this one-line method for _everything_. In fact, I do not quite see a very compelling reason to even have the utility method. Sharing the code between `Method` and `Constructor` is clean enough and provides a good balance between reuse and cleanliness. Everything else can have the copy of the method definition, if needed.

Alternatively, if a utility method is overkill, we can inline these to `Executable`:

public Class<?>[] getParameterTypes() {
    var shared = getSharedParameterTypes();
    return shared.length == 0 ? shared : shared.clone();
}

And the overrides in `Method` and `Constructor` will simply call super; the declarations are kept to preserve the API documentation.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/19327#discussion_r1610561896


More information about the core-libs-dev mailing list