1 /*
   2  * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  *  @test
  26  *  @summary Test Method.variables() and the like.
  27  *
  28  *  @author Robert Field
  29  *
  30  *  @modules jdk.jdi
  31  *  @run build JDIScaffold VMConnection
  32  *  @run compile -g Vars.java
  33  *  @run driver Vars
  34  */
  35 
  36 import com.sun.jdi.*;
  37 import com.sun.jdi.event.*;
  38 import java.util.*;
  39 
  40 /*
  41  * This class is internal
  42  */
  43 abstract class AbstractTestVars {
  44     abstract float test1(String blah, int i);
  45     native int test2(double k, boolean b);
  46     String test3(short sh, long lo) {
  47         String st = "roses";
  48         return st;
  49     }
  50 }
  51 
  52 /*
  53  * This class is internal
  54  */
  55 class TestVars extends AbstractTestVars {
  56     float test1(String blah, int i) {
  57         return (float)1.1;
  58     }
  59 
  60     void hi() {
  61         return;
  62     }
  63 
  64     public static void main(String[] args) throws Exception {
  65         new TestVars().hi();
  66         return;
  67     }
  68 }
  69 
  70 /*
  71  * "Vars" test runs TestVars and makes LocalVariable queries
  72  */
  73 public class Vars extends JDIScaffold {
  74     final String[] args;
  75 
  76     boolean failed = false;
  77 
  78     Vars(String args[]) {
  79         super();
  80         this.args = args;
  81     }
  82 
  83     public static void main(String[] args) throws Exception {
  84         new Vars(args).runTests();
  85     }
  86 
  87     static final int VARIABLES = 1;
  88     static final int BYNAME = 2;
  89     static final int ARGUMENTS = 3;
  90 
  91     String testCase(Method method, int which) {
  92         try {
  93             List vars;
  94             switch (which) {
  95                 case VARIABLES:
  96                     vars = method.variables();
  97                     break;
  98                 case BYNAME:
  99                     vars = method.variablesByName("st");
 100                     break;
 101                 case ARGUMENTS:
 102                     vars = method.arguments();
 103                     break;
 104                 default:
 105                     throw new InternalException("should not happen");
 106             }
 107             StringBuffer sb = new StringBuffer();
 108             for (Iterator it = vars.iterator(); it.hasNext(); ) {
 109                 LocalVariable lv = (LocalVariable)it.next();
 110                 if (sb.length() > 0) {
 111                     sb.append(",");
 112                 }
 113                 sb.append(lv.name());
 114             }
 115             return sb.toString();
 116         } catch (Exception exc) {
 117             String st = exc.getClass().getName();
 118             int inx = st.lastIndexOf('.');
 119             return st.substring(inx+1);
 120         }
 121     }
 122 
 123     /**
 124      * Sets failed if fails.
 125      */
 126     void test(Method method, int which, String name, String expected) {
 127         String got = testCase(method, which);
 128         if (got.equals(expected)) {
 129             System.out.println(name + ": got expected: " + got);
 130         } else {
 131             failed = true;
 132             System.out.println(name + ": ERROR expected: " + expected);
 133             System.out.println("      got: " + got);
 134         }
 135     }
 136 
 137     void test2(Method method, int which, String name, String expected, String expected2) {
 138         String got = testCase(method, which);
 139         if (got.equals(expected) || got.equals(expected2)) {
 140             System.out.println(name + ": got expected: " + got);
 141         } else {
 142             failed = true;
 143             System.out.println(name + ": ERROR expected: " + expected);
 144             System.out.println("      got: " + got);
 145         }
 146     }
 147 
 148     protected void runTests() throws Exception {
 149         List argList = new ArrayList(Arrays.asList(args));
 150         argList.add("TestVars");
 151         System.out.println("run args: " + argList);
 152         connect((String[])argList.toArray(args));
 153         waitForVMStart();
 154 
 155         /*
 156          * Get to a point where the classes are loaded.
 157          */
 158         BreakpointEvent bp = resumeTo("TestVars", "hi", "()V");
 159 
 160         /*
 161          * These classes should have no line numbers, except for
 162          * one in the implicit constructor.
 163          */
 164         ReferenceType rt = findReferenceType("AbstractTestVars");
 165         if (rt == null) {
 166             throw new Exception("AbstractTestVars: not loaded");
 167         }
 168         Method method = findMethod(rt, "test1", "(Ljava/lang/String;I)F");
 169         if (method == null) {
 170             throw new Exception("Method not found");
 171         }
 172         test(method, VARIABLES, "abstract/variables",
 173              "AbsentInformationException");
 174         test(method, BYNAME, "abstract/variablesByName",
 175              "AbsentInformationException");
 176         test(method, ARGUMENTS, "abstract/arguments",
 177              "AbsentInformationException");
 178 
 179         method = findMethod(rt, "test2", "(DZ)I");
 180         if (method == null) {
 181             throw new Exception("Method not found");
 182         }
 183         test(method, VARIABLES, "native/variables",
 184              "AbsentInformationException");
 185         test(method, BYNAME, "native/variablesByName",
 186              "AbsentInformationException");
 187         test(method, ARGUMENTS, "native/arguments",
 188              "AbsentInformationException");
 189 
 190         method = findMethod(rt, "test3", "(SJ)Ljava/lang/String;");
 191         if (method == null) {
 192             throw new Exception("Method not found");
 193         }
 194         // javac can put these in whatever order it desires.  hopper
 195         // does it one way and mantis another.
 196         test2(method, VARIABLES, "normal/variables", "sh,lo,st", "st,sh,lo");
 197         test(method, BYNAME, "normal/variablesByName", "st");
 198         test(method, ARGUMENTS, "normal/arguments", "sh,lo");
 199 
 200         // Allow application to complete
 201         resumeToVMDeath();
 202 
 203         if (failed) {
 204             throw new Exception("Vars: failed");
 205         } else {
 206             System.out.println("Vars: passed");
 207         }
 208     }
 209 }