View Javadoc

1    /****************************************************************************
2    * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/lgpl.html
7    ****************************************************************************/
8   package it.imolinfo.jbi4corba.webservice.generator.bcm;
9   
10  import java.util.HashSet;
11  import java.util.Set;
12  
13  import it.imolinfo.jbi4corba.Logger;
14  import it.imolinfo.jbi4corba.LoggerFactory;
15  import it.imolinfo.jbi4corba.webservice.generator.ClassMetaInfo;
16  
17  import org.objectweb.asm.ClassAdapter;
18  import org.objectweb.asm.ClassVisitor;
19  import org.objectweb.asm.FieldVisitor;
20  import org.objectweb.asm.Type;
21  
22  /**
23   * This adapter inspect a class to find Serializable interface and serial
24   * version UID.
25   */
26  public class SerializableInspectorAdapter extends ClassAdapter {
27  
28    
29    /**
30     * The internal name of the java interface 'Serializable'.
31     */
32    public static final String INTERNAL_NAME_OF_SERIALIZABLE
33      = "java/io/Serializable";
34  
35    /**
36     * The field 'serialVersionUID'.
37     */
38    public static final String FIELDNAME_SERIAL_VERSION_UID = "serialVersionUID";
39  
40    /**
41     * Logger.
42     */
43    private static final Logger LOG
44      = LoggerFactory.getLogger(SerializableInspectorAdapter.class);
45  
46    
47    /**
48     * The meta information of the class inspected.
49     */
50    protected ClassMetaInfo classMetaInfo = new ClassMetaInfo();
51  
52    /**
53     * Constructor.
54     *
55     * @param        cv        The class visitor instance.
56     */
57    public SerializableInspectorAdapter(ClassVisitor cv) {
58      super(cv);
59    }
60    
61    /**
62     * Override.
63     * @param version     The version
64     * @param access      The access
65     * @param name        The name
66     * @param signature   The signature
67     * @param superName   The superName
68     * @param interfaces  The interfaces
69     */
70    @Override
71    public void visit(int version, int access, String name, String signature,
72      String superName, String [] interfaces) {
73  
74      LOG.debug("visit. version=" + version + "; access=" + access
75        + "; name" + name + "; superName=" + superName);
76  
77      if (implementsSerializable(interfaces)) {
78        LOG.debug("The class " + name + " implements Serializable.");
79        classMetaInfo.setSerializable(true);
80  
81      } else {
82        LOG.debug("The class " + name + " does not implement Serializable.");
83        classMetaInfo.setSerializable(false);
84      }
85  
86      classMetaInfo.setClassName(name.replace('/', '.'));
87      classMetaInfo.setSuperClassName(superName.replace('/', '.'));
88  
89      Set<String> set = new HashSet<String>();
90      if (interfaces != null) {
91        for (String current : interfaces) {
92          set.add(current.replace('/', '.'));
93        }
94      }
95      classMetaInfo.setInterfaces(set);
96  
97      super.visit(version, access, name, signature, superName, interfaces);
98    }
99  
100   /**
101    * Override.
102    * @param access      The access
103    * @param name        The name
104    * @param desc        The description
105    * @param signature   The signature
106    * @param value       The value
107    * @return            The return
108    */
109   @Override
110   public FieldVisitor visitField(int access, String name, String desc,
111     String signature, Object value) {
112 
113     LOG.debug("visitField. access=" + access + "; name=" + name
114       + "; desc=" + desc + "; signature=" + signature + "; value=" + value);
115 
116     if (hasSerialVersionUIDField(name, desc)) {
117       LOG.debug("The class " + name + " has a serial version UID:" + value);
118 
119       classMetaInfo.setClassSerialVersionUid((Long) value);
120     }
121 
122     return super.visitField(access, name, desc, signature, value);
123   }
124 
125   /**
126    * Utility.
127    * @param name  The name
128    * @param desc  The description
129    * @return      The return
130    */
131   //Utility
132   protected boolean hasSerialVersionUIDField(String name, String desc) {
133     if (FIELDNAME_SERIAL_VERSION_UID.equals(name)
134         && Type.LONG_TYPE.getDescriptor().equals(desc)) {
135 
136       LOG.debug(FIELDNAME_SERIAL_VERSION_UID + " found.");
137       return true;
138     }
139     // else
140     return false;
141   }
142 
143   /**
144    * @param interfaces  The interfaces
145    * @return            The return
146    */
147   protected boolean implementsSerializable(String [] interfaces) {
148     if (interfaces == null || interfaces.length == 0) {
149       return false;
150     }
151     // else
152     for (String current : interfaces) {
153       if (INTERNAL_NAME_OF_SERIALIZABLE.equals(current)) {
154         return true;
155       }
156     }
157     // else
158     return false;
159   }
160 
161   /**
162    * @return            The return
163    */
164   public ClassMetaInfo getClassMetaInfo() {
165     return classMetaInfo;
166   }
167 
168   /**
169    * @param classMetaInfo  The class meta info
170    */
171   public void setClassMetaInfo(ClassMetaInfo classMetaInfo) {
172     this.classMetaInfo = classMetaInfo;
173   }
174 
175 }