Path: blob/master/src/jdk.jdeps/share/classes/com/sun/tools/classfile/Attributes.java
41161 views
/*1* Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.classfile;2627import java.io.IOException;28import java.util.Arrays;29import java.util.HashMap;30import java.util.Iterator;31import java.util.Map;3233/*34* <p><b>This is NOT part of any supported API.35* If you write code that depends on this, you do so at your own risk.36* This code and its internal interfaces are subject to change or37* deletion without notice.</b>38*/39public class Attributes implements Iterable<Attribute> {4041public final Attribute[] attrs;42public final Map<String, Attribute> map;4344Attributes(ClassReader cr) throws IOException {45map = new HashMap<>();46int attrs_count = cr.readUnsignedShort();47attrs = new Attribute[attrs_count];48for (int i = 0; i < attrs_count; i++) {49Attribute attr = Attribute.read(cr);50attrs[i] = attr;51try {52map.put(attr.getName(cr.getConstantPool()), attr);53} catch (ConstantPoolException e) {54// don't enter invalid names in map55}56}57}5859public Attributes(ConstantPool constant_pool, Attribute[] attrs) {60this.attrs = attrs;61map = new HashMap<>();62for (Attribute attr : attrs) {63try {64map.put(attr.getName(constant_pool), attr);65} catch (ConstantPoolException e) {66// don't enter invalid names in map67}68}69}7071public Attributes(Map<String, Attribute> attributes) {72this.attrs = attributes.values().toArray(new Attribute[attributes.size()]);73map = attributes;74}7576public Iterator<Attribute> iterator() {77return Arrays.asList(attrs).iterator();78}7980public Attribute get(int index) {81return attrs[index];82}8384public Attribute get(String name) {85return map.get(name);86}8788public int getIndex(ConstantPool constant_pool, String name) {89for (int i = 0; i < attrs.length; i++) {90Attribute attr = attrs[i];91try {92if (attr != null && attr.getName(constant_pool).equals(name))93return i;94} catch (ConstantPoolException e) {95// ignore invalid entries96}97}98return -1;99}100101public int size() {102return attrs.length;103}104105public int byteLength() {106int length = 2;107for (Attribute a: attrs)108length += a.byteLength();109return length;110}111}112113114