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  import it.imolinfo.jbi4corba.webservice.generator.InterfaceType;
13  import it.imolinfo.jbi4corba.webservice.generator.TypeUtils;
14  
15  import java.util.HashMap;
16  import java.util.Map;
17  
18  import org.objectweb.asm.ClassAdapter;
19  import org.objectweb.asm.ClassVisitor;
20  import org.objectweb.asm.ClassWriter;
21  import org.objectweb.asm.MethodVisitor;
22  import org.objectweb.asm.Type;
23  
24  /**
25   * For each constructor replace all parameter interface type with W3CEndpointReference.
26   */
27  public class ConstructorAdapter extends ClassAdapter {
28  
29  	/**
30  	 * Logger.
31  	 */
32  	private static final Logger LOG = LoggerFactory
33  			.getLogger(ConstructorAdapter.class);
34  
35  	protected String className = null;
36  
37  	protected ClassWriter classWriter = null;
38  
39  	protected Map<String, String> mapOfFields = new HashMap<String, String>();
40  
41  	// True if the class is an Exception
42  	//private boolean isException = false;
43  
44  	private Map<String, InterfaceType> allInterfaceTypes;
45  
46  	/**
47  	 * The adapter used to manipulate the code.
48  	 * 
49  	 * @param cv
50  	 *            The ClassVisitor used in this object.
51  	 * @param cw
52  	 *            The ClassWriter used in this object.
53  	 * @param cn
54  	 *            The class name to manipulate.
55  	 */
56  	public ConstructorAdapter(ClassVisitor cv, ClassWriter cw, String cn,
57  			Map<String, InterfaceType> allInTypes, boolean isEx) {
58  
59  		super(cv);
60  
61  		classWriter = cw;
62  		className = cn;
63  //		isException = isEx;
64  		allInterfaceTypes = allInTypes;
65  
66  		LOG.debug("CRB000604_new_GetterSetterAdapter", new Object[] { cv, cw,
67  				cn, isEx });
68  	}
69  
70  	/**
71  	 * Override.
72  	 * 
73  	 * @param version
74  	 *            The method visitor
75  	 * @param access
76  	 *            The access
77  	 * @param name
78  	 *            The name
79  	 * @param signature
80  	 *            The signature
81  	 * @param superName
82  	 *            The super name
83  	 * @param interfaces
84  	 *            The interfaces
85  	 */
86  
87  	@Override
88  	public void visit(int version, int access, String name, String signature,
89  			String superName, String[] interfaces) {
90  		LOG.debug(">>>>> visit - begin");
91  
92  		LOG.debug("CRB000603_VISIT", new Object[] { version, access, name,
93  				signature, superName, interfaces });
94  
95  		super.visit(version, access, name, signature, superName, interfaces);
96  
97  	}
98  
99  	/**
100 	 * Override.
101 	 * 
102 	 * @param access
103 	 *            The access
104 	 * @param name
105 	 *            The name
106 	 * @param desc
107 	 *            The description
108 	 * @param signature
109 	 *            The signature
110 	 * @param exceptions
111 	 *            The exceptions
112 	 * @return The return
113 	 */
114 	@Override
115 	public MethodVisitor visitMethod(int access, String name, String desc,
116 			String signature, String[] exceptions) {
117 
118 		LOG.debug(">>>>> visitMethod - begin");
119 
120 		LOG.debug("visitMethod. access=" + access + "; name=" + name
121 				+ "; desc=" + desc + "; signature=" + signature
122 				+ "; exceptions=" + exceptions);
123 
124 		// if we found the default constructor then ...
125 		if ("<init>".equals(name)) {
126 
127 			LOG.debug("default constructor modifications.");
128 			// Replace interfaceType from constructor
129 			desc = changeInterfaceTypeDesc(desc);
130 
131 			ConstructorModMethodVisitor mv = new ConstructorModMethodVisitor(
132 					super
133 							.visitMethod(access, name, desc, signature,
134 									exceptions), className, allInterfaceTypes);
135 
136 			LOG.debug("<<<<< visitMethod - end. Constructor MethodVisitor="
137 					+ mv);
138 
139 			return mv;
140 
141 		}
142 
143 		// else
144 		LOG.debug("<<<<< visitMethod - end. " + "methodName=" + name
145 				+ "; methodDescription=" + desc);
146 		return super.visitMethod(access, name, desc, signature, exceptions);
147 	}
148 
149 	/**
150 	 * This Method change the type of parameter of the constructor
151 	 * 
152 	 * @param String
153 	 *            the descriptor that contains the parameter
154 	 * */
155 
156 	private String changeInterfaceTypeDesc(String desc) {
157 
158 		// Remove "(" and ";")
159 		// Extract param from desc => there are some param with the form
160 		int start = desc.indexOf("(");        
161 	        int end = desc.lastIndexOf(")");
162 
163 		String descStart = desc.substring(0, start + 1);
164 		String descEnd = desc.substring(end, desc.length());
165 
166 		InternalMethodDescriptionParser parser = new InternalMethodDescriptionParser(
167 				desc);
168 		String params[] = parser.parse().toArray(new String[] {});
169 		String result = descStart;		
170 
171 		for (int i = 0; i < params.length; i++) {
172 			boolean isArray = TypeUtils.isArray(params[i]);
173 			if (TypeUtils.isSearchedType(params[i], isArray,
174 					allInterfaceTypes, TypeUtils.INTERFACE) != null) {
175 
176 				if (isArray) {
177 					result += Type
178 							.getDescriptor(javax.xml.ws.wsaddressing.W3CEndpointReference[].class);
179 				} else {
180 					result += Type
181 							.getDescriptor(javax.xml.ws.wsaddressing.W3CEndpointReference.class);
182 				}
183 
184 			} else {
185 				result += params[i];
186 			}
187 		}
188 		result += descEnd;
189 		return result;
190 
191 	}
192 
193 	@Override
194 	public void visitEnd() {
195 
196 		super.visitEnd();
197 	}
198 
199 }