apache/pulsar · info · AssertionError

Class with static utility methods only cannot be instantiate

Error message

Class with static utility methods only cannot be instantiated

What it means

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.

Source

Thrown at pulsar-common/src/main/java/org/apache/pulsar/common/util/netty/ChannelFutures.java:33

 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.pulsar.common.util.netty;

import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import java.util.concurrent.CompletableFuture;
import org.apache.pulsar.common.util.FutureUtil;

/**
 * Static utility methods for operating on {@link ChannelFuture}s.
 *
 */
public class ChannelFutures {

    private ChannelFutures() {
        throw new AssertionError("Class with static utility methods only cannot be instantiated");
    }

    /**
     * Convert a {@link ChannelFuture} into a {@link CompletableFuture}.
     *
     * @param channelFuture the {@link ChannelFuture}
     * @return a {@link CompletableFuture} that completes successfully when the channelFuture completes successfully,
     *         and completes exceptionally if the channelFuture completes with a {@link Throwable}
     */
    public static CompletableFuture<Channel> toCompletableFuture(ChannelFuture channelFuture) {
        if (channelFuture == null) {
            return FutureUtil.failedFuture(new NullPointerException("channelFuture cannot be null"));
        }

        CompletableFuture<Channel> adapter = new CompletableFuture<>();
        if (channelFuture.isDone()) {
            if (channelFuture.isSuccess()) {
                adapter.complete(channelFuture.channel());

View on GitHub (pinned to 820761864e)

Solutions

  1. Do not instantiate the class; call its static methods directly, e.g. ChannelFutures.toCompletableFuture(future)
  2. Exclude the class from constructor-coverage requirements in test tooling
  3. Remove any reflective instantiation code

Example fix

// before
ChannelFutures cf = new ChannelFutures();
CompletableFuture<Channel> f = cf.toCompletableFuture(channelFuture);
// after
CompletableFuture<Channel> f = ChannelFutures.toCompletableFuture(channelFuture);
Defensive patterns

Strategy: validation

Validate before calling

// nothing to validate: simply never instantiate; use static methods
boolean isUtilityClassInstance(Object o) { return o instanceof ChannelFutures; } // should never be true

Try / catch

try { Constructor<?> c = ChannelFutures.class.getDeclaredConstructor(); c.setAccessible(true); c.newInstance(); } catch (InvocationTargetException e) { /* expected: AssertionError from the guard */ }

Prevention

When it happens

Trigger: 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.

Common situations: Unit tests attempting to boost constructor coverage; code generators or frameworks that instantiate all classes in a package reflectively.

Related errors


AI-assisted analysis of apache/pulsar@820761864e (2026-09-06). Data as JSON: /api/errors/801c64b790f61413. Report an issue: GitHub.