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: package org.apache.harmony.pack200;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: /**
024: * Stores a mapping from attribute names to their corresponding layout types.
025: * Note that names of attribute layouts and their formats are <emph>not</emph>
026: * internationalized, and should not be translated.
027: */
028: public class AttributeLayoutMap {
029:
030: // Create all the default AttributeLayouts here
031: private static AttributeLayout[] getDefaultAttributeLayouts()
032: throws Pack200Exception {
033: return new AttributeLayout[] {
034: new AttributeLayout(AttributeLayout.ACC_PUBLIC,
035: AttributeLayout.CONTEXT_CLASS, "", 0),
036: new AttributeLayout(AttributeLayout.ACC_PUBLIC,
037: AttributeLayout.CONTEXT_FIELD, "", 0),
038: new AttributeLayout(AttributeLayout.ACC_PUBLIC,
039: AttributeLayout.CONTEXT_METHOD, "", 0),
040: new AttributeLayout(AttributeLayout.ACC_PRIVATE,
041: AttributeLayout.CONTEXT_CLASS, "", 1),
042: new AttributeLayout(AttributeLayout.ACC_PRIVATE,
043: AttributeLayout.CONTEXT_FIELD, "", 1),
044: new AttributeLayout(AttributeLayout.ACC_PRIVATE,
045: AttributeLayout.CONTEXT_METHOD, "", 1),
046: new AttributeLayout(
047: AttributeLayout.ATTRIBUTE_LINE_NUMBER_TABLE,
048: AttributeLayout.CONTEXT_CODE, "NH[PHH]", 1),
049:
050: new AttributeLayout(AttributeLayout.ACC_PROTECTED,
051: AttributeLayout.CONTEXT_CLASS, "", 2),
052: new AttributeLayout(AttributeLayout.ACC_PROTECTED,
053: AttributeLayout.CONTEXT_FIELD, "", 2),
054: new AttributeLayout(AttributeLayout.ACC_PROTECTED,
055: AttributeLayout.CONTEXT_METHOD, "", 2),
056: new AttributeLayout(
057: AttributeLayout.ATTRIBUTE_LOCAL_VARIABLE_TABLE,
058: AttributeLayout.CONTEXT_CODE,
059: "NH[PHOHRUHRSHH]", 2),
060:
061: new AttributeLayout(AttributeLayout.ACC_STATIC,
062: AttributeLayout.CONTEXT_CLASS, "", 3),
063: new AttributeLayout(AttributeLayout.ACC_STATIC,
064: AttributeLayout.CONTEXT_FIELD, "", 3),
065: new AttributeLayout(AttributeLayout.ACC_STATIC,
066: AttributeLayout.CONTEXT_METHOD, "", 3),
067: new AttributeLayout(
068: AttributeLayout.ATTRIBUTE_LOCAL_VARIABLE_TYPE_TABLE,
069: AttributeLayout.CONTEXT_CODE,
070: "NH[PHOHRUHRSHH]", 3),
071:
072: new AttributeLayout(AttributeLayout.ACC_FINAL,
073: AttributeLayout.CONTEXT_CLASS, "", 4),
074: new AttributeLayout(AttributeLayout.ACC_FINAL,
075: AttributeLayout.CONTEXT_FIELD, "", 4),
076: new AttributeLayout(AttributeLayout.ACC_FINAL,
077: AttributeLayout.CONTEXT_METHOD, "", 4),
078: new AttributeLayout(AttributeLayout.ACC_SYNCHRONIZED,
079: AttributeLayout.CONTEXT_CLASS, "", 5),
080: new AttributeLayout(AttributeLayout.ACC_SYNCHRONIZED,
081: AttributeLayout.CONTEXT_FIELD, "", 5),
082: new AttributeLayout(AttributeLayout.ACC_SYNCHRONIZED,
083: AttributeLayout.CONTEXT_METHOD, "", 5),
084: new AttributeLayout(AttributeLayout.ACC_VOLATILE,
085: AttributeLayout.CONTEXT_CLASS, "", 6),
086: new AttributeLayout(AttributeLayout.ACC_VOLATILE,
087: AttributeLayout.CONTEXT_FIELD, "", 6),
088: new AttributeLayout(AttributeLayout.ACC_VOLATILE,
089: AttributeLayout.CONTEXT_METHOD, "", 6),
090: new AttributeLayout(AttributeLayout.ACC_TRANSIENT,
091: AttributeLayout.CONTEXT_CLASS, "", 7),
092: new AttributeLayout(AttributeLayout.ACC_TRANSIENT,
093: AttributeLayout.CONTEXT_FIELD, "", 7),
094: new AttributeLayout(AttributeLayout.ACC_TRANSIENT,
095: AttributeLayout.CONTEXT_METHOD, "", 7),
096: new AttributeLayout(AttributeLayout.ACC_NATIVE,
097: AttributeLayout.CONTEXT_CLASS, "", 8),
098: new AttributeLayout(AttributeLayout.ACC_NATIVE,
099: AttributeLayout.CONTEXT_FIELD, "", 8),
100: new AttributeLayout(AttributeLayout.ACC_NATIVE,
101: AttributeLayout.CONTEXT_METHOD, "", 8),
102: new AttributeLayout(AttributeLayout.ACC_INTERFACE,
103: AttributeLayout.CONTEXT_CLASS, "", 9),
104: new AttributeLayout(AttributeLayout.ACC_INTERFACE,
105: AttributeLayout.CONTEXT_FIELD, "", 9),
106: new AttributeLayout(AttributeLayout.ACC_INTERFACE,
107: AttributeLayout.CONTEXT_METHOD, "", 9),
108: new AttributeLayout(AttributeLayout.ACC_ABSTRACT,
109: AttributeLayout.CONTEXT_CLASS, "", 10),
110: new AttributeLayout(AttributeLayout.ACC_ABSTRACT,
111: AttributeLayout.CONTEXT_FIELD, "", 10),
112: new AttributeLayout(AttributeLayout.ACC_ABSTRACT,
113: AttributeLayout.CONTEXT_METHOD, "", 10),
114: new AttributeLayout(AttributeLayout.ACC_STRICT,
115: AttributeLayout.CONTEXT_CLASS, "", 11),
116: new AttributeLayout(AttributeLayout.ACC_STRICT,
117: AttributeLayout.CONTEXT_FIELD, "", 11),
118: new AttributeLayout(AttributeLayout.ACC_STRICT,
119: AttributeLayout.CONTEXT_METHOD, "", 11),
120: new AttributeLayout(AttributeLayout.ACC_SYNTHETIC,
121: AttributeLayout.CONTEXT_CLASS, "", 12),
122: new AttributeLayout(AttributeLayout.ACC_SYNTHETIC,
123: AttributeLayout.CONTEXT_FIELD, "", 12),
124: new AttributeLayout(AttributeLayout.ACC_SYNTHETIC,
125: AttributeLayout.CONTEXT_METHOD, "", 12),
126: new AttributeLayout(AttributeLayout.ACC_ANNOTATION,
127: AttributeLayout.CONTEXT_CLASS, "", 13),
128: new AttributeLayout(AttributeLayout.ACC_ANNOTATION,
129: AttributeLayout.CONTEXT_FIELD, "", 13),
130: new AttributeLayout(AttributeLayout.ACC_ANNOTATION,
131: AttributeLayout.CONTEXT_METHOD, "", 13),
132: new AttributeLayout(AttributeLayout.ACC_ENUM,
133: AttributeLayout.CONTEXT_CLASS, "", 14),
134: new AttributeLayout(AttributeLayout.ACC_ENUM,
135: AttributeLayout.CONTEXT_FIELD, "", 14),
136: new AttributeLayout(AttributeLayout.ACC_ENUM,
137: AttributeLayout.CONTEXT_METHOD, "", 14),
138: new AttributeLayout(
139: AttributeLayout.ATTRIBUTE_SOURCE_FILE,
140: AttributeLayout.CONTEXT_CLASS, "RUNH", 17),
141: new AttributeLayout(
142: AttributeLayout.ATTRIBUTE_CONSTANT_VALUE,
143: AttributeLayout.CONTEXT_FIELD, "KQH", 17),
144: new AttributeLayout(AttributeLayout.ATTRIBUTE_CODE,
145: AttributeLayout.CONTEXT_METHOD, "", 17),
146: new AttributeLayout(
147: AttributeLayout.ATTRIBUTE_ENCLOSING_METHOD,
148: AttributeLayout.CONTEXT_CLASS, "RCHRDNH", 18),
149: new AttributeLayout(
150: AttributeLayout.ATTRIBUTE_EXCEPTIONS,
151: AttributeLayout.CONTEXT_METHOD, "NH[RCH]", 18),
152: new AttributeLayout(
153: AttributeLayout.ATTRIBUTE_SIGNATURE,
154: AttributeLayout.CONTEXT_CLASS, "RSH", 19),
155: new AttributeLayout(
156: AttributeLayout.ATTRIBUTE_SIGNATURE,
157: AttributeLayout.CONTEXT_FIELD, "RSH", 19),
158: new AttributeLayout(
159: AttributeLayout.ATTRIBUTE_SIGNATURE,
160: AttributeLayout.CONTEXT_METHOD, "RSH", 19),
161: new AttributeLayout(
162: AttributeLayout.ATTRIBUTE_DEPRECATED,
163: AttributeLayout.CONTEXT_CLASS, "", 20),
164: new AttributeLayout(
165: AttributeLayout.ATTRIBUTE_DEPRECATED,
166: AttributeLayout.CONTEXT_FIELD, "", 20),
167: new AttributeLayout(
168: AttributeLayout.ATTRIBUTE_DEPRECATED,
169: AttributeLayout.CONTEXT_METHOD, "", 20),
170: new AttributeLayout(
171: AttributeLayout.ATTRIBUTE_RUNTIME_VISIBLE_ANNOTATIONS,
172: AttributeLayout.CONTEXT_CLASS, "*", 21),
173: new AttributeLayout(
174: AttributeLayout.ATTRIBUTE_RUNTIME_VISIBLE_ANNOTATIONS,
175: AttributeLayout.CONTEXT_FIELD, "*", 21),
176: new AttributeLayout(
177: AttributeLayout.ATTRIBUTE_RUNTIME_VISIBLE_ANNOTATIONS,
178: AttributeLayout.CONTEXT_METHOD, "*", 21),
179: new AttributeLayout(
180: AttributeLayout.ATTRIBUTE_RUNTIME_INVISIBLE_ANNOTATIONS,
181: AttributeLayout.CONTEXT_CLASS, "*", 22),
182: new AttributeLayout(
183: AttributeLayout.ATTRIBUTE_RUNTIME_INVISIBLE_ANNOTATIONS,
184: AttributeLayout.CONTEXT_FIELD, "*", 22),
185: new AttributeLayout(
186: AttributeLayout.ATTRIBUTE_RUNTIME_INVISIBLE_ANNOTATIONS,
187: AttributeLayout.CONTEXT_METHOD, "*", 22),
188: new AttributeLayout(
189: AttributeLayout.ATTRIBUTE_INNER_CLASSES,
190: AttributeLayout.CONTEXT_CLASS, "", 23),
191: new AttributeLayout(
192: AttributeLayout.ATTRIBUTE_RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS,
193: AttributeLayout.CONTEXT_METHOD, "*", 23),
194: new AttributeLayout(
195: AttributeLayout.ATTRIBUTE_CLASS_FILE_VERSION,
196: AttributeLayout.CONTEXT_CLASS, "*", 24),
197: new AttributeLayout(
198: AttributeLayout.ATTRIBUTE_RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS,
199: AttributeLayout.CONTEXT_METHOD, "*", 24),
200: new AttributeLayout(
201: AttributeLayout.ATTRIBUTE_ANNOTATION_DEFAULT,
202: AttributeLayout.CONTEXT_METHOD, "*", 25) };
203: }
204:
205: private final Map classLayouts = new HashMap();
206: private final Map fieldLayouts = new HashMap();
207: private final Map methodLayouts = new HashMap();
208: private final Map codeLayouts = new HashMap();
209:
210: // The order of the maps in this array should not be changed as their indices correspond to
211: // the value of their context constants (AttributeLayout.CONTEXT_CLASS etc.)
212: private final Map[] layouts = new Map[] { classLayouts,
213: fieldLayouts, methodLayouts, codeLayouts };
214:
215: private final Map layoutsToBands = new HashMap();
216:
217: public AttributeLayoutMap() throws Pack200Exception {
218: AttributeLayout[] defaultAttributeLayouts = getDefaultAttributeLayouts();
219: for (int i = 0; i < defaultAttributeLayouts.length; i++) {
220: add(defaultAttributeLayouts[i]);
221: }
222: }
223:
224: public void add(AttributeLayout layout) {
225: layouts[layout.getContext()].put(
226: new Integer(layout.getIndex()), layout);
227: }
228:
229: public void add(AttributeLayout layout, NewAttributeBands newBands) {
230: add(layout);
231: layoutsToBands.put(layout, newBands);
232: }
233:
234: public AttributeLayout getAttributeLayout(String name, int context)
235: throws Pack200Exception {
236: Map map = layouts[context];
237: for (Iterator iter = map.values().iterator(); iter.hasNext();) {
238: AttributeLayout layout = (AttributeLayout) iter.next();
239: if (layout.getName().equals(name)) {
240: return layout;
241: }
242: }
243: return null;
244: }
245:
246: public AttributeLayout getAttributeLayout(int index, int context)
247: throws Pack200Exception {
248: Map map = layouts[context];
249: return (AttributeLayout) map.get(new Integer(index));
250: }
251:
252: /**
253: * The map should not contain the same layout and name combination more than
254: * once for each context.
255: * @throws Pack200Exception
256: *
257: */
258: public void checkMap() throws Pack200Exception {
259: for (int i = 0; i < layouts.length; i++) {
260: Map map = layouts[i];
261: for (Iterator iter = map.values().iterator(); iter
262: .hasNext();) {
263: AttributeLayout layout1 = (AttributeLayout) iter.next();
264: for (Iterator iter2 = map.values().iterator(); iter2
265: .hasNext();) {
266: AttributeLayout layout2 = (AttributeLayout) iter2
267: .next();
268: if (layout1 != layout2) {
269: if (layout1.getName().equals(layout2.getName())
270: && layout1.getLayout().equals(
271: layout2.getLayout())) {
272: throw new Pack200Exception(
273: "Same layout/name combination: "
274: + layout1.getLayout()
275: + "/"
276: + layout1.getName()
277: + " exists twice for context: "
278: + AttributeLayout.contextNames[layout1
279: .getContext()]);
280: }
281: }
282: }
283: }
284: }
285: }
286:
287: public NewAttributeBands getAttributeBands(AttributeLayout layout) {
288: return (NewAttributeBands) layoutsToBands.get(layout);
289: }
290:
291: }
|