diff --git a/front-controller/src/main/java/com/iluwatar/front/controller/FrontController.java b/front-controller/src/main/java/com/iluwatar/front/controller/FrontController.java index 21a49f625e5c..995f917b3fd5 100644 --- a/front-controller/src/main/java/com/iluwatar/front/controller/FrontController.java +++ b/front-controller/src/main/java/com/iluwatar/front/controller/FrontController.java @@ -38,7 +38,7 @@ public void handleRequest(String request) { private Command getCommand(String request) { var commandClass = getCommandClass(request); try { - return (Command) commandClass.newInstance(); + return (Command) commandClass.getDeclaredConstructor().newInstance(); } catch (Exception e) { throw new ApplicationException(e); } diff --git a/role-object/src/main/java/com/iluwatar/roleobject/Role.java b/role-object/src/main/java/com/iluwatar/roleobject/Role.java index ef808c15dd6b..de216b0d7a07 100644 --- a/role-object/src/main/java/com/iluwatar/roleobject/Role.java +++ b/role-object/src/main/java/com/iluwatar/roleobject/Role.java @@ -24,6 +24,7 @@ */ package com.iluwatar.roleobject; +import java.lang.reflect.InvocationTargetException; import java.util.Optional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -50,8 +51,8 @@ public enum Role { public Optional instance() { var typeCst = this.typeCst; try { - return (Optional) Optional.of(typeCst.newInstance()); - } catch (InstantiationException | IllegalAccessException e) { + return (Optional) Optional.of(typeCst.getDeclaredConstructor().newInstance()); + } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { logger.error("error creating an object", e); } return Optional.empty();