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.ConstantUtf8; 024import org.apache.bcel.classfile.ElementValue; 025import org.apache.bcel.classfile.ElementValuePair; 026 027/** 028 * @since 6.0 029 */ 030public class ElementValuePairGen 031{ 032 private int nameIdx; 033 034 private final ElementValueGen value; 035 036 private final ConstantPoolGen cpool; 037 038 public ElementValuePairGen(final ElementValuePair nvp, final ConstantPoolGen cpool, 039 final boolean copyPoolEntries) 040 { 041 this.cpool = cpool; 042 // J5ASSERT: 043 // Could assert nvp.getNameString() points to the same thing as 044 // cpool.getConstant(nvp.getNameIndex()) 045 // if 046 // (!nvp.getNameString().equals(((ConstantUtf8)cpool.getConstant(nvp.getNameIndex())).getBytes())) 047 // { 048 // throw new RuntimeException("envp buggered"); 049 // } 050 if (copyPoolEntries) 051 { 052 nameIdx = cpool.addUtf8(nvp.getNameString()); 053 } 054 else 055 { 056 nameIdx = nvp.getNameIndex(); 057 } 058 value = ElementValueGen.copy(nvp.getValue(), cpool, copyPoolEntries); 059 } 060 061 /** 062 * Retrieve an immutable version of this ElementNameValuePairGen 063 */ 064 public ElementValuePair getElementNameValuePair() 065 { 066 final ElementValue immutableValue = value.getElementValue(); 067 return new ElementValuePair(nameIdx, immutableValue, cpool 068 .getConstantPool()); 069 } 070 071 protected ElementValuePairGen(final int idx, final ElementValueGen value, 072 final ConstantPoolGen cpool) 073 { 074 this.nameIdx = idx; 075 this.value = value; 076 this.cpool = cpool; 077 } 078 079 public ElementValuePairGen(final String name, final ElementValueGen value, 080 final ConstantPoolGen cpool) 081 { 082 this.nameIdx = cpool.addUtf8(name); 083 this.value = value; 084 this.cpool = cpool; 085 } 086 087 protected void dump(final DataOutputStream dos) throws IOException 088 { 089 dos.writeShort(nameIdx); // u2 name of the element 090 value.dump(dos); 091 } 092 093 public int getNameIndex() 094 { 095 return nameIdx; 096 } 097 098 public final String getNameString() 099 { 100 // ConstantString cu8 = (ConstantString)cpool.getConstant(nameIdx); 101 return ((ConstantUtf8) cpool.getConstant(nameIdx)).getBytes(); 102 } 103 104 public final ElementValueGen getValue() 105 { 106 return value; 107 } 108 109 @Override 110 public String toString() 111 { 112 return "ElementValuePair:[" + getNameString() + "=" 113 + value.stringifyValue() + "]"; 114 } 115}