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

https://gitlab.com/bradstewart/ee360t · Java · 66 lines · 34 code · 11 blank · 21 comment · 1 complexity · 50c14f8f2377a2155e3c2514e67abe17 MD5 · raw file

  1. //
  2. // Copyright (C) 2009 United States Government as represented by the
  3. // Administrator of the National Aeronautics and Space Administration
  4. // (NASA). All Rights Reserved.
  5. //
  6. // This software is distributed under the NASA Open Source Agreement
  7. // (NOSA), version 1.3. The NOSA has been approved by the Open Source
  8. // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
  9. // directory tree for the complete NOSA document.
  10. //
  11. // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
  12. // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
  13. // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
  14. // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
  15. // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
  16. // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
  17. // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
  18. //
  19. package gov.nasa.jpf.test.vm.reflection;
  20. import gov.nasa.jpf.util.test.TestJPF;
  21. import org.junit.Test;
  22. public class ReflectionTest extends TestJPF {
  23. static class MyClass {
  24. void bar(){
  25. foo();
  26. }
  27. // compilation will cause a warning about internal proprietary API that cannot be suppressed, but we have to test this
  28. // since it is still used by standard libs
  29. void foo (){
  30. Class<?> callerCls = sun.reflect.Reflection.getCallerClass(0); // that would be getCallerClass()
  31. System.out.println("-- getCallerClass(0) = " + callerCls);
  32. assertTrue(callerCls.getName().equals("sun.reflect.Reflection"));
  33. callerCls = sun.reflect.Reflection.getCallerClass(1); // foo()
  34. System.out.println("-- getCallerClass(1) = " + callerCls);
  35. assertTrue(callerCls.getName().equals("gov.nasa.jpf.test.vm.reflection.ReflectionTest$MyClass"));
  36. callerCls = sun.reflect.Reflection.getCallerClass(2); // bar()
  37. System.out.println("-- getCallerClass(2) = " + callerCls);
  38. assertTrue(callerCls.getName().equals("gov.nasa.jpf.test.vm.reflection.ReflectionTest$MyClass"));
  39. callerCls = sun.reflect.Reflection.getCallerClass(3); // callIt()
  40. System.out.println("-- getCallerClass(3) = " + callerCls);
  41. assertTrue(callerCls.getName().equals("gov.nasa.jpf.test.vm.reflection.ReflectionTest"));
  42. // <2do> should also test Method.invoke skipping
  43. }
  44. }
  45. void callIt(){
  46. MyClass o = new MyClass();
  47. o.bar();
  48. }
  49. @Test
  50. public void testCallerClass() {
  51. if (verifyNoPropertyViolation()){
  52. callIt();
  53. }
  54. }
  55. }