RFR: 8301220: Return value of toArray() of Сollection types from java.base should be trusted [v3]
ExE Boss
duke at openjdk.org
Sun Feb 5 18:39:49 UTC 2023
On Mon, 30 Jan 2023 03:13:01 GMT, Glavo <duke at openjdk.org> wrote:
> > > > Why not use set of classes?
> > >
> > >
> > > Because some classes are not visible here (such as `Arrays.ArrayList`).
> > > I'm not sure what the best choice is, so I'm trying to explore the implementation plan.
> >
> >
> > Only `Arrays.ArrayList` is not visible here. You can change it to package-private.
>
> Another problem is that use set of classes may cause unnecessary classes to be loaded, which has a negative impact on startup speed. I want to do some tests in the next few days.
Class constants don’t cause class initialization, only bare‑bones loading (in case `NoClassDefFoundError` needs to be thrown).
--------------------------------------------------------------------------------
One option would be to move these sets into a method inner class, such as:
static boolean isTrustedCollection(Collection<?> coll) {
final class Holder {
private static final Set<Class<? extends Collection<?>>> TRUSTED_COLLECTIONS = Set.of(
// ...
);
private static final Set<Class<? extends Map<?, ?>>> TRUSTED_MAPS = Set.of(
// ...
);
}
if (coll.getClass().getModule() == Object.class.getModule()) {
if (coll instanceof CollectionWrapper wrapper) {
return isTrustedCollection(wrapper.getCollection());
}
if (Holder.TRUSTED_COLLECTIONS.contains(coll.getClass().getName())) {
return true;
}
if (coll instanceof ImmutableCollections.AbstractImmutableCollection<?>) {
return true;
}
// Map keys set or entries set
if (coll instanceof Set<?> && coll.getClass().isMemberClass()) {
return Holder.TRUSTED_MAPS.contains(coll.getClass().getNestHost().getName());
}
}
return false;
}
-------------
PR: https://git.openjdk.org/jdk/pull/12212
More information about the core-libs-dev
mailing list