Replace Class.newInstance() with Constructor.newInstance() (#1635)

This commit is contained in:
Akihito Nakano 2018-12-08 23:31:57 +09:00 committed by William Cheng
parent c4f0521e10
commit 7e3e9dba11
2 changed files with 5 additions and 4 deletions

View File

@ -45,7 +45,7 @@ public class CodegenConfigLoader {
// else try to load directly // else try to load directly
try { try {
return (CodegenConfig) Class.forName(name).newInstance(); return (CodegenConfig) Class.forName(name).getDeclaredConstructor().newInstance();
} catch (Exception e) { } catch (Exception e) {
throw new GeneratorNotFoundException("Can't load config class with name '".concat(name) + "'\nAvailable:\n" + availableConfigs.toString(), e); throw new GeneratorNotFoundException("Can't load config class with name '".concat(name) + "'\nAvailable:\n" + availableConfigs.toString(), e);
} }

View File

@ -17,6 +17,7 @@
package org.openapitools.codegen; package org.openapitools.codegen;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -35,7 +36,7 @@ public final class CodegenModelFactory {
throw new IllegalArgumentException(implementation.getSimpleName() + " doesn't extend " + type.getDefaultImplementation().getSimpleName()); throw new IllegalArgumentException(implementation.getSimpleName() + " doesn't extend " + type.getDefaultImplementation().getSimpleName());
} }
try { try {
implementation.newInstance(); implementation.getDeclaredConstructor().newInstance();
} catch (Exception e) { } catch (Exception e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
} }
@ -46,8 +47,8 @@ public final class CodegenModelFactory {
public static <T> T newInstance(CodegenModelType type) { public static <T> T newInstance(CodegenModelType type) {
Class<?> classType = typeMapping.get(type); Class<?> classType = typeMapping.get(type);
try { try {
return (T) (classType != null ? classType : type.getDefaultImplementation()).newInstance(); return (T) (classType != null ? classType : type.getDefaultImplementation()).getDeclaredConstructor().newInstance();
} catch (IllegalAccessException | InstantiationException e) { } catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }