File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung ;
2+
3+ import java .lang .reflect .Method ;
4+
5+ public class Outer {
6+
7+ public void outerPublic () {
8+ }
9+
10+ private void outerPrivate () {
11+ }
12+
13+ class Inner {
14+
15+ public void innerPublic () {
16+ outerPrivate ();
17+ }
18+
19+ public void innerPublicReflection (Outer ob ) throws Exception {
20+ Method method = ob .getClass ().getDeclaredMethod ("outerPrivate" );
21+ method .invoke (ob );
22+ }
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung ;
2+
3+ import static org .hamcrest .CoreMatchers .is ;
4+
5+ import java .util .Arrays ;
6+ import java .util .List ;
7+ import java .util .stream .Collectors ;
8+ import org .junit .Test ;
9+
10+ public class OuterUnitTest {
11+
12+ private static final String NEST_HOST_NAME = "com.baeldung.Outer" ;
13+
14+ @ Test
15+ public void whenGetNestHostFromOuter_thenGetNestHost () {
16+ is (Outer .class .getNestHost ().getName ()).equals (NEST_HOST_NAME );
17+ }
18+
19+ @ Test
20+ public void whenGetNestHostFromInner_thenGetNestHost () {
21+ is (Outer .Inner .class .getNestHost ().getName ()).equals (NEST_HOST_NAME );
22+ }
23+
24+ @ Test
25+ public void whenCheckNestmatesForNestedClasses_thenGetTrue () {
26+ is (Outer .Inner .class .isNestmateOf (Outer .class )).equals (true );
27+ }
28+
29+ @ Test
30+ public void whenCheckNestmatesForUnrelatedClasses_thenGetFalse () {
31+ is (Outer .Inner .class .isNestmateOf (Outer .class )).equals (false );
32+ }
33+
34+ @ Test
35+ public void whenGetNestMembersForNestedClasses_thenGetAllNestedClasses () {
36+ List <String > nestMembers = Arrays .stream (Outer .Inner .class .getNestMembers ())
37+ .map (Class ::getName )
38+ .collect (Collectors .toList ());
39+
40+ is (nestMembers .size ()).equals (2 );
41+
42+ boolean containsOuter = nestMembers .stream ()
43+ .anyMatch ("com.baeldung.Outer" ::equals );
44+ is (containsOuter ).equals (true );
45+
46+ boolean containsInner = nestMembers .stream ()
47+ .anyMatch ("com.baeldung.Outer$Inner" ::equals );
48+ is (containsInner ).equals (true );
49+ }
50+ }
You can’t perform that action at this time.
0 commit comments