Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/ArrayReference/setValue/setvalue002.java
41161 views
/*1* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324package nsk.jdi.ArrayReference.setValue;2526import nsk.share.*;27import nsk.share.jpda.*;28import nsk.share.jdi.*;2930import com.sun.jdi.*;31import java.io.*;3233public class setvalue002 {34final static int MIN_INDEX = -50;35final static int MAX_INDEX = 51;36final static String FIELD_NAME[][] = {37{"z1", "5", "boolean"},38{"b1", "5", "byte"},39{"c1", "6", "char"},40{"d1", "1", "double"},41{"f1", "1", "float"},42{"i1", "10", "int"},43{"l1", "2", "long"},44{"r1", "5", "short"},4546{"lF1", "0", "long"},47{"lP1", "2", "long"},48{"lU1", "3", "long"},49{"lR1", "4", "long"},50{"lT1", "5", "long"},51{"lV1", "6", "long"}52};5354private static Log log;55private final static String prefix = "nsk.jdi.ArrayReference.setValue.";56private final static String className = "setvalue002";57private final static String debugerName = prefix + className;58private final static String debugeeName = debugerName + "a";59private final static String classToCheckName = prefix + "setvalue002aClassToCheck";6061public static void main(String argv[]) {62System.exit(95 + run(argv, System.out));63}6465public static int run(String argv[], PrintStream out) {66ArgumentHandler argHandler = new ArgumentHandler(argv);67log = new Log(out, argHandler);68Binder binder = new Binder(argHandler, log);69Debugee debugee = binder.bindToDebugee(debugeeName70+ (argHandler.verbose() ? " -verbose" : ""));71IOPipe pipe = debugee.createIOPipe();72boolean testFailed = false;73Field fieldBoolean = null;74Field fieldByte = null;75Field fieldChar = null;76Field fieldDouble = null;77Field fieldFloat = null;78Field fieldInt = null;79Field fieldLong = null;80Field fieldShort = null;81Value valueBoolean = null;82Value valueByte = null;83Value valueChar = null;84Value valueDouble = null;85Value valueFloat = null;86Value valueInt = null;87Value valueLong = null;88Value valueShort = null;8990// Connect with debugee and resume it91debugee.redirectStderr(out);92debugee.resume();93String line = pipe.readln();94if (line == null) {95log.complain("debuger FAILURE> UNEXPECTED debugee's signal - null");96return 2;97}98if (!line.equals("ready")) {99log.complain("debuger FAILURE> UNEXPECTED debugee's signal - "100+ line);101return 2;102}103else {104log.display("debuger> debugee's \"ready\" signal recieved.");105}106107ReferenceType refType = debugee.classByName(classToCheckName);108if (refType == null) {109log.complain("debuger FAILURE> Class " + classToCheckName110+ " not found.");111return 2;112}113114log.display("debuger> Total fields in debugee read: "115+ refType.allFields().size() + " total fields in debuger: "116+ FIELD_NAME.length + "\n");117118// Get all samples of primitive types Values to set it to the119// ArrayReference120try {121fieldBoolean = refType.fieldByName("z");122fieldByte = refType.fieldByName("b");123fieldChar = refType.fieldByName("c");124fieldDouble = refType.fieldByName("d");125fieldFloat = refType.fieldByName("f");126fieldInt = refType.fieldByName("i");127fieldLong = refType.fieldByName("l");128fieldShort = refType.fieldByName("r");129} catch (ClassNotPreparedException e) {130log.complain("debuger FAILURE> Cannot get field by name.");131log.complain("debuger FAILURE> Exception: " + e);132log.complain("debuger FAILURE> boolean is " + fieldBoolean);133log.complain("debuger FAILURE> byte is " + fieldByte);134log.complain("debuger FAILURE> char is " + fieldChar);135log.complain("debuger FAILURE> double is " + fieldDouble);136log.complain("debuger FAILURE> float is " + fieldFloat);137log.complain("debuger FAILURE> int is " + fieldInt);138log.complain("debuger FAILURE> long is " + fieldLong);139log.complain("debuger FAILURE> short is " + fieldShort);140return 2;141} catch (ObjectCollectedException e) {142log.complain("debuger FAILURE> Cannot get field by name.");143log.complain("debuger FAILURE> Exception: " + e);144log.complain("debuger FAILURE> boolean is " + fieldBoolean);145log.complain("debuger FAILURE> byte is " + fieldByte);146log.complain("debuger FAILURE> char is " + fieldChar);147log.complain("debuger FAILURE> double is " + fieldDouble);148log.complain("debuger FAILURE> float is " + fieldFloat);149log.complain("debuger FAILURE> int is " + fieldInt);150log.complain("debuger FAILURE> long is " + fieldLong);151log.complain("debuger FAILURE> short is " + fieldShort);152return 2;153}154log.display("debuger> Got sample fields for primitive types.");155log.display("debuger> boolean is " + fieldBoolean);156log.display("debuger> byte is " + fieldByte);157log.display("debuger> char is " + fieldChar);158log.display("debuger> double is " + fieldDouble);159log.display("debuger> float is " + fieldFloat);160log.display("debuger> int is " + fieldInt);161log.display("debuger> long is " + fieldLong);162log.display("debuger> short is " + fieldShort + "\n");163164if ((fieldBoolean == null) || (fieldByte == null) ||165(fieldChar == null) || (fieldDouble == null) ||166(fieldFloat == null) || (fieldInt == null) ||167(fieldLong == null) || (fieldShort == null)) {168log.complain("debuger FAILURE> Cannot find field in debuggee.");169return 2;170}171172try {173valueBoolean = refType.getValue(fieldBoolean);174valueByte = refType.getValue(fieldByte);175valueChar = refType.getValue(fieldChar);176valueDouble = refType.getValue(fieldDouble);177valueFloat = refType.getValue(fieldFloat);178valueInt = refType.getValue(fieldInt);179valueLong = refType.getValue(fieldLong);180valueShort = refType.getValue(fieldShort);181} catch (IllegalArgumentException e) {182log.complain("debuger FAILURE> Cannot get values for fields.");183log.complain("debuger FAILURE> Exception: " + e);184log.complain("debuger FAILURE> boolean is " + fieldBoolean);185log.complain("debuger FAILURE> byte is " + fieldByte);186log.complain("debuger FAILURE> char is " + fieldChar);187log.complain("debuger FAILURE> double is " + fieldDouble);188log.complain("debuger FAILURE> float is " + fieldFloat);189log.complain("debuger FAILURE> int is " + fieldInt);190log.complain("debuger FAILURE> long is " + fieldLong);191log.complain("debuger FAILURE> short is " + fieldShort);192return 2;193} catch (ObjectCollectedException e) {194log.complain("debuger FAILURE> Cannot get values for fields.");195log.complain("debuger FAILURE> Exception: " + e);196log.complain("debuger FAILURE> boolean is " + fieldBoolean);197log.complain("debuger FAILURE> byte is " + fieldByte);198log.complain("debuger FAILURE> char is " + fieldChar);199log.complain("debuger FAILURE> double is " + fieldDouble);200log.complain("debuger FAILURE> float is " + fieldFloat);201log.complain("debuger FAILURE> int is " + fieldInt);202log.complain("debuger FAILURE> long is " + fieldLong);203log.complain("debuger FAILURE> short is " + fieldShort);204return 2;205}206log.display("debuger> Got sample values for primitive types.");207log.display("debuger> boolean is " + valueBoolean);208log.display("debuger> byte is " + valueByte);209log.display("debuger> char is " + valueChar);210log.display("debuger> double is " + valueDouble);211log.display("debuger> float is " + valueFloat);212log.display("debuger> int is " + valueInt);213log.display("debuger> long is " + valueLong);214log.display("debuger> short is " + valueShort + "\n");215216// Check all array fields from debugee217for (int i = 0; i < FIELD_NAME.length; i++) {218Field field;219String name = FIELD_NAME[i][0];220Integer totalElements = Integer.valueOf(FIELD_NAME[i][1]);221String type = FIELD_NAME[i][2];222int lastElementIndex = totalElements.intValue() - 1;223Value value;224ArrayReference arrayRef;225Value valueToSet;226227// Get field from debuggee by name228try {229field = refType.fieldByName(name);230} catch (ClassNotPreparedException e) {231log.complain("debuger FAILURE 1> Can't get field by name "232+ name);233log.complain("debuger FAILURE 1> Exception: " + e);234testFailed = true;235continue;236} catch (ObjectCollectedException e) {237log.complain("debuger FAILURE 1> Can't get field by name "238+ name);239log.complain("debuger FAILURE 1> Exception: " + e);240testFailed = true;241continue;242}243log.display("debuger> " + i + " field " + field + " read.");244245// Get field's value246try {247value = refType.getValue(field);248} catch (IllegalArgumentException e) {249log.complain("debuger FAILURE 2> Cannot get value for field "250+ name);251log.complain("debuger FAILURE 2> Exception: " + e);252testFailed = true;253continue;254} catch (ObjectCollectedException e) {255log.complain("debuger FAILURE 2> Cannot get value for field "256+ name);257log.complain("debuger FAILURE 2> Exception: " + e);258testFailed = true;259continue;260}261log.display("debuger> " + i + " field value is " + value);262263// Cast to ArrayReference. All fields in debugee are264// arrays, so ClassCastException should not be thrown265try {266arrayRef = (ArrayReference)value;267} catch (ClassCastException e) {268log.complain("debuger FAILURE 3> Cannot cast value for field "269+ name + " to ArrayReference.");270log.complain("debuger FAILURE 3> Exception: " + e);271testFailed = true;272continue;273}274275// Prepare Value to set276if (type.equals("boolean")) {277valueToSet = (Value)valueBoolean;278} else if (type.equals("byte")) {279valueToSet = (Value)valueByte;280} else if (type.equals("char")) {281valueToSet = (Value)valueChar;282} else if (type.equals("double")) {283valueToSet = (Value)valueDouble;284} else if (type.equals("float")) {285valueToSet = (Value)valueFloat;286} else if (type.equals("int")) {287valueToSet = (Value)valueInt;288} else if (type.equals("long")) {289valueToSet = (Value)valueLong;290} else if (type.equals("short")) {291valueToSet = (Value)valueShort;292} else {293log.complain("debuger FAILURE 4> Unexpected type: " + type);294testFailed = true;295continue;296}297298// Try to set value by index from MIN_INDEX to -1 and from299// arrayRef.length() to MAX_INDEX300for (int j = MIN_INDEX; j < MAX_INDEX; j++) {301if ( (j < 0) || (j > lastElementIndex) ) {302303// Set the Value, IndexOutOfBoundsException is expected304try {305arrayRef.setValue(j, valueToSet);306log.complain("debuger FAILURE 5> IndexOutOfBoundsException"307+ "is not thrown. ");308testFailed = true;309} catch (ObjectCollectedException e) {310log.complain("debuger FAILURE 5> Cannot set value "311+ valueToSet + " to " + j + " index of "312+ "field " + name);313log.complain("debuger FAILURE 5> Exception: " + e);314testFailed = true;315} catch (IndexOutOfBoundsException e) {316log.display("debuger> " + i + " field: cannot set "317+ "value " + valueToSet + " with index "318+ j + ". Expected exception: " + e);319} catch (InvalidTypeException e) {320log.complain("debuger FAILURE 5> Cannot set value "321+ valueToSet + " to " + j + " index of "322+ "field " + name);323log.complain("debuger FAILURE 5> Exception: " + e);324testFailed = true;325} catch (ClassNotLoadedException e) {326log.complain("debuger FAILURE 5> Cannot set value "327+ valueToSet + " to " + j + " index of "328+ "field " + name);329log.complain("debuger FAILURE 5> Exception: " + e);330testFailed = true;331} catch (VMMismatchException e) {332log.complain("debuger FAILURE 5> Cannot set value "333+ valueToSet + " to " + j + " index of "334+ "field " + name);335log.complain("debuger FAILURE 5> Exception: " + e);336testFailed = true;337}338}339}340log.display("debuger> " + i + " field checked.\n");341}342343pipe.println("quit");344debugee.waitFor();345int status = debugee.getStatus();346if (testFailed) {347log.complain("debuger FAILURE> TEST FAILED");348return 2;349} else {350if (status == 95) {351log.display("debuger> expected Debugee's exit "352+ "status - " + status);353return 0;354} else {355log.complain("debuger FAILURE> UNEXPECTED Debugee's exit "356+ "status (not 95) - " + status);357return 2;358}359}360}361}362363364