View Javadoc

1   /*******************************************************************************
2    *  Copyright (c) 2005, 2006, 2007 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.jbi4ejb.webservice.generator.bcm;
9   
10  import it.imolinfo.jbi4ejb.Logger;
11  import it.imolinfo.jbi4ejb.LoggerFactory;
12  import it.imolinfo.jbi4ejb.exception.ClassGenerationException;
13  import it.imolinfo.jbi4ejb.webservice.generator.EJBUtils;
14  
15  import java.util.ArrayList;
16  import java.util.List;
17  
18  import org.objectweb.asm.ClassAdapter;
19  import org.objectweb.asm.ClassVisitor;
20  import org.objectweb.asm.MethodVisitor;
21  
22  
23  /**
24   * Removes the Remote/EJBObject interfaces and the throws RemoteException.
25   * 
26   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a>
27   */
28  public class RemoveEJBInterfaceAdapter extends ClassAdapter {
29  
30      /** The logger. */
31      private static final Logger LOG
32      = LoggerFactory.getLogger(RemoveEJBInterfaceAdapter.class); 
33      
34      // The classes dir name.
35      private String classesDirName = null;
36  
37      /**
38       * Constructor.
39       * 
40       * @param arg0
41       *            The <code>ClassVisitor</code>
42       */
43      public RemoveEJBInterfaceAdapter(ClassVisitor arg0, String classesDirName) {
44          super(arg0);      
45          this.classesDirName = classesDirName;
46      }
47  
48      /**
49       * Removes the "implements Remote, EJBObject" clause. 
50       * 
51       * @param version
52       *          The versione
53       * @param access
54       *          the access modifier
55       * @param name
56       *          The class name
57       * @param signature
58       *          The signatur
59       * @param superName
60       *          The superclass name
61       * @param interfaces
62       *          The interfaces array.
63       * 
64       * @see org.objectweb.asm.ClassAdapter#visit(int, int, java.lang.String,
65       *      java.lang.String, java.lang.String, java.lang.String[])
66       */
67      public void visit(int version, int access, String name, String signature,
68              String superName, String [] interfaces) {
69  
70          LOG.debug("Removing java/rmi/Remote from the class: " + name);
71          List<String> newInterfaces = new ArrayList<String>();
72  
73          if (interfaces != null) {
74              for (int i = 0; i < interfaces.length; i++) {                
75                  String newInterface = interfaces[i];
76                  // If the exception is NOT a Remote, adds it.
77                  if (!(newInterface.equals("java/rmi/Remote")) && (!newInterface.equals("javax/ejb/EJBObject"))) {
78                      newInterfaces.add(interfaces[i]);
79                  }
80              }
81          } 
82  
83          // Recursive call: every interface can throw RemoteException
84          for (int i = 0; i < newInterfaces.size(); i++) {
85              String interfaceClassname  = newInterfaces.get(i).replace('/', '.');
86              try {
87                  EJBUtils.removeEJBRemoteInterface(interfaceClassname, classesDirName);
88              } catch (ClassGenerationException e) {
89                  // TODO i18n
90                  String msg = "Error in removing java.rmi.Remote from interface " + e.getMessage();
91                  LOG.warn(msg, e);
92              }
93          }
94          
95          String[] newInterfacesArray = newInterfaces.toArray(new String[0]); 
96  
97          super.visit(version, access, name, signature, superName, newInterfacesArray);
98      }
99  
100 
101     
102     
103     /**
104      * Visit the method, tremoving the "throws RemoteException, EJBException" clause.
105      * 
106      * @param access
107      *          The access modifier
108      * @param name
109      *          The method name
110      * @param desc
111      *          The description
112      * @param signature
113      *          The signature
114      * @param exceptions    
115      *          The exceptions array
116      * 
117      * @return
118      *      The MethodVisitor
119      * 
120      * @see org.objectweb.asm.ClassAdapter#visitMethod(int, java.lang.String,
121      *      java.lang.String, java.lang.String, java.lang.String[])
122      */
123     public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
124 
125         List<String> newExceptions = new ArrayList<String>();
126 
127         if (exceptions != null) {
128             for (int i = 0; i < exceptions.length; i++) {                
129                 String exception = exceptions[i];
130                 // If the exception is NOT a RemoteException, adds it.
131                 if (!(exception.equals("java/rmi/RemoteException")) && (!(exception.equals("javax/ejb/EJBException")))) {
132                     newExceptions.add(exceptions[i]);
133                 } else {
134                     LOG.debug("Removing java/rmi/RemoteException from the method: " +  name);
135                 }
136             }
137         }
138         String[] newExceptionsArray = newExceptions.toArray(new String[0]);                 
139         return super.visitMethod(access, name, desc, signature, newExceptionsArray);
140     }
141 
142 }
143 
144