Skip to content

Commit

Permalink
convert Long,long,Integer,int values to Long/Integer types (issue4930)
Browse files Browse the repository at this point in the history
  • Loading branch information
alwin-joseph authored and jansupol committed Jan 21, 2022
1 parent 2703ee5 commit 3c6bc4b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -285,6 +285,15 @@ private static Object getLegacyFallbackValue(Map<String, ?> properties, Map<Stri
* @return value converted to the specified class type.
*/
public static <T> T convertValue(Object value, Class<T> type) {

if (((type.equals(Integer.class)) || (type.equals(int.class))) && Number.class.isInstance(value)) {
final Integer number2Int = ((Number) value).intValue();
return (T) number2Int;
} else if (((type.equals(Long.class)) || (type.equals(long.class))) && Number.class.isInstance(value)) {
final Long number2Long = ((Number) value).longValue();
return (T) number2Long;
}

if (!type.isInstance(value)) {
// TODO: Move string value readers from server to common and utilize them here
final Constructor constructor = AccessController.doPrivileged(ReflectionHelper.getStringConstructorPA(type));
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -173,4 +173,30 @@ public void testPropertyNameDeclination() {
assertEquals(myNonJerseyProperty, PropertiesHelper.getPropertyNameForRuntime(myNonJerseyProperty, null));
}

@Test
public void testconvertValue() {
Long lg = 10L;
long l = 10L;
Integer ig = 10;
int i = 10;

assertEquals(ig, PropertiesHelper.convertValue(i, Integer.class));
assertEquals(ig, PropertiesHelper.convertValue(ig, Integer.class));
assertEquals(ig, PropertiesHelper.convertValue(l, Integer.class));
assertEquals(ig, PropertiesHelper.convertValue(lg, Integer.class));
assertEquals(lg, PropertiesHelper.convertValue(i, Long.class));
assertEquals(lg, PropertiesHelper.convertValue(ig, Long.class));
assertEquals(lg, PropertiesHelper.convertValue(l, Long.class));
assertEquals(lg, PropertiesHelper.convertValue(lg, Long.class));
assertEquals(ig, PropertiesHelper.convertValue(i, int.class));
assertEquals(ig, PropertiesHelper.convertValue(ig, int.class));
assertEquals(ig, PropertiesHelper.convertValue(l, int.class));
assertEquals(ig, PropertiesHelper.convertValue(lg, int.class));
assertEquals(lg, PropertiesHelper.convertValue(i, long.class));
assertEquals(lg, PropertiesHelper.convertValue(ig, long.class));
assertEquals(lg, PropertiesHelper.convertValue(l, long.class));
assertEquals(lg, PropertiesHelper.convertValue(lg, long.class));

}

}

0 comments on commit 3c6bc4b

Please sign in to comment.