aboutsummaryrefslogtreecommitdiffstats
path: root/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/EnumElementValueGen.java
blob: 85d443a93c61fc99919e816b43419a50722b07f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* *******************************************************************
 * Copyright (c) 2004 IBM
 * All rights reserved. 
 * This program and the accompanying materials are made available 
 * under the terms of the Common Public License v1.0 
 * which accompanies this distribution and is available at 
 * http://www.eclipse.org/legal/cpl-v10.html 
 *  
 * Contributors: 
 *     Andy Clement -     initial implementation {date}
 * ******************************************************************/

package org.aspectj.apache.bcel.generic.annotation;

import java.io.DataOutputStream;
import java.io.IOException;

import org.aspectj.apache.bcel.classfile.ConstantUtf8;
import org.aspectj.apache.bcel.classfile.annotation.ElementValue;
import org.aspectj.apache.bcel.classfile.annotation.EnumElementValue;
import org.aspectj.apache.bcel.generic.ConstantPoolGen;
import org.aspectj.apache.bcel.generic.ObjectType;


public class EnumElementValueGen extends ElementValueGen {
	
	// For enum types, these two indices point to the type and value
	private int typeIdx;
	private int valueIdx;
	
	/**
	 * This ctor assumes the constant pool already contains the right type and value -
	 * as indicated by typeIdx and valueIdx.  This ctor is used for deserialization
	 */
    protected EnumElementValueGen(int typeIdx,int valueIdx,ConstantPoolGen cpool) {
    	super(ElementValueGen.ENUM_CONSTANT,cpool);
    	if (type != ENUM_CONSTANT) 
    		throw new RuntimeException("Only element values of type enum can be built with this ctor");
    	this.typeIdx = typeIdx;
    	this.valueIdx= valueIdx;
    }
	
	/**
     * Return immutable variant of this EnumElementValue
     */
	public ElementValue getElementValue() {
		System.err.println("Duplicating value: "+getEnumTypeString()+":"+getEnumValueString());
		return new EnumElementValue(type,typeIdx,valueIdx,cpGen.getConstantPool());
	}
    
    public EnumElementValueGen(ObjectType t,String value,ConstantPoolGen cpool) {
    	super(ElementValueGen.ENUM_CONSTANT,cpool);
    	typeIdx = cpool.addUtf8(t.getSignature());// was addClass(t);
    	valueIdx= cpool.addUtf8(value);// was addString(value);
    }

	public EnumElementValueGen(EnumElementValue value, ConstantPoolGen cpool, boolean copyPoolEntries) {
		super(ENUM_CONSTANT,cpool);
		if (copyPoolEntries) {
			typeIdx = cpool.addUtf8(value.getEnumTypeString());// was addClass(value.getEnumTypeString());
			valueIdx = cpool.addUtf8(value.getEnumValueString()); // was addString(value.getEnumValueString());
		} else {
			typeIdx  = value.getTypeIndex();
			valueIdx = value.getValueIndex();
		}
	}

	public void dump(DataOutputStream dos) throws IOException {
    	dos.writeByte(type);      // u1 type of value (ENUM_CONSTANT == 'e')
		dos.writeShort(typeIdx);  // u2
		dos.writeShort(valueIdx); // u2
    }
    
    public String stringifyValue() {
    	ConstantUtf8 cu8 = (ConstantUtf8)getConstantPool().getConstant(valueIdx);
		return cu8.getBytes();
//    	ConstantString cu8 = (ConstantString)getConstantPool().getConstant(valueIdx);
//    	return ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
    }
    
    
    // BCELBUG: Should we need to call utility.signatureToString() on the output here?
    public String getEnumTypeString() {
//		Constant cc = getConstantPool().getConstant(typeIdx);
//    	ConstantClass cu8 = (ConstantClass)getConstantPool().getConstant(typeIdx);		
//    	return ((ConstantUtf8)getConstantPool().getConstant(cu8.getNameIndex())).getBytes();
		return ((ConstantUtf8)getConstantPool().getConstant(typeIdx)).getBytes();
		// return Utility.signatureToString(cu8.getBytes());
    }
    
	public String getEnumValueString() {
		return ((ConstantUtf8)getConstantPool().getConstant(valueIdx)).getBytes();
//		ConstantString cu8 = (ConstantString)getConstantPool().getConstant(valueIdx);
//    	return ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
	}
    
    public int getValueIndex() { return valueIdx;}
    public int getTypeIndex()  { return typeIdx; }

   
}