Remove exported everywhere.
[SquirrelJME.git] / modules / tool-classfile / src / main / java / net / multiphasicapps / classfile / AttributeTable.java
bloba024ea2e1f2526ef0c0aaf46b2ab2fae83f90f68
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package net.multiphasicapps.classfile;
12 import cc.squirreljme.runtime.cldc.debug.Debugging;
13 import java.io.DataInputStream;
14 import java.io.IOException;
15 import java.util.Arrays;
16 import java.util.LinkedHashMap;
17 import java.util.Map;
19 /**
20 * This represents the attribute table that exists within a class.
22 * @since 2018/05/14
24 public final class AttributeTable
26 /** The attribute table. */
27 private final Map<String, Attribute> _attributes;
29 /**
30 * Initializes the attribute table.
32 * @param __a The input attributes.
33 * @throws NullPointerException On null arguments.
34 * @since 2018/05/15
36 public AttributeTable(Attribute... __a)
37 throws NullPointerException
39 this(Arrays.<Attribute>asList((__a != null ? __a :
40 new Attribute[0])));
43 /**
44 * Initializes the attribute table.
46 * @param __a The input attributes.
47 * @throws NullPointerException On null arguments.
48 * @since 2018/05/15
50 public AttributeTable(Iterable<Attribute> __a)
51 throws NullPointerException
53 if (__a == null)
54 throw new NullPointerException("NARG");
56 Map<String, Attribute> attributes = new LinkedHashMap<>();
57 for (Attribute a : __a)
59 if (a == null)
60 throw new NullPointerException("NARG");
62 String n = a.name();
63 if (!attributes.containsKey(n))
64 attributes.put(n, a);
66 this._attributes = attributes;
69 /**
70 * {@inheritDoc}
71 * @since 2018/05/15
73 @Override
74 public final boolean equals(Object __o)
76 throw Debugging.todo();
79 /**
80 * Returns the attribute that uses the specified name.
82 * @param __n The name of the attribute to get.
83 * @return The specified attribute or {@code null} if it does not exist.
84 * @throws NullPointerException On null arguments.
85 * @since 2018/05/15
87 public final Attribute get(String __n)
88 throws NullPointerException
90 if (__n == null)
91 throw new NullPointerException("NARG");
93 return this._attributes.get(__n);
96 /**
97 * {@inheritDoc}
98 * @since 2018/05/15
100 @Override
101 public final int hashCode()
103 throw Debugging.todo();
107 * Opens the attribute by the specified key.
109 * @param __n The name of the attribute to open.
110 * @return The stream to the given attribute or {@code null} if it is not
111 * valid.
112 * @throws NullPointerException On null arguments.
113 * @since 2018/05/15
115 public final DataInputStream open(String __n)
116 throws NullPointerException
118 if (__n == null)
119 throw new NullPointerException("NARG");
121 Attribute a = this.get(__n);
122 if (a == null)
123 return null;
124 return a.open();
128 * {@inheritDoc}
129 * @since 2018/05/15
131 @Override
132 public final String toString()
134 throw Debugging.todo();
138 * Returns the length of the attribute table.
140 * @return The attribute table length.
141 * @since 2018/05/14
143 public final int size()
145 return this._attributes.size();
149 * Parses the attribute table.
151 * @param __in The input stream.
152 * @param __pool The constant pool.
153 * @return The attribute table.
154 * @throws InvalidClassFormatException If the table is not correct.
155 * @throws IOException On read errors.
156 * @throws NullPointerException On null arguments.
157 * @since 2018/05/14
159 public static AttributeTable parse(Pool __pool, DataInputStream __in)
160 throws InvalidClassFormatException, IOException, NullPointerException
162 if (__in == null || __pool == null)
163 throw new NullPointerException("NARG");
165 int n = __in.readUnsignedShort();
166 Attribute[] rv = new Attribute[n];
168 // Read each entry
169 for (int i = 0; i < n; i++)
171 String name = __pool.<UTFConstantEntry>require(
172 UTFConstantEntry.class, __in.readUnsignedShort()).toString();
174 // {@squirreljme.error JC1x Attributes with a size larger than two
175 // gigabytes are not supported.}
176 int len = __in.readInt();
177 if (len < 0)
178 throw new InvalidClassFormatException("JC1x");
180 byte[] data = new byte[len];
181 __in.readFully(data);
183 rv[i] = new Attribute(name, data, 0, len);
186 return new AttributeTable(rv);