1 /**
   2  *  @test/nodynamiccopyright/
   3  *  @bug 4952629 4870514
   4  *  @summary REGRESSION: javac generates a spurious line number entry on } else {
   5  *
   6  *  @author jjh
   7  *
   8  *  @run build VMConnection TargetListener TargetAdapter
   9  *  @run compile -g LineNumberOnBraceTest.java
  10  *  @run driver LineNumberOnBraceTest
  11  */
  12 import com.sun.jdi.*;
  13 import com.sun.jdi.event.*;
  14 import com.sun.jdi.request.*;
  15 
  16 import java.util.*;
  17 
  18     /********** LINE NUMBER SENSITIVE! *****************************************************************/
  19 class LineNumberOnBraceTarg {
  20 
  21     public final static int stopLine = 28;   // THIS MUST BE THE LINE NUMBER OF THE // stopline LINE
  22     public final static int stopLine2 = 34;  // THIS MUST BE THE LINE NUMBER OF THE // stopline2 LINE
  23 
  24 
  25     public static void main(String[] args){
  26         System.out.println("Howdy!");
  27         if (args.length == 0) {
  28             System.out.println("No args to debuggee");             // stopLine
  29         } else {
  30             System.out.println("Some args to debuggee");
  31         }
  32         if (args.length == 0) {
  33             boolean b1 = false;
  34             if (b1) {                                              // stopLine2
  35                 System.out.println("In 2nd else");                 // bug 4870514 is that we stop here.
  36             }
  37         } else {
  38             System.out.println("In 2nd else");
  39         }
  40         System.out.println("Goodbye from LineNumberOnBraceTarg!");  // stopLine2 + 6
  41     }
  42 
  43     // This isn't part of the test; it is just here
  44     // so one can see what line numbers are generated for a finally.
  45     public void exampleOfThrow() {
  46         try {
  47             throw new Exception();
  48         } catch (Exception e) {
  49             System.out.println("caught exception");
  50         } finally {
  51             System.out.println("finally");
  52         }
  53     }
  54 
  55 }
  56 
  57     /********** test program **********/
  58 
  59 public class LineNumberOnBraceTest extends TestScaffold {
  60     ReferenceType targetClass;
  61     ThreadReference mainThread;
  62 
  63     LineNumberOnBraceTest (String args[]) {
  64         super(args);
  65     }
  66 
  67     public static void main(String[] args)      throws Exception {
  68         new LineNumberOnBraceTest(args).startTests();
  69     }
  70     /********** test core **********/
  71 
  72     protected void runTests() throws Exception {
  73         /*
  74          * Get to the top of main()
  75          * to determine targetClass and mainThread
  76          */
  77         BreakpointEvent bpe = startToMain("LineNumberOnBraceTarg");
  78         targetClass = bpe.location().declaringType();
  79         mainThread = bpe.thread();
  80 
  81         resumeTo("LineNumberOnBraceTarg", LineNumberOnBraceTarg.stopLine);
  82         StepEvent stepev = stepOverLine(mainThread);       // step to 2nd if (args.length
  83 
  84         // Bug 4952629 is that javac outputs a line number
  85         // on the goto around the else which causes us to
  86         // be stopped at that goto instead of the println("Goodbye ...")
  87 
  88         int ln = stepev.location().lineNumber();
  89         System.out.println("Debuggee is stopped at line " + ln);
  90         if (ln != LineNumberOnBraceTarg.stopLine + 4) {
  91             failure("FAIL: Bug 4952629: Should be at line " +
  92                     (LineNumberOnBraceTarg.stopLine + 4) +
  93                     ", am at " + ln);
  94         } else {
  95             System.out.println("Passed test for 4952629");
  96         }
  97 
  98         // Test for bug 4870514
  99         System.out.println("Resuming to " + LineNumberOnBraceTarg.stopLine2);
 100         resumeTo("LineNumberOnBraceTarg", LineNumberOnBraceTarg.stopLine2);
 101         System.out.println("Stopped at " + LineNumberOnBraceTarg.stopLine2);
 102         stepev = stepOverLine(mainThread);
 103         ln = stepev.location().lineNumber();
 104         System.out.println("Debuggee is stopped at line " + ln);
 105         if (ln == LineNumberOnBraceTarg.stopLine2 + 1) {
 106             failure("FAIL: bug 4870514: Incorrectly stopped at " +
 107                     (LineNumberOnBraceTarg.stopLine2 + 1));
 108         } else {
 109             System.out.println("Passed test for 4870514");
 110         }
 111 
 112 
 113         /*
 114          * resume the target listening for events
 115          */
 116         listenUntilVMDisconnect();
 117 
 118         /*
 119          * deal with results of test
 120          * if anything has called failure("foo") testFailed will be true
 121          */
 122         if (!testFailed) {
 123             println("LineNumberOnBraceTest: passed");
 124         } else {
 125             throw new Exception("LineNumberOnBraceTest: failed");
 126         }
 127     }
 128 }