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

org.jsimpledb.parse.expr.EmptyArrayNode Maven / Gradle / Ivy


/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package org.jsimpledb.parse.expr;

import java.lang.reflect.Array;
import java.util.List;

import org.jsimpledb.parse.ParseSession;
import org.jsimpledb.parse.ParseUtil;

/**
 * Node representing an "empty" array instantiation expression, i.e., with dimensions but no literal values.
 */
public class EmptyArrayNode extends AbstractArrayNode {

    private final List dimensionList;

    /**
     * Constructor.
     *
     * @param baseTypeNode array base type class node
     * @param dimensionList list containing the sizes of each array dimension
     */
    public EmptyArrayNode(ClassNode baseTypeNode, List dimensionList) {
        super(baseTypeNode, dimensionList.size());
        this.dimensionList = dimensionList;
    }

    @Override
    public Value evaluate(final ParseSession session) {
        final Class elemType = ParseUtil.getArrayClass(this.getBaseType(session), this.numDimensions - 1);
        return new ConstValue(EmptyArrayNode.createEmpty(session, elemType, this.dimensionList));
    }

    private static Object createEmpty(ParseSession session, Class elemType, List dims) {
        final int length = dims.get(0).evaluate(session).checkIntegral(session, "array creation");
        final Object array = Array.newInstance(elemType, length);
        final List remainingDims = dims.subList(1, dims.size());
        if (!remainingDims.isEmpty() && remainingDims.get(0) != null) {
            for (int i = 0; i < length; i++)
                Array.set(array, i, EmptyArrayNode.createEmpty(session, elemType.getComponentType(), remainingDims));
        }
        return array;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy