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 // For array types, this is the array 028 private final ElementValue[] elementValues; 029 030 public ArrayElementValue(final int type, final ElementValue[] datums, final ConstantPool cpool) { 031 super(type, cpool); 032 if (type != ARRAY) { 033 throw new IllegalArgumentException("Only element values of type array can be built with this ctor - type specified: " + type); 034 } 035 this.elementValues = datums; 036 } 037 038 @Override 039 public void dump(final DataOutputStream dos) throws IOException { 040 dos.writeByte(super.getType()); // u1 type of value (ARRAY == '[') 041 dos.writeShort(elementValues.length); 042 for (final ElementValue evalue : elementValues) { 043 evalue.dump(dos); 044 } 045 } 046 047 public ElementValue[] getElementValuesArray() { 048 return elementValues; 049 } 050 051 public int getElementValuesArraySize() { 052 return elementValues.length; 053 } 054 055 @Override 056 public String stringifyValue() { 057 final StringBuilder sb = new StringBuilder(); 058 sb.append("["); 059 for (int i = 0; i < elementValues.length; i++) { 060 sb.append(elementValues[i].stringifyValue()); 061 if (i + 1 < elementValues.length) { 062 sb.append(","); 063 } 064 } 065 sb.append("]"); 066 return sb.toString(); 067 } 068 069 @Override 070 public String toString() { 071 final StringBuilder sb = new StringBuilder(); 072 sb.append("{"); 073 for (int i = 0; i < elementValues.length; i++) { 074 sb.append(elementValues[i]); 075 if (i + 1 < elementValues.length) { 076 sb.append(","); 077 } 078 } 079 sb.append("}"); 080 return sb.toString(); 081 } 082}