I just faced an issue with OrbitBehavior. Older Java3D don't have a field MOUSE_WHEEL_LISTENER, but it is a feature that I find very useful for my project (blog).
Here is a code on how I solved dynamical determination if runtime version of Java3D supports this new field without throwing an exception.
void addMouseWheelListener( int flags )
{
int listenerFlags = MOUSE_LISTENER | MOUSE_MOTION_LISTENER | flags ;
try
{
String field = "MOUSE_WHEEL_LISTENER";
Class parent = getClass().getSuperclass();
if( parent.getDeclaredField(field) != null )
{
int flag = parent.getDeclaredField(field).getInt(this);
listenerFlags |= flag ;
}
}
catch (SecurityException e)
{
}
catch (NoSuchFieldException e)
{
}
catch (IllegalArgumentException e)
{
}
catch (IllegalAccessException e)
{
}
setListenerFlags(listenerFlags);
}
As you can see I use reflection to check first if the field exist, and if and if it does I modify my listenerFlags with field's value.
Even if the field is visible in this class it does not need to be declared there. In my case field is declared in superclass.
No comments:
Post a Comment