Skip to content

Commit a4840da

Browse files
sratzptziegler
authored andcommitted
Consider primitive types in explicit type check in DefaultConverter
Follow-up on #3009.
1 parent 69d52f3 commit a4840da

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

bundles/org.eclipse.core.databinding/src/org/eclipse/core/databinding/UpdateStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ static final class DefaultConverter implements IConverter<Object, Object> {
679679
public Object convert(Object fromObject) {
680680
// Explicit cast necessary due to potential type erasure
681681
if (toType instanceof Class<?> clazz) {
682-
return clazz.cast(fromObject);
682+
return (clazz.isPrimitive() ? autoboxed(clazz) : clazz).cast(fromObject);
683683
}
684684
return fromObject;
685685
}

tests/org.eclipse.jface.tests.databinding/src/org/eclipse/core/tests/databinding/UpdateStrategyTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.eclipse.core.databinding.conversion.IConverter;
3131
import org.eclipse.core.databinding.conversion.text.NumberToStringConverter;
3232
import org.eclipse.core.databinding.conversion.text.StringToNumberConverter;
33+
import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
3334
import org.eclipse.core.databinding.observable.value.IObservableValue;
3435
import org.eclipse.core.databinding.observable.value.WritableValue;
3536
import org.eclipse.core.internal.databinding.conversion.DateToStringConverter;
@@ -370,4 +371,27 @@ public void testDefaultConverterWithTypeErasure() {
370371
RuntimeException ex = assertThrows(RuntimeException.class, () -> strategy.convert(new HashSet<>()));
371372
assertTrue("Type erasure was missed", ex.getCause() instanceof ClassCastException);
372373
}
374+
375+
// https://github.com/eclipse-platform/eclipse.platform.ui/pull/3009#issuecomment-3012956414
376+
@Test
377+
public void testDefaultConverterWithPrimitiveTypeAsDestinationAndNonClassTypeAsSourceType() {
378+
IObservableValue<Boolean> source = new AbstractObservableValue() {
379+
@Override
380+
public Object getValueType() {
381+
return new Object(); // non-class type, happens e.g. in EMF databinding
382+
}
383+
384+
@Override
385+
protected Object doGetValue() {
386+
return Boolean.TRUE;
387+
}
388+
};
389+
WritableValue<Boolean> destination = WritableValue.withValueType(boolean.class);
390+
391+
UpdateStrategyStub<Object, Boolean> strategy = new UpdateStrategyStub<>();
392+
strategy.fillDefaults(source, destination);
393+
394+
Boolean result = strategy.convert(source.getValue());
395+
assertTrue(result);
396+
}
373397
}

0 commit comments

Comments
 (0)