mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-27 03:46:17 -06:00
Styling: fixed IllegalAccessException when using component specific style [style]Spinner = foreground: #f00 and spinner labels are painted (caused by private subclass of JLabel where getForeground() is overridden)
This commit is contained in:
@@ -485,22 +485,34 @@ public class FlatStylingSupport
|
||||
String getterName = buildMethodName( "get", name );
|
||||
String setterName = buildMethodName( "set", name );
|
||||
|
||||
try {
|
||||
Method getter;
|
||||
for(;;) {
|
||||
try {
|
||||
getter = cls.getMethod( getterName );
|
||||
Method getter;
|
||||
try {
|
||||
getter = cls.getMethod( getterName );
|
||||
} catch( NoSuchMethodException ex ) {
|
||||
getter = cls.getMethod( buildMethodName( "is", name ) );
|
||||
}
|
||||
Method setter = cls.getMethod( setterName, getter.getReturnType() );
|
||||
Object oldValue = getter.invoke( obj );
|
||||
setter.invoke( obj, convertToEnum( value, getter.getReturnType() ) );
|
||||
return oldValue;
|
||||
} catch( NoSuchMethodException ex ) {
|
||||
getter = cls.getMethod( buildMethodName( "is", name ) );
|
||||
throw new UnknownStyleException( name );
|
||||
} catch( Exception ex ) {
|
||||
if( ex instanceof IllegalAccessException ) {
|
||||
// this may happen for private subclasses of public Swing classes
|
||||
// that override public property getter/setter
|
||||
// e.g. class JSlider.SmartHashtable.LabelUIResource.getForeground()
|
||||
// --> try again with superclass
|
||||
cls = cls.getSuperclass();
|
||||
if( cls != null && cls != Object.class )
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException( "failed to invoke property methods '" + cls.getName() + "."
|
||||
+ getterName + "()' or '" + setterName + "(...)'", ex );
|
||||
}
|
||||
Method setter = cls.getMethod( setterName, getter.getReturnType() );
|
||||
Object oldValue = getter.invoke( obj );
|
||||
setter.invoke( obj, convertToEnum( value, getter.getReturnType() ) );
|
||||
return oldValue;
|
||||
} catch( NoSuchMethodException ex ) {
|
||||
throw new UnknownStyleException( name );
|
||||
} catch( Exception ex ) {
|
||||
throw new IllegalArgumentException( "failed to invoke property methods '" + cls.getName() + "."
|
||||
+ getterName + "()' or '" + setterName + "(...)'", ex );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -219,6 +219,19 @@ public class TestFlatStyleType
|
||||
assertEquals( new Color( 0x000022 ), c.getForeground() );
|
||||
}
|
||||
|
||||
@Test
|
||||
void slider2() {
|
||||
JSlider c = new JSlider();
|
||||
|
||||
// when slider labels are painted, then a Java private subclass of JLabel
|
||||
// is used that overrides getForeground(), which is not accessible via reflection
|
||||
// see class JSlider.SmartHashtable.LabelUIResource
|
||||
c.setPaintLabels( true );
|
||||
c.setMajorTickSpacing( 50 );
|
||||
|
||||
assertEquals( new Color( 0x000022 ), c.getForeground() );
|
||||
}
|
||||
|
||||
@Test
|
||||
void spinner() {
|
||||
JSpinner c = new JSpinner();
|
||||
|
||||
Reference in New Issue
Block a user