KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > classfile > AttributeMap


1 /*
2  * AttributeMap.java
3  *
4  * The contents of this file are subject to the terms of the Common Development
5  * and Distribution License (the License). You may not use this file except in
6  * compliance with the License.
7  *
8  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
9  * or http://www.netbeans.org/cddl.txt.
10  *
11  * When distributing Covered Code, include this CDDL Header Notice in each file
12  * and include the License file at http://www.netbeans.org/cddl.txt.
13  * If applicable, add the following below the CDDL Header, with the fields
14  * enclosed by brackets [] replaced by your own identifying information:
15  * "Portions Copyrighted [year] [name of copyright owner]"
16  *
17  * The Original Software is NetBeans. The Initial Developer of the Original
18  * Software is Sun Microsystems, Inc. Portions Copyright 2000-2001 Sun
19  * Microsystems, Inc. All Rights Reserved.
20  *
21  * Contributor(s): Thomas Ball
22  *
23  * Version: $Revision: 1.6 $
24  */

25
26 package org.netbeans.modules.classfile;
27
28 import java.io.*;
29 import java.util.*;
30
31 /**
32  * Class representing a map of classfile attributes. The
33  * keys for this map are the names of the attributes (as Strings,
34  * not constant pool indexes). The values are byte arrays that
35  * hold the contents of the attribute.
36  *
37  * Note: if a ClassFile is created with includeCode parameter set to
38  * false, then no AttributeMaps for the classfile's methods will
39  * have Code attributes.
40  *
41  * @author Thomas Ball
42  */

43 public final class AttributeMap {
44
45     Map map;
46
47     /**
48      * Load zero or more attributes from a class, field or method.
49      */

50     static AttributeMap load(DataInputStream in, ConstantPool pool)
51       throws IOException {
52     return load(in, pool, false);
53     }
54
55     static AttributeMap load(DataInputStream in, ConstantPool pool,
56                  boolean includeCode) throws IOException {
57         int count = in.readUnsignedShort();
58         Map<String JavaDoc,byte[]> map = new HashMap<String JavaDoc,byte[]>(count + 1, (float)1.0);
59         for (int i = 0; i < count; i++) {
60         Object JavaDoc o = pool.get(in.readUnsignedShort());
61             if (!(o instanceof CPUTF8Info))
62                 throw new InvalidClassFormatException();
63         CPUTF8Info entry = (CPUTF8Info)o;
64         String JavaDoc name = entry.getName();
65         int len = in.readInt();
66         if (!includeCode && "Code".equals(name)) {
67         int n;
68         while ((n = (int)in.skip(len)) > 0 && n < len)
69             len -= n;
70         } else {
71         byte[] attr = new byte[len];
72         in.readFully(attr);
73         map.put(name, attr);
74         }
75         }
76     return new AttributeMap(map);
77     }
78
79     AttributeMap(Map<String JavaDoc,byte[]> attributes) {
80     this.map = attributes;
81     }
82
83     DataInputStream getStream(String JavaDoc name) {
84     byte[] attr = (byte[])map.get(name);
85     return attr != null ?
86         new DataInputStream(new ByteArrayInputStream(attr)) : null;
87     }
88
89     /**
90      * Returns an array containing the bytes in a specified attribute.
91      * If the attribute exists but doesn't have any length (such as
92      * the <code>Deprecated</code> attribute), then a zero-length
93      * array is returned. If the attribute doesn't exist, then null
94      * is returned.
95      */

96     byte[] get(String JavaDoc name) {
97     return (byte[])map.get(name);
98     }
99
100     /**
101      * Returns the number of attributes in this map.
102      */

103     public int size() {
104     return map.size();
105     }
106
107     /**
108      * Returns true if no attributes exist in this map.
109      */

110     public boolean isEmpty() {
111     return map.isEmpty();
112     }
113
114     /**
115      * Returns true if an attribute of the specified name exists in this map.
116      */

117     public boolean containsAttribute(String JavaDoc key) {
118     return map.containsKey(key);
119     }
120
121     /**
122      * Returns a set of names of all of the attributes in this map.
123      */

124     public Set keySet() {
125     return map.keySet();
126     }
127
128 }
129
Popular Tags