Source code

    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 *
    017 */
    018package org.apache.bcel.classfile;
    019
    020import java.io.DataOutputStream;
    021import java.io.IOException;
    022
    023/**
    024 * @since 6.0
    025 */
    026public class ArrayElementValue extends ElementValue
    027{
    028    // For array types, this is the array
    029    private final ElementValue[] evalues;
    030
    031    @Override
    032    public String toString()
    033    {
    034        final StringBuilder sb = new StringBuilder();
    035        sb.append("{");
    036        for (int i = 0; i < evalues.length; i++)
    037        {
    038            sb.append(evalues[i]);
    039            if ((i + 1) < evalues.length) {
    040                sb.append(",");
    041            }
    042        }
    043        sb.append("}");
    044        return sb.toString();
    045    }
    046
    047    public ArrayElementValue(final int type, final ElementValue[] datums, final ConstantPool cpool)
    048    {
    049        super(type, cpool);
    050        if (type != ARRAY) {
    051            throw new RuntimeException(
    052                    "Only element values of type array can be built with this ctor - type specified: " + type);
    053        }
    054        this.evalues = datums;
    055    }
    056
    057    @Override
    058    public void dump(final DataOutputStream dos) throws IOException
    059    {
    060        dos.writeByte(super.getType()); // u1 type of value (ARRAY == '[')
    061        dos.writeShort(evalues.length);
    062        for (final ElementValue evalue : evalues) {
    063            evalue.dump(dos);
    064        }
    065    }
    066
    067    @Override
    068    public String stringifyValue()
    069    {
    070        final StringBuilder sb = new StringBuilder();
    071        sb.append("[");
    072        for (int i = 0; i < evalues.length; i++)
    073        {
    074            sb.append(evalues[i].stringifyValue());
    075            if ((i + 1) < evalues.length) {
    076                sb.append(",");
    077            }
    078        }
    079        sb.append("]");
    080        return sb.toString();
    081    }
    082
    083    public ElementValue[] getElementValuesArray()
    084    {
    085        return evalues;
    086    }
    087
    088    public int getElementValuesArraySize()
    089    {
    090        return evalues.length;
    091    }
    092}