utafrali commented on code in PR #2899: URL: https://github.com/apache/groovy/pull/2899#discussion_r3946924263
########## subprojects/groovy-swing/src/test/groovy/groovy/swing/LookAndFeelHelperThemeTest.groovy: ########## @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package groovy.swing + +import org.junit.jupiter.api.Test + +import javax.swing.plaf.metal.DefaultMetalTheme +import javax.swing.plaf.metal.MetalLookAndFeel + +import static groovy.test.GroovyAssert.shouldFail + +class LookAndFeelHelperThemeTest { + + // the Metal 'theme' attribute handler; invoked directly so the test does not + // touch UIManager and therefore runs headless + private static metalThemeHandler() { + LookAndFeelHelper.instance.@extendedAttributes['javax.swing.plaf.metal.MetalLookAndFeel'].theme + } + + @Test + void rejectsNonMetalThemeClassWithoutConstructingIt() { + ThemeProbeFlag.constructed = false + def handler = metalThemeHandler() + shouldFail(IllegalArgumentException) { + handler(null, 'groovy.swing.NonMetalThemeProbe') + } + assert !ThemeProbeFlag.constructed: 'a non-MetalTheme class must not be constructed' + } + + @Test + void acceptsMetalThemeSubclass() { + def handler = metalThemeHandler() + handler(null, 'groovy.swing.CustomMetalThemeProbe') + assert MetalLookAndFeel.currentTheme instanceof CustomMetalThemeProbe Review Comment: `handler(null, ...)` sets `MetalLookAndFeel.currentTheme` as a side effect and the test leaves it modified after completion. If other tests in the suite read `currentTheme`, this could introduce ordering-dependent failures. Consider saving and restoring: ```groovy def previousTheme = MetalLookAndFeel.currentTheme try { handler(null, 'groovy.swing.CustomMetalThemeProbe') assert MetalLookAndFeel.currentTheme instanceof CustomMetalThemeProbe } finally { MetalLookAndFeel.currentTheme = previousTheme } ``` ########## subprojects/groovy-swing/src/main/groovy/groovy/swing/LookAndFeelHelper.groovy: ########## @@ -94,7 +94,16 @@ class LookAndFeelHelper { } else if (theme == 'steel') { theme = new DefaultMetalTheme(); } else { - theme = Class.forName(theme as String).getConstructor().newInstance() + // resolve without initializing, confirm it is a MetalTheme before + // constructing it, and prefer the context classloader so application + // and @Grab-supplied theme classes resolve; the context loader may be + // null, so fall back to this class's loader (the pre-existing behaviour) + def loader = Thread.currentThread().contextClassLoader ?: LookAndFeelHelper.classLoader + def themeClass = Class.forName(theme as String, false, loader) + if (!MetalTheme.isAssignableFrom(themeClass)) { + throw new IllegalArgumentException("Metal theme class ${themeClass.name} is not a ${MetalTheme.name}") Review Comment: If `themeClass` is an abstract `MetalTheme` subtype, `isAssignableFrom` passes but `getConstructor().newInstance()` will throw `InstantiationException` with a confusing message. The fix already provides a clear `IllegalArgumentException` for wrong types; it should do the same for abstract classes. Consider adding before the `getConstructor()` call: ```groovy if (java.lang.reflect.Modifier.isAbstract(themeClass.modifiers)) { throw new IllegalArgumentException("Metal theme class ${themeClass.name} is abstract and cannot be instantiated") } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
