Merge pull request #5977 from rockoder/java11-nests
[BAEL-2359] Java 11 Nests
This commit is contained in:
commit
bb060260dc
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class Outer {
|
||||
|
||||
public void outerPublic() {
|
||||
}
|
||||
|
||||
private void outerPrivate() {
|
||||
}
|
||||
|
||||
class Inner {
|
||||
|
||||
public void innerPublic() {
|
||||
outerPrivate();
|
||||
}
|
||||
|
||||
public void innerPublicReflection(Outer ob) throws Exception {
|
||||
Method method = ob.getClass().getDeclaredMethod("outerPrivate");
|
||||
method.invoke(ob);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import org.junit.Test;
|
||||
|
||||
public class OuterUnitTest {
|
||||
|
||||
private static final String NEST_HOST_NAME = "com.baeldung.Outer";
|
||||
|
||||
@Test
|
||||
public void whenGetNestHostFromOuter_thenGetNestHost() {
|
||||
is(Outer.class.getNestHost().getName()).equals(NEST_HOST_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetNestHostFromInner_thenGetNestHost() {
|
||||
is(Outer.Inner.class.getNestHost().getName()).equals(NEST_HOST_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckNestmatesForNestedClasses_thenGetTrue() {
|
||||
is(Outer.Inner.class.isNestmateOf(Outer.class)).equals(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckNestmatesForUnrelatedClasses_thenGetFalse() {
|
||||
is(Outer.Inner.class.isNestmateOf(Outer.class)).equals(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetNestMembersForNestedClasses_thenGetAllNestedClasses() {
|
||||
Set<String> nestMembers = Arrays.stream(Outer.Inner.class.getNestMembers())
|
||||
.map(Class::getName)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
is(nestMembers.size()).equals(2);
|
||||
|
||||
assertTrue(nestMembers.contains("com.baeldung.Outer"));
|
||||
assertTrue(nestMembers.contains("com.baeldung.Outer$Inner"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue