-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #268: More improvements in constructor selection for signals
- Improved detection of constructors when compatible types are used instead of equal types (e.g. CharSequence and String) - Improved handling of primitive and wrapper array type (int[] / Integer[])
- Loading branch information
Showing
7 changed files
with
174 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
dbus-java-core/src/main/java/org/freedesktop/dbus/utils/PrimitiveUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package org.freedesktop.dbus.utils; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* Utility class containing methods dealing with object and primitive class types. | ||
* @since 5.1.1 - 2024-09-15 | ||
* @author hypfvieh | ||
*/ | ||
public final class PrimitiveUtils { | ||
private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER = Collections.unmodifiableMap( | ||
new ConcurrentHashMap<>( | ||
Map.of( | ||
Boolean.TYPE, Boolean.class, | ||
Byte.TYPE, Byte.class, | ||
Short.TYPE, Short.class, | ||
Character.TYPE, Character.class, | ||
Integer.TYPE, Integer.class, | ||
Long.TYPE, Long.class, | ||
Float.TYPE, Float.class, | ||
Double.TYPE, Double.class) | ||
) | ||
); | ||
|
||
private static final Map<Class<?>, Class<?>> WRAPPER_TO_PRIMITIVE = Collections.unmodifiableMap( | ||
new ConcurrentHashMap<>( | ||
Map.of( | ||
Boolean.class, Boolean.TYPE, | ||
Byte.class, Byte.TYPE, | ||
Short.class, Short.TYPE, | ||
Character.class, Character.TYPE, | ||
Integer.class, Integer.TYPE, | ||
Long.class, Long.TYPE, | ||
Float.class, Float.TYPE, | ||
Double.class, Double.TYPE) | ||
) | ||
); | ||
|
||
private PrimitiveUtils() { | ||
|
||
} | ||
|
||
/** | ||
* Map with all primitives and the corresponding wrapper type. | ||
* @return unmodifiable map | ||
*/ | ||
public static Map<Class<?>, Class<?>> getPrimitiveToWrapperTypes() { | ||
return Collections.unmodifiableMap(PRIMITIVE_TO_WRAPPER); | ||
} | ||
|
||
/** | ||
* Map with all wrapper types and the corresponding primitive. | ||
* @return unmodifiable map | ||
*/ | ||
public static Map<Class<?>, Class<?>> getWrapperToPrimitiveTypes() { | ||
return Collections.unmodifiableMap(WRAPPER_TO_PRIMITIVE); | ||
} | ||
|
||
/** | ||
* Check if the given classes are equal or are equal wrapper types (e.g. byte == Byte). | ||
* | ||
* @param _clz1 | ||
* @param _clz2 | ||
* @return true if classes equal or both of compatible (wrapper) types, | ||
* false otherwise or if any parameter is null | ||
*/ | ||
public static boolean isCompatiblePrimitiveOrWrapper(Class<?> _clz1, Class<?> _clz2) { | ||
if (_clz1 == null || _clz2 == null) { | ||
return false; | ||
} | ||
|
||
if (_clz1 == _clz2) { | ||
return true; | ||
} else if (_clz1.isPrimitive() && _clz2.isPrimitive() && _clz1 == _clz2) { | ||
return true; | ||
} else if (_clz1.isPrimitive()) { | ||
Class<?> wrappedType = PRIMITIVE_TO_WRAPPER.get(_clz1); | ||
return wrappedType == _clz2; | ||
} else if (_clz2.isPrimitive()) { | ||
Class<?> wrappedType = PRIMITIVE_TO_WRAPPER.get(_clz2); | ||
return wrappedType == _clz1; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
dbus-java-tests/src/test/java/org/freedesktop/dbus/utils/PrimitiveUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.freedesktop.dbus.utils; | ||
|
||
import org.freedesktop.dbus.test.AbstractBaseTest; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.stream.Stream; | ||
|
||
public class PrimitiveUtilsTest extends AbstractBaseTest { | ||
|
||
static Stream<PrimitiveWrapperTestData> createComparePrimitiveWrapperData() { | ||
return Stream.of( | ||
new PrimitiveWrapperTestData("Equal types", Boolean.class, Boolean.class, true), | ||
new PrimitiveWrapperTestData("Equal primitve types", boolean.class, boolean.class, true), | ||
new PrimitiveWrapperTestData("Equal object and primitive", Boolean.class, boolean.class, true), | ||
new PrimitiveWrapperTestData("Equal primitive and object", Boolean.class, boolean.class, true), | ||
new PrimitiveWrapperTestData("Unequal types", Boolean.class, String.class, false), | ||
new PrimitiveWrapperTestData("Unequal primitive types", int.class, long.class, false), | ||
new PrimitiveWrapperTestData("Unequal primitive and object", boolean.class, String.class, false), | ||
new PrimitiveWrapperTestData("Unequal object and primitive", Byte.class, int.class, false) | ||
); | ||
} | ||
|
||
@ParameterizedTest(name = "{0}") | ||
@MethodSource("createComparePrimitiveWrapperData") | ||
void testComparePrimitiveWrapper(PrimitiveWrapperTestData _testData) { | ||
assertEquals(_testData.expected(), PrimitiveUtils.isCompatiblePrimitiveOrWrapper(_testData.clz1(), _testData.clz2())); | ||
} | ||
|
||
record PrimitiveWrapperTestData(String name, Class<?> clz1, Class<?> clz2, boolean expected) { | ||
@Override | ||
public String toString() { | ||
return name; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,4 @@ | |
<appender-ref ref="STDOUT" /> | ||
</root> | ||
|
||
</configuration> | ||
</configuration> |