mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 08:58:26 +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) {
|
||||
var commandClass = getCommandClass(request);
|
||||
try {
|
||||
return (Command) commandClass.newInstance();
|
||||
return (Command) commandClass.getDeclaredConstructor().newInstance();
|
||||
} catch (Exception e) {
|
||||
throw new ApplicationException(e);
|
||||
}
|
||||
|
||||
@@ -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 <T extends CustomerRole> Optional<T> instance() {
|
||||
var typeCst = this.typeCst;
|
||||
try {
|
||||
return (Optional<T>) Optional.of(typeCst.newInstance());
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
return (Optional<T>) Optional.of(typeCst.getDeclaredConstructor().newInstance());
|
||||
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
||||
logger.error("error creating an object", e);
|
||||
}
|
||||
return Optional.empty();
|
||||
|
||||
Reference in New Issue
Block a user