ArrayElementValueGen.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel.generic;

  18. import java.io.DataOutputStream;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.stream.Collectors;

  23. import org.apache.bcel.classfile.ArrayElementValue;
  24. import org.apache.bcel.classfile.ElementValue;
  25. import org.apache.commons.lang3.stream.Streams;

  26. /**
  27.  * @since 6.0
  28.  */
  29. public class ArrayElementValueGen extends ElementValueGen {
  30.     // J5TODO: Should we make this an array or a list? A list would be easier to
  31.     // modify ...
  32.     private final List<ElementValueGen> evalues;

  33.     /**
  34.      * @param value
  35.      * @param cpool
  36.      */
  37.     public ArrayElementValueGen(final ArrayElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
  38.         super(ARRAY, cpool);
  39.         evalues = new ArrayList<>();
  40.         final ElementValue[] in = value.getElementValuesArray();
  41.         for (final ElementValue element : in) {
  42.             evalues.add(copy(element, cpool, copyPoolEntries));
  43.         }
  44.     }

  45.     public ArrayElementValueGen(final ConstantPoolGen cp) {
  46.         super(ARRAY, cp);
  47.         evalues = new ArrayList<>();
  48.     }

  49.     public ArrayElementValueGen(final int type, final ElementValue[] elementValues, final ConstantPoolGen cpool) {
  50.         super(type, cpool);
  51.         if (type != ARRAY) {
  52.             throw new IllegalArgumentException("Only element values of type array can be built with this ctor - type specified: " + type);
  53.         }
  54.         this.evalues = Streams.of(elementValues).map(e -> copy(e, cpool, true)).collect(Collectors.toList());
  55.     }

  56.     public void addElement(final ElementValueGen gen) {
  57.         evalues.add(gen);
  58.     }

  59.     @Override
  60.     public void dump(final DataOutputStream dos) throws IOException {
  61.         dos.writeByte(super.getElementValueType()); // u1 type of value (ARRAY == '[')
  62.         dos.writeShort(evalues.size());
  63.         for (final ElementValueGen element : evalues) {
  64.             element.dump(dos);
  65.         }
  66.     }

  67.     /**
  68.      * Return immutable variant of this ArrayElementValueGen
  69.      */
  70.     @Override
  71.     public ElementValue getElementValue() {
  72.         final ElementValue[] immutableData = new ElementValue[evalues.size()];
  73.         int i = 0;
  74.         for (final ElementValueGen element : evalues) {
  75.             immutableData[i++] = element.getElementValue();
  76.         }
  77.         return new ArrayElementValue(super.getElementValueType(), immutableData, getConstantPool().getConstantPool());
  78.     }

  79.     public List<ElementValueGen> getElementValues() {
  80.         return evalues;
  81.     }

  82.     public int getElementValuesSize() {
  83.         return evalues.size();
  84.     }

  85.     @Override
  86.     public String stringifyValue() {
  87.         final StringBuilder sb = new StringBuilder();
  88.         sb.append("[");
  89.         String comma = "";
  90.         for (final ElementValueGen element : evalues) {
  91.             sb.append(comma);
  92.             comma = ",";
  93.             sb.append(element.stringifyValue());
  94.         }
  95.         sb.append("]");
  96.         return sb.toString();
  97.     }
  98. }