Author: ebourg Date: Fri Feb 20 01:05:15 2015 New Revision: 1661050 URL: http://svn.apache.org/r1661050 Log: Allow Attributes to be read from a DataInput
Added: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/UnknownAttributeReader.java - copied, changed from r1654901, commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/AttributeReader.java Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Attribute.java commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/AttributeReader.java commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Code.java commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Field.java commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Method.java commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Unknown.java Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Attribute.java URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Attribute.java?rev=1661050&r1=1661049&r2=1661050&view=diff ============================================================================== --- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Attribute.java (original) +++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Attribute.java Fri Feb 20 01:05:15 2015 @@ -17,6 +17,7 @@ */ package org.apache.bcel.classfile; +import java.io.DataInput; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; @@ -90,7 +91,7 @@ public abstract class Attribute implemen file.writeInt(length); } - private static final Map<String, AttributeReader> readers = new HashMap<String, AttributeReader>(); + private static final Map<String, Object> readers = new HashMap<String, Object>(); /** * Add an Attribute reader capable of parsing (user-defined) attributes @@ -99,6 +100,7 @@ public abstract class Attribute implemen * * @param name the name of the attribute as stored in the class file * @param r the reader object + * @deprecated Use {@link #addAttributeReader(String, UnknownAttributeReader)} instead */ public static void addAttributeReader(String name, AttributeReader r) { @@ -106,6 +108,19 @@ public abstract class Attribute implemen } /** + * Add an Attribute reader capable of parsing (user-defined) attributes + * named "name". You should not add readers for the standard attributes such + * as "LineNumberTable", because those are handled internally. + * + * @param name the name of the attribute as stored in the class file + * @param r the reader object + */ + public static void addAttributeReader(String name, UnknownAttributeReader r) + { + readers.put(name, r); + } + + /** * Remove attribute reader * * @param name the name of the attribute as stored in the class file @@ -132,6 +147,26 @@ public abstract class Attribute implemen public static Attribute readAttribute(DataInputStream file, ConstantPool constant_pool) throws IOException, ClassFormatException { + return readAttribute((DataInput) file, constant_pool); + } + + /** + * Class method reads one attribute from the input data stream. This method + * must not be accessible from the outside. It is called by the Field and + * Method constructor methods. + * + * @see Field + * @see Method + * + * @param file Input stream + * @param constant_pool Array of constants + * @return Attribute + * @throws IOException + * @throws ClassFormatException + */ + public static Attribute readAttribute(DataInput file, ConstantPool constant_pool) + throws IOException, ClassFormatException + { byte tag = Constants.ATTR_UNKNOWN; // Unknown attribute // Get class name from constant pool via `name_index' indirection int name_index = file.readUnsignedShort(); @@ -155,10 +190,14 @@ public abstract class Attribute implemen switch (tag) { case Constants.ATTR_UNKNOWN: - AttributeReader r = readers.get(name); - if (r != null) + Object r = readers.get(name); + if (r instanceof UnknownAttributeReader) + { + return ((UnknownAttributeReader) r).createAttribute(name_index, length, file, constant_pool); + } + else if (r instanceof AttributeReader && file instanceof DataInputStream) { - return r.createAttribute(name_index, length, file, constant_pool); + return ((AttributeReader) r).createAttribute(name_index, length, (DataInputStream) file, constant_pool); } return new Unknown(name_index, length, file, constant_pool); case Constants.ATTR_CONSTANT_VALUE: Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/AttributeReader.java URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/AttributeReader.java?rev=1661050&r1=1661049&r2=1661050&view=diff ============================================================================== --- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/AttributeReader.java (original) +++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/AttributeReader.java Fri Feb 20 01:05:15 2015 @@ -25,6 +25,8 @@ package org.apache.bcel.classfile; * @see Attribute * @version $Id$ * @author <A HREF="mailto:m.d...@gmx.de">M. Dahm</A> + * + * @deprecated Use UnknownAttributeReader instead */ public interface AttributeReader { @@ -54,6 +56,5 @@ public interface AttributeReader { @see Attribute#addAttributeReader( String, AttributeReader ) */ - Attribute createAttribute( int name_index, int length, java.io.DataInputStream file, - ConstantPool constant_pool ); + Attribute createAttribute( int name_index, int length, java.io.DataInputStream file, ConstantPool constant_pool ); } Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Code.java URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Code.java?rev=1661050&r1=1661049&r2=1661050&view=diff ============================================================================== --- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Code.java (original) +++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Code.java Fri Feb 20 01:05:15 2015 @@ -17,7 +17,7 @@ */ package org.apache.bcel.classfile; -import java.io.DataInputStream; +import java.io.DataInput; import java.io.DataOutputStream; import java.io.IOException; import org.apache.bcel.Constants; @@ -70,7 +70,7 @@ public final class Code extends Attribut * @param file Input stream * @param constant_pool Array of constants */ - Code(int name_index, int length, DataInputStream file, ConstantPool constant_pool) + Code(int name_index, int length, DataInput file, ConstantPool constant_pool) throws IOException { // Initialize with some default values which will be overwritten later this(name_index, length, file.readUnsignedShort(), file.readUnsignedShort(), (byte[]) null, Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Field.java URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Field.java?rev=1661050&r1=1661049&r2=1661050&view=diff ============================================================================== --- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Field.java (original) +++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Field.java Fri Feb 20 01:05:15 2015 @@ -17,7 +17,7 @@ */ package org.apache.bcel.classfile; -import java.io.DataInputStream; +import java.io.DataInput; import java.io.IOException; import org.apache.bcel.Constants; import org.apache.bcel.generic.Type; @@ -63,7 +63,7 @@ public final class Field extends FieldOr * Construct object from file stream. * @param file Input stream */ - Field(DataInputStream file, ConstantPool constant_pool) throws IOException, + Field(DataInput file, ConstantPool constant_pool) throws IOException, ClassFormatException { super(file, constant_pool); } Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java?rev=1661050&r1=1661049&r2=1661050&view=diff ============================================================================== --- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java (original) +++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/FieldOrMethod.java Fri Feb 20 01:05:15 2015 @@ -17,6 +17,7 @@ */ package org.apache.bcel.classfile; +import java.io.DataInput; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; @@ -68,9 +69,20 @@ public abstract class FieldOrMethod exte * @param file Input stream * @throws IOException * @throws ClassFormatException + * @deprecated Use {@link #FieldOrMethod(java.io.DataInput, ConstantPool)} instead. */ protected FieldOrMethod(DataInputStream file, ConstantPool constant_pool) throws IOException, ClassFormatException { + this((DataInput) file, constant_pool); + } + + /** + * Construct object from file stream. + * @param file Input stream + * @throws IOException + * @throws ClassFormatException + */ + protected FieldOrMethod(DataInput file, ConstantPool constant_pool) throws IOException, ClassFormatException { this(file.readUnsignedShort(), file.readUnsignedShort(), file.readUnsignedShort(), null, constant_pool); attributes_count = file.readUnsignedShort(); Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Method.java URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Method.java?rev=1661050&r1=1661049&r2=1661050&view=diff ============================================================================== --- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Method.java (original) +++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Method.java Fri Feb 20 01:05:15 2015 @@ -17,7 +17,7 @@ */ package org.apache.bcel.classfile; -import java.io.DataInputStream; +import java.io.DataInput; import java.io.IOException; import org.apache.bcel.Constants; import org.apache.bcel.generic.Type; @@ -76,7 +76,7 @@ public final class Method extends FieldO * @throws IOException * @throws ClassFormatException */ - Method(DataInputStream file, ConstantPool constant_pool) throws IOException, + Method(DataInput file, ConstantPool constant_pool) throws IOException, ClassFormatException { super(file, constant_pool); } Modified: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Unknown.java URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Unknown.java?rev=1661050&r1=1661049&r2=1661050&view=diff ============================================================================== --- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Unknown.java (original) +++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/Unknown.java Fri Feb 20 01:05:15 2015 @@ -29,13 +29,13 @@ import org.apache.bcel.Constants; * application-specific) attribute of a class. It is instantiated from the * {@link Attribute#readAttribute(java.io.DataInput, ConstantPool)} method. * Applications that need to read in application-specific attributes should create an - * {@link AttributeReader} implementation and attach it via - * {@link Attribute#addAttributeReader(String, AttributeReader)}. + * {@link UnknownAttributeReader} implementation and attach it via + * {@link Attribute#addAttributeReader(String, UnknownAttributeReader)}. * * @version $Id$ * @see org.apache.bcel.classfile.Attribute - * @see org.apache.bcel.classfile.AttributeReader + * @see org.apache.bcel.classfile.UnknownAttributeReader * @author <A HREF="mailto:m.d...@gmx.de">M. Dahm</A> */ public final class Unknown extends Attribute { Copied: commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/UnknownAttributeReader.java (from r1654901, commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/AttributeReader.java) URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/UnknownAttributeReader.java?p2=commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/UnknownAttributeReader.java&p1=commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/AttributeReader.java&r1=1654901&r2=1661050&rev=1661050&view=diff ============================================================================== --- commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/AttributeReader.java (original) +++ commons/proper/bcel/trunk/src/main/java/org/apache/bcel/classfile/UnknownAttributeReader.java Fri Feb 20 01:05:15 2015 @@ -13,47 +13,37 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ + package org.apache.bcel.classfile; /** * Unknown (non-standard) attributes may be read via user-defined factory * objects that can be registered with the Attribute.addAttributeReader * method. These factory objects should implement this interface. - + * * @see Attribute * @version $Id$ - * @author <A HREF="mailto:m.d...@gmx.de">M. Dahm</A> */ -public interface AttributeReader { +public interface UnknownAttributeReader { /** - When this attribute reader is added via the static method - Attribute.addAttributeReader, an attribute name is associated with it. - As the class file parser parses attributes, it will call various - AttributeReaders based on the name of the attributes it is - constructing. - - @param name_index An index into the constant pool, indexing a - ConstantUtf8 that represents the name of the attribute. - - @param length The length of the data contained in the attribute. This - is written into the constant pool and should agree with what the - factory expects the length to be. - - @param file This is the data input stream that the factory needs to read - its data from. - - @param constant_pool This is the constant pool associated with the - Attribute that we are constructing. - - @return The user-defined AttributeReader should take this data and use - it to construct an attribute. In the case of errors, a null can be - returned which will cause the parsing of the class file to fail. - - @see Attribute#addAttributeReader( String, AttributeReader ) + * When this attribute reader is added via the static method Attribute.addAttributeReader, + * an attribute name is associated with it. As the class file parser parses attributes, + * it will call various AttributeReaders based on the name of the attributes it is constructing. + * + * @param name_index An index into the constant pool, indexing a ConstantUtf8 + * that represents the name of the attribute. + * @param length The length of the data contained in the attribute. This is written + * into the constant pool and should agree with what the factory expects the length to be. + * @param input This is the data input that the factory needs to read its data from. + * @param constant_pool This is the constant pool associated with the Attribute that we are constructing. + * + * @return The user-defined AttributeReader should take this data and use + * it to construct an attribute. In the case of errors, a null can be + * returned which will cause the parsing of the class file to fail. + * + * @see Attribute#addAttributeReader(String, UnknownAttributeReader) */ - Attribute createAttribute( int name_index, int length, java.io.DataInputStream file, - ConstantPool constant_pool ); + Attribute createAttribute( int name_index, int length, java.io.DataInput file, ConstantPool constant_pool ); }