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