001    /*
002     * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.classfile;
027    
028    import java.io.IOException;
029    import java.util.Arrays;
030    import java.util.HashMap;
031    import java.util.Iterator;
032    import java.util.Map;
033    
034    /*
035     *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
036     *  you write code that depends on this, you do so at your own risk.
037     *  This code and its internal interfaces are subject to change or
038     *  deletion without notice.</b>
039     */
040    public class Attributes implements Iterable<Attribute> {
041        Attributes(ClassReader cr) throws IOException {
042            map = new HashMap<String,Attribute>();
043            int attrs_count = cr.readUnsignedShort();
044            attrs = new Attribute[attrs_count];
045            for (int i = 0; i < attrs_count; i++) {
046                Attribute attr = Attribute.read(cr);
047                attrs[i] = attr;
048                try {
049                    map.put(attr.getName(cr.getConstantPool()), attr);
050                } catch (ConstantPoolException e) {
051                    // don't enter invalid names in map
052                }
053            }
054        }
055    
056        public Attributes(ConstantPool constant_pool, Attribute[] attrs) {
057            this.attrs = attrs;
058            map = new HashMap<String,Attribute>();
059            for (int i = 0; i < attrs.length; i++) {
060                Attribute attr = attrs[i];
061                try {
062                    map.put(attr.getName(constant_pool), attr);
063                } catch (ConstantPoolException e) {
064                    // don't enter invalid names in map
065                }
066            }
067        }
068    
069        public Iterator<Attribute> iterator() {
070            return Arrays.asList(attrs).iterator();
071        }
072    
073        public Attribute get(int index) {
074            return attrs[index];
075        }
076    
077        public Attribute get(String name) {
078            return map.get(name);
079        }
080    
081        public int getIndex(ConstantPool constant_pool, String name) {
082            for (int i = 0; i < attrs.length; i++) {
083                Attribute attr = attrs[i];
084                try {
085                    if (attr != null && attr.getName(constant_pool).equals(name))
086                        return i;
087                } catch (ConstantPoolException e) {
088                    // ignore invalid entries
089                }
090            }
091            return -1;
092        }
093    
094        public int size() {
095            return attrs.length;
096        }
097    
098        public final Attribute[] attrs;
099        public final Map<String, Attribute> map;
100    }