0N/A/*
2362N/A * Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.jdi;
0N/A
0N/Aimport com.sun.jdi.*;
0N/A
0N/Aimport java.util.List;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Arrays;
0N/Aimport java.util.Iterator;
0N/A
0N/Apublic class ArrayReferenceImpl extends ObjectReferenceImpl
0N/A implements ArrayReference
0N/A{
0N/A int length = -1;
0N/A
0N/A ArrayReferenceImpl(VirtualMachine aVm,long aRef) {
0N/A super(aVm,aRef);
0N/A }
0N/A
0N/A protected ClassTypeImpl invokableReferenceType(Method method) {
0N/A // The method has to be a method on Object since
0N/A // arrays don't have methods nor any other 'superclasses'
0N/A // So, use the ClassTypeImpl for Object instead of
0N/A // the ArrayTypeImpl for the array itself.
0N/A return (ClassTypeImpl)method.declaringType();
0N/A }
0N/A
0N/A ArrayTypeImpl arrayType() {
0N/A return (ArrayTypeImpl)type();
0N/A }
0N/A
0N/A /**
0N/A * Return array length.
0N/A * Need not be synchronized since it cannot be provably stale.
0N/A */
0N/A public int length() {
0N/A if(length == -1) {
0N/A try {
0N/A length = JDWP.ArrayReference.Length.
0N/A process(vm, this).arrayLength;
0N/A } catch (JDWPException exc) {
0N/A throw exc.toJDIException();
0N/A }
0N/A }
0N/A return length;
0N/A }
0N/A
0N/A public Value getValue(int index) {
0N/A List list = getValues(index, 1);
0N/A return (Value)list.get(0);
0N/A }
0N/A
0N/A public List<Value> getValues() {
0N/A return getValues(0, -1);
0N/A }
0N/A
0N/A /**
0N/A * Validate that the range to set/get is valid.
0N/A * length of -1 (meaning rest of array) has been converted
0N/A * before entry.
0N/A */
0N/A private void validateArrayAccess(int index, int length) {
0N/A // because length can be computed from index,
0N/A // index must be tested first for correct error message
0N/A if ((index < 0) || (index > length())) {
0N/A throw new IndexOutOfBoundsException(
0N/A "Invalid array index: " + index);
0N/A }
0N/A if (length < 0) {
0N/A throw new IndexOutOfBoundsException(
0N/A "Invalid array range length: " + length);
0N/A }
0N/A if (index + length > length()) {
0N/A throw new IndexOutOfBoundsException(
0N/A "Invalid array range: " +
0N/A index + " to " + (index + length - 1));
0N/A }
0N/A }
0N/A
0N/A @SuppressWarnings("unchecked")
0N/A private static <T> T cast(Object x) {
0N/A return (T)x;
0N/A }
0N/A
0N/A public List<Value> getValues(int index, int length) {
0N/A if (length == -1) { // -1 means the rest of the array
0N/A length = length() - index;
0N/A }
0N/A validateArrayAccess(index, length);
0N/A if (length == 0) {
0N/A return new ArrayList<Value>();
0N/A }
0N/A
0N/A List<Value> vals;
0N/A try {
0N/A vals = cast(JDWP.ArrayReference.GetValues.process(vm, this, index, length).values);
0N/A } catch (JDWPException exc) {
0N/A throw exc.toJDIException();
0N/A }
0N/A
0N/A return vals;
0N/A }
0N/A
0N/A public void setValue(int index, Value value)
0N/A throws InvalidTypeException,
0N/A ClassNotLoadedException {
0N/A List<Value> list = new ArrayList<Value>(1);
0N/A list.add(value);
0N/A setValues(index, list, 0, 1);
0N/A }
0N/A
0N/A public void setValues(List<? extends Value> values)
0N/A throws InvalidTypeException,
0N/A ClassNotLoadedException {
0N/A setValues(0, values, 0, -1);
0N/A }
0N/A
0N/A public void setValues(int index, List<? extends Value> values,
0N/A int srcIndex, int length)
0N/A throws InvalidTypeException,
0N/A ClassNotLoadedException {
0N/A
0N/A if (length == -1) { // -1 means the rest of the array
0N/A // shorter of, the rest of the array and rest of
0N/A // the source values
0N/A length = Math.min(length() - index,
0N/A values.size() - srcIndex);
0N/A }
0N/A validateMirrorsOrNulls(values);
0N/A validateArrayAccess(index, length);
0N/A
0N/A if ((srcIndex < 0) || (srcIndex > values.size())) {
0N/A throw new IndexOutOfBoundsException(
0N/A "Invalid source index: " + srcIndex);
0N/A }
0N/A if (srcIndex + length > values.size()) {
0N/A throw new IndexOutOfBoundsException(
0N/A "Invalid source range: " +
0N/A srcIndex + " to " +
0N/A (srcIndex + length - 1));
0N/A }
0N/A
0N/A boolean somethingToSet = false;;
0N/A ValueImpl[] setValues = new ValueImpl[length];
0N/A
0N/A for (int i = 0; i < length; i++) {
0N/A ValueImpl value = (ValueImpl)values.get(srcIndex + i);
0N/A
0N/A try {
0N/A // Validate and convert if necessary
0N/A setValues[i] =
0N/A ValueImpl.prepareForAssignment(value,
0N/A new Component());
0N/A somethingToSet = true;
0N/A } catch (ClassNotLoadedException e) {
0N/A /*
0N/A * Since we got this exception,
0N/A * the component must be a reference type.
0N/A * This means the class has not yet been loaded
0N/A * through the defining class's class loader.
0N/A * If the value we're trying to set is null,
0N/A * then setting to null is essentially a
0N/A * no-op, and we should allow it without an
0N/A * exception.
0N/A */
0N/A if (value != null) {
0N/A throw e;
0N/A }
0N/A }
0N/A }
0N/A if (somethingToSet) {
0N/A try {
0N/A JDWP.ArrayReference.SetValues.
0N/A process(vm, this, index, setValues);
0N/A } catch (JDWPException exc) {
0N/A throw exc.toJDIException();
0N/A }
0N/A }
0N/A }
0N/A
0N/A public String toString() {
0N/A return "instance of " + arrayType().componentTypeName() +
0N/A "[" + length() + "] (id=" + uniqueID() + ")";
0N/A }
0N/A
0N/A byte typeValueKey() {
0N/A return JDWP.Tag.ARRAY;
0N/A }
0N/A
0N/A void validateAssignment(ValueContainer destination)
0N/A throws InvalidTypeException, ClassNotLoadedException {
0N/A try {
0N/A super.validateAssignment(destination);
0N/A } catch (ClassNotLoadedException e) {
0N/A /*
0N/A * An array can be used extensively without the
0N/A * enclosing loader being recorded by the VM as an
0N/A * initiating loader of the array type. In addition, the
0N/A * load of an array class is fairly harmless as long as
0N/A * the component class is already loaded. So we relax the
0N/A * rules a bit and allow the assignment as long as the
0N/A * ultimate component types are assignable.
0N/A */
0N/A boolean valid = false;
0N/A JNITypeParser destParser = new JNITypeParser(
0N/A destination.signature());
0N/A JNITypeParser srcParser = new JNITypeParser(
0N/A arrayType().signature());
0N/A int destDims = destParser.dimensionCount();
0N/A if (destDims <= srcParser.dimensionCount()) {
0N/A /*
0N/A * Remove all dimensions from the destination. Remove
0N/A * the same number of dimensions from the source.
0N/A * Get types for both and check to see if they are
0N/A * compatible.
0N/A */
0N/A String destComponentSignature =
0N/A destParser.componentSignature(destDims);
0N/A Type destComponentType =
0N/A destination.findType(destComponentSignature);
0N/A String srcComponentSignature =
0N/A srcParser.componentSignature(destDims);
0N/A Type srcComponentType =
0N/A arrayType().findComponentType(srcComponentSignature);
0N/A valid = ArrayTypeImpl.isComponentAssignable(destComponentType,
0N/A srcComponentType);
0N/A }
0N/A
0N/A if (!valid) {
0N/A throw new InvalidTypeException("Cannot assign " +
0N/A arrayType().name() +
0N/A " to " +
0N/A destination.typeName());
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Represents an array component to other internal parts of this
0N/A * implementation. This is not exposed at the JDI level. Currently,
0N/A * this class is needed only for type checking so it does not even
0N/A * reference a particular component - just a generic component
0N/A * of this array. In the future we may need to expand its use.
0N/A */
0N/A class Component implements ValueContainer {
0N/A public Type type() throws ClassNotLoadedException {
0N/A return arrayType().componentType();
0N/A }
0N/A public String typeName() {
0N/A return arrayType().componentTypeName();
0N/A }
0N/A public String signature() {
0N/A return arrayType().componentSignature();
0N/A }
0N/A public Type findType(String signature) throws ClassNotLoadedException {
0N/A return arrayType().findComponentType(signature);
0N/A }
0N/A }
0N/A}