Skip to content

Commit

Permalink
[FLINK-35234][minor][cdc-common] Fix potential NPE in ConfigurationUtils
Browse files Browse the repository at this point in the history
This closes apache#3255.
  • Loading branch information
loserwang1024 authored Aug 6, 2024
1 parent 44dafe3 commit d76beec
Showing 1 changed file with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public class ConfigurationUtils {
*/
@SuppressWarnings("unchecked")
public static <T> T convertValue(Object rawValue, Class<?> clazz) {
if (rawValue == null) {
return null;
}
if (Integer.class.equals(clazz)) {
return (T) convertToInt(rawValue);
} else if (Long.class.equals(clazz)) {
Expand All @@ -70,7 +73,9 @@ public static <T> T convertValue(Object rawValue, Class<?> clazz) {

@SuppressWarnings("unchecked")
static <T> T convertToList(Object rawValue, Class<?> atomicClass) {
if (rawValue instanceof List) {
if (rawValue == null) {
return null;
} else if (rawValue instanceof List) {
return (T) rawValue;
} else {
return (T)
Expand All @@ -82,7 +87,9 @@ static <T> T convertToList(Object rawValue, Class<?> atomicClass) {

@SuppressWarnings("unchecked")
static Map<String, String> convertToProperties(Object o) {
if (o instanceof Map) {
if (o == null) {
return null;
} else if (o instanceof Map) {
return (Map<String, String>) o;
} else {
List<String> listOfRawProperties =
Expand All @@ -102,7 +109,9 @@ static Map<String, String> convertToProperties(Object o) {

@SuppressWarnings("unchecked")
public static <E extends Enum<?>> E convertToEnum(Object o, Class<E> clazz) {
if (o.getClass().equals(clazz)) {
if (o == null) {
return null;
} else if (o.getClass().equals(clazz)) {
return (E) o;
}

Expand All @@ -122,15 +131,19 @@ public static <E extends Enum<?>> E convertToEnum(Object o, Class<E> clazz) {
}

static Duration convertToDuration(Object o) {
if (o.getClass() == Duration.class) {
if (o == null) {
return null;
} else if (o.getClass() == Duration.class) {
return (Duration) o;
}

return TimeUtils.parseDuration(o.toString());
}

static String convertToString(Object o) {
if (o.getClass() == String.class) {
if (o == null) {
return null;
} else if (o.getClass() == String.class) {
return (String) o;
} else if (o.getClass() == Duration.class) {
Duration duration = (Duration) o;
Expand Down Expand Up @@ -165,7 +178,9 @@ static String convertToString(Object o) {
}

static Integer convertToInt(Object o) {
if (o.getClass() == Integer.class) {
if (o == null) {
return null;
} else if (o.getClass() == Integer.class) {
return (Integer) o;
} else if (o.getClass() == Long.class) {
long value = (Long) o;
Expand All @@ -183,7 +198,9 @@ static Integer convertToInt(Object o) {
}

static Long convertToLong(Object o) {
if (o.getClass() == Long.class) {
if (o == null) {
return null;
} else if (o.getClass() == Long.class) {
return (Long) o;
} else if (o.getClass() == Integer.class) {
return ((Integer) o).longValue();
Expand All @@ -193,7 +210,9 @@ static Long convertToLong(Object o) {
}

static Boolean convertToBoolean(Object o) {
if (o.getClass() == Boolean.class) {
if (o == null) {
return null;
} else if (o.getClass() == Boolean.class) {
return (Boolean) o;
}

Expand All @@ -211,6 +230,9 @@ static Boolean convertToBoolean(Object o) {
}

static Float convertToFloat(Object o) {
if (o == null) {
return null;
}
if (o.getClass() == Float.class) {
return (Float) o;
} else if (o.getClass() == Double.class) {
Expand All @@ -231,6 +253,9 @@ static Float convertToFloat(Object o) {
}

static Double convertToDouble(Object o) {
if (o == null) {
return null;
}
if (o.getClass() == Double.class) {
return (Double) o;
} else if (o.getClass() == Float.class) {
Expand Down

0 comments on commit d76beec

Please sign in to comment.