RFR: Add utility for creating objects via factory method when unable to create via constructor
Kyle J Stiemann
duke at openjdk.org
Tue Apr 30 15:30:30 UTC 2024
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());
}
}
-------------
Commit messages:
- Add utility for creating objects via factory method when unable to create via constructor
Changes: https://git.openjdk.org/jol/pull/58/files
Webrev: https://webrevs.openjdk.org/?repo=jol&pr=58&range=00
Stats: 154 lines in 7 files changed: 143 ins; 0 del; 11 mod
Patch: https://git.openjdk.org/jol/pull/58.diff
Fetch: git fetch https://git.openjdk.org/jol.git pull/58/head:pull/58
PR: https://git.openjdk.org/jol/pull/58
More information about the jol-dev
mailing list