Path: blob/master/src/jdk.jdi/share/classes/com/sun/tools/jdi/ArrayReferenceImpl.java
41161 views
/*1* Copyright (c) 1998, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.jdi;2627import java.util.ArrayList;28import java.util.List;2930import com.sun.jdi.ArrayReference;31import com.sun.jdi.ClassNotLoadedException;32import com.sun.jdi.InvalidTypeException;33import com.sun.jdi.Method;34import com.sun.jdi.Type;35import com.sun.jdi.Value;36import com.sun.jdi.VirtualMachine;3738public class ArrayReferenceImpl extends ObjectReferenceImpl39implements ArrayReference40{41int length = -1;4243ArrayReferenceImpl(VirtualMachine aVm, long aRef) {44super(aVm, aRef);45}4647protected ClassTypeImpl invokableReferenceType(Method method) {48// The method has to be a method on Object since49// arrays don't have methods nor any other 'superclasses'50// So, use the ClassTypeImpl for Object instead of51// the ArrayTypeImpl for the array itself.52return (ClassTypeImpl)method.declaringType();53}5455ArrayTypeImpl arrayType() {56return (ArrayTypeImpl)type();57}5859/**60* Return array length.61* Need not be synchronized since it cannot be provably stale.62*/63public int length() {64if(length == -1) {65try {66length = JDWP.ArrayReference.Length.67process(vm, this).arrayLength;68} catch (JDWPException exc) {69throw exc.toJDIException();70}71}72return length;73}7475public Value getValue(int index) {76List<Value> list = getValues(index, 1);77return list.get(0);78}7980public List<Value> getValues() {81return getValues(0, -1);82}8384/**85* Validate that the range to set/get is valid.86* length of -1 (meaning rest of array) has been converted87* before entry.88*/89private void validateArrayAccess(int index, int length) {90// because length can be computed from index,91// index must be tested first for correct error message92if ((index < 0) || (index > length())) {93throw new IndexOutOfBoundsException(94"Invalid array index: " + index);95}96if (length < 0) {97throw new IndexOutOfBoundsException(98"Invalid array range length: " + length);99}100if (index + length > length()) {101throw new IndexOutOfBoundsException(102"Invalid array range: " +103index + " to " + (index + length - 1));104}105}106107@SuppressWarnings("unchecked")108private static <T> T cast(Object x) {109return (T)x;110}111112public List<Value> getValues(int index, int length) {113if (length == -1) { // -1 means the rest of the array114length = length() - index;115}116validateArrayAccess(index, length);117if (length == 0) {118return new ArrayList<Value>();119}120121List<Value> vals;122try {123vals = cast(JDWP.ArrayReference.GetValues.process(vm, this, index, length).values);124} catch (JDWPException exc) {125throw exc.toJDIException();126}127128return vals;129}130131public void setValue(int index, Value value)132throws InvalidTypeException,133ClassNotLoadedException {134List<Value> list = new ArrayList<Value>(1);135list.add(value);136setValues(index, list, 0, 1);137}138139public void setValues(List<? extends Value> values)140throws InvalidTypeException,141ClassNotLoadedException {142setValues(0, values, 0, -1);143}144145public void setValues(int index, List<? extends Value> values,146int srcIndex, int length)147throws InvalidTypeException,148ClassNotLoadedException {149150if (length == -1) { // -1 means the rest of the array151// shorter of, the rest of the array and rest of152// the source values153length = Math.min(length() - index,154values.size() - srcIndex);155}156validateMirrorsOrNulls(values);157validateArrayAccess(index, length);158159if ((srcIndex < 0) || (srcIndex > values.size())) {160throw new IndexOutOfBoundsException(161"Invalid source index: " + srcIndex);162}163if (srcIndex + length > values.size()) {164throw new IndexOutOfBoundsException(165"Invalid source range: " +166srcIndex + " to " +167(srcIndex + length - 1));168}169170boolean somethingToSet = false;;171ValueImpl[] setValues = new ValueImpl[length];172173for (int i = 0; i < length; i++) {174ValueImpl value = (ValueImpl)values.get(srcIndex + i);175176try {177// Validate and convert if necessary178setValues[i] =179ValueImpl.prepareForAssignment(value,180new Component());181somethingToSet = true;182} catch (ClassNotLoadedException e) {183/*184* Since we got this exception,185* the component must be a reference type.186* This means the class has not yet been loaded187* through the defining class's class loader.188* If the value we're trying to set is null,189* then setting to null is essentially a190* no-op, and we should allow it without an191* exception.192*/193if (value != null) {194throw e;195}196}197}198if (somethingToSet) {199try {200JDWP.ArrayReference.SetValues.201process(vm, this, index, setValues);202} catch (JDWPException exc) {203throw exc.toJDIException();204}205}206}207208public String toString() {209return "instance of " + arrayType().componentTypeName() +210"[" + length() + "] (id=" + uniqueID() + ")";211}212213byte typeValueKey() {214return JDWP.Tag.ARRAY;215}216217void validateAssignment(ValueContainer destination)218throws InvalidTypeException, ClassNotLoadedException {219try {220super.validateAssignment(destination);221} catch (ClassNotLoadedException e) {222/*223* An array can be used extensively without the224* enclosing loader being recorded by the VM as an225* initiating loader of the array type. In addition, the226* load of an array class is fairly harmless as long as227* the component class is already loaded. So we relax the228* rules a bit and allow the assignment as long as the229* ultimate component types are assignable.230*/231boolean valid = false;232JNITypeParser destParser = new JNITypeParser(233destination.signature());234JNITypeParser srcParser = new JNITypeParser(235arrayType().signature());236int destDims = destParser.dimensionCount();237if (destDims <= srcParser.dimensionCount()) {238/*239* Remove all dimensions from the destination. Remove240* the same number of dimensions from the source.241* Get types for both and check to see if they are242* compatible.243*/244String destComponentSignature =245destParser.componentSignature(destDims);246Type destComponentType =247destination.findType(destComponentSignature);248String srcComponentSignature =249srcParser.componentSignature(destDims);250Type srcComponentType =251arrayType().findType(srcComponentSignature);252valid = ArrayTypeImpl.isComponentAssignable(destComponentType,253srcComponentType);254}255256if (!valid) {257throw new InvalidTypeException("Cannot assign " +258arrayType().name() +259" to " +260destination.typeName());261}262}263}264265/*266* Represents an array component to other internal parts of this267* implementation. This is not exposed at the JDI level. Currently,268* this class is needed only for type checking so it does not even269* reference a particular component - just a generic component270* of this array. In the future we may need to expand its use.271*/272class Component implements ValueContainer {273public Type type() throws ClassNotLoadedException {274return arrayType().componentType();275}276public String typeName() {277return arrayType().componentTypeName();278}279public String signature() {280return arrayType().componentSignature();281}282public Type findType(String signature) throws ClassNotLoadedException {283return arrayType().findType(signature);284}285}286}287288289