001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 */ 018package org.apache.bcel.generic; 019 020import java.io.DataOutputStream; 021import java.io.IOException; 022 023import org.apache.bcel.classfile.ClassElementValue; 024import org.apache.bcel.classfile.ConstantUtf8; 025import org.apache.bcel.classfile.ElementValue; 026 027/** 028 * @since 6.0 029 */ 030public class ClassElementValueGen extends ElementValueGen { 031 // For primitive types and string type, this points to the value entry in 032 // the cpool 033 // For 'class' this points to the class entry in the cpool 034 private final int idx; 035 036 public ClassElementValueGen(final ClassElementValue value, final ConstantPoolGen cpool, final boolean copyPoolEntries) { 037 super(CLASS, cpool); 038 if (copyPoolEntries) { 039 // idx = cpool.addClass(value.getClassString()); 040 idx = cpool.addUtf8(value.getClassString()); 041 } else { 042 idx = value.getIndex(); 043 } 044 } 045 046 protected ClassElementValueGen(final int typeIdx, final ConstantPoolGen cpool) { 047 super(ElementValueGen.CLASS, cpool); 048 this.idx = typeIdx; 049 } 050 051 public ClassElementValueGen(final ObjectType t, final ConstantPoolGen cpool) { 052 super(ElementValueGen.CLASS, cpool); 053 // this.idx = cpool.addClass(t); 054 idx = cpool.addUtf8(t.getSignature()); 055 } 056 057 @Override 058 public void dump(final DataOutputStream dos) throws IOException { 059 dos.writeByte(super.getElementValueType()); // u1 kind of value 060 dos.writeShort(idx); 061 } 062 063 public String getClassString() { 064 final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(idx); 065 return cu8.getBytes(); 066 // ConstantClass c = (ConstantClass)getConstantPool().getConstant(idx); 067 // ConstantUtf8 utf8 = 068 // (ConstantUtf8)getConstantPool().getConstant(c.getNameIndex()); 069 // return utf8.getBytes(); 070 } 071 072 /** 073 * Return immutable variant of this ClassElementValueGen 074 */ 075 @Override 076 public ElementValue getElementValue() { 077 return new ClassElementValue(super.getElementValueType(), idx, getConstantPool().getConstantPool()); 078 } 079 080 public int getIndex() { 081 return idx; 082 } 083 084 @Override 085 public String stringifyValue() { 086 return getClassString(); 087 } 088}