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 it.imolinfo.jbi4corba.Logger;
11  import it.imolinfo.jbi4corba.LoggerFactory;
12  
13  import org.objectweb.asm.ClassVisitor;
14  import org.objectweb.asm.FieldVisitor;
15  import org.objectweb.asm.Opcodes;
16  import org.objectweb.asm.Type;
17  
18  /**
19   * This adapter makes a class serializable and add a serial version UID.
20   */
21  public class SerializableDecorationAdapter
22    extends SerializableInspectorAdapter {
23  
24    /**
25     * Logger.
26     */
27    private static final Logger LOG
28      = LoggerFactory.getLogger(SerializableDecorationAdapter.class);
29  
30    /**
31     * The new serial version UID for the class inspected.
32     */
33    protected Long newSerialVersionUid = 0L;
34  
35    /**
36     * Constructor.
37     *
38     * @param        cv                        The class visitor instance.
39     * @param        aNewSerialVersionUid      The new serial version UID.
40     */
41    public SerializableDecorationAdapter(ClassVisitor cv,
42                                         Long aNewSerialVersionUid) {
43      super(cv);
44      newSerialVersionUid = aNewSerialVersionUid != null ? aNewSerialVersionUid
45                                                         : new Long(0L);
46    }
47  
48    /**
49     * Override.
50     * @param version     The version
51     * @param access      The access
52     * @param name        The name
53     * @param signature   The signature
54     * @param superName   The super name
55     * @param interfaces  The interfaces
56     */
57    @Override
58    public void visit(int version, int access, String name, String signature,
59      String superName, String [] interfaces) {
60  
61      String [] implementsArray = interfaces;
62  
63      if (! implementsSerializable(interfaces)) {
64        LOG.debug("The class " + name + " does not implement Serializable.");
65  
66        int implementsSize = interfaces == null ? 0 : interfaces.length;
67  
68        implementsArray = new String[implementsSize + 1];
69  
70        if (implementsSize == 0 ) {
71  
72          implementsArray[0]
73            = SerializableInspectorAdapter.INTERNAL_NAME_OF_SERIALIZABLE;
74  
75        } else {
76  
77          System.arraycopy(interfaces, 0, implementsArray, 0, implementsSize);
78          implementsArray[implementsSize]
79            = SerializableInspectorAdapter.INTERNAL_NAME_OF_SERIALIZABLE;
80  
81        }
82  
83      }
84  
85      super.visit(version, access, name, signature, superName, implementsArray);
86    }
87  
88    /**
89     * Override.
90     * @param access     The access
91     * @param name       The name
92     * @param desc       The description
93     * @param signature  The signature
94     * @param value      The value
95     * @return           The return
96     */
97    @Override
98    public FieldVisitor visitField(int access, String name, String desc,
99      String signature, Object value) {
100 
101     LOG.debug("visitField. access=" + access + "; name=" + name
102       + "; desc=" + desc + "; signature=" + signature + "; value=" + value);
103 
104     if (hasSerialVersionUIDField(name, desc)) {
105       LOG.debug("The class " + name + " has a serial version UID:" + value);
106 
107       value = newSerialVersionUid;
108     }
109 
110     return super.visitField(access, name, desc, signature, value);
111   }
112 
113   /**
114    * Override.
115    */
116   @Override
117   public void visitEnd() {
118     if (getClassMetaInfo().getClassSerialVersionUid() == null) {
119       super.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC,
120         SerializableInspectorAdapter.FIELDNAME_SERIAL_VERSION_UID,
121         Type.LONG_TYPE.getDescriptor(),
122         null,
123         newSerialVersionUid);
124 
125       getClassMetaInfo().setClassSerialVersionUid(newSerialVersionUid);
126     }
127   }
128 }