Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected void write(ClassWriter classWriter, MethodWriter methodWriter, WriteSc
case Short shortValue -> methodWriter.push(shortValue);
case Byte byteValue -> methodWriter.push(byteValue);
case Boolean boolValue -> methodWriter.push(boolValue);
default -> throw new IllegalStateException("unexpected constant [" + constant + "]");
case null, default -> throw new IllegalStateException("unexpected constant [" + constant + "]");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public void visitLeaf(Query query) {
case MatchAllDocsQuery ignored -> terms.add(new Result(true, true));
case MatchNoDocsQuery ignored -> terms.add(Result.MATCH_NONE);
case PointRangeQuery pointRangeQuery -> terms.add(pointRangeQuery(pointRangeQuery));
default -> terms.add(Result.UNKNOWN);
case null, default -> terms.add(Result.UNKNOWN);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public static Map<String, ObjectMap.Value> metadataToProto(OpenSearchException e
case SearchParseException spe -> SearchParseExceptionProtoUtils.metadataToProto(spe);
case SearchPhaseExecutionException spee -> SearchPhaseExecutionExceptionProtoUtils.metadataToProto(spee);
case MultiBucketConsumerService.TooManyBucketsException tmbe -> TooManyBucketsExceptionProtoUtils.metadataToProto(tmbe);
default -> new HashMap<>();
case null, default -> new HashMap<>();
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public static ShardFailure toProto(DefaultShardOperationFailedException exceptio
indicesShardStoresFailure
);
case CloseIndexResponse.ShardResult.Failure closeIndexFailure -> innerToProto(shardFailureBuilder, closeIndexFailure);
case null -> {
}
default -> parentInnerToProto(shardFailureBuilder, exception);
}
return shardFailureBuilder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public static ShardFailure toProto(ShardOperationFailedException exception) thro
case SnapshotShardFailure ssf -> SnapshotShardFailureProtoUtils.toProto(ssf);
case DefaultShardOperationFailedException dsofe -> DefaultShardOperationFailedExceptionProtoUtils.toProto(dsofe);
case ReplicationResponse.ShardInfo.Failure sf -> ReplicationResponseShardInfoFailureProtoUtils.toProto(sf);
case null -> throw new UnsupportedOperationException(
"Unsupported ShardOperationFailedException [null] cannot be converted to proto."
);
default -> throw new UnsupportedOperationException(
"Unsupported ShardOperationFailedException " + exception.getClass().getName() + "cannot be converted to proto."
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ public static StatusRuntimeException convertToGrpcError(Exception e) {
case IOException ioException -> {
return Status.INTERNAL.withDescription(ExceptionsHelper.stackTrace(ioException)).asRuntimeException();
}

case null -> {
logger.warn("Unexpected null exception type, treating as INTERNAL error");
return Status.INTERNAL.withDescription("Unexpected null exception").asRuntimeException();
}
// ========== 5. Unknown/Unmapped Exceptions ==========
// Safety fallback for any unexpected exception to {@code Status.INTERNAL} with full debugging info
default -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,13 @@ public void testToProtoWithNullNodeId() throws IOException {
assertFalse("Node should not be set", shardFailure.hasNode());
assertNotNull("Reason should not be null", shardFailure.getReason());
}

public void testToProtoWithNull() throws IOException {
ShardFailure shardFailure = DefaultShardOperationFailedExceptionProtoUtils.toProto(null);
assertNotNull("ShardFailure should not be null", shardFailure);
assertFalse("Index should not be set", shardFailure.hasIndex());
assertFalse("Status should not be set", shardFailure.hasStatus());
assertFalse("Node should not be set", shardFailure.hasNode());
assertFalse("Reason should not be set", shardFailure.hasReason());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,14 @@ public void testToProtoWithUnsupportedShardOperationFailedException() {
exception.getMessage().contains("Unsupported ShardOperationFailedException")
);
}

public void testToProtoWithNullShardOperationFailedException() {
// Call the method under test with null, should throw UnsupportedOperationException
UnsupportedOperationException exception = expectThrows(
UnsupportedOperationException.class,
() -> ShardOperationFailedExceptionProtoUtils.toProto(null)
);

assertEquals("Unsupported ShardOperationFailedException [null] cannot be converted to proto.", exception.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,12 @@ public RestStatus status() {
assertTrue(result.getMessage().contains("Root cause"));
assertTrue(result.getMessage().contains("Wrapper exception"));
}

public void testNullExceptionConversion() {
StatusRuntimeException result = GrpcErrorHandler.convertToGrpcError(null);

// null -> INTERNAL via case null handling
assertEquals(Status.INTERNAL.getCode(), result.getStatus().getCode());
assertEquals("INTERNAL: Unexpected null exception", result.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public static Collection<Message> getMessagesFromThrowable(Throwable throwable)
case ProvisionException provisionException -> provisionException.getErrorMessages();
case ConfigurationException configurationException -> configurationException.getErrorMessages();
case CreationException creationException -> creationException.getErrorMessages();
default -> emptySet();
case null, default -> emptySet();
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,32 +143,37 @@ public static Type canonicalize(Type type) {
}

public static Class<?> getRawType(Type type) {
if (type instanceof Class<?>) {
// type is a normal class.
return (Class<?>) type;

} else if (type instanceof ParameterizedType parameterizedType) {

// I'm not exactly sure why getRawType() returns Type instead of Class.
// Neal isn't either but suspects some pathological case related
// to nested classes exists.
Type rawType = parameterizedType.getRawType();
if (!(rawType instanceof Class)) {
throw new IllegalArgumentException("Expected a Class, but <" + type + "> is of type " + type.getClass().getName());
switch (type) {
case Class<?> aClass -> {
// type is a normal class.
return aClass;
// type is a normal class.
}
return (Class<?>) rawType;

} else if (type instanceof GenericArrayType) {
// TODO: Is this sufficient?
return Object[].class;

} else if (type instanceof TypeVariable) {
// we could use the variable's bounds, but that'll won't work if there are multiple.
// having a raw type that's more general than necessary is okay
return Object.class;
case ParameterizedType parameterizedType -> {

} else {
throw new IllegalArgumentException(
// I'm not exactly sure why getRawType() returns Type instead of Class.
// Neal isn't either but suspects some pathological case related
// to nested classes exists.
Type rawType = parameterizedType.getRawType();
if (!(rawType instanceof Class)) {
throw new IllegalArgumentException("Expected a Class, but <" + type + "> is of type " + type.getClass().getName());
}
return (Class<?>) rawType;
}
case GenericArrayType ignored -> {
// TODO: Is this sufficient?
return Object[].class;
// TODO: Is this sufficient?
}
case TypeVariable<?> ignored -> {
// we could use the variable's bounds, but that'll won't work if there are multiple.
// having a raw type that's more general than necessary is okay
return Object.class;
// we could use the variable's bounds, but that'll won't work if there are multiple.
// having a raw type that's more general than necessary is okay
}
case null -> throw new IllegalArgumentException("Unsupported type [null]");
default -> throw new IllegalArgumentException(
"Expected a Class, ParameterizedType, or " + "GenericArrayType, but <" + type + "> is of type " + type.getClass().getName()
);
}
Expand Down Expand Up @@ -227,15 +232,15 @@ public static boolean equals(Type a, Type b) {
*/
public static int hashCode(Type type) {
return switch (type) {
case Class ignored ->
case Class<?> ignored ->
// Class specifies hashCode().
type.hashCode();
case ParameterizedType p -> Arrays.hashCode(p.getActualTypeArguments()) ^ p.getRawType().hashCode() ^ hashCodeOrZero(
p.getOwnerType()
);
case GenericArrayType genericArrayType -> hashCode(genericArrayType.getGenericComponentType());
case WildcardType w -> Arrays.hashCode(w.getLowerBounds()) ^ Arrays.hashCode(w.getUpperBounds());
default ->
case null, default ->
// This isn't a type we support. Probably a type variable
hashCodeOrZero(type);
};
Expand Down Expand Up @@ -288,6 +293,7 @@ public static String toString(Type type) {
return "? extends " + toString(upperBounds[0]);
}
}
case null -> throw new UnsupportedOperationException("Unsupported wildcard type [null]");
default -> {
return type.toString();
}
Expand All @@ -304,7 +310,7 @@ public static Class<? extends Member> memberType(Member member) {
case MemberImpl memberImpl -> memberImpl.memberType;
case Field ignored -> Field.class;
case Method ignored -> Method.class;
case Constructor ignored -> Constructor.class;
case Constructor<?> ignored -> Constructor.class;
default -> throw new IllegalArgumentException("Unsupported implementation class for Member, " + member.getClass());
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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.common.inject.internal;

import org.opensearch.test.OpenSearchTestCase;

import java.lang.reflect.Type;

public class MoreTypesTests extends OpenSearchTestCase {

public void testHashCode() {
assertEquals(0, MoreTypes.hashCode(null));
assertEquals(String.class.hashCode(), MoreTypes.hashCode(String.class));

MoreTypes.ParameterizedTypeImpl paramType = new MoreTypes.ParameterizedTypeImpl(null, java.util.List.class, String.class);
int expectedParamHash = java.util.Arrays.hashCode(new Type[] { String.class }) ^ java.util.List.class.hashCode();
assertEquals(expectedParamHash, MoreTypes.hashCode(paramType));

MoreTypes.GenericArrayTypeImpl arrayType = new MoreTypes.GenericArrayTypeImpl(String.class);
assertEquals(String.class.hashCode(), MoreTypes.hashCode(arrayType));

MoreTypes.WildcardTypeImpl wildcardType = new MoreTypes.WildcardTypeImpl(new Type[] { String.class }, new Type[] {});
int expectedWildcardHash = java.util.Arrays.hashCode(new Type[] {}) ^ java.util.Arrays.hashCode(new Type[] { String.class });
assertEquals(expectedWildcardHash, MoreTypes.hashCode(wildcardType));
}

public void testToString() {
assertEquals("java.lang.String", MoreTypes.toString(String.class));

MoreTypes.ParameterizedTypeImpl paramType = new MoreTypes.ParameterizedTypeImpl(null, java.util.List.class, String.class);
assertEquals("java.util.List<java.lang.String>", MoreTypes.toString(paramType));

MoreTypes.GenericArrayTypeImpl arrayType = new MoreTypes.GenericArrayTypeImpl(String.class);
assertEquals("java.lang.String[]", MoreTypes.toString(arrayType));

MoreTypes.WildcardTypeImpl unboundedWildcard = new MoreTypes.WildcardTypeImpl(
new java.lang.reflect.Type[] { Object.class },
new java.lang.reflect.Type[] {}
);
assertEquals("?", MoreTypes.toString(unboundedWildcard));

MoreTypes.WildcardTypeImpl upperBoundedWildcard = new MoreTypes.WildcardTypeImpl(
new java.lang.reflect.Type[] { String.class },
new java.lang.reflect.Type[] {}
);
assertEquals("? extends java.lang.String", MoreTypes.toString(upperBoundedWildcard));

MoreTypes.WildcardTypeImpl lowerBoundedWildcard = new MoreTypes.WildcardTypeImpl(
new java.lang.reflect.Type[] { Object.class },
new java.lang.reflect.Type[] { String.class }
);
assertEquals("? super java.lang.String", MoreTypes.toString(lowerBoundedWildcard));

UnsupportedOperationException exception = expectThrows(
UnsupportedOperationException.class,
() -> { MoreTypes.toString((Type) null); }
);
assertEquals("Unsupported wildcard type [null]", exception.getMessage());
}

public void testGetRawType() {
assertEquals(String.class, MoreTypes.getRawType(String.class));

MoreTypes.ParameterizedTypeImpl paramType = new MoreTypes.ParameterizedTypeImpl(null, java.util.List.class, String.class);
assertEquals(java.util.List.class, MoreTypes.getRawType(paramType));

MoreTypes.GenericArrayTypeImpl arrayType = new MoreTypes.GenericArrayTypeImpl(String.class);
assertEquals(Object[].class, MoreTypes.getRawType(arrayType));

java.lang.reflect.TypeVariable<?> typeVar = java.util.List.class.getTypeParameters()[0];
assertEquals(Object.class, MoreTypes.getRawType(typeVar));

MoreTypes.WildcardTypeImpl wildcardType = new MoreTypes.WildcardTypeImpl(new Type[] { String.class }, new Type[] {});
IllegalArgumentException wildcardException = expectThrows(IllegalArgumentException.class, () -> MoreTypes.getRawType(wildcardType));
assertTrue(wildcardException.getMessage().contains("Expected a Class, ParameterizedType, or GenericArrayType"));

IllegalArgumentException nullException = expectThrows(IllegalArgumentException.class, () -> MoreTypes.getRawType(null));
assertTrue(nullException.getMessage().contains("Unsupported type [null]"));
}
}
Loading