Skip to content

Commit

Permalink
DefaultFactory#create(Class<T>):
Browse files Browse the repository at this point in the history
Reinstated use of `Class#newInstance()` instead of `Constructor#newInstance()`.

Only use `Constructor#setAccessible(true)` if `IllegalAccessException` thrown.
  • Loading branch information
rgoldberg authored and remkop committed Dec 15, 2021
1 parent 4e06c96 commit 2c6747e
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -5505,12 +5505,19 @@ public <T> T create(Class<T> cls) throws Exception {
return cls.cast(new LinkedHashMap<Object, Object>());
}
}
Constructor<T> constructor = cls.getDeclaredConstructor();
try {
return constructor.newInstance();
} catch (IllegalAccessException ex) {
constructor.setAccessible(true);
return constructor.newInstance();
@SuppressWarnings("deprecation") // Class.newInstance is deprecated in Java 9
T result = cls.newInstance();
return result;
} catch (Exception ex) {
// TODO log the error at debug level
Constructor<T> constructor = cls.getDeclaredConstructor();
try {
return constructor.newInstance();
} catch (IllegalAccessException iaex) {
constructor.setAccessible(true);
return constructor.newInstance();
}
}
}
private static ITypeConverter<?>[] createConverter(IFactory factory, Class<? extends ITypeConverter<?>>[] classes) {
Expand Down

0 comments on commit 2c6747e

Please sign in to comment.