1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.generic;
  23 
  24 import java.io.DataOutputStream;
  25 import java.io.IOException;
  26 import java.util.ArrayList;
  27 import java.util.List;
  28 
  29 import com.sun.org.apache.bcel.internal.classfile.ArrayElementValue;
  30 import com.sun.org.apache.bcel.internal.classfile.ElementValue;
  31 
  32 /**
  33  * @since 6.0
  34  */
  35 public class ArrayElementValueGen extends ElementValueGen
  36 {
  37     // J5TODO: Should we make this an array or a list? A list would be easier to
  38     // modify ...
  39     private final List<ElementValueGen> evalues;
  40 
  41     public ArrayElementValueGen(final ConstantPoolGen cp)
  42     {
  43         super(ARRAY, cp);
  44         evalues = new ArrayList<>();
  45     }
  46 
  47     public ArrayElementValueGen(final int type, final ElementValue[] datums,
  48             final ConstantPoolGen cpool)
  49     {
  50         super(type, cpool);
  51         if (type != ARRAY) {
  52             throw new RuntimeException(
  53                     "Only element values of type array can be built with this ctor - type specified: " + type);
  54         }
  55         this.evalues = new ArrayList<>();
  56         for (final ElementValue datum : datums) {
  57             evalues.add(ElementValueGen.copy(datum, cpool, true));
  58         }
  59     }
  60 
  61     /**
  62      * Return immutable variant of this ArrayElementValueGen
  63      */
  64     @Override
  65     public ElementValue getElementValue()
  66     {
  67         final ElementValue[] immutableData = new ElementValue[evalues.size()];
  68         int i = 0;
  69         for (final ElementValueGen element : evalues) {
  70             immutableData[i++] = element.getElementValue();
  71         }
  72         return new ArrayElementValue(super.getElementValueType(),
  73                 immutableData,
  74                 getConstantPool().getConstantPool());
  75     }
  76 
  77     /**
  78      * @param value
  79      * @param cpool
  80      */
  81     public ArrayElementValueGen(final ArrayElementValue value, final ConstantPoolGen cpool,
  82             final boolean copyPoolEntries)
  83     {
  84         super(ARRAY, cpool);
  85         evalues = new ArrayList<>();
  86         final ElementValue[] in = value.getElementValuesArray();
  87         for (final ElementValue element : in) {
  88             evalues.add(ElementValueGen.copy(element, cpool, copyPoolEntries));
  89         }
  90     }
  91 
  92     @Override
  93     public void dump(final DataOutputStream dos) throws IOException
  94     {
  95         dos.writeByte(super.getElementValueType()); // u1 type of value (ARRAY == '[')
  96         dos.writeShort(evalues.size());
  97         for (final ElementValueGen element : evalues) {
  98             element.dump(dos);
  99         }
 100     }
 101 
 102     @Override
 103     public String stringifyValue()
 104     {
 105         final StringBuilder sb = new StringBuilder();
 106         sb.append("[");
 107         String comma = "";
 108         for (final ElementValueGen element : evalues) {
 109             sb.append(comma);
 110             comma = ",";
 111             sb.append(element.stringifyValue());
 112         }
 113         sb.append("]");
 114         return sb.toString();
 115     }
 116 
 117     public List<ElementValueGen> getElementValues()
 118     {
 119         return evalues;
 120     }
 121 
 122     public int getElementValuesSize()
 123     {
 124         return evalues.size();
 125     }
 126 
 127     public void addElement(final ElementValueGen gen)
 128     {
 129         evalues.add(gen);
 130     }
 131 }