RFR: Add utility for creating objects via factory method when unable to create via constructor

Aleksey Shipilev shade at openjdk.org
Fri Jul 19 11:21:43 UTC 2024


On Thu, 7 Mar 2024 17:27:38 GMT, Kyle J Stiemann <duke at openjdk.org> wrote:

> Adds CLI option to provide a factory FQCN which can create objects that you want to analyze. This is useful in situations where you have dependency classes or JDK classes that require non-null objects or have other validation in the constructor. These kinds of classes can't easily be modified, so it's nice to provide an alternative option for creating them.
> 
> For example:
> 
> 
>     public static final class RequiresFactory {
>        public RequiresFactory(final String string) {
>            Objects.requireNonNull(string);
>            if (string.isEmpty()) {
>                throw new IllegalArgumentException("string must not be empty");
>            }
>        }
>     }
> 
> 
> Usage:
> 
> 
> java -jar jol-cli.jar internals --classpath my-classes.jar --factory my.Factory my.RequiresFactory
> 
> 
> Where `my.Factory` is:
> 
> 
> public class Factory {
>     public static <T> T newInstance(Class<T> aClass) {
> 
>         if (aClass.equals(RequiresFactory.class)) {
>             return aClass.cast(new RequiresFactory("test"));
>         }
> 
>         throw new UnsupportedOperationException(aClass.getTypeName());
>     }
> }

Sorry, it slipped off my radar.

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

PR Comment: https://git.openjdk.org/jol/pull/58#issuecomment-2238930302


More information about the jol-dev mailing list