{"record":{"id":"801c64b790f61413","repo":"apache/pulsar","slug":"class-with-static-utility-methods-only-cannot-be-i","errorCode":null,"errorMessage":"Class with static utility methods only cannot be instantiated","messagePattern":"Class with static utility methods only cannot be instantiated","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"info","filePath":"pulsar-common/src/main/java/org/apache/pulsar/common/util/netty/ChannelFutures.java","lineNumber":33,"sourceCode":" * KIND, either express or implied.  See the License for the\n * specific language governing permissions and limitations\n * under the License.\n */\npackage org.apache.pulsar.common.util.netty;\n\nimport io.netty.channel.Channel;\nimport io.netty.channel.ChannelFuture;\nimport java.util.concurrent.CompletableFuture;\nimport org.apache.pulsar.common.util.FutureUtil;\n\n/**\n * Static utility methods for operating on {@link ChannelFuture}s.\n *\n */\npublic class ChannelFutures {\n\n    private ChannelFutures() {\n        throw new AssertionError(\"Class with static utility methods only cannot be instantiated\");\n    }\n\n    /**\n     * Convert a {@link ChannelFuture} into a {@link CompletableFuture}.\n     *\n     * @param channelFuture the {@link ChannelFuture}\n     * @return a {@link CompletableFuture} that completes successfully when the channelFuture completes successfully,\n     *         and completes exceptionally if the channelFuture completes with a {@link Throwable}\n     */\n    public static CompletableFuture<Channel> toCompletableFuture(ChannelFuture channelFuture) {\n        if (channelFuture == null) {\n            return FutureUtil.failedFuture(new NullPointerException(\"channelFuture cannot be null\"));\n        }\n\n        CompletableFuture<Channel> adapter = new CompletableFuture<>();\n        if (channelFuture.isDone()) {\n            if (channelFuture.isSuccess()) {\n                adapter.complete(channelFuture.channel());","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-common/src/main/java/org/apache/pulsar/common/util/netty/ChannelFutures.java#L15-L51","documentation":"ChannelFutures is a static-utility class whose sole constructor is private and deliberately throws AssertionError to prevent instantiation via reflection or accidental subclassing. This is an intentional guard, not a runtime failure of normal library use.","triggerScenarios":"Calling new ChannelFutures() (only possible via reflection, since the constructor is private) — e.g. reflection-based test coverage tools or misuse of the class as an instance object.","commonSituations":"Unit tests attempting to boost constructor coverage; code generators or frameworks that instantiate all classes in a package reflectively.","solutions":["Do not instantiate the class; call its static methods directly, e.g. ChannelFutures.toCompletableFuture(future)","Exclude the class from constructor-coverage requirements in test tooling","Remove any reflective instantiation code"],"exampleFix":"// before\nChannelFutures cf = new ChannelFutures();\nCompletableFuture<Channel> f = cf.toCompletableFuture(channelFuture);\n// after\nCompletableFuture<Channel> f = ChannelFutures.toCompletableFuture(channelFuture);","handlingStrategy":"validation","validationCode":"// nothing to validate: simply never instantiate; use static methods\nboolean isUtilityClassInstance(Object o) { return o instanceof ChannelFutures; } // should never be true","typeGuard":null,"tryCatchPattern":"try { Constructor<?> c = ChannelFutures.class.getDeclaredConstructor(); c.setAccessible(true); c.newInstance(); } catch (InvocationTargetException e) { /* expected: AssertionError from the guard */ }","preventionTips":["Never instantiate utility classes; call ChannelFutures.toCompletableFuture directly","Configure JaCoCo/coverage tools to skip private constructors of utility classes","Do not write reflective instantiation of library classes"],"tags":["java","utility-class","reflection"],"backgroundTag":"utility-class-instantiation","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}