ElementValuePairGen.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 org.apache.bcel.classfile.ConstantUtf8;
  21. import org.apache.bcel.classfile.ElementValue;
  22. import org.apache.bcel.classfile.ElementValuePair;

  23. /**
  24.  * @since 6.0
  25.  */
  26. public class ElementValuePairGen {
  27.     private final int nameIdx;

  28.     private final ElementValueGen value;

  29.     private final ConstantPoolGen constantPoolGen;

  30.     public ElementValuePairGen(final ElementValuePair nvp, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
  31.         this.constantPoolGen = cpool;
  32.         // J5ASSERT:
  33.         // Could assert nvp.getNameString() points to the same thing as
  34.         // constantPoolGen.getConstant(nvp.getNameIndex())
  35.         // if
  36.         // (!nvp.getNameString().equals(((ConstantUtf8) constantPoolGen.getConstant(nvp.getNameIndex())).getBytes()))
  37.         // {
  38.         // throw new IllegalArgumentException("envp buggered");
  39.         // }
  40.         if (copyPoolEntries) {
  41.             nameIdx = cpool.addUtf8(nvp.getNameString());
  42.         } else {
  43.             nameIdx = nvp.getNameIndex();
  44.         }
  45.         value = ElementValueGen.copy(nvp.getValue(), cpool, copyPoolEntries);
  46.     }

  47.     protected ElementValuePairGen(final int idx, final ElementValueGen value, final ConstantPoolGen cpool) {
  48.         this.nameIdx = idx;
  49.         this.value = value;
  50.         this.constantPoolGen = cpool;
  51.     }

  52.     public ElementValuePairGen(final String name, final ElementValueGen value, final ConstantPoolGen cpool) {
  53.         this.nameIdx = cpool.addUtf8(name);
  54.         this.value = value;
  55.         this.constantPoolGen = cpool;
  56.     }

  57.     protected void dump(final DataOutputStream dos) throws IOException {
  58.         dos.writeShort(nameIdx); // u2 name of the element
  59.         value.dump(dos);
  60.     }

  61.     /**
  62.      * Retrieve an immutable version of this ElementNameValuePairGen
  63.      */
  64.     public ElementValuePair getElementNameValuePair() {
  65.         final ElementValue immutableValue = value.getElementValue();
  66.         return new ElementValuePair(nameIdx, immutableValue, constantPoolGen.getConstantPool());
  67.     }

  68.     public int getNameIndex() {
  69.         return nameIdx;
  70.     }

  71.     public final String getNameString() {
  72.         // ConstantString cu8 = (ConstantString) constantPoolGen.getConstant(nameIdx);
  73.         return ((ConstantUtf8) constantPoolGen.getConstant(nameIdx)).getBytes();
  74.     }

  75.     public final ElementValueGen getValue() {
  76.         return value;
  77.     }

  78.     @Override
  79.     public String toString() {
  80.         return "ElementValuePair:[" + getNameString() + "=" + value.stringifyValue() + "]";
  81.     }
  82. }