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

https://bitbucket.org/potter/jpf-core · Java · 64 lines · 34 code · 11 blank · 19 comment · 1 complexity · 322b4d1a872560f25ecc747533184c2e 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. void foo (){
  28. Class<?> callerCls = sun.reflect.Reflection.getCallerClass(0); // that would be getCallerClass()
  29. System.out.println("-- getCallerClass(0) = " + callerCls);
  30. assertTrue(callerCls.getName().equals("sun.reflect.Reflection"));
  31. callerCls = sun.reflect.Reflection.getCallerClass(1); // foo()
  32. System.out.println("-- getCallerClass(1) = " + callerCls);
  33. assertTrue(callerCls.getName().equals("gov.nasa.jpf.test.vm.reflection.ReflectionTest$MyClass"));
  34. callerCls = sun.reflect.Reflection.getCallerClass(2); // bar()
  35. System.out.println("-- getCallerClass(2) = " + callerCls);
  36. assertTrue(callerCls.getName().equals("gov.nasa.jpf.test.vm.reflection.ReflectionTest$MyClass"));
  37. callerCls = sun.reflect.Reflection.getCallerClass(3); // callIt()
  38. System.out.println("-- getCallerClass(3) = " + callerCls);
  39. assertTrue(callerCls.getName().equals("gov.nasa.jpf.test.vm.reflection.ReflectionTest"));
  40. // <2do> should also test Method.invoke skipping
  41. }
  42. }
  43. void callIt(){
  44. MyClass o = new MyClass();
  45. o.bar();
  46. }
  47. @Test
  48. public void testCallerClass() {
  49. if (verifyNoPropertyViolation()){
  50. callIt();
  51. }
  52. }
  53. }