format
This commit is contained in:
parent
e8b07fea1d
commit
338b0bc80c
|
@ -4,21 +4,21 @@ import java.lang.reflect.Method;
|
||||||
|
|
||||||
public class Outer {
|
public class Outer {
|
||||||
|
|
||||||
public void outerPublic() {
|
public void outerPublic() {
|
||||||
}
|
|
||||||
|
|
||||||
private void outerPrivate() {
|
|
||||||
}
|
|
||||||
|
|
||||||
class Inner {
|
|
||||||
|
|
||||||
public void innerPublic() {
|
|
||||||
outerPrivate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void innerPublicReflection(Outer ob) throws Exception {
|
private void outerPrivate() {
|
||||||
Method method = ob.getClass().getDeclaredMethod("outerPrivate");
|
}
|
||||||
method.invoke(ob);
|
|
||||||
}
|
class Inner {
|
||||||
}
|
|
||||||
|
public void innerPublic() {
|
||||||
|
outerPrivate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void innerPublicReflection(Outer ob) throws Exception {
|
||||||
|
Method method = ob.getClass().getDeclaredMethod("outerPrivate");
|
||||||
|
method.invoke(ob);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -9,42 +9,42 @@ import org.junit.Test;
|
||||||
|
|
||||||
public class OuterUnitTest {
|
public class OuterUnitTest {
|
||||||
|
|
||||||
private static final String NEST_HOST_NAME = "com.baeldung.Outer";
|
private static final String NEST_HOST_NAME = "com.baeldung.Outer";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenGetNestHostFromOuter_thenGetNestHost() {
|
public void whenGetNestHostFromOuter_thenGetNestHost() {
|
||||||
is(Outer.class.getNestHost().getName()).equals(NEST_HOST_NAME);
|
is(Outer.class.getNestHost().getName()).equals(NEST_HOST_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenGetNestHostFromInner_thenGetNestHost() {
|
public void whenGetNestHostFromInner_thenGetNestHost() {
|
||||||
is(Outer.Inner.class.getNestHost().getName()).equals(NEST_HOST_NAME);
|
is(Outer.Inner.class.getNestHost().getName()).equals(NEST_HOST_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCheckNestmatesForNestedClasses_thenGetTrue() {
|
public void whenCheckNestmatesForNestedClasses_thenGetTrue() {
|
||||||
is(Outer.Inner.class.isNestmateOf(Outer.class)).equals(true);
|
is(Outer.Inner.class.isNestmateOf(Outer.class)).equals(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCheckNestmatesForUnrelatedClasses_thenGetFalse() {
|
public void whenCheckNestmatesForUnrelatedClasses_thenGetFalse() {
|
||||||
is(Outer.Inner.class.isNestmateOf(Outer.class)).equals(false);
|
is(Outer.Inner.class.isNestmateOf(Outer.class)).equals(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenGetNestMembersForNestedClasses_thenGetAllNestedClasses() {
|
public void whenGetNestMembersForNestedClasses_thenGetAllNestedClasses() {
|
||||||
List<String> nestMembers = Arrays.stream(Outer.Inner.class.getNestMembers())
|
List<String> nestMembers = Arrays.stream(Outer.Inner.class.getNestMembers())
|
||||||
.map(Class::getName)
|
.map(Class::getName)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
is(nestMembers.size()).equals(2);
|
is(nestMembers.size()).equals(2);
|
||||||
|
|
||||||
boolean containsOuter = nestMembers.stream()
|
boolean containsOuter = nestMembers.stream()
|
||||||
.anyMatch("com.baeldung.Outer"::equals);
|
.anyMatch("com.baeldung.Outer"::equals);
|
||||||
is(containsOuter).equals(true);
|
is(containsOuter).equals(true);
|
||||||
|
|
||||||
boolean containsInner = nestMembers.stream()
|
boolean containsInner = nestMembers.stream()
|
||||||
.anyMatch("com.baeldung.Outer$Inner"::equals);
|
.anyMatch("com.baeldung.Outer$Inner"::equals);
|
||||||
is(containsInner).equals(true);
|
is(containsInner).equals(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue