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

  23. /**
  24.  * @since 6.0
  25.  */
  26. public class ClassElementValueGen extends ElementValueGen {
  27.     // For primitive types and string type, this points to the value entry in
  28.     // the cpool
  29.     // For 'class' this points to the class entry in the cpool
  30.     private final int idx;

  31.     public ClassElementValueGen(final ClassElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) {
  32.         super(CLASS, cpool);
  33.         if (copyPoolEntries) {
  34.             // idx = cpool.addClass(value.getClassString());
  35.             idx = cpool.addUtf8(value.getClassString());
  36.         } else {
  37.             idx = value.getIndex();
  38.         }
  39.     }

  40.     protected ClassElementValueGen(final int typeIdx, final ConstantPoolGen cpool) {
  41.         super(CLASS, cpool);
  42.         this.idx = typeIdx;
  43.     }

  44.     public ClassElementValueGen(final ObjectType t, final ConstantPoolGen cpool) {
  45.         super(CLASS, cpool);
  46.         // this.idx = cpool.addClass(t);
  47.         idx = cpool.addUtf8(t.getSignature());
  48.     }

  49.     @Override
  50.     public void dump(final DataOutputStream dos) throws IOException {
  51.         dos.writeByte(super.getElementValueType()); // u1 kind of value
  52.         dos.writeShort(idx);
  53.     }

  54.     public String getClassString() {
  55.         final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(idx);
  56.         return cu8.getBytes();
  57.         // ConstantClass c = (ConstantClass) getConstantPool().getConstant(idx);
  58.         // ConstantUtf8 utf8 =
  59.         // (ConstantUtf8) getConstantPool().getConstant(c.getNameIndex());
  60.         // return utf8.getBytes();
  61.     }

  62.     /**
  63.      * Return immutable variant of this ClassElementValueGen
  64.      */
  65.     @Override
  66.     public ElementValue getElementValue() {
  67.         return new ClassElementValue(super.getElementValueType(), idx, getConstantPool().getConstantPool());
  68.     }

  69.     public int getIndex() {
  70.         return idx;
  71.     }

  72.     @Override
  73.     public String stringifyValue() {
  74.         return getClassString();
  75.     }
  76. }