[REFACTOR] Replaced to pattern matching instanceof (#664)

This commit is contained in:
microwin7 2023-07-15 10:37:26 +03:00 committed by GitHub
parent 216928f258
commit 2aa8dffcaa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 35 additions and 37 deletions

View file

@ -60,8 +60,8 @@ public void invoke(String... args) throws Exception {
logger.error("Component {} not found", componentName);
return;
}
if (component instanceof AutoCloseable) {
((AutoCloseable) component).close();
if (component instanceof AutoCloseable autoCloseable) {
autoCloseable.close();
}
server.unregisterObject("component." + componentName, component);
server.config.components.remove(componentName);

View file

@ -209,9 +209,9 @@ public void close(LaunchServer.ReloadType type) {
if (type.equals(LaunchServer.ReloadType.FULL)) {
components.forEach((k, component) -> {
server.unregisterObject("component.".concat(k), component);
if (component instanceof AutoCloseable) {
if (component instanceof AutoCloseable autoCloseable) {
try {
((AutoCloseable) component).close();
autoCloseable.close();
} catch (Exception e) {
logger.error(e);
}

View file

@ -133,7 +133,7 @@ public AuthReport auth(AuthResponse.AuthContext context, AuthRequest.AuthPasswor
internalAuth(context.client, context.authType, context.pair, user.getUsername(), user.getUUID(), user.getPermissions(), result.isUsingOAuth());
return result;
} catch (IOException e) {
if (e instanceof AuthException) throw (AuthException) e;
if (e instanceof AuthException authException) throw authException;
logger.error(e);
throw new AuthException("Internal Auth Error");
}
@ -272,19 +272,19 @@ public AuthRequest.AuthPasswordInterface decryptPassword(AuthRequest.AuthPasswor
}
private AuthRequest.AuthPasswordInterface tryDecryptPasswordPlain(AuthRequest.AuthPasswordInterface password) throws AuthException {
if (password instanceof AuthAESPassword) {
if (password instanceof AuthAESPassword authAESPassword) {
try {
return new AuthPlainPassword(IOHelper.decode(SecurityHelper.decrypt(server.runtime.passwordEncryptKey
, ((AuthAESPassword) password).password)));
, authAESPassword.password)));
} catch (Exception ignored) {
throw new AuthException("Password decryption error");
}
}
if (password instanceof AuthRSAPassword) {
if (password instanceof AuthRSAPassword authRSAPassword) {
try {
Cipher cipher = SecurityHelper.newRSADecryptCipher(server.keyAgreementManager.rsaPrivateKey);
return new AuthPlainPassword(
IOHelper.decode(cipher.doFinal(((AuthRSAPassword) password).password))
IOHelper.decode(cipher.doFinal(authRSAPassword.password))
);
} catch (Exception ignored) {
throw new AuthException("Password decryption error");

View file

@ -145,9 +145,7 @@ void process(WebSocketRequestContext context, WebSocketServerResponse response,
logger.error("WebSocket request processing failed", e);
RequestEvent event;
event = new ErrorRequestEvent("Fatal server error. Contact administrator");
if (response instanceof SimpleResponse) {
event.requestUUID = ((SimpleResponse) response).requestUUID;
}
if (response instanceof SimpleResponse simpleResponse) event.requestUUID = simpleResponse.requestUUID;
sendObject(ctx.channel(), event);
}
hookComplete.hook(context);

View file

@ -19,8 +19,8 @@ public NettyIpForwardHandler(NettyConnectContext context) {
@Override
protected void decode(ChannelHandlerContext ctx, HttpRequest msg, List<Object> out) {
if (msg instanceof ReferenceCounted) {
((ReferenceCounted) msg).retain();
if (msg instanceof ReferenceCounted referenceCounted) {
referenceCounted.retain();
}
if (context.ip != null) {
out.add(msg);

View file

@ -63,16 +63,16 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) {
} catch (Throwable ex) {
logger.error("WebSocket frame handler hook error", ex);
}
if (frame instanceof TextWebSocketFrame) {
if (frame instanceof TextWebSocketFrame textWebSocketFrame) {
if (logger.isTraceEnabled()) {
logger.trace("Message from {}: {}", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip, ((TextWebSocketFrame) frame).text());
logger.trace("Message from {}: {}", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip, textWebSocketFrame.text());
}
try {
service.process(ctx, (TextWebSocketFrame) frame, client, context.ip);
service.process(ctx, textWebSocketFrame, client, context.ip);
} catch (Throwable ex) {
logger.warn("Client {} send invalid request. Connection force closed.", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip);
if (logger.isTraceEnabled()) {
logger.trace("Client message: {}", ((TextWebSocketFrame) frame).text());
logger.trace("Client message: {}", textWebSocketFrame.text());
logger.error("Process websockets request failed", ex);
}
ctx.channel().close();
@ -81,10 +81,10 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) {
frame.content().retain();
ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content()));
//return;
} else if ((frame instanceof PongWebSocketFrame)) {
} else if (frame instanceof PongWebSocketFrame) {
logger.trace("WebSocket Client received pong");
} else if ((frame instanceof CloseWebSocketFrame)) {
int statusCode = ((CloseWebSocketFrame) frame).statusCode();
} else if (frame instanceof CloseWebSocketFrame closeWebSocketFrame) {
int statusCode = closeWebSocketFrame.statusCode();
ctx.channel().close();
} else {
String message = "unsupported frame type: " + frame.getClass().getName();
@ -93,11 +93,11 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) {
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception {
if (future != null) future.cancel(true);
if (logger.isTraceEnabled()) {
logger.trace("Client {} disconnected", IOHelper.getIP(ctx.channel().remoteAddress()));
logger.trace("Client {} disconnected", IOHelper.getIP(channelHandlerContext.channel().remoteAddress()));
}
super.channelInactive(ctx);
super.channelInactive(channelHandlerContext);
}
}

View file

@ -34,8 +34,8 @@ public void execute(ChannelHandlerContext ctx, Client client) {
boolean success;
try {
server.authHookManager.joinServerHook.hook(this, client);
if (server.config.protectHandler instanceof JoinServerProtectHandler) {
success = ((JoinServerProtectHandler) server.config.protectHandler).onJoinServer(serverID, username, client);
if (server.config.protectHandler instanceof JoinServerProtectHandler joinServerProtectHandler) {
success = joinServerProtectHandler.onJoinServer(serverID, username, client);
if (!success) {
sendResult(new JoinServerRequestEvent(false));
return;

View file

@ -36,7 +36,7 @@ public String getType() {
@Override
public void execute(ChannelHandlerContext ctx, Client client) {
if (server.config.protectHandler instanceof ProfilesProtectHandler && !((ProfilesProtectHandler) server.config.protectHandler).canGetProfiles(client)) {
if (server.config.protectHandler instanceof ProfilesProtectHandler profilesProtectHandler && !profilesProtectHandler.canGetProfiles(client)) {
sendError("Access denied");
return;
}

View file

@ -28,8 +28,8 @@ public void execute(ChannelHandlerContext ctx, Client client) {
Collection<ClientProfile> profiles = server.getProfiles();
for (ClientProfile p : profiles) {
if (p.getTitle().equals(this.client)) {
if (server.config.protectHandler instanceof ProfilesProtectHandler &&
!((ProfilesProtectHandler) server.config.protectHandler).canChangeProfile(p, client)) {
if (server.config.protectHandler instanceof ProfilesProtectHandler profilesProtectHandler &&
!profilesProtectHandler.canChangeProfile(p, client)) {
sendError("Access denied");
return;
}

View file

@ -21,9 +21,9 @@ public void execute(ChannelHandlerContext ctx, Client client) {
sendError("Invalid request");
return;
}
if (server.config.protectHandler instanceof HardwareProtectHandler) {
if (server.config.protectHandler instanceof HardwareProtectHandler hardwareProtectHandler) {
try {
((HardwareProtectHandler) server.config.protectHandler).onHardwareReport(this, client);
hardwareProtectHandler.onHardwareReport(this, client);
} catch (SecurityException e) {
sendError(e.getMessage());
}

View file

@ -21,12 +21,12 @@ public String getType() {
@Override
public void execute(ChannelHandlerContext ctx, Client client) {
if (!(server.config.protectHandler instanceof SecureProtectHandler)) {
if (!(server.config.protectHandler instanceof SecureProtectHandler secureProtectHandler)) {
sendError("Method not allowed");
} else {
SecurityReportRequestEvent event = secureProtectHandler.onSecurityReport(this, client);
server.modulesManager.invokeEvent(new SecurityReportModuleEvent(event, this, client));
sendResult(event);
}
SecureProtectHandler secureProtectHandler = (SecureProtectHandler) server.config.protectHandler;
SecurityReportRequestEvent event = secureProtectHandler.onSecurityReport(this, client);
server.modulesManager.invokeEvent(new SecurityReportModuleEvent(event, this, client));
sendResult(event);
}
}

View file

@ -19,7 +19,7 @@ public String getType() {
@Override
public void execute(ChannelHandlerContext ctx, Client client) {
if (server.config.protectHandler instanceof ProfilesProtectHandler && !((ProfilesProtectHandler) server.config.protectHandler).canGetUpdates(dirName, client)) {
if (server.config.protectHandler instanceof ProfilesProtectHandler profilesProtectHandler && !profilesProtectHandler.canGetUpdates(dirName, client)) {
sendError("Access denied");
return;
}