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.ByteArrayOutputStream;
11  import java.io.IOException;
12  import org.objectweb.asm.Attribute;
13  import org.objectweb.asm.ClassAdapter;
14  import org.objectweb.asm.ClassVisitor;
15  import org.codehaus.metaclass.model.ClassDescriptor;
16  
17  /***
18   * Create a ClassAdapter that simply adds the MetaClass metadata to .class
19   * file.
20   *
21   * @author Peter Donald
22   * @version $Revision: 1.2 $ $Date: 2003/12/11 08:41:50 $
23   */
24  class AddMetaDataAdapter
25      extends ClassAdapter
26  {
27      /*** The class descriptor to add. */
28      final ClassDescriptor m_descriptor;
29  
30      /*** The IOE exception thrown when serializing descriptor. */
31      private IOException m_ioe;
32  
33      /***
34       * Create the adapter.
35       *
36       * @param cv the visitor to delegate to.
37       * @param descriptor the descriptor to add
38       */
39      AddMetaDataAdapter( final ClassVisitor cv,
40                          final ClassDescriptor descriptor )
41      {
42          super( cv );
43          m_descriptor = descriptor;
44      }
45  
46      /***
47       * @see ClassAdapter#visitAttribute(Attribute)
48       */
49      public void visitAttribute( final Attribute attr )
50      {
51          if( !MetaClassIOASM.ATTRIBUTE_NAME.equals( attr.type ) )
52          {
53              super.visitAttribute( attr );
54          }
55      }
56  
57      /***
58       * @see ClassAdapter#visit(int, String, String, String[], String)
59       */
60      public void visit( final int access,
61                         final String name,
62                         final String superName,
63                         final String[] interfaces,
64                         final String sourceFile )
65      {
66          super.visit( access, name, superName, interfaces, sourceFile );
67          try
68          {
69              final byte[] bytes = toBytes();
70              final Attribute attr =
71                  new Attribute( MetaClassIOASM.ATTRIBUTE_NAME,
72                                 bytes,
73                                 0,
74                                 bytes.length );
75              super.visitAttribute( attr );
76          }
77          catch( final IOException ioe )
78          {
79              m_ioe = ioe;
80          }
81      }
82  
83      /***
84       * Convert ClassDescriptor into bytes.
85       *
86       * @return the bytes
87       * @throws IOException if unable to convert descriptor into bytes
88       */
89      byte[] toBytes()
90          throws IOException
91      {
92          final ByteArrayOutputStream output = new ByteArrayOutputStream();
93          MetaClassIOBinary.IO.serializeClass( output, m_descriptor );
94          final byte[] bytes = output.toByteArray();
95          return bytes;
96      }
97  
98      /***
99       * Return the IOException thrown during serialization.
100      *
101      * @return the IOException thrown during serialization.
102      */
103     IOException getIoe()
104     {
105         return m_ioe;
106     }
107 }