1    
2    /*
3    
4      The author of this software is Ian Kaplan
5      Bear Products International
6      www.bearcave.com
7      iank@bearcave.com
8    
9      Copyright (c) Ian Kaplan, 1999, 2000
10   
11     See copyright file for usage and licensing
12   
13   */
14   
15   package attr;
16   
17   import java.io.*;
18   import util.*;
19   import jconst.*;
20   
21   
22   /**
23      Attribute "factory" class used to allocate attributes.
24   
25      <p>
26   
27      This class reads the first two entries in the attribute structure,
28      the attribute_name_index and the attribute_length.  The factory
29      class uses the attribute name to select the proper attribute to
30      allocate.  As specified in the JVM spec, unknown attributes are
31      skipped.
32   
33      @author Ian Kaplan
34   
35    */
36   public final class attrFactory {
37     static dataRead dataIn = new dataRead();
38   
39     private attrFactory() {}
40   
41     //
42     // skip_data
43     // 
44     // Unknown attributes are allowed and are simply skipped.
45     //
46     private static void skip_data(int len, DataInputStream dStream ) {
47       int junk;
48   
49       for (int i = 0; i < len; i++) {
50         junk = dataIn.readU1( dStream );
51       }
52     } // skip_data
53   
54   
55     /**
56     
57        Read data from the class file and allocate the correct
58        attribute subclass.  If there is no known attribute
59        corresponding to the attribute name, skip the attribute.
60       
61        @return An attrInfo object or null.
62   
63      */
64     public static attrInfo allocAttr( DataInputStream dStream, 
65   				    constPool constPoolSec ) {
66       int name_index;
67       int length;
68       constBase obj;
69       constUtf8 name;
70       attrInfo retObj = null;
71   
72       name_index = dataIn.readU2( dStream );
73       length = dataIn.readU4( dStream );
74   
75       obj = constPoolSec.constPoolElem( name_index );
76       if (obj != null && obj instanceof constUtf8) {
77         String nameStr;
78   
79         name = (constUtf8)obj;
80         nameStr = name.getString();
81   
82         if (nameStr.compareTo( "SourceFile" ) == 0) {
83   	retObj = new srcFileAttr( nameStr, length, dStream, constPoolSec );
84         } else if (nameStr.compareTo( "ConstantValue" ) == 0) {
85   	retObj = new constValueAttr( nameStr, length, dStream, constPoolSec );
86         } else if (nameStr.compareTo( "Code" ) == 0) {
87   	retObj = new codeAttr( nameStr, length, dStream, constPoolSec );
88         }
89         else if (nameStr.compareTo( "Exceptions" ) == 0) {
90   	retObj = new exceptAttr( nameStr, length, dStream, constPoolSec );
91         }
92         else if (nameStr.compareTo( "InnerClasses" ) == 0) {
93         }
94         else if (nameStr.compareTo( "LineNumberTable" ) == 0) {
95   	retObj = new lineNumTabAttr( nameStr, length, dStream );
96         }
97         else if (nameStr.compareTo( "LocalVariableTable" ) == 0) {
98   	retObj = new localVarTabAttr(  nameStr, length, dStream, constPoolSec );
99         }
100        else if (nameStr.compareTo( "Synthetic" ) == 0) {
101  	retObj = new synthAttr( nameStr, length );
102        }
103        else if (nameStr.compareTo( "Deprecated" ) == 0) {
104  	retObj = new deprecAttr( nameStr, length );
105        }
106        else {
107  	// unrecognized attributes are skipped.
108  	skip_data( length, dStream );
109        }
110      }
111      else {
112        errorMessage.errorPrint("allocAttr: bad name index");
113      }
114  
115      return retObj;
116    } // attrAlloc
117  
118  } // attrFactory
119