/src/tests/gov/nasa/jpf/test/vm/reflection/ReflectionTest.java

https://github.com/javapathfinder/jpf-core · Java · 65 lines · 34 code · 11 blank · 20 comment · 1 complexity · 4f75a45b8cf6383817f5b04e492a01cf MD5 · raw file

  1. /*
  2. * Copyright (C) 2014, United States Government, as represented by the
  3. * Administrator of the National Aeronautics and Space Administration.
  4. * All rights reserved.
  5. *
  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
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0.
  11. *
  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.
  17. */
  18. package gov.nasa.jpf.test.vm.reflection;
  19. import gov.nasa.jpf.util.test.TestJPF;
  20. import org.junit.Test;
  21. public class ReflectionTest extends TestJPF {
  22. static class MyClass {
  23. void bar(){
  24. foo();
  25. }
  26. // compilation will cause a warning about internal proprietary API that cannot be suppressed, but we have to test this
  27. // since it is still used by standard libs
  28. void foo (){
  29. Class<?> callerCls = sun.reflect.Reflection.getCallerClass(0); // that would be getCallerClass()
  30. System.out.println("-- getCallerClass(0) = " + callerCls);
  31. assertTrue(callerCls.getName().equals("sun.reflect.Reflection"));
  32. callerCls = sun.reflect.Reflection.getCallerClass(1); // foo()
  33. System.out.println("-- getCallerClass(1) = " + callerCls);
  34. assertTrue(callerCls.getName().equals("gov.nasa.jpf.test.vm.reflection.ReflectionTest$MyClass"));
  35. callerCls = sun.reflect.Reflection.getCallerClass(2); // bar()
  36. System.out.println("-- getCallerClass(2) = " + callerCls);
  37. assertTrue(callerCls.getName().equals("gov.nasa.jpf.test.vm.reflection.ReflectionTest$MyClass"));
  38. callerCls = sun.reflect.Reflection.getCallerClass(3); // callIt()
  39. System.out.println("-- getCallerClass(3) = " + callerCls);
  40. assertTrue(callerCls.getName().equals("gov.nasa.jpf.test.vm.reflection.ReflectionTest"));
  41. // <2do> should also test Method.invoke skipping
  42. }
  43. }
  44. void callIt(){
  45. MyClass o = new MyClass();
  46. o.bar();
  47. }
  48. @Test
  49. public void testCallerClass() {
  50. if (verifyNoPropertyViolation()){
  51. callIt();
  52. }
  53. }
  54. }