bcdev / jpy

A bi-directional Python-Java bridge used to embed Java in CPython or the other way round.
Apache License 2.0
187 stars 37 forks source link

Unsafe cast in PyObject.getAttribute(…, Class) #132

Open desruisseaux opened 6 years ago

desruisseaux commented 6 years ago

Javadoc of the following method said that it accepts null value for the valueType argument:

public <T> T getAttribute(String name, Class<? extends T> valueType);

However there is no way this method can be type-safe with a null valueType. For example the following code compile without errors even if the "recipe" attribute is not an Integer:

Integer recipe = obj.getAttribute("recipe", null);

This can be fixed by:

On a related topic, I don't see where getAttribute(String, Class) verifies the type. Is it done in the native code? If not, then we should add a valueType.cast(theResult) call in the method implementation.