View Javadoc

1   /*
2    * Copyright (C) The MetaClass Group. All rights reserved.
3    *
4    * This software is published under the terms of the Spice
5    * Software License version 1.1, a copy of which has been included
6    * with this distribution in the LICENSE.txt file.
7    */
8   package org.codehaus.metaclass.io;
9   
10  import java.io.ByteArrayInputStream;
11  import java.io.IOException;
12  import org.objectweb.asm.Attribute;
13  import org.objectweb.asm.ClassVisitor;
14  import org.objectweb.asm.CodeVisitor;
15  import org.codehaus.metaclass.model.ClassDescriptor;
16  
17  /***
18   * Visitor used to extract ClassDescriptor from .class file.
19   *
20   * @author Peter Donald
21   * @version $Revision: 1.2 $ $Date: 2003/12/11 08:41:50 $
22   */
23  class ExtractMetaDataVisitor
24      implements ClassVisitor
25  {
26      /*** The ClassDescriptor loaded if any. */
27      private ClassDescriptor m_classDescriptor;
28  
29      /*** The IOException caused when loading descriptor if any. */
30      private IOException m_ioe;
31  
32      /***
33       * @see ClassVisitor#visitEnd()
34       */
35      public void visitEnd()
36      {
37      }
38  
39      /***
40       * @see ClassVisitor#visitInnerClass(String, String, String, int)
41       */
42      public void visitInnerClass( final String name,
43                                   final String outerName,
44                                   final String innerName,
45                                   final int access )
46      {
47      }
48  
49      /***
50       * @see ClassVisitor#visit(int, String, String, String[], String)
51       */
52      public void visit( final int access,
53                         final String name,
54                         final String superName,
55                         final String[] interfaces,
56                         final String sourceFile )
57      {
58      }
59  
60      /***
61       * @see ClassVisitor#visitField(int, String, String, Object, Attribute)
62       */
63      public void visitField( final int access,
64                              final String name,
65                              final String desc,
66                              final Object value,
67                              final Attribute attrs )
68      {
69      }
70  
71      /***
72       * @see ClassVisitor#visitMethod(int, String, String, String[], Attribute)
73       */
74      public CodeVisitor visitMethod( final int access,
75                                      final String name,
76                                      final String desc,
77                                      final String[] exceptions,
78                                      final Attribute attrs )
79      {
80          return null;
81      }
82  
83      /***
84       * @see ClassVisitor#visitAttribute(Attribute)
85       */
86      public void visitAttribute( final Attribute attr )
87      {
88          if( !MetaClassIOASM.ATTRIBUTE_NAME.equals( attr.type ) )
89          {
90              return;
91          }
92          final ByteArrayInputStream stream =
93              new ByteArrayInputStream( attr.b, attr.off, attr.len );
94          try
95          {
96              m_classDescriptor = MetaClassIOBinary.IO.
97                  deserializeClass( stream );
98          }
99          catch( final IOException ioe )
100         {
101             m_ioe = ioe;
102         }
103     }
104 
105     /***
106      * Return the ClassDescriptor that was loaded.
107      *
108      * @return the ClassDescriptor that was loaded.
109      */
110     ClassDescriptor getClassDescriptor()
111     {
112         return m_classDescriptor;
113     }
114 
115     /***
116      * Return the IOException thrown during load.
117      *
118      * @return the IOException thrown during load.
119      */
120     IOException getIoe()
121     {
122         return m_ioe;
123     }
124 }