2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package gov.nasa.jpf.test.java.lang.reflect;
21 import gov.nasa.jpf.util.test.TestJPF;
22 import java.lang.reflect.Method;
23 import org.junit.Test;
25 public class MethodTest extends TestJPF {
27 public static void main (String[] args){
28 runTestsOfThisClass(args);
32 public void equalsTest () throws SecurityException, NoSuchMethodException{
33 if (verifyNoPropertyViolation()){
34 Method m1 = MethodTest.class.getMethod("equalsTest", new Class[0]);
35 Method m2 = MethodTest.class.getMethod("equalsTest", new Class[0]);
36 assertTrue(m1.equals(m2));
37 assertFalse(m1 == m2);
41 public void testIsVarArg1s (Class<?>... argTypes){
44 public void testIsVarArgs2 (Class<?>[] argTypes){
48 public void isVarArgsTest () throws SecurityException, NoSuchMethodException{
49 if (verifyNoPropertyViolation()){
50 for (Method m : MethodTest.class.getDeclaredMethods()){
51 if (m.getName().equals("testIsVarArg1s"))
52 assertTrue(m.isVarArgs());
53 else if (m.getName().equals("testIsVarArg1s")) {
54 assertFalse(m.isVarArgs());
61 public void hashCodeTest () throws SecurityException, NoSuchMethodException{
62 if (verifyNoPropertyViolation()){
63 Method m1 = MethodTest.class.getMethod("hashCodeTest", new Class[0]);
64 Method m2 = MethodTest.class.getMethod("hashCodeTest", new Class[0]);
65 Method m3 = MethodTest.class.getMethod("equalsTest", new Class[0]);
66 assertTrue(m1.equals(m2));
67 assertTrue(m1.hashCode() == m2.hashCode());
68 assertFalse(m1.hashCode() == m3.hashCode());
72 public static class A {
78 public static class B extends A {
86 public void isBridgeTest (){
87 if (verifyNoPropertyViolation()){
88 assertFalse(B.class.getDeclaredMethods()[0].isBridge());
89 assertTrue(B.class.getDeclaredMethods()[1].isBridge());
95 void recordSeen (boolean[] seen, String[] expected, Method m){
96 String mname = m.toString();
97 for (int i=0; i<expected.length; i++){
98 if (expected[i].equals(mname)){
104 boolean checkSeen(boolean[] seen, String[] expected){
105 for (int i=0; i<expected.length; i++){
107 System.out.println("NOT seen: " + expected[i]);
114 //------------ getMethods() on interfaces
119 interface I2 extends I1 {
124 public void testGetMethodsOnIfc(){
125 if (verifyNoPropertyViolation()){
126 String[] expected = {
127 "public abstract void gov.nasa.jpf.test.java.lang.reflect.MethodTest$I2.i2()",
128 "public abstract void gov.nasa.jpf.test.java.lang.reflect.MethodTest$I1.i1()"
130 boolean[] seen = new boolean[expected.length];
132 Method[] methods = I2.class.getMethods();
134 for (Method m : methods){
135 System.out.println(m);
136 recordSeen(seen, expected, m);
138 assertTrue(methods.length == expected.length);
139 //assertTrue(checkSeen(seen, expected));
144 //------------ getMethods() on classes
145 public static class C extends B {
147 System.out.println("C.<clinit>");
156 System.out.println("C.<init>");
161 public void testGetMethodsOnClass(){
162 if (verifyNoPropertyViolation()){
163 String[] expected = {
164 "public native int java.lang.Object.hashCode()",
165 "public final native void java.lang.Object.notify()",
166 "public final native void java.lang.Object.notifyAll()",
167 "public java.lang.String java.lang.Object.toString()",
168 "public final native java.lang.Class java.lang.Object.getClass()",
169 "public final native void java.lang.Object.wait(long)",
170 "public final void java.lang.Object.wait(long,int)",
171 "public gov.nasa.jpf.test.java.lang.reflect.MethodTest$B gov.nasa.jpf.test.java.lang.reflect.MethodTest$B.foo(int)",
172 "public boolean java.lang.Object.equals(java.lang.Object)",
173 "public volatile gov.nasa.jpf.test.java.lang.reflect.MethodTest$A gov.nasa.jpf.test.java.lang.reflect.MethodTest$B.foo(int)",
174 "public final void java.lang.Object.wait()"
176 boolean[] seen = new boolean[expected.length];
178 Method[] methods = C.class.getMethods();
179 for (Method m : methods){
180 System.out.println(m);