TooTallNate / Java-WebSocket

A barebones WebSocket client and server implementation written in 100% Java.
http://tootallnate.github.io/Java-WebSocket
MIT License
10.47k stars 2.57k forks source link

Bug Report: Violation of Liskov Substitution Principle #1427

Closed 2Dsa3 closed 3 weeks ago

2Dsa3 commented 3 months ago

Description of the Problem In the Java-WebSocket repository, the class WebSocketServer violates the Liskov Substitution Principle (LSP) by having an implementation of a method that does nothing, while it can be expected that this method performs a specific action.

Steps to Reproduce Open the WebSocketServer.java file located at src/main/java/org/java_websocket/server/. Observe the implementation of methods that do not perform any actions, such as onStart. Expected Behavior Methods in the WebSocketServer class should either provide meaningful implementations or be declared as abstract to ensure that subclasses are aware that they need to provide specific behaviors.

Proposed Solution To resolve this violation, the following steps are recommended:

Make Methods Abstract: Convert methods that are expected to perform actions but currently do nothing into abstract methods. Provide Default Implementation: If certain behavior is common, provide a default implementation that performs a meaningful action. Code Example Current Implementation of WebSocketServer:

java Copiar código public class WebSocketServer { // Other methods and fields

public void onStart() {
    // Method does nothing
}

// Other methods and logic

} Proposed Solution:

Option 1: Make onStart Abstract

java Copiar código public abstract class WebSocketServer { // Other methods and fields

public abstract void onStart();

// Other methods and logic

} Option 2: Provide Default Implementation

java Copiar código public class WebSocketServer { // Other methods and fields

public void onStart() {
    // Provide a meaningful default implementation
    System.out.println("Server started!");
    setConnectionLostTimeout(0);
    setConnectionLostTimeout(100);
}

// Other methods and logic

} Example of Subclass that Implements onStart If opting for the first solution (abstract method), subclasses must implement onStart:

java Copiar código public class CustomWebSocketServer extends WebSocketServer { @Override public void onStart() { // Custom implementation for CustomWebSocketServer System.out.println("Custom WebSocket Server started!"); // Additional initialization logic }

// Other methods and logic

} Benefits of This Refactor Compliance with LSP: Ensures that all subclasses adhere to the contract defined by the superclass, maintaining consistent behavior. Clarity: Makes it clear which methods require specific implementations in subclasses. Robustness: Reduces the risk of subclasses inadvertently inheriting methods that do not perform the necessary actions. Additional Information Providing any other details that can help resolve the issue would be beneficial.

This approach will help address the violation of the Liskov Substitution Principle and ensure more maintainable and reliable code within the Java-WebSocket project.

e3ndr commented 4 weeks ago

1426, #1425 are almost exact word-for-word copies of eachother, but with different classes and names. It also makes zero sense in the context provided. Is this some new class of code-scraping AI bot that files junk issues?

marci4 commented 4 weeks ago

Yeah looks like it