Skip to content

Commit 4456d55

Browse files
dk2kdbwiddis
andauthored
Removed suspicious call of getClass() on instance of Class, which erased type info (#16002)
* Removed suspicious getClass() call on Class Signed-off-by: Dmitry Kryukov <dk2k@ya.ru> * Changed the exception's message. Added unit test. Signed-off-by: Dmitry Kryukov <dk2k@ya.ru> * Run spotless, add license header Signed-off-by: Daniel Widdis <widdis@gmail.com> --------- Signed-off-by: Dmitry Kryukov <dk2k@ya.ru> Signed-off-by: Daniel Widdis <widdis@gmail.com> Co-authored-by: Daniel Widdis <widdis@gmail.com>
1 parent 3b004bf commit 4456d55

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

libs/core/src/main/java/org/opensearch/core/common/io/stream/Writeable.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static <R extends Reader<?>> void registerReader(final byte ordinal, fina
8383

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

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

9898
/**
99-
* Returns the ristered reader keyed by the unique ordinal
99+
* Returns the registered reader keyed by the unique ordinal
100100
*/
101101
@SuppressWarnings("unchecked")
102102
public static <R extends Reader<?>> R getReader(final byte b) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.core.common.io.stream;
10+
11+
import org.opensearch.test.OpenSearchTestCase;
12+
import org.junit.Assert;
13+
14+
import java.util.concurrent.atomic.AtomicInteger;
15+
16+
public class WriteableTests extends OpenSearchTestCase {
17+
18+
public void testRegisterClassAlias() {
19+
Writeable.WriteableRegistry.registerClassAlias(StringBuilder.class, AtomicInteger.class);
20+
try {
21+
Writeable.WriteableRegistry.registerClassAlias(StringBuilder.class, AtomicInteger.class);
22+
Assert.fail("expected exception not thrown");
23+
} catch (IllegalArgumentException illegalArgumentException) {
24+
Assert.assertEquals(
25+
"Streamable custom class already registered [java.lang.StringBuilder]",
26+
illegalArgumentException.getMessage()
27+
);
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)