Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/ArrayReference/setValue/setvalue001.java
41162 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 setvalue001 {34final static int HALF = 9;35final static String FIELD_NAME[][] = {36{"z1", "boolean"},37{"b1", "byte"},38{"c1", "char"},39{"d1", "double"},40{"f1", "float"},41{"i1", "int"},42{"l1", "long"},43{"r1", "short"},4445{"lF1", "long"},46{"lP1", "long"},47{"lU1", "long"},48{"lR1", "long"},49{"lT1", "long"},50{"lV1", "long"}51};5253private static Log log;54private final static String prefix = "nsk.jdi.ArrayReference.setValue.";55private final static String className = "setvalue001";56private final static String debugerName = prefix + className;57private final static String debugeeName = debugerName + "a";58private final static String classToCheckName = prefix + "setvalue001aClassToCheck";5960public static void main(String argv[]) {61System.exit(95 + run(argv, System.out));62}6364public static int run(String argv[], PrintStream out) {65ArgumentHandler argHandler = new ArgumentHandler(argv);66log = new Log(out, argHandler);67Binder binder = new Binder(argHandler, log);68Debugee debugee = binder.bindToDebugee(debugeeName69+ (argHandler.verbose() ? " -verbose" : ""));70IOPipe pipe = debugee.createIOPipe();71boolean testFailed = false;7273// Connect with debugee and resume it74debugee.redirectStderr(out);75debugee.resume();76String line = pipe.readln();77if (line == null) {78log.complain("debuger FAILURE> UNEXPECTED debugee's signal - null");79return 2;80}81if (!line.equals("ready")) {82log.complain("debuger FAILURE> UNEXPECTED debugee's signal - "83+ line);84return 2;85}86else {87log.display("debuger> debugee's \"ready\" signal recieved.");88}8990ReferenceType refType = debugee.classByName(classToCheckName);91if (refType == null) {92log.complain("debuger FAILURE> Class " + classToCheckName93+ " not found.");94return 2;95}96log.display("debuger> Total fields in debugee read: "97+ refType.allFields().size() + " total fields in debuger: "98+ FIELD_NAME.length + "\n");99100// Check all array fields from debugee101for (int i = 0; i < FIELD_NAME.length; i++) {102Field field;103String name = FIELD_NAME[i][0];104String realType = FIELD_NAME[i][1];105Value value;106ArrayReference arrayRef;107108// Get field from debuggee by name109try {110field = refType.fieldByName(name);111} catch (ClassNotPreparedException e) {112log.complain("debuger FAILURE 1> Can't get field by name "113+ name);114log.complain("debuger FAILURE 1> Exception: " + e);115testFailed = true;116continue;117} catch (ObjectCollectedException e) {118log.complain("debuger FAILURE 1> Can't get field by name "119+ name);120log.complain("debuger FAILURE 1> Exception: " + e);121testFailed = true;122continue;123}124log.display("debuger> " + i + " field " + field + " read.");125126// Get field's value127try {128value = refType.getValue(field);129} catch (IllegalArgumentException e) {130log.complain("debuger FAILURE 2> Cannot get value for field "131+ name);132log.complain("debuger FAILURE 2> Exception: " + e);133testFailed = true;134continue;135} catch (ObjectCollectedException e) {136log.complain("debuger FAILURE 2> Cannot get value for field "137+ name);138log.complain("debuger FAILURE 2> Exception: " + e);139testFailed = true;140continue;141}142log.display("debuger> " + i + " field value is " + value);143144// Cast to ArrayReference. All fields in debugee are145// arrays, so ClassCastException should not be thrown146try {147arrayRef = (ArrayReference)value;148} catch (ClassCastException e) {149log.complain("debuger FAILURE 3> Cannot cast value for field "150+ name + " to ArrayReference.");151log.complain("debuger FAILURE 3> Exception: " + e);152testFailed = true;153continue;154}155156for (int j = 0; j < HALF; j++) {157int indexSample = j + HALF;158Value sample;159Value valueNew;160161// Get Value from sample-area (9..17 elements)162try {163sample = arrayRef.getValue(indexSample);164} catch (ObjectCollectedException e) {165log.complain("debuger FAILURE 4> Cannot get " + indexSample166+ " value from field " + name);167log.complain("debuger FAILURE 4> Exception: " + e);168testFailed = true;169continue;170} catch (IndexOutOfBoundsException e) {171log.complain("debuger FAILURE 4> Cannot get " + indexSample172+ " value from field " + name);173log.complain("debuger FAILURE 4> Exception: " + e);174testFailed = true;175continue;176}177log.display("debuger> " + i + " field: sample from index "178+ indexSample + " is " + sample);179180// Set the Sample to the correspondent index181// Any exception means that the test fails182try {183arrayRef.setValue(j, sample);184} catch (ObjectCollectedException e) {185log.complain("debuger FAILURE 5> Cannot set value " + sample186+ " to " + j + " index of field " + name);187log.complain("debuger FAILURE 5> Exception: " + e);188testFailed = true;189continue;190} catch (IndexOutOfBoundsException e) {191log.complain("debuger FAILURE 5> Cannot set value " + sample192+ " to " + j + " index of field " + name);193log.complain("debuger FAILURE 5> Exception: " + e);194testFailed = true;195continue;196} catch (InvalidTypeException e) {197log.complain("debuger FAILURE 5> Cannot set value " + sample198+ " to " + j + " index of field " + name);199log.complain("debuger FAILURE 5> Exception: " + e);200testFailed = true;201continue;202} catch (ClassNotLoadedException e) {203log.complain("debuger FAILURE 5> Cannot set value " + sample204+ " to " + j + " index of field " + name);205log.complain("debuger FAILURE 5> Exception: " + e);206testFailed = true;207continue;208} catch (VMMismatchException e) {209log.complain("debuger FAILURE 5> Cannot set value " + sample210+ " to " + j + " index of field " + name);211log.complain("debuger FAILURE 5> Exception: " + e);212testFailed = true;213continue;214}215log.display("debuger> " + i + " field: sample " + sample216+ " has been set to index " + j);217218219// Get the Value from the correspondent index220try {221valueNew = arrayRef.getValue(j);222} catch (ObjectCollectedException e) {223log.complain("debuger FAILURE 6> Cannot get " + j + " value"224+ " from field " + name);225log.complain("debuger FAILURE 6> Exception: " + e);226testFailed = true;227continue;228} catch (IndexOutOfBoundsException e) {229log.complain("debuger FAILURE 6> Cannot get " + j + " value"230+ " from field " + name);231log.complain("debuger FAILURE 6> Exception: " + e);232testFailed = true;233continue;234}235log.display("debuger> " + i + " field: element from index " + j236+ " is " + valueNew);237238// Check the Value that has been read with the sample239if (realType.equals("boolean")) {240241///////////////////// Check boolean[] /////////////////////242BooleanValue boolValue;243boolean boolSample;244boolean boolNew;245246// Cast sample and read Value to primitive type247try {248boolValue = (BooleanValue)sample;249} catch (ClassCastException e) {250log.complain("debuger FAILURE Z1> Cannot cast to "251+ "boolean sample.");252log.complain("debuger FAILURE Z1> Exception: " + e);253testFailed = true;254continue;255}256boolSample = boolValue.value();257try {258boolValue = (BooleanValue)valueNew;259} catch (ClassCastException e) {260log.complain("debuger FAILURE Z2> Cannot cast to "261+ "boolean read value.");262log.complain("debuger FAILURE Z2> Exception: " + e);263testFailed = true;264continue;265}266boolNew = boolValue.value();267268// Check two primitive values269if (boolNew != boolSample) {270log.complain("debuger FAILURE Z3> " + j + " element "271+ "of array " + name + " was expected "272+ boolSample + ", but returned " + boolNew);273testFailed = true;274} else {275log.display("debuger> " + i + " field: primitive "276+ "sample is " + boolSample + ", primitive "277+ " read value is " + boolNew);278}279} else if (realType.equals("byte")) {280281///////////////////// Check byte[] /////////////////////282ByteValue byteValue;283byte byteSample;284byte byteNew;285286// Cast sample and read Value to primitive type287try {288byteValue = (ByteValue)sample;289} catch (ClassCastException e) {290log.complain("debuger FAILURE B1> Cannot cast to "291+ "byte sample.");292log.complain("debuger FAILURE B1> Exception: " + e);293testFailed = true;294continue;295}296byteSample = byteValue.value();297try {298byteValue = (ByteValue)valueNew;299} catch (ClassCastException e) {300log.complain("debuger FAILURE B2> Cannot cast to "301+ "byte read value.");302log.complain("debuger FAILURE B2> Exception: " + e);303testFailed = true;304continue;305}306byteNew = byteValue.value();307308// Check two primitive values309if (byteNew != byteSample) {310log.complain("debuger FAILURE B3> " + j + " element "311+ "of array " + name + " was expected "312+ byteSample + ", but returned " + byteNew);313testFailed = true;314} else {315log.display("debuger> " + i + " field: primitive "316+ "sample is " + byteSample + ", primitive "317+ "read value is " + byteNew);318}319} else if (realType.equals("char")) {320321///////////////////// Check char[] /////////////////////322CharValue charValue;323char charSample;324char charNew;325326// Cast sample and read Value to primitive type327try {328charValue = (CharValue)sample;329} catch (ClassCastException e) {330log.complain("debuger FAILURE C1> Cannot cast to "331+ "char sample.");332log.complain("debuger FAILURE C1> Exception: " + e);333testFailed = true;334continue;335}336charSample = charValue.value();337try {338charValue = (CharValue)valueNew;339} catch (ClassCastException e) {340log.complain("debuger FAILURE C2> Cannot cast to "341+ "char read value.");342log.complain("debuger FAILURE C2> Exception: " + e);343testFailed = true;344continue;345}346charNew = charValue.value();347348// Check two primitive values349if (charNew != charSample) {350log.complain("debuger FAILURE C3> " + j + " element "351+ "of array " + name + " was expected "352+ charSample + ", but returned " + charNew);353testFailed = true;354} else {355log.display("debuger> " + i + " field: primitive "356+ "sample is " + charSample + ", primitive "357+ "read value is " + charNew);358}359} else if (realType.equals("int")) {360361///////////////////// Check int[] /////////////////////362IntegerValue intValue;363int intSample;364int intNew;365366// Cast sample and read Value to primitive type367try {368intValue = (IntegerValue)sample;369} catch (ClassCastException e) {370log.complain("debuger FAILURE I1> Cannot cast to "371+ "int sample.");372log.complain("debuger FAILURE I1> Exception: " + e);373testFailed = true;374continue;375}376intSample = intValue.value();377try {378intValue = (IntegerValue)valueNew;379} catch (ClassCastException e) {380log.complain("debuger FAILURE I2> Cannot cast to "381+ "int read value.");382log.complain("debuger FAILURE I2> Exception: " + e);383testFailed = true;384continue;385}386intNew = intValue.value();387388// Check two primitive values389if (intNew != intSample) {390log.complain("debuger FAILURE I3> " + j + " element "391+ "of array " + name + " was expected "392+ intSample + ", but returned " + intNew);393testFailed = true;394} else {395log.display("debuger> " + i + " field: primitive "396+ "sample is " + intSample + ", primitive "397+ "read value is " + intNew);398}399} else if (realType.equals("long")) {400401///////////////////// Check long[] /////////////////////402LongValue longValue;403long longSample;404long longNew;405406// Cast sample and read Value to primitive type407try {408longValue = (LongValue)sample;409} catch (ClassCastException e) {410log.complain("debuger FAILURE L1> Cannot cast to "411+ "long sample.");412log.complain("debuger FAILURE L1> Exception: " + e);413testFailed = true;414continue;415}416longSample = longValue.value();417try {418longValue = (LongValue)valueNew;419} catch (ClassCastException e) {420log.complain("debuger FAILURE L2> Cannot cast to "421+ "long read value.");422log.complain("debuger FAILURE L2> Exception: " + e);423testFailed = true;424continue;425}426longNew = longValue.value();427428// Check two primitive values429if (longNew != longSample) {430log.complain("debuger FAILURE L3> " + j + " element "431+ "of array " + name + " was expected "432+ longSample + ", but returned " + longNew);433testFailed = true;434} else {435log.display("debuger> " + i + " field: primitive "436+ "sample is " + longSample + ", primitive "437+ "read value is " + longNew);438}439} else if (realType.equals("short")) {440441///////////////////// Check short[] /////////////////////442ShortValue shortValue;443short shortSample;444short shortNew;445446// Cast sample and read Value to primitive type447try {448shortValue = (ShortValue)sample;449} catch (ClassCastException e) {450log.complain("debuger FAILURE R1> Cannot cast to "451+ "short sample.");452log.complain("debuger FAILURE R1> Exception: " + e);453testFailed = true;454continue;455}456shortSample = shortValue.value();457try {458shortValue = (ShortValue)valueNew;459} catch (ClassCastException e) {460log.complain("debuger FAILURE R2> Cannot cast to "461+ "short read value.");462log.complain("debuger FAILURE R2> Exception: " + e);463testFailed = true;464continue;465}466shortNew = shortValue.value();467468// Check two primitive values469if (shortNew != shortSample) {470log.complain("debuger FAILURE R3> " + j + " element "471+ "of array " + name + " was expected "472+ shortSample + ", but returned "473+ shortNew);474testFailed = true;475} else {476log.display("debuger> " + i + " field: primitive "477+ "sample is " + shortSample + ", primitive "478+ "read value is " + shortNew);479}480} else if (realType.equals("double")) {481482///////////////////// Check double[] /////////////////////483DoubleValue dblValue;484Double dblSample;485Double dblNew;486487// Cast sample and read Value to primitive type488try {489dblValue = (DoubleValue)sample;490} catch (ClassCastException e) {491log.complain("debuger FAILURE D1> Cannot cast to "492+ "double sample.");493log.complain("debuger FAILURE D1> Exception: " + e);494testFailed = true;495continue;496}497dblSample = Double.valueOf(dblValue.value());498try {499dblValue = (DoubleValue)valueNew;500} catch (ClassCastException e) {501log.complain("debuger FAILURE D2> Cannot cast to "502+ "double read value.");503log.complain("debuger FAILURE D2> Exception: " + e);504testFailed = true;505continue;506}507dblNew = Double.valueOf(dblValue.value());508509// Check two primitive values510if (!dblNew.equals(dblSample)) {511log.complain("debuger FAILURE D3> " + j + " element "512+ "of array " + name + " was expected "513+ dblSample + ", but returned " + dblNew);514testFailed = true;515} else {516log.display("debuger> " + i + " field: primitive "517+ "sample is " + dblSample + ", primitive "518+ "read value is " + dblNew);519}520} else if (realType.equals("float")) {521522///////////////////// Check float[] /////////////////////523FloatValue fltValue;524Float fltSample;525Float fltNew;526527// Cast sample and read Value to primitive type528try {529fltValue = (FloatValue)sample;530} catch (ClassCastException e) {531log.complain("debuger FAILURE F1> Cannot cast to "532+ "float sample.");533log.complain("debuger FAILURE F1> Exception: " + e);534testFailed = true;535continue;536}537fltSample = Float.valueOf(fltValue.value());538try {539fltValue = (FloatValue)valueNew;540} catch (ClassCastException e) {541log.complain("debuger FAILURE F2> Cannot cast to "542+ "float read value.");543log.complain("debuger FAILURE F2> Exception: " + e);544testFailed = true;545continue;546}547fltNew = Float.valueOf(fltValue.value());548549// Check two primitive values550if (!fltNew.equals(fltSample)) {551log.complain("debuger FAILURE F3> " + j + " element "552+ "of array " + name + " was expected "553+ fltSample + ", but returned " + fltNew);554testFailed = true;555} else {556log.display("debuger> " + i + " field: primitive "557+ "sample is " + fltSample + ", primitive "558+ "read value is " + fltNew);559}560} else {561log.complain("debuger FAILURE 6> Unexpected type: "562+ realType);563testFailed = true;564break;565}566}567log.display("debuger> " + i + " field checked.\n");568}569570pipe.println("quit");571debugee.waitFor();572int status = debugee.getStatus();573if (testFailed) {574log.complain("debuger FAILURE> TEST FAILED");575return 2;576} else {577if (status == 95) {578log.display("debuger> expected Debugee's exit "579+ "status - " + status);580return 0;581} else {582log.complain("debuger FAILURE> UNEXPECTED Debugee's exit "583+ "status (not 95) - " + status);584return 2;585}586}587}588}589590591