aboutsummaryrefslogtreecommitdiffstats
path: root/bcel-builder/src/org/aspectj/apache/bcel/generic/annotation/ElementValueGen.java
blob: 64afdc175e402d4bb811f2af3ed9f06eda9794b1 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* *******************************************************************
 * 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.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.aspectj.apache.bcel.classfile.annotation.AnnotationElementValue;
import org.aspectj.apache.bcel.classfile.annotation.ArrayElementValue;
import org.aspectj.apache.bcel.classfile.annotation.ClassElementValue;
import org.aspectj.apache.bcel.classfile.annotation.ElementValue;
import org.aspectj.apache.bcel.classfile.annotation.EnumElementValue;
import org.aspectj.apache.bcel.classfile.annotation.SimpleElementValue;
import org.aspectj.apache.bcel.generic.ConstantPoolGen;

public abstract class ElementValueGen {

	protected int type;
	protected ConstantPoolGen cpGen;
	
	
    protected ElementValueGen(int type,ConstantPoolGen cpGen) {
    	this.type = type;
    	this.cpGen = cpGen;
    }
	
	/**
     * Subtypes return an immutable variant of the ElementValueGen
     */
	public abstract ElementValue getElementValue();
    
	public int getElementValueType() {
    	return type;
    }
	
	public abstract String stringifyValue();
	public abstract void dump(DataOutputStream dos) throws IOException;
    
	public static final int STRING = 's';
	public static final int ENUM_CONSTANT = 'e';
	public static final int CLASS = 'c';
	public static final int ANNOTATION = '@';
    public static final int ARRAY = '[';
    
    public static final int PRIMITIVE_INT    = 'I';
    public static final int PRIMITIVE_BYTE   = 'B';
    public static final int PRIMITIVE_CHAR   = 'C';
    public static final int PRIMITIVE_DOUBLE = 'D';
    public static final int PRIMITIVE_FLOAT  = 'F';
    public static final int PRIMITIVE_LONG   = 'J';
    public static final int PRIMITIVE_SHORT  = 'S';
    public static final int PRIMITIVE_BOOLEAN= 'Z';    
    
    public static ElementValueGen readElementValue(DataInputStream dis,ConstantPoolGen cpGen) throws IOException {
		int type= dis.readUnsignedByte();
		switch (type) {
		  case 'B': // byte
		  	return new SimpleElementValueGen(PRIMITIVE_BYTE,dis.readUnsignedShort(),cpGen);
		  case 'C': // char
		  	return new SimpleElementValueGen(PRIMITIVE_CHAR,dis.readUnsignedShort(),cpGen);
		  case 'D': // double
		  	return new SimpleElementValueGen(PRIMITIVE_DOUBLE,dis.readUnsignedShort(),cpGen);
		  case 'F': // float
		  	return new SimpleElementValueGen(PRIMITIVE_FLOAT,dis.readUnsignedShort(),cpGen);
		  case 'I': // int
		  	return new SimpleElementValueGen(PRIMITIVE_INT,dis.readUnsignedShort(),cpGen);
		  case 'J': // long
		  	return new SimpleElementValueGen(PRIMITIVE_LONG,dis.readUnsignedShort(),cpGen);
		  case 'S': // short
		  	return new SimpleElementValueGen(PRIMITIVE_SHORT,dis.readUnsignedShort(),cpGen);
		  case 'Z': // boolean
		  	return new SimpleElementValueGen(PRIMITIVE_BOOLEAN,dis.readUnsignedShort(),cpGen);		  	
		  case 's': // String
		  	return new SimpleElementValueGen(STRING,dis.readUnsignedShort(),cpGen);

		  case 'e': // Enum constant
		  	return new EnumElementValueGen(dis.readUnsignedShort(),dis.readUnsignedShort(),cpGen);

		  case 'c': // Class
		  	return new ClassElementValueGen(dis.readUnsignedShort(),cpGen);
//
//		  case '@': // Annotation
//		  	return new AnnotationElementValueGen(ANNOTATION,Annotation.read(dis,cpGen),cpGen);
//		  	
//		  case '[': // Array
//		  	int numArrayVals = dis.readUnsignedShort();
//		  	List arrayVals = new ArrayList();
//		  	ElementValue[] evalues = new ElementValue[numArrayVals];
//		  	for (int j=0;j<numArrayVals;j++) {
//		  		evalues[j] = ElementValue.readElementValue(dis,cpGen);
//		  	}
//		  	return new ArrayElementValue(ARRAY,evalues,cpGen);

		  default: 
		  	throw new RuntimeException("Unexpected element value kind in annotation: "+type);
		}
	}

	protected ConstantPoolGen getConstantPool() {
		return cpGen;
	}

	/** 
	 * Creates an (modifiable) ElementValueGen copy of an (immutable) ElementValue - constant pool is assumed correct.
	 */
	public static ElementValueGen copy(ElementValue value,ConstantPoolGen cpool,boolean copyPoolEntries) {
		switch (value.getElementValueType()) {
			 case 'B': // byte
			 case 'C': // char
			 case 'D': // double
			 case 'F': // float
			 case 'I': // int
			 case 'J': // long
			 case 'S': // short
			 case 'Z': // boolean
			 case 's': // String
			  return new SimpleElementValueGen((SimpleElementValue)value,cpool,copyPoolEntries);
				
			 case 'e': // Enum constant
			  	return new EnumElementValueGen((EnumElementValue)value,cpool,copyPoolEntries);

			 case '@': // Annotation
			  	return new AnnotationElementValueGen((AnnotationElementValue)value,cpool,copyPoolEntries);
			  	
		     case '[': // Array
		     	return new ArrayElementValueGen((ArrayElementValue)value,cpool,copyPoolEntries);

		     case 'c': // Class
		     	return new ClassElementValueGen((ClassElementValue)value,cpool,copyPoolEntries);
		     	
			default:
				throw new RuntimeException("Not implemented yet! ("+value.getElementValueType()+")");
		}
	}
}