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