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

https://github.com/danbryce/jpf-core2 · Java · 73 lines · 37 code · 11 blank · 25 comment · 1 complexity · d8c7879661c615379085ad51c515f6ce 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
  28. // cannot be suppressed, but we have to test this
  29. // since it is still used by standard libs
  30. void foo() {
  31. Class<?> callerCls = sun.reflect.Reflection.getCallerClass(0); // that
  32. // would
  33. // be
  34. // getCallerClass()
  35. System.out.println("-- getCallerClass(0) = " + callerCls);
  36. assertTrue(callerCls.getName().equals("sun.reflect.Reflection"));
  37. callerCls = sun.reflect.Reflection.getCallerClass(1); // foo()
  38. System.out.println("-- getCallerClass(1) = " + callerCls);
  39. assertTrue(callerCls.getName().equals(
  40. "gov.nasa.jpf.test.vm.reflection.ReflectionTest$MyClass"));
  41. callerCls = sun.reflect.Reflection.getCallerClass(2); // bar()
  42. System.out.println("-- getCallerClass(2) = " + callerCls);
  43. assertTrue(callerCls.getName().equals(
  44. "gov.nasa.jpf.test.vm.reflection.ReflectionTest$MyClass"));
  45. callerCls = sun.reflect.Reflection.getCallerClass(3); // callIt()
  46. System.out.println("-- getCallerClass(3) = " + callerCls);
  47. assertTrue(callerCls.getName().equals(
  48. "gov.nasa.jpf.test.vm.reflection.ReflectionTest"));
  49. // <2do> should also test Method.invoke skipping
  50. }
  51. }
  52. void callIt() {
  53. MyClass o = new MyClass();
  54. o.bar();
  55. }
  56. @Test
  57. public void testCallerClass() {
  58. if (verifyNoPropertyViolation()) {
  59. callIt();
  60. }
  61. }
  62. }