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.generic; 019 020import java.io.DataOutputStream; 021import java.io.IOException; 022import java.util.ArrayList; 023import java.util.List; 024 025import org.apache.bcel.classfile.ArrayElementValue; 026import org.apache.bcel.classfile.ElementValue; 027 028/** 029 * @since 6.0 030 */ 031public class ArrayElementValueGen extends ElementValueGen { 032 // J5TODO: Should we make this an array or a list? A list would be easier to 033 // modify ... 034 private final List<ElementValueGen> evalues; 035 036 /** 037 * @param value 038 * @param cpool 039 */ 040 public ArrayElementValueGen(final ArrayElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) { 041 super(ARRAY, cpool); 042 evalues = new ArrayList<>(); 043 final ElementValue[] in = value.getElementValuesArray(); 044 for (final ElementValue element : in) { 045 evalues.add(ElementValueGen.copy(element, cpool, copyPoolEntries)); 046 } 047 } 048 049 public ArrayElementValueGen(final ConstantPoolGen cp) { 050 super(ARRAY, cp); 051 evalues = new ArrayList<>(); 052 } 053 054 public ArrayElementValueGen(final int type, final ElementValue[] datums, final ConstantPoolGen cpool) { 055 super(type, cpool); 056 if (type != ARRAY) { 057 throw new IllegalArgumentException("Only element values of type array can be built with this ctor - type specified: " + type); 058 } 059 this.evalues = new ArrayList<>(); 060 for (final ElementValue datum : datums) { 061 evalues.add(ElementValueGen.copy(datum, cpool, true)); 062 } 063 } 064 065 public void addElement(final ElementValueGen gen) { 066 evalues.add(gen); 067 } 068 069 @Override 070 public void dump(final DataOutputStream dos) throws IOException { 071 dos.writeByte(super.getElementValueType()); // u1 type of value (ARRAY == '[') 072 dos.writeShort(evalues.size()); 073 for (final ElementValueGen element : evalues) { 074 element.dump(dos); 075 } 076 } 077 078 /** 079 * Return immutable variant of this ArrayElementValueGen 080 */ 081 @Override 082 public ElementValue getElementValue() { 083 final ElementValue[] immutableData = new ElementValue[evalues.size()]; 084 int i = 0; 085 for (final ElementValueGen element : evalues) { 086 immutableData[i++] = element.getElementValue(); 087 } 088 return new ArrayElementValue(super.getElementValueType(), immutableData, getConstantPool().getConstantPool()); 089 } 090 091 public List<ElementValueGen> getElementValues() { 092 return evalues; 093 } 094 095 public int getElementValuesSize() { 096 return evalues.size(); 097 } 098 099 @Override 100 public String stringifyValue() { 101 final StringBuilder sb = new StringBuilder(); 102 sb.append("["); 103 String comma = ""; 104 for (final ElementValueGen element : evalues) { 105 sb.append(comma); 106 comma = ","; 107 sb.append(element.stringifyValue()); 108 } 109 sb.append("]"); 110 return sb.toString(); 111 } 112}