Skip to content

Commit 7332907

Browse files
committed
Add another test for
org.apache.commons.lang3.reflect.MethodUtils.getAccessibleMethod(Class<?>, String, Class<?>...)
1 parent c47cd53 commit 7332907

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

src/test/java/org/apache/commons/lang3/reflect/MethodUtilsTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.apache.commons.lang3.reflect.testbed.GenericConsumer;
5353
import org.apache.commons.lang3.reflect.testbed.GenericParent;
5454
import org.apache.commons.lang3.reflect.testbed.PublicChild;
55+
import org.apache.commons.lang3.reflect.testbed.PublicSubBeanOtherPackage;
5556
import org.apache.commons.lang3.reflect.testbed.StringParameterizedChild;
5657
import org.apache.commons.lang3.tuple.ImmutablePair;
5758
import org.junit.jupiter.api.BeforeEach;
@@ -552,6 +553,41 @@ void testGetAccessibleMethodPublicSub() throws Exception {
552553
assertEquals("2222", bean.getBar(), "Set value (bar:3)");
553554
}
554555

556+
@Test
557+
void testGetAccessibleMethodPublicSubOtherPackage() throws Exception {
558+
// PackageBeanOtherPackage class is package-private
559+
final int modifiers = Class.forName("org.apache.commons.lang3.reflect.testbed.PackageBeanOtherPackage").getModifiers();
560+
assertFalse(Modifier.isPrivate(modifiers));
561+
assertFalse(Modifier.isProtected(modifiers));
562+
assertFalse(Modifier.isPublic(modifiers));
563+
// make sure that bean does what it should: compile
564+
new PublicSubBeanOtherPackage().setBar("");
565+
// make sure that bean does what it should
566+
final PublicSubBeanOtherPackage bean = new PublicSubBeanOtherPackage();
567+
assertEquals(bean.getFoo(), "This is foo", "Start value (foo)");
568+
assertEquals(bean.getBar(), "This is bar", "Start value (bar)");
569+
bean.setFoo("new foo");
570+
bean.setBar("new bar");
571+
assertEquals(bean.getFoo(), "new foo", "Set value (foo)");
572+
assertEquals(bean.getBar(), "new bar", "Set value (bar)");
573+
// see if we can access public methods in a default access superclass
574+
// from a public access subclass instance
575+
MethodUtils.invokeExactMethod(bean, "setFoo", "alpha");
576+
assertEquals(bean.getFoo(), "alpha", "Set value (foo:2)");
577+
MethodUtils.invokeExactMethod(bean, "setBar", "beta");
578+
assertEquals(bean.getBar(), "beta", "Set value (bar:2)");
579+
// PublicSubBean.setFoo(String)
580+
Method method = MethodUtils.getAccessibleMethod(PublicSubBeanOtherPackage.class, "setFoo", String.class);
581+
assertNotNull(method, "getAccessibleMethod() setFoo is Null");
582+
method.invoke(bean, "1111");
583+
assertEquals("1111", bean.getFoo(), "Set value (foo:3)");
584+
// PublicSubBean.setBar(String)
585+
method = MethodUtils.getAccessibleMethod(PublicSubBeanOtherPackage.class, "setBar", String.class);
586+
assertNotNull(method, "getAccessibleMethod() setBar is Null");
587+
method.invoke(bean, "2222");
588+
assertEquals("2222", bean.getBar(), "Set value (bar:3)");
589+
}
590+
555591
@Test
556592
void testGetAccessiblePublicMethod() throws Exception {
557593
assertSame(MutableObject.class,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.lang3.reflect.testbed;
19+
20+
/**
21+
* 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
22+
* superclass are returned by reflection but an IllegalAccessException is thrown when they are invoked.
23+
* <p>
24+
* This is the default access superclass
25+
* </p>
26+
*/
27+
class PackageBeanOtherPackage {
28+
29+
private String bar = "This is bar";
30+
31+
/**
32+
* Package private constructor, can only use factory method to create beans.
33+
*/
34+
PackageBeanOtherPackage() {
35+
}
36+
37+
public String getBar() {
38+
return this.bar;
39+
}
40+
41+
public void setBar(final String bar) {
42+
this.bar = bar;
43+
}
44+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.lang3.reflect.testbed;
19+
20+
/**
21+
* 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
22+
* superclass are returned by reflection but an IllegalAccessException is thrown when they are invoked.
23+
* <p>
24+
* This is the default access superclass.
25+
* </p>
26+
*/
27+
public class PublicSubBeanOtherPackage extends PackageBeanOtherPackage {
28+
29+
/**
30+
* A directly implemented property.
31+
*/
32+
private String foo = "This is foo";
33+
34+
/**
35+
* Package private constructor, can only use factory method to create beans.
36+
*/
37+
public PublicSubBeanOtherPackage() {
38+
}
39+
40+
public String getFoo() {
41+
return this.foo;
42+
}
43+
44+
public void setFoo(final String foo) {
45+
this.foo = foo;
46+
}
47+
}

0 commit comments

Comments
 (0)