mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-18 00:23:32 +00:00
refactor: Remove deprecated method of newInstance (#2594)
This commit is contained in:
@@ -38,7 +38,7 @@ public class FrontController {
|
|||||||
private Command getCommand(String request) {
|
private Command getCommand(String request) {
|
||||||
var commandClass = getCommandClass(request);
|
var commandClass = getCommandClass(request);
|
||||||
try {
|
try {
|
||||||
return (Command) commandClass.newInstance();
|
return (Command) commandClass.getDeclaredConstructor().newInstance();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ApplicationException(e);
|
throw new ApplicationException(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.iluwatar.roleobject;
|
package com.iluwatar.roleobject;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -50,8 +51,8 @@ public enum Role {
|
|||||||
public <T extends CustomerRole> Optional<T> instance() {
|
public <T extends CustomerRole> Optional<T> instance() {
|
||||||
var typeCst = this.typeCst;
|
var typeCst = this.typeCst;
|
||||||
try {
|
try {
|
||||||
return (Optional<T>) Optional.of(typeCst.newInstance());
|
return (Optional<T>) Optional.of(typeCst.getDeclaredConstructor().newInstance());
|
||||||
} catch (InstantiationException | IllegalAccessException e) {
|
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
||||||
logger.error("error creating an object", e);
|
logger.error("error creating an object", e);
|
||||||
}
|
}
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|||||||
Reference in New Issue
Block a user