< prev index next >

test/java/lang/StackWalker/Basic.java

Print this page

        

@@ -21,18 +21,20 @@
  * questions.
  */
 
 /*
  * @test
- * @bug 8140450
+ * @bug 8140450 8173898
  * @summary Basic test for the StackWalker::walk method
  * @run testng Basic
  */
 
 import java.lang.StackWalker.StackFrame;
 import java.util.List;
+import java.util.Objects;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 import static java.lang.StackWalker.Option.*;
 
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 

@@ -58,10 +60,24 @@
         for (int estimate : estimates) {
             test.walk(estimate);
         }
     }
 
+    @Test
+    /**
+     * @bug 8173898
+     */
+    public static void testConstructor() throws Exception {
+        System.out.println("testConstructor:");
+        List<String> found = ((Dummy)Dummy.class.getMethod("create")
+                             .invoke(null)).found;
+        assertEquals(List.of(Dummy.class.getName()+"::<init>",
+                             Dummy.class.getName()+"::create",
+                             Basic.class.getName()+"::testConstructor"),
+                     found);
+    }
+
     private final int depth;
     Basic(int depth) {
         this.depth = depth;
     }
 

@@ -75,10 +91,37 @@
         System.out.format("depth=%d estimate=%d expected=%d walked=%d%n",
                           depth, estimate, limit, frames.size());
         assertEquals(limit, frames.size());
     }
 
+    static class Dummy {
+        static final StackWalker walker =
+            StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
+        List<String> found;
+        public Dummy() {
+            found = walker.walk(this::parse);
+        }
+        public boolean accept(StackFrame f) {
+            if (!f.getDeclaringClass().getName().contains(".")) {
+                System.out.println("    " + f);
+                return true;
+            }
+            return false;
+        }
+        public String frame(StackFrame f) {
+            return f.getDeclaringClass().getName() + "::" + f.getMethodName();
+        }
+        List<String> parse(Stream<StackFrame> s) {
+            return s.filter(this::accept)
+                    .map(this::frame)
+                    .collect(Collectors.toList());
+        }
+        public static Dummy create() throws Exception {
+            return Dummy.class.getConstructor().newInstance();
+        }
+    }
+
     class StackBuilder {
         private final int stackDepth;
         private final int limit;
         private int depth = 0;
         private List<StackFrame> result;

@@ -134,6 +177,12 @@
     static void assertEquals(int x, int y) {
         if (x != y) {
             throw new RuntimeException(x + " != " + y);
         }
     }
+
+    static void assertEquals(Object x, Object y) {
+        if (!Objects.equals(x,y)) {
+            throw new RuntimeException(x + " != " + y);
+        }
+    }
 }
< prev index next >