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.ClassAdapter;
14  import org.objectweb.asm.ClassVisitor;
15  import org.objectweb.asm.ClassWriter;
16  import org.objectweb.asm.FieldVisitor;
17  import org.objectweb.asm.MethodVisitor;
18  import org.objectweb.asm.Opcodes;
19  
20  /**
21   * This class visit the bytecode of another class and executes the following
22   * three main tasks:
23   * 1) add java.io.Serializable interface
24   * 2) remove getter and setter
25   * 3) modify the scope of the member from private to public.
26   */
27  public class ValueTypeAdapter extends ClassAdapter {
28  
29    /**
30     * Logger.
31     */
32    private static final Logger LOG
33            = LoggerFactory.getLogger(ValueTypeAdapter.class);
34  
35    /**
36     * The Class Writer.
37     */
38    protected ClassWriter classWriter = null;
39  
40    /**
41     * The class name.
42     */
43    protected String className = null;
44  
45    /**
46     * Constructor.
47     *
48     * @param    cv        The class visitor.
49     * @param    cw        The class writer.
50     * @param    cn        The class name.
51     */
52    public ValueTypeAdapter(ClassVisitor cv, ClassWriter cw, String cn) {
53      super(cv);
54      classWriter = cw;
55      className = cn;
56    }
57  
58    /**
59     * Override.
60     * @param version     The version
61     * @param access      The access
62     * @param name        The name
63     * @param signature   The signature
64     * @param superName   The super name
65     * @param interfaces  The interfaces
66     */
67      @Override
68      public void visit(int version,
69                      int access,
70                      String name,
71                      String signature,
72                      String superName,
73                      String [] interfaces) {
74  
75          LOG.debug(">>>>> visit - begin");
76  
77          LOG.debug("VISIT"
78                  + ".\n version="    + version
79                  + ";\n access="     + access
80                  + ";\n name="       + name
81                  + ";\n signature="  + signature
82                  + ";\n superName="  + superName
83                  + ";\n interfaces=" + interfaces);
84  
85          // extends java.io.Serializable
86          //         java/io/Serializable
87  
88          if (interfaces == null || interfaces.length == 0) {
89  
90              // no interfaces found ... create the array and add my own one.
91              interfaces = new String [] {"java/io/Serializable"};
92  
93          } else {
94  
95              // there are one or more interfaces ...
96              // ... adjust the size of the array and add my interface.
97              String [] temp = new String [interfaces.length + 1];
98  
99              System.arraycopy(interfaces, 0, temp, 0, interfaces.length);
100             temp[interfaces.length] = "java/io/Serializable";
101 
102             interfaces = temp;
103         }
104 
105         LOG.debug("<<<<< visit - end");
106         super.visit(version, access, name, signature, superName, interfaces);
107     }
108 
109     /**
110      * Override.
111      * @param access      The access
112      * @param name        The name
113      * @param desc        The description
114      * @param signature   The signature
115      * @param value       The value
116      * @return            The return
117      */
118     @Override
119     public FieldVisitor visitField(int access,
120                                    String name,
121                                    String desc,
122                                    String signature,
123                                    Object value) {
124         LOG.debug(">>>>> visitField - begin");
125 
126         if ((Opcodes.ACC_STATIC & access) != Opcodes.ACC_STATIC) {
127             access = Opcodes.ACC_PUBLIC;
128         }
129         LOG.debug("The field " + name + " will be 'public'.");
130 
131         LOG.debug(">>>>> visitField - end");
132         return super.visitField(access, name, desc, signature, value);
133     }
134 
135     /**
136      * Override.
137      * @param access      The access
138      * @param name        The name
139      * @param desc        The description
140      * @param signature   The signature
141      * @param exceptions  The exceptions
142      * @return            The return
143      */
144     @Override
145     public MethodVisitor visitMethod(int access,
146             String name,
147             String desc,
148             String signature,
149             String[] exceptions) {
150 
151         LOG.debug(">>>>> visitMethod - begin");
152 
153         LOG.debug("visitMethod. access="     + access
154                         + "; name="       + name
155                         + "; desc="       + desc
156                         + "; signature="  + signature
157                         + "; exceptions=" + exceptions);
158 
159         // Excludes getFaultInfo...
160         if (!name.equals("getFaultInfo")) {
161             // if we found the default constructor then ...
162             if (isGetter(name, desc) || isSetter(name, desc)) {
163 
164                 LOG.debug("<<<<< visitMethod - remove method:" + name);
165                 return null;
166             }
167         }
168 
169         // else
170         LOG.debug("<<<<< visitMethod - untouched:" + name);
171         return super.visitMethod(access, name, desc, signature, exceptions);
172     }
173 
174   
175     /**
176      * @param methodName  The method name
177      * @param params      The params
178      * @return            The return
179      */
180     private boolean isGetter(String methodName, String params) {
181         if (methodName == null) {
182         	return false;
183         }
184         // else
185         if (methodName.startsWith("get") && params.startsWith("()")
186                 && methodName.length() > 3) {
187             return true;
188         }
189         // else
190         if (methodName.startsWith("is") && params.startsWith("()")
191                 && methodName.length() > 2) {
192             return true;
193         }
194         // else
195         return false;
196     }
197 
198     /**
199      * @param methodName  The method name
200      * @param params      The params
201      * @return            The return
202      */
203     private boolean isSetter(String methodName, String params) {
204         if (methodName == null) { 
205         	return false;
206         }
207         // else
208         if (methodName.length() <= 3) { 
209         	return false;
210         }
211         // else
212         if (methodName.startsWith("set") && (! params.startsWith("()"))) {
213             return true;
214         }
215         // else
216         return false;
217     }
218 
219 }