1 /*
   2  * Copyright (c) 2007, 2017, 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 //    THIS TEST IS LINE NUMBER SENSITIVE
  25 
  26 /**
  27  * @test
  28  * @bug 4490824
  29  * @summary JDI: provide arguments when no debug attributes present
  30  *
  31  * @author jjh
  32  *
  33  * @run build TestScaffold VMConnection TargetListener TargetAdapter
  34  * @run compile ArgumentValuesTest.java
  35  * @run driver ArgumentValuesTest
  36  */
  37 import com.sun.jdi.*;
  38 import com.sun.jdi.event.*;
  39 import com.sun.jdi.request.*;
  40 
  41 import java.util.*;
  42 
  43  /********** target program **********/
  44 
  45 class ArgumentValuesTarg {
  46     static char s_char1 = 'a';
  47     static byte s_byte1 = (byte) 146;
  48     static short s_short1 = (short) 28123;
  49     static int s_int1 = 3101246;
  50     static long s_long1 = 0x0123456789ABCDEFL;
  51     static float s_float1 = 2.3145f;
  52     static double s_double1 = 1.469d;
  53     static int s_iarray1[] = {1, 2, 3};
  54     static int s_marray1[][] = {{1, 2, 3}, {3, 4, 5}, null, {6, 7}};
  55     static String s_sarray1[] = {"abc", null, "def", "ghi"};
  56     static String s_string1 = "abcdef";
  57 
  58     static String s_string2 = "xy";
  59     static String s_string3 = "wz";
  60     static List<Integer> intList;
  61 
  62     public static void noArgs() {
  63         int index = 0;     // line NO_ARGS_LINE_1
  64     }
  65 
  66     public static void allArgs(char p_char, byte p_byte, short p_short,
  67                                int p_int, long p_long, float p_float,
  68                                double p_double, int p_iarray[], int p_marray[][],
  69                                String p_sarray1[], String p_string) {
  70         int index = 0;      // line ALL_ARGS_LINE_1
  71     }
  72 
  73     public static void varArgs(String ... p1) {
  74         int index = 0;     // line VAR_ARGS_LINE_1
  75     }
  76 
  77     public static void genericArgs(List<Integer> p1) {
  78         int index = 0;     // line GENERIC_ARGS_LINE_1
  79     }
  80 
  81     public void instanceMethod(char p_char, byte p_byte) {
  82         int index = 0;     // line INSTANCE_METHOD_LINE_1
  83     }
  84 
  85     public static void main(String[] args) {
  86         System.out.println("Howdy!");
  87         allArgs(
  88                 s_char1,   s_byte1,   s_short1,  s_int1,
  89                 s_long1,   s_float1,  s_double1, s_iarray1,
  90                 s_marray1, s_sarray1, s_string1);
  91 
  92         noArgs();
  93         varArgs(s_string1, s_string2, s_string3);
  94         ArgumentValuesTarg avt = new ArgumentValuesTarg();
  95         intList = new ArrayList<Integer>(10);
  96         intList.add(10);
  97         intList.add(20);
  98         genericArgs(intList);
  99 
 100         avt.instanceMethod(s_char1, s_byte1);
 101 
 102         System.out.println("Goodbye from ArgumentValuesTarg!");
 103     }
 104 }
 105 
 106  /********** test program **********/
 107 
 108 public class ArgumentValuesTest extends TestScaffold {
 109     static final int NO_ARGS_LINE_1 = 63;
 110     static final int ALL_ARGS_LINE_1 = 70;
 111     static final int VAR_ARGS_LINE_1 = 74;
 112     static final int GENERIC_ARGS_LINE_1 = 78;
 113     static final int INSTANCE_METHOD_LINE_1 = 82;
 114 
 115     // Must be in same order as args to allArgs(....)
 116     String fieldNames[] = {"s_char1",   "s_byte1",   "s_short1",  "s_int1",
 117                            "s_long1",   "s_float1",  "s_double1", "s_iarray1",
 118                            "s_marray1", "s_sarray1", "s_string1"};
 119 
 120     String fieldNamesVarArgs[] = {"s_string1", "s_string2", "s_string3"};
 121     String fieldNamesInstance[] = {"s_char1",   "s_byte1"};
 122 
 123     ReferenceType targetClass;
 124     ThreadReference mainThread;
 125 
 126     ArgumentValuesTest (String args[]) {
 127         super(args);
 128     }
 129 
 130     public static void main(String[] args)
 131         throws Exception
 132     {
 133         new ArgumentValuesTest (args).startTests();
 134     }
 135 
 136     /********** test core **********/
 137 
 138     protected void runTests()
 139         throws Exception
 140     {
 141         /*
 142          * Get to the top of main() to determine targetClass and mainThread
 143          */
 144         BreakpointEvent bpe = startToMain("ArgumentValuesTarg");
 145         targetClass = bpe.location().declaringType();
 146         mainThread = bpe.thread();
 147         EventRequestManager erm = vm().eventRequestManager();
 148 
 149 
 150         {
 151             System.out.println("----- Testing each type of arg");
 152             bpe = resumeTo("ArgumentValuesTarg", ALL_ARGS_LINE_1);
 153             StackFrame frame = bpe.thread().frame(0);
 154 
 155             Method mmm = frame.location().method();
 156             System.out.println("Arg types are: " + mmm.argumentTypeNames());
 157 
 158             List<Value> argVals = frame.getArgumentValues();
 159 
 160             if (argVals.size() != fieldNames.length) {
 161                 failure("failure: Varargs: expected length " + fieldNames.length +
 162                         " args, got: " + argVals);
 163             }
 164             for (int ii = 0; ii < argVals.size(); ii++) {
 165                 Value gotVal = argVals.get(ii);
 166 
 167                 Field theField = targetClass.fieldByName(fieldNames[ii]);
 168                 Value expectedVal = targetClass.getValue(theField);
 169                 System.out.println(fieldNames[ii] + ": gotVal = " + gotVal +
 170                                    ", expected = " + expectedVal);
 171                 //System.out.println(gotVal.getClass() + ", " + expectedVal.getClass());
 172                 if (!gotVal.equals(expectedVal)) {
 173                     failure("     failure: gotVal != expected");
 174                 }
 175             }
 176         }
 177 
 178         // a method with no params
 179         {
 180             System.out.println("----- Testing no args");
 181             bpe = resumeTo("ArgumentValuesTarg", NO_ARGS_LINE_1);
 182             StackFrame frame = bpe.thread().frame(0);
 183 
 184             Method mmm = frame.location().method();
 185             System.out.println("Arg types are: " + mmm.argumentTypeNames());
 186 
 187             List<Value> argVals = frame.getArgumentValues();
 188             if (argVals.size() == 0) {
 189                 System.out.println("Empty arg list ok");
 190             } else {
 191                 failure("failure: Expected empty val list, got: " + argVals);
 192             }
 193         }
 194 
 195         // var args.  3 Strings are passed in and they appear
 196         // as a String[3] in the method.
 197         {
 198             System.out.println("----- Testing var args");
 199             bpe = resumeTo("ArgumentValuesTarg", VAR_ARGS_LINE_1);
 200             StackFrame frame = bpe.thread().frame(0);
 201 
 202             Method mmm = frame.location().method();
 203             System.out.println("Arg types are: " + mmm.argumentTypeNames());
 204 
 205             List<Value> argVals = frame.getArgumentValues();
 206             if (argVals.size() != 1) {
 207                 failure("failure: Varargs: expected one arg, got: " + argVals);
 208             }
 209             argVals = ((ArrayReference)argVals.get(0)).getValues();
 210 
 211             if (argVals.size() != fieldNamesVarArgs.length) {
 212                 failure("failure: Varargs: expected length " + fieldNamesVarArgs.length +
 213                         " array elements, got: " + argVals);
 214             }
 215 
 216             for (int ii = 0; ii < argVals.size(); ii++) {
 217                 Value gotVal = argVals.get(ii);
 218 
 219                 Field theField = targetClass.fieldByName(fieldNamesVarArgs[ii]);
 220                 Value expectedVal = targetClass.getValue(theField);
 221                 System.out.println(fieldNamesVarArgs[ii] + ": gotVal = " + gotVal +
 222                                    ", expected = " + expectedVal);
 223                 //System.out.println(gotVal.getClass() + ", " + expectedVal.getClass());
 224                 if (!gotVal.equals(expectedVal)) {
 225                     failure("     failure: gotVal != expected");
 226                 }
 227             }
 228         }
 229 
 230         // a method with with one generic param
 231         {
 232             System.out.println("----- Testing generic args");
 233             bpe = resumeTo("ArgumentValuesTarg", GENERIC_ARGS_LINE_1);
 234             StackFrame frame = bpe.thread().frame(0);
 235 
 236             Method mmm = frame.location().method();
 237             System.out.println("Arg types are: " + mmm.argumentTypeNames());
 238 
 239             List<Value> argVals = frame.getArgumentValues();
 240             if (argVals.size() != 1) {
 241                 failure("failure: Expected one arg, got: " + argVals);
 242             } else {
 243                 Value gotVal = argVals.get(0);
 244 
 245                 Field theField = targetClass.fieldByName("intList");
 246                 Value expectedVal = targetClass.getValue(theField);
 247                 System.out.println("intList " + ": gotVal = " + gotVal +
 248                                    ", expected = " + expectedVal);
 249                 if (!gotVal.equals(expectedVal)) {
 250                     failure("failure: gotVal != expected");
 251                 }
 252             }
 253         }
 254 
 255         // test instance method call
 256         {
 257             System.out.println("----- Testing instance method call");
 258             bpe = resumeTo("ArgumentValuesTarg", INSTANCE_METHOD_LINE_1);
 259             StackFrame frame = bpe.thread().frame(0);
 260 
 261             Method mmm = frame.location().method();
 262             System.out.println("Arg types are: " + mmm.argumentTypeNames());
 263 
 264             List<Value> argVals = frame.getArgumentValues();
 265 
 266             if (argVals.size() != fieldNamesInstance.length) {
 267                 failure("failure: Varargs: expected length " + fieldNamesInstance.length +
 268                         " args, got: " + argVals);
 269             }
 270             for (int ii = 0; ii < argVals.size(); ii++) {
 271                 Value gotVal = argVals.get(ii);
 272 
 273                 Field theField = targetClass.fieldByName(fieldNamesInstance[ii]);
 274                 Value expectedVal = targetClass.getValue(theField);
 275                 System.out.println(fieldNamesInstance[ii] + ": gotVal = " + gotVal +
 276                                    ", expected = " + expectedVal);
 277                 //System.out.println(gotVal.getClass() + ", " + expectedVal.getClass());
 278                 if (!gotVal.equals(expectedVal)) {
 279                     failure("     failure: gotVal != expected");
 280                 }
 281             }
 282         }
 283 
 284 
 285         /*
 286          * resume the target listening for events
 287          */
 288         listenUntilVMDisconnect();
 289 
 290         /*
 291          * deal with results of test if anything has called failure("foo")
 292          * testFailed will be true
 293          */
 294         if (!testFailed) {
 295             println("ArgumentValuesTest: passed");
 296         } else {
 297             throw new Exception("ArgumentValuesTest: failed");
 298         }
 299     }
 300 }