Skip to content

Commit

Permalink
Removed suspicious call of getClass() on instance of Class, which era…
Browse files Browse the repository at this point in the history
…sed type info (#16002)

* Removed suspicious getClass() call on Class

Signed-off-by: Dmitry Kryukov <[email protected]>

* Changed the exception's message. Added unit test.

Signed-off-by: Dmitry Kryukov <[email protected]>

* Run spotless, add license header

Signed-off-by: Daniel Widdis <[email protected]>

---------

Signed-off-by: Dmitry Kryukov <[email protected]>
Signed-off-by: Daniel Widdis <[email protected]>
Co-authored-by: Daniel Widdis <[email protected]>
  • Loading branch information
dk2k and dbwiddis authored Oct 18, 2024
1 parent 3b004bf commit 4456d55
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static <R extends Reader<?>> void registerReader(final byte ordinal, fina

public static void registerClassAlias(final Class<?> classInstance, final Class<?> classGeneric) {
if (WRITER_CUSTOM_CLASS_MAP.putIfAbsent(classInstance, classGeneric) != null) {
throw new IllegalArgumentException("Streamable custom class already registered [" + classInstance.getClass() + "]");
throw new IllegalArgumentException("Streamable custom class already registered [" + classInstance.getName() + "]");
}
}

Expand All @@ -96,7 +96,7 @@ public static <W extends Writer<?>> W getWriter(final Class<?> clazz) {
}

/**
* Returns the ristered reader keyed by the unique ordinal
* Returns the registered reader keyed by the unique ordinal
*/
@SuppressWarnings("unchecked")
public static <R extends Reader<?>> R getReader(final byte b) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.core.common.io.stream;

import org.opensearch.test.OpenSearchTestCase;
import org.junit.Assert;

import java.util.concurrent.atomic.AtomicInteger;

public class WriteableTests extends OpenSearchTestCase {

public void testRegisterClassAlias() {
Writeable.WriteableRegistry.registerClassAlias(StringBuilder.class, AtomicInteger.class);
try {
Writeable.WriteableRegistry.registerClassAlias(StringBuilder.class, AtomicInteger.class);
Assert.fail("expected exception not thrown");
} catch (IllegalArgumentException illegalArgumentException) {
Assert.assertEquals(
"Streamable custom class already registered [java.lang.StringBuilder]",
illegalArgumentException.getMessage()
);
}
}
}

0 comments on commit 4456d55

Please sign in to comment.