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