This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push: new 733290769 Add another test for org.apache.commons.lang3.reflect.MethodUtils.getAccessibleMethod(Class<?>, String, Class<?>...) 733290769 is described below commit 7332907694aeb0be80417faf9aac6f5fc8b55e0e Author: Gary D. Gregory <garydgreg...@gmail.com> AuthorDate: Fri Aug 22 10:00:09 2025 -0400 Add another test for org.apache.commons.lang3.reflect.MethodUtils.getAccessibleMethod(Class<?>, String, Class<?>...) --- .../commons/lang3/reflect/MethodUtilsTest.java | 36 +++++++++++++++++ .../reflect/testbed/PackageBeanOtherPackage.java | 44 ++++++++++++++++++++ .../reflect/testbed/PublicSubBeanOtherPackage.java | 47 ++++++++++++++++++++++ 3 files changed, 127 insertions(+) diff --git a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java index 57dd4b4ad..9da2e8b68 100644 --- a/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java @@ -52,6 +52,7 @@ import org.apache.commons.lang3.reflect.testbed.GenericConsumer; import org.apache.commons.lang3.reflect.testbed.GenericParent; import org.apache.commons.lang3.reflect.testbed.PublicChild; +import org.apache.commons.lang3.reflect.testbed.PublicSubBeanOtherPackage; import org.apache.commons.lang3.reflect.testbed.StringParameterizedChild; import org.apache.commons.lang3.tuple.ImmutablePair; import org.junit.jupiter.api.BeforeEach; @@ -552,6 +553,41 @@ void testGetAccessibleMethodPublicSub() throws Exception { assertEquals("2222", bean.getBar(), "Set value (bar:3)"); } + @Test + void testGetAccessibleMethodPublicSubOtherPackage() throws Exception { + // PackageBeanOtherPackage class is package-private + final int modifiers = Class.forName("org.apache.commons.lang3.reflect.testbed.PackageBeanOtherPackage").getModifiers(); + assertFalse(Modifier.isPrivate(modifiers)); + assertFalse(Modifier.isProtected(modifiers)); + assertFalse(Modifier.isPublic(modifiers)); + // make sure that bean does what it should: compile + new PublicSubBeanOtherPackage().setBar(""); + // make sure that bean does what it should + final PublicSubBeanOtherPackage bean = new PublicSubBeanOtherPackage(); + assertEquals(bean.getFoo(), "This is foo", "Start value (foo)"); + assertEquals(bean.getBar(), "This is bar", "Start value (bar)"); + bean.setFoo("new foo"); + bean.setBar("new bar"); + assertEquals(bean.getFoo(), "new foo", "Set value (foo)"); + assertEquals(bean.getBar(), "new bar", "Set value (bar)"); + // see if we can access public methods in a default access superclass + // from a public access subclass instance + MethodUtils.invokeExactMethod(bean, "setFoo", "alpha"); + assertEquals(bean.getFoo(), "alpha", "Set value (foo:2)"); + MethodUtils.invokeExactMethod(bean, "setBar", "beta"); + assertEquals(bean.getBar(), "beta", "Set value (bar:2)"); + // PublicSubBean.setFoo(String) + Method method = MethodUtils.getAccessibleMethod(PublicSubBeanOtherPackage.class, "setFoo", String.class); + assertNotNull(method, "getAccessibleMethod() setFoo is Null"); + method.invoke(bean, "1111"); + assertEquals("1111", bean.getFoo(), "Set value (foo:3)"); + // PublicSubBean.setBar(String) + method = MethodUtils.getAccessibleMethod(PublicSubBeanOtherPackage.class, "setBar", String.class); + assertNotNull(method, "getAccessibleMethod() setBar is Null"); + method.invoke(bean, "2222"); + assertEquals("2222", bean.getBar(), "Set value (bar:3)"); + } + @Test void testGetAccessiblePublicMethod() throws Exception { assertSame(MutableObject.class, diff --git a/src/test/java/org/apache/commons/lang3/reflect/testbed/PackageBeanOtherPackage.java b/src/test/java/org/apache/commons/lang3/reflect/testbed/PackageBeanOtherPackage.java new file mode 100644 index 000000000..90c383466 --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/reflect/testbed/PackageBeanOtherPackage.java @@ -0,0 +1,44 @@ +/* + * 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 + * + * https://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 org.apache.commons.lang3.reflect.testbed; + +/** + * This class is designed to test the default access JVM problem workaround. The issue is that public methods of a public subclass contained in a default access + * superclass are returned by reflection but an IllegalAccessException is thrown when they are invoked. + * <p> + * This is the default access superclass + * </p> + */ +class PackageBeanOtherPackage { + + private String bar = "This is bar"; + + /** + * Package private constructor, can only use factory method to create beans. + */ + PackageBeanOtherPackage() { + } + + public String getBar() { + return this.bar; + } + + public void setBar(final String bar) { + this.bar = bar; + } +} diff --git a/src/test/java/org/apache/commons/lang3/reflect/testbed/PublicSubBeanOtherPackage.java b/src/test/java/org/apache/commons/lang3/reflect/testbed/PublicSubBeanOtherPackage.java new file mode 100644 index 000000000..0827809ff --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/reflect/testbed/PublicSubBeanOtherPackage.java @@ -0,0 +1,47 @@ +/* + * 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 + * + * https://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 org.apache.commons.lang3.reflect.testbed; + +/** + * This class is designed to test the default access JVM problem workaround. The issue is that public methods of a public subclass contained in a default access + * superclass are returned by reflection but an IllegalAccessException is thrown when they are invoked. + * <p> + * This is the default access superclass. + * </p> + */ +public class PublicSubBeanOtherPackage extends PackageBeanOtherPackage { + + /** + * A directly implemented property. + */ + private String foo = "This is foo"; + + /** + * Package private constructor, can only use factory method to create beans. + */ + public PublicSubBeanOtherPackage() { + } + + public String getFoo() { + return this.foo; + } + + public void setFoo(final String foo) { + this.foo = foo; + } +}