aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/apache/bcel/generic/AnnotationEntryGen.java
blob: c54cd79627cf79820a8190068585c01f88846aad (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package org.apache.bcel.generic;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.bcel.classfile.AnnotationEntry;
import org.apache.bcel.classfile.ConstantUtf8;
import org.apache.bcel.classfile.ElementValuePair;


public class AnnotationEntryGen
{
	private int typeIndex;

	private List /* ElementNameValuePairGen */evs;

	private ConstantPoolGen cpool;

	private boolean isRuntimeVisible = false;

	/**
	 * Here we are taking a fixed annotation of type Annotation and building a
	 * modifiable AnnotationGen object. If the pool passed in is for a different
	 * class file, then copyPoolEntries should have been passed as true as that
	 * will force us to do a deep copy of the annotation and move the cpool
	 * entries across. We need to copy the type and the element name value pairs
	 * and the visibility.
	 */
	public AnnotationEntryGen(AnnotationEntry a, ConstantPoolGen cpool,
			boolean copyPoolEntries)
	{
		this.cpool = cpool;
		if (copyPoolEntries)
		{
			typeIndex = cpool.addUtf8(a.getAnnotationType());
		}
		else
		{
			typeIndex = a.getAnnotationTypeIndex();
		}
		isRuntimeVisible = a.isRuntimeVisible();
		evs = copyValues(a.getElementValuePairs(), cpool, copyPoolEntries);
	}

	private List copyValues(ElementValuePair[] in, ConstantPoolGen cpool,
			boolean copyPoolEntries)
	{
		List out = new ArrayList();
		int l = in.length;
		for (int i = 0; i < l; i++)
		{
			ElementValuePair nvp = (ElementValuePair) in[i];
			out.add(new ElementValuePairGen(nvp, cpool, copyPoolEntries));
		}
		return out;
	}

	private AnnotationEntryGen(ConstantPoolGen cpool)
	{
		this.cpool = cpool;
	}

	/**
	 * Retrieve an immutable version of this AnnotationGen
	 */
	public AnnotationEntry getAnnotation()
	{
		AnnotationEntry a = new AnnotationEntry(typeIndex, cpool.getConstantPool(),
				isRuntimeVisible);
		for (Iterator iter = evs.iterator(); iter.hasNext();)
		{
			ElementValuePairGen element = (ElementValuePairGen) iter
					.next();
			a.addElementNameValuePair(element.getElementNameValuePair());
		}
		return a;
	}

	public AnnotationEntryGen(ObjectType type,
			List /* ElementNameValuePairGen */elements, boolean vis,
			ConstantPoolGen cpool)
	{
		this.cpool = cpool;
		this.typeIndex = cpool.addUtf8(type.getSignature());
		evs = elements;
		isRuntimeVisible = vis;
	}

	public static AnnotationEntryGen read(DataInputStream dis,
			ConstantPoolGen cpool, boolean b) throws IOException
	{
		AnnotationEntryGen a = new AnnotationEntryGen(cpool);
		a.typeIndex = dis.readUnsignedShort();
		int elemValuePairCount = dis.readUnsignedShort();
		for (int i = 0; i < elemValuePairCount; i++)
		{
			int nidx = dis.readUnsignedShort();
			a.addElementNameValuePair(new ElementValuePairGen(nidx,
					ElementValueGen.readElementValue(dis, cpool), cpool));
		}
		a.isRuntimeVisible(b);
		return a;
	}

	public void dump(DataOutputStream dos) throws IOException
	{
		dos.writeShort(typeIndex); // u2 index of type name in cpool
		dos.writeShort(evs.size()); // u2 element_value pair count
		for (int i = 0; i < evs.size(); i++)
		{
			ElementValuePairGen envp = (ElementValuePairGen) evs.get(i);
			envp.dump(dos);
		}
	}

	public void addElementNameValuePair(ElementValuePairGen evp)
	{
		if (evs == null)
			evs = new ArrayList();
		evs.add(evp);
	}

	public int getTypeIndex()
	{
		return typeIndex;
	}

	public final String getTypeSignature()
	{
		// ConstantClass c = (ConstantClass)cpool.getConstant(typeIndex);
		ConstantUtf8 utf8 = (ConstantUtf8) cpool
				.getConstant(typeIndex/* c.getNameIndex() */);
		return utf8.getBytes();
	}

	public final String getTypeName()
	{
		return getTypeSignature();// BCELBUG: Should I use this instead?
									// Utility.signatureToString(getTypeSignature());
	}

	/**
	 * Returns list of ElementNameValuePair objects
	 */
	public List getValues()
	{
		return evs;
	}

	public String toString()
	{
		StringBuffer s = new StringBuffer();
		s.append("AnnotationGen:[" + getTypeName() + " #" + evs.size() + " {");
		for (int i = 0; i < evs.size(); i++)
		{
			s.append(evs.get(i));
			if (i + 1 < evs.size())
				s.append(",");
		}
		s.append("}]");
		return s.toString();
	}

	public String toShortString()
	{
		StringBuffer s = new StringBuffer();
		s.append("@" + getTypeName() + "(");
		for (int i = 0; i < evs.size(); i++)
		{
			s.append(evs.get(i));
			if (i + 1 < evs.size())
				s.append(",");
		}
		s.append(")");
		return s.toString();
	}

	private void isRuntimeVisible(boolean b)
	{
		isRuntimeVisible = b;
	}

	public boolean isRuntimeVisible()
	{
		return isRuntimeVisible;
	}
}