jdk/test/com/sun/jdi/NoLocInfoTest.java
author jjg
Thu, 22 May 2008 15:51:41 -0700
changeset 655 1ebc7ce89018
parent 2 90ce3da70b43
child 5506 202f599c92aa
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
 * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *  @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *  @bug 4642611
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *  @summary Test that method.allLineLocations() should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *           throw AbsentInformationException exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *  @author Serguei Spitsyn
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *  @run build TestScaffold VMConnection TargetListener TargetAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  @run compile -g:none NoLocInfoTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *  @run main NoLocInfoTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /********** target program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
interface InterfaceNoLocInfoTarg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    int instanceMeth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    int instanceMeth1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
abstract class AbstractNoLocInfoTarg implements InterfaceNoLocInfoTarg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    protected int fld;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    AbstractNoLocInfoTarg() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        fld = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public abstract int instanceMeth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public int instanceMeth1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        fld = 999;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        fld = instanceMeth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
class NoLocInfoTarg extends AbstractNoLocInfoTarg {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // Class has a default constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static void main(String[] args){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        System.out.println("A number is: " + new NoLocInfoTarg().instanceMeth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static int staticMeth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        int i = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public int instanceMeth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return i + staticMeth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private void voidInstanceMeth() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static native int staticNativeMeth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    native boolean instanceNativeMeth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /********** test program **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
public class NoLocInfoTest extends TestScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    final String[] args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    NoLocInfoTest (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        super(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.args = args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static void main(String[] args)      throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        new NoLocInfoTest(args).startTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /********** test assist **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    Method getMethod(String className, String methodName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        List refs = vm().classesByName(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (refs.size() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            failure("Failure: " + refs.size() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    " ReferenceTypes named: " + className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        ReferenceType refType = (ReferenceType)refs.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        List meths = refType.methodsByName(methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (meths.size() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            failure("Failure: " + meths.size() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    " methods named: " + methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return (Method)meths.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    void checkLineNumberTable(String className, String methodName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        println("GetLineNumberTable for method: " + className + "." + methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        Method method = getMethod(className, methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            List locations = method.allLineLocations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            failure("Failure: com.sun.jdi.AbsentInformationException was expected; " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    "LineNumberTable.size() = " + locations.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        catch (com.sun.jdi.AbsentInformationException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            println("Success: com.sun.jdi.AbsentInformationException thrown as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    void checkEmptyLineNumberTable(String className, String methodName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        println("GetLineNumberTable for abstract/native method: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                 className + "." + methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Method method = getMethod(className, methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            int size = method.allLineLocations().size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
               println("Succes: LineNumberTable.size() == " + size + " as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
               failure("Failure: LineNumberTable.size()==" + size + ", but ZERO was expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        catch (com.sun.jdi.AbsentInformationException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            failure("Failure: com.sun.jdi.AbsentInformationException was not expected; ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /********** test core **********/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    protected void runTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         * Get to the top of main() to get everything loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        startToMain("NoLocInfoTarg");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        println("\n Abstract Methods:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // For abtsract methods allLineLocations() always returns empty List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        checkEmptyLineNumberTable("InterfaceNoLocInfoTarg", "instanceMeth");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        checkEmptyLineNumberTable("InterfaceNoLocInfoTarg", "instanceMeth1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        checkEmptyLineNumberTable("AbstractNoLocInfoTarg",  "instanceMeth");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        println("\n Native Methods:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        // For native methods allLineLocations() always returns empty List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        checkEmptyLineNumberTable("NoLocInfoTarg", "staticNativeMeth");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        checkEmptyLineNumberTable("NoLocInfoTarg", "instanceNativeMeth");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        println("\n Non-Abstract Methods of Abstract class:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        checkLineNumberTable("AbstractNoLocInfoTarg", "<init>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        checkLineNumberTable("AbstractNoLocInfoTarg", "instanceMeth1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        println("\n Methods of Non-Abstract class:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        checkLineNumberTable("NoLocInfoTarg", "<init>"); // default constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        checkLineNumberTable("NoLocInfoTarg", "main");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        checkLineNumberTable("NoLocInfoTarg", "instanceMeth");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        checkLineNumberTable("NoLocInfoTarg", "instanceMeth1"); // inherited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        checkLineNumberTable("NoLocInfoTarg", "voidInstanceMeth");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
         * resume until the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        listenUntilVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         * deal with results of test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         * if anything has called failure("foo") testFailed will be true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (!testFailed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            println("NoLocInfoTest: passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw new Exception("NoLocInfoTest: failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
}