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.generic; 020 021import java.io.DataOutputStream; 022import java.io.IOException; 023import java.util.ArrayList; 024import java.util.List; 025import java.util.stream.Collectors; 026 027import org.apache.bcel.classfile.ArrayElementValue; 028import org.apache.bcel.classfile.ElementValue; 029import org.apache.bcel.util.Args; 030import org.apache.commons.lang3.stream.Streams; 031 032/** 033 * Generates array element values in annotations. 034 * 035 * @since 6.0 036 */ 037public class ArrayElementValueGen extends ElementValueGen { 038 // J5TODO: Should we make this an array or a list? A list would be easier to 039 // modify ... 040 private final List<ElementValueGen> evalues; 041 042 /** 043 * Constructs an ArrayElementValueGen. 044 * 045 * @param value The array element value. 046 * @param cpool The constant pool generator. 047 * @param copyPoolEntries whether to copy pool entries. 048 */ 049 public ArrayElementValueGen(final ArrayElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) { 050 super(ARRAY, cpool); 051 evalues = new ArrayList<>(); 052 final ElementValue[] in = value.getElementValuesArray(); 053 for (final ElementValue element : in) { 054 evalues.add(copy(element, cpool, copyPoolEntries)); 055 } 056 } 057 058 /** 059 * Constructs an ArrayElementValueGen. 060 * 061 * @param cp The constant pool generator. 062 */ 063 public ArrayElementValueGen(final ConstantPoolGen cp) { 064 super(ARRAY, cp); 065 evalues = new ArrayList<>(); 066 } 067 068 /** 069 * Constructs an ArrayElementValueGen. 070 * 071 * @param type The type. 072 * @param elementValues The element values. 073 * @param cpool The constant pool generator. 074 */ 075 public ArrayElementValueGen(final int type, final ElementValue[] elementValues, final ConstantPoolGen cpool) { 076 super(type, cpool); 077 if (type != ARRAY) { 078 throw new IllegalArgumentException("Only element values of type array can be built with this ctor - type specified: " + type); 079 } 080 this.evalues = Streams.of(elementValues).map(e -> copy(e, cpool, true)).collect(Collectors.toList()); 081 } 082 083 /** 084 * Adds an element. 085 * 086 * @param gen The element value generator. 087 */ 088 public void addElement(final ElementValueGen gen) { 089 evalues.add(gen); 090 } 091 092 @Override 093 public void dump(final DataOutputStream dos) throws IOException { 094 dos.writeByte(super.getElementValueType()); // u1 type of value (ARRAY == '[') 095 dos.writeShort(Args.requireU2(evalues.size(), "evalues.size()")); 096 for (final ElementValueGen element : evalues) { 097 element.dump(dos); 098 } 099 } 100 101 /** 102 * Return immutable variant of this ArrayElementValueGen. 103 * 104 * @return immutable variant of this ArrayElementValueGen. 105 */ 106 @Override 107 public ElementValue getElementValue() { 108 final ElementValue[] immutableData = new ElementValue[evalues.size()]; 109 int i = 0; 110 for (final ElementValueGen element : evalues) { 111 immutableData[i++] = element.getElementValue(); 112 } 113 return new ArrayElementValue(super.getElementValueType(), immutableData, getConstantPool().getConstantPool()); 114 } 115 116 /** 117 * Gets the element values. 118 * 119 * @return The element values. 120 */ 121 public List<ElementValueGen> getElementValues() { 122 return evalues; 123 } 124 125 /** 126 * Gets the element values size. 127 * 128 * @return The element values size. 129 */ 130 public int getElementValuesSize() { 131 return evalues.size(); 132 } 133 134 @Override 135 public String stringifyValue() { 136 final StringBuilder sb = new StringBuilder(); 137 sb.append("["); 138 String comma = ""; 139 for (final ElementValueGen element : evalues) { 140 sb.append(comma); 141 comma = ","; 142 sb.append(element.stringifyValue()); 143 } 144 sb.append("]"); 145 return sb.toString(); 146 } 147}