Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/classes/modules/java.base/java/lang/Class.java
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,18 @@ public boolean isSynthetic (){
public Module getModule() {
return module;
}
public Class<?> getNestHost() {
Class<?> host = this;
while (host.getEnclosingClass() != null) {
host = host.getEnclosingClass();
}
return host;
}

public boolean isNestmateOf(Class<?> c) {
if (c == null) {
return false;
}
return this.getNestHost() == c.getNestHost();
}
}
61 changes: 61 additions & 0 deletions src/tests/gov/nasa/jpf/test/java/lang/NestmateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2018, United States Government, as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All rights reserved.
*
* The Java Pathfinder core (jpf-core) platform is licensed 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
*
* http://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 gov.nasa.jpf.test.java.lang;

import gov.nasa.jpf.util.test.TestJPF;
import org.junit.Test;
import static org.junit.Assert.*; // <-- This was the missing magic line!

public class NestmateTest extends TestJPF {

public class Inner {}

@Test
public void testNestHost() {
if (verifyNoPropertyViolation()) {
Class<?> host = NestmateTest.class.getNestHost();
assertEquals(NestmateTest.class, host);
Class<?> innerHost = Inner.class.getNestHost();
assertEquals(NestmateTest.class, innerHost);
}
}

@Test
public void testIsNestmateOf() {
if (verifyNoPropertyViolation()) {
assertTrue(NestmateTest.class.isNestmateOf(Inner.class));
assertTrue(Inner.class.isNestmateOf(NestmateTest.class));
}
}

@Test
public void testPrimitiveArrayVoidNestHost() {
if (verifyNoPropertyViolation()) {
// Primitive
assertEquals(int.class, int.class.getNestHost());
assertEquals(double.class, double.class.getNestHost());

// Array
assertEquals(int[].class, int[].class.getNestHost());
assertEquals(String[].class, String[].class.getNestHost());

// Void
assertEquals(void.class, void.class.getNestHost());
}
}
}