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