All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.ruebner.jvisualizer.backend.vm.values.ArrayReferenceValue Maven / Gradle / Ivy

Go to download

This project aims to provide an easy tool to visualize data flow and objects within the JVM. It is intended for students that are starting to learn programming with Java.

The newest version!
package me.ruebner.jvisualizer.backend.vm.values;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.sun.jdi.ArrayReference;
import com.sun.jdi.ClassNotLoadedException;
import me.ruebner.jvisualizer.backend.vm.json.ArrayElementSerializer;
import me.ruebner.jvisualizer.backend.vm.types.ArrayType;
import me.ruebner.jvisualizer.backend.vm.types.Type;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Represents an array value on the heap.
 */
public class ArrayReferenceValue extends ReferenceValue {

    /**
     * the list of element values of the array
     */
    @JsonProperty
    @JsonSerialize(using = ArrayElementSerializer.class)
    private final List elements = new ArrayList<>();

    /**
     * the type of the array elements
     */
    @JsonProperty
    private final Type elementType;

    /**
     * Creates a new array value object. To create a new array value object based on an
     * {@link ArrayReference} object, use {@link #fromJdi(ArrayReference)}.
     *
     * @param type the type of the array
     * @param uniqueId the unique id of this value
     * @param elements a list of elements of this array (the elements will be copied to a
     *         new internal list)
     * @param elementType the type of the array elements
     */
    private ArrayReferenceValue(ArrayType type, long uniqueId, List elements, Type elementType) {
        super(type, uniqueId);
        this.elements.addAll(elements);
        this.elementType = elementType;
    }

    /**
     * Creates a new array value object based on a {@link ArrayReference} object.
     *
     * @param jdiArrayReference the JDI array reference object
     * @return the new array value object based on {@code jdiArrayReference}
     */
    public static ArrayReferenceValue fromJdi(ArrayReference jdiArrayReference) {
        ArrayReferenceValue newValue;
        try {
            newValue = new ArrayReferenceValue(
                    (ArrayType) Type.fromJdi(jdiArrayReference.type()),
                    jdiArrayReference.uniqueID(),
                    new ArrayList<>(),
                    Type.fromJdi(((com.sun.jdi.ArrayType) jdiArrayReference.type()).componentType())
            );
        } catch (ClassNotLoadedException ex) {
            newValue = new ArrayReferenceValue(
                    (ArrayType) Type.fromJdi(jdiArrayReference.type()),
                    jdiArrayReference.uniqueID(),
                    new ArrayList<>(),
                    new Type(((com.sun.jdi.ArrayType) jdiArrayReference.type()).componentTypeName()) {
                    }
            );
        }
        newValue.cache();

        for (var value : jdiArrayReference.getValues()) {
            newValue.elements.add(Value.fromJdi(value));
        }

        return newValue;
    }

    /**
     * Retrieves an unmodifiable list of the elements of the array.
     *
     * @return the list of elements of the array
     */
    public List getElements() {
        return Collections.unmodifiableList(elements);
    }

    /**
     * Retrieves the size of the array.
     *
     * @return the size of the array
     */
    @JsonProperty
    public int getLength() {
        return elements.size();
    }

    /**
     * Retrieves the type of the array's elements.
     *
     * @return the type of the array's elements.
     */
    public Type getElementType() {
        return elementType;
    }

    @Override
    public String toString() {
        return "{"
                + elements.stream()
                .limit(7)
                .map(Value::toString)
                .collect(Collectors.joining(", "))
                + (elements.size() > 7 ? ", ..." : "")
                + "}";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy