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   
9   package it.imolinfo.jbi4corba.webservice.generator.bcm;
10  
11  import it.imolinfo.jbi4corba.Logger;
12  import it.imolinfo.jbi4corba.LoggerFactory;
13  
14  import java.util.HashMap;
15  import java.util.Map;
16  
17  import org.objectweb.asm.ClassAdapter;
18  import org.objectweb.asm.ClassVisitor;
19  import org.objectweb.asm.ClassWriter;
20  import org.objectweb.asm.FieldVisitor;
21  import org.objectweb.asm.Opcodes;
22  
23  /**
24   * bytecode manipulation.
25   *
26   * For each Object visited we modify this bytecode:
27   *
28   * 1) remove the keyword 'abstract' in the class declaration.
29   */
30  public class WsdlToCorbaAdapter extends ClassAdapter {
31  
32      /**
33       * Logger.
34       */
35      private static final Logger LOG
36              = LoggerFactory.getLogger(WsdlToCorbaAdapter.class);
37  
38  
39      protected String classesDirName = null;
40      protected String className = null;
41      protected ClassWriter classWriter = null;
42  
43      protected Map<String, String> mapOfFields = new HashMap<String, String>();
44  
45      /**
46       * ByteCode Manipulation Utility.
47       */
48      private ByteCodeManipulationUtil bcmUtil = new ByteCodeManipulationUtil();
49  
50  
51      /**
52      * The adapater used to manipulate the code.
53      *
54      * @param cv      The ClassVisitor used in this object.
55      * @param cw      The ClassWriter used in this object.
56      * @param cn      The class name to manipulate.
57      * @param classes The clases used in this object.
58      */
59      public WsdlToCorbaAdapter(ClassVisitor cv,
60                                ClassWriter cw,
61                                String cn,
62                                String classes) {
63      super(cv);
64  
65      classesDirName = classes;
66      classWriter = cw;
67      className = cn;
68  
69      LOG.debug("new WsdlToCorbaAdapter; ClassVisitor=" + cv
70                  + "; ClassWriter=" + cw
71                  + "; ClassName=" + cn);
72      }
73  
74      /**
75      * Override.
76      * @param version     The version
77      * @param access      The access
78      * @param name        The name
79      * @param signature   The signature
80      * @param superName   The super name
81      * @param interfaces  The interfaces
82      */
83      @Override
84      public void visit(int version,
85                      int access,
86                      String name,
87                      String signature,
88                      String superName,
89                      String [] interfaces) {
90          LOG.debug(">>>>> visit - begin");
91  
92          LOG.debug("VISIT"
93                  + ".\n version="    + version
94                  + ";\n access="     + access
95                  + ";\n name="       + name
96                  + ";\n signature="  + signature
97                  + ";\n superName="  + superName
98                  + ";\n interfaces=" + interfaces
99                  + ".\n abstract-code=" + Opcodes.ACC_ABSTRACT
100                 + ".\n (Opcodes.ACC_ABSTRACT & access)="
101                 + (Opcodes.ACC_ABSTRACT & access)
102                 + ".\n access & (~ Opcodes.ACC_ABSTRACT)="
103                 + (access & (~ Opcodes.ACC_ABSTRACT)));
104 
105 
106         //access = Opcodes.ACC_PUBLIC;
107 
108         LOG.debug("This class (" + name + ") was an abstract "
109                 + "... now is a concrete class.");
110 
111         LOG.debug("<<<<< visit - end");
112         super.visit(version, access, name, signature, superName, interfaces);
113     }
114 
115       /**
116        * @param access        the field's access flags (see Opcodes).
117        *                    This parameter also indicates if the field is synthetic
118        *                    and/or deprecated.
119        * @param name        the field's name.
120        * @param desc        the field's descriptor (see Type).
121        * @param signature    the field's signature. May be null if the field's type
122        *                    does not use generic types.
123        * @param value        the field's initial value.
124        *                    This parameter, which may be null if the field does not
125        *                    have an initial value, must be an Integer, a Float,
126        *                    a Long, a Double or a String (for int, float, long or
127        *                    String fields respectively).
128        *                    This parameter is only used for static fields.
129        *                    Its value is IGNORED for non static fields,
130        *                    which must be initialized through bytecode
131        *                    instructions in constructors or methods.
132        *
133        * @return    a visitor to visit field annotations and attributes,
134        *             or null if this class visitor is not interested
135        *             in visiting these annotations and attributes.
136        */
137       @Override
138       public FieldVisitor visitField(
139               int access,
140               String name,
141               String desc,
142               String signature,
143               Object value) {
144     
145           LOG.debug(">>>>> visitField - begin:" + desc);
146     
147           LOG.debug("visitField. access="    + access
148                             + "; name="      + name
149                             + "; desc="      + desc
150                             + "; signature=" + signature
151                             + "; value="     + value);
152     
153           if (Opcodes.ACC_PUBLIC == access || Opcodes.ACC_PROTECTED == access) {
154 //              bcmUtil.createSetter(classWriter, className, name, desc);
155 //              bcmUtil.createGetter(classWriter, className, name, desc);
156     
157               mapOfFields.put(name, desc);
158           }
159           LOG.debug("<<<<< visitField - end");
160     
161           //return super.visitField(access, name, desc, signature, value);
162           return super.visitField(Opcodes.ACC_PROTECTED, name, desc, signature, value);
163       }
164 
165     /**
166      * XXX javadoc.
167      */
168     @Override
169     public void visitEnd() {
170 
171       for (String name : mapOfFields.keySet()) {
172         String desc = mapOfFields.get(name);
173 
174         
175         bcmUtil.createSetter(classWriter,getClassNameWithoutClassesDir(), name, desc);
176         bcmUtil.createGetter(classWriter,getClassNameWithoutClassesDir(), name, desc, false);
177       }
178 
179       bcmUtil.createToString(classWriter);
180       bcmUtil.createEquals(classWriter);
181 
182       super.visitEnd();
183     }
184 
185     /**
186      * @return  The return
187      */
188     public String getClassNameWithoutClassesDir() {
189       String c = className.substring(
190         classesDirName.length() + 1,
191         className.length());
192 
193       LOG.debug("###### getClassNameWithoutClassesDir=" + c);
194       return c;
195     }
196 
197 }