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