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;
9   
10  /**
11   * UnionTypeUtils - class that searches for union types in corba's method
12   * signatures
13   * 
14   * @author laurlg
15   */
16  
17  import it.imolinfo.jbi4corba.Logger;
18  import it.imolinfo.jbi4corba.LoggerFactory;
19  import it.imolinfo.jbi4corba.exception.ClassGenerationException;
20  import it.imolinfo.jbi4corba.exception.Jbi4CorbaRuntimeException;
21  import it.imolinfo.jbi4corba.webservice.generator.typedef.TypeDef;
22  import it.imolinfo.jbi4corba.webservice.runtime.CorbaTransformationUtils;
23  import it.imolinfo.jbi4corba.webservice.runtime.RuntimeInformation;
24  
25  import java.lang.reflect.Array;
26  import java.lang.reflect.InvocationTargetException;
27  import java.lang.reflect.Method;
28  import java.net.URLClassLoader;
29  import java.util.ArrayList;
30  import java.util.Arrays;
31  import java.util.HashMap;
32  import java.util.HashSet;
33  import java.util.List;
34  import java.util.Map;
35  import java.util.Set;
36  
37  import org.omg.CORBA.Any;
38  import org.omg.CORBA.TCKind;
39  import org.omg.CORBA.TypeCode;
40  import org.omg.CORBA.TypeCodePackage.BadKind;
41  import org.omg.CORBA.portable.InputStream;
42  
43  @SuppressWarnings("unchecked")
44  public abstract class TypeUtils {
45  
46  	public static final TCKind INTERFACE = TCKind.tk_objref;
47  	public static final TCKind UNION = TCKind.tk_union;
48  	public static final TCKind ANY = TCKind.tk_any;
49  
50  	private static final Map PRIMITIVE_NAME_TYPE_MAP = new HashMap();
51  	private static final Set<String> PRIMITIVE_JAVA_PARAMETER_SIGN = new HashSet<String>();
52  	private static final Map<String, Class> PRIMITIVE_JAVA_LANG_TYPE_MAP = new HashMap<String, Class>();
53  
54      private static final Logger LOG
55            = LoggerFactory.getLogger(TypeUtils.class);
56  
57  	public static final String JAXB_XML_ELEMENTS = "Ljavax/xml/bind/annotation/XmlElements;";
58  
59  	public static final String JAXB_XML_ELEMENT = "Ljavax/xml/bind/annotation/XmlElement;";
60  
61  	public static final String JAXB_XML_TYPE = "Ljavax/xml/bind/annotation/XmlType;";
62  
63  	/** Setup the primitives map. */
64  	static {
65  		PRIMITIVE_NAME_TYPE_MAP.put("boolean", Boolean.TYPE);
66  		PRIMITIVE_NAME_TYPE_MAP.put("byte", Byte.TYPE);
67  		PRIMITIVE_NAME_TYPE_MAP.put("char", Character.TYPE);
68  		PRIMITIVE_NAME_TYPE_MAP.put("short", Short.TYPE);
69  		PRIMITIVE_NAME_TYPE_MAP.put("int", Integer.TYPE);
70  		PRIMITIVE_NAME_TYPE_MAP.put("long", Long.TYPE);
71  		PRIMITIVE_NAME_TYPE_MAP.put("float", Float.TYPE);
72  		PRIMITIVE_NAME_TYPE_MAP.put("double", Double.TYPE);
73  		PRIMITIVE_JAVA_LANG_TYPE_MAP.put("boolean", Boolean.class);
74  		PRIMITIVE_JAVA_LANG_TYPE_MAP.put("byte", Byte.class);
75  		PRIMITIVE_JAVA_LANG_TYPE_MAP.put("char", Character.class);
76  		PRIMITIVE_JAVA_LANG_TYPE_MAP.put("short", Short.class);
77  		PRIMITIVE_JAVA_LANG_TYPE_MAP.put("int", Integer.class);
78  		PRIMITIVE_JAVA_LANG_TYPE_MAP.put("long", Long.class);
79  		PRIMITIVE_JAVA_LANG_TYPE_MAP.put("float", Float.class);
80  		PRIMITIVE_JAVA_LANG_TYPE_MAP.put("double", Double.class);
81  
82  		PRIMITIVE_JAVA_PARAMETER_SIGN.add("B");// byte
83  		PRIMITIVE_JAVA_PARAMETER_SIGN.add("C");// char
84  		PRIMITIVE_JAVA_PARAMETER_SIGN.add("D");// double
85  		PRIMITIVE_JAVA_PARAMETER_SIGN.add("F");// float
86  		PRIMITIVE_JAVA_PARAMETER_SIGN.add("I");// int
87  		PRIMITIVE_JAVA_PARAMETER_SIGN.add("J");// long
88  		PRIMITIVE_JAVA_PARAMETER_SIGN.add("S");// short
89  		PRIMITIVE_JAVA_PARAMETER_SIGN.add("Z");// boolean
90  	}
91  
92  	/**
93  	 * search in methodSignatures return types and parameter types for union
94  	 * types
95  	 * 
96  	 * @param classesDir
97  	 * @param methodSignatures
98  	 * @throws it.imolinfo.jbi4corba.exception.ClassGenerationException
99  	 */
100 	public void processTypes(Set<Class> allIDLTypes, String classesDir,
101 			Map operationTypes, TCKind searchedType)
102 			throws ClassGenerationException {
103 
104 		for (Class typeClass : allIDLTypes) {
105 
106 			findType(typeClass, classesDir, operationTypes, searchedType);
107 
108 		}
109 
110 	}
111 
112 	/**
113 	 * find union in a certain type and in its member classes recursively
114 	 * 
115 	 * @param typeName
116 	 * @param classesDir
117 	 * @param interfaceClassType
118 	 * @param isArray
119 	 * @param typeList
120 	 * @throws it.imolinfo.jbi4corba.exception.ClassGenerationException
121 	 */
122 	protected void findType(Class typeName, String classesDir,
123 
124 	Map<String, SearchedType> operationTypes, TCKind searchedType)
125 			throws ClassGenerationException {
126 
127 		if (isPrimitive(typeName.getName())) {
128 			return;
129 		}
130 
131 		// if array remove brakes from the typeName
132 		// if (isArray) {
133 		// int idx = typeName.indexOf('[');
134 		// if (idx > 0)
135 		// typeName = typeName.substring(0, idx);
136 		// }
137 
138 		// determine if the corba type is searchedType
139 		if (isSearchedType(typeName, classesDir, searchedType)) {
140 
141 			if (typeName == null)
142 				return;
143 
144 			// collect the uniontype needed info
145 
146 			SearchedType st = processType(typeName);
147 
148 			// add union types to the list for MethodSignature
149 			operationTypes.put(st.getTypeName(), st);
150 		}
151 	}
152 
153 	/**
154 	 * determines if the certain type is searchedType
155 	 * 
156 	 * @param typeName
157 	 * @param classesDir
158 	 * @param interfaceClassType
159 	 * @return
160 	 * @throws ClassGenerationException
161 	 */
162 	private static boolean isSearchedType(Class typeClass, String classesDir,
163 			TCKind searchedType) throws ClassGenerationException {
164 		// load the Helper class to determine if the corba type is union
165 		
166     	TypeCode typeC = getCorbaTypeTypeCode(typeClass, classesDir);
167 			if (typeC == null)
168 				return false;
169 
170 			else
171 			if (typeC.kind().equals(searchedType))
172 				return true;
173 	
174 
175 		return false;
176 	}
177 
178 	/**
179 	 * getCorbaTypeTypeCode
180 	 * 
181 	 * 
182 	 * @param typeClass
183 	 * @param classesDir
184 	 * @param searchedType
185 	 * @return
186 	 * @throws ClassGenerationException
187 	 */
188 	public static TypeCode getCorbaTypeTypeCode(Class typeClass,
189 			String classesDir)
190 			throws ClassGenerationException {
191 		Class typeHelperClass = Util.classLoad(classesDir, typeClass.getName()
192 				+ "Helper");
193 
194 		if (typeHelperClass != null) {
195 
196 			return (TypeCode) invokeMethod(typeHelperClass, "type");
197 		} else
198 			return null;
199 	}
200 	
201 	/**
202 	 * getCorbaTypeTypeCode (based on URLClassLoader)
203 	 * @param paramAny 
204 	 * @param origObj 
205 	 * @param typeClass
206 	 * @param classesDir
207 	 * @param searchedType
208 	 * 
209 	 * 
210 	 * @return
211 	 * @throws ClassGenerationException
212 	 */
213 	public static void encloseCorbaTypeInAny(Any paramAny,
214 			Object origObj, Class typeClass, RuntimeInformation runtimeInfo, Map<String, TypeDef>typeDefs)
215 			 {
216 		URLClassLoader classLoader = runtimeInfo.getCorbaClassLoader();
217 		Class typeHelperClass = null;
218 		try {
219 			String typeClassStr = typeClass.getName();
220 			try
221 			{
222 			typeHelperClass = classLoader.loadClass( typeClassStr
223 					+ "Helper");
224 			} catch (ClassNotFoundException e) {
225 				// case of value types
226 				
227 				if (typeClassStr.endsWith("Impl"))
228 				{
229 					typeClassStr = typeClassStr.substring(0, typeClassStr.length() - 4);
230 					typeHelperClass = classLoader.loadClass( typeClassStr
231 							+ "Helper");
232 				}
233 				
234 			}
235 		
236 		//  TypeDefs
237 		if ((typeDefs.containsKey(typeClassStr)) && (typeHelperClass != null)) {
238 			TypeDef typeDef = typeDefs.get(typeClassStr);			
239 			String aliasedClassName = typeDef.getAliasedClassName();
240 			if (LOG.isDebugEnabled()) {
241 				LOG.debug("typeDef:" + typeDef);
242 				LOG.debug("aliasedClassName: " + aliasedClassName);
243 				LOG.debug("typeHelperClass: " + typeHelperClass.getName());			
244 				LOG.debug("typeHelperClass:" + typeHelperClass + " with classloader: "  + typeHelperClass.getClassLoader());
245 			}
246 			Class aliasedClass = null;
247 			if (TypeUtils.isPrimitive(aliasedClassName)) {
248 				aliasedClass = typeDef.getAliasedClass();
249 			} else {
250 				// java.lang.String[]
251 				// To avoids a smix cl problem with 3.3.1
252 				aliasedClass = Class.forName(aliasedClassName, false, classLoader);
253 				// aliasedClass = classLoader.loadClass(aliasedClassName);
254 			}	
255 			if (LOG.isDebugEnabled()) {
256 				LOG.debug("aliasedClass: " + aliasedClass + " with classloader: "  + aliasedClass.getClassLoader());
257 				LOG.debug("origObj: " + origObj.getClass().getName() + " with classloader: "  + origObj.getClass().getClassLoader());
258 			}
259 			
260 			Object objectValue = null;
261 			try {
262 				objectValue = typeDef.getAliasedObject(origObj);
263 				if (aliasedClass.equals(java.lang.Object.class)) {
264 					// Handles the "TypeDef of any" case
265 					if (LOG.isDebugEnabled()) {
266 						LOG.debug("TypeDef of Any case");
267 					}
268 					aliasedClass = org.omg.CORBA.Any.class;
269 					Any myAny = runtimeInfo.getOrb().create_any();
270 					// We have to "transform" this any...
271 					CorbaTransformationUtils.transformToAny(objectValue, myAny, runtimeInfo);
272 					objectValue = myAny;
273 				}
274 			} catch (NoSuchFieldException e) {
275 				e.printStackTrace();
276 			} catch (IllegalArgumentException e) {
277 				e.printStackTrace();
278 			} catch (IllegalAccessException e) {
279 				e.printStackTrace();
280 			} catch (InvocationTargetException e) {
281 				e.printStackTrace();
282 			} catch (InstantiationException e) {
283 				e.printStackTrace();
284 			}
285 			Method method = typeHelperClass.getMethod("insert", Any.class, aliasedClass);
286 			try {				
287 				// Object objectValue = typeDef.getAliasedObject(origObj);					
288 				method.invoke(null, paramAny, objectValue);
289 			} catch (IllegalArgumentException e) {
290 				e.printStackTrace();
291 			} catch (IllegalAccessException e) {
292 				e.printStackTrace();
293 			} catch (InvocationTargetException e) {
294 				e.printStackTrace();
295 			} 			
296 		} else if (typeHelperClass != null) {
297 			Method method = typeHelperClass.getMethod("insert", Any.class, classLoader.loadClass(origObj.getClass().getName()));
298 			try {
299 				method.invoke(null, paramAny, origObj);
300 			} catch (IllegalArgumentException e) {
301 				e.printStackTrace();
302 			} catch (IllegalAccessException e) {
303 				e.printStackTrace();
304 			} catch (InvocationTargetException e) {
305 				e.printStackTrace();
306 			}
307 			 
308 		}
309 		} catch (SecurityException e) {
310 			e.printStackTrace();
311 		} catch (NoSuchMethodException e) {
312 			e.printStackTrace();
313 		}catch (ClassNotFoundException e1) {
314 			e1.printStackTrace();
315 		}
316 	}
317 	
318 	
319 
320 	/**
321 	 * extractCorbaTypeFromAny
322 	 * 
323 	 * @param paramAny
324 	 * @param typeClass
325 	 * @param classLoader
326 	 * @return
327 	 */
328 	public static Object extractCorbaTypeFromAny(Any paramAny,
329 			String typeClassStr, URLClassLoader classLoader) {
330 
331 		ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
332 		try {
333 			Thread.currentThread().setContextClassLoader(classLoader);
334 
335 			try {
336 				if (LOG.isDebugEnabled()) {
337 					LOG.debug("To extract ANY of id: " + paramAny.type().id()
338 							+ " for class: " + typeClassStr);
339 					LOG.debug("paramAny: " + paramAny.type().id()
340 							+ " for class: " + typeClassStr);
341 				}
342 			} catch (BadKind e1) {
343 				// TODO Auto-generated catch block
344 				e1.printStackTrace();
345 			}
346 
347 			Class typeHelperClass = null;
348 			try {
349 				if (!typeClassStr.endsWith("Helper")) {
350 					typeHelperClass = classLoader.loadClass(typeClassStr
351 							+ "Helper");
352 				} else {
353 					typeHelperClass = classLoader.loadClass(typeClassStr);
354 				}
355 				if (typeHelperClass != null) {
356 					if (LOG.isDebugEnabled()) {
357 						LOG.debug("typeHelperClass class:"
358 								+ typeHelperClass.getName());
359 						LOG.debug("typeHelperClass classloader:"
360 								+ typeHelperClass.getClassLoader());
361 						LOG.debug("Thread classloader:"
362 								+ Thread.currentThread()
363 										.getContextClassLoader());
364 					}
365 					Method method = typeHelperClass.getMethod("extract",
366 							Any.class);
367 					try {
368 
369 						Object obj = method.invoke(null, paramAny);
370 
371 						return obj;
372 					} catch (IllegalArgumentException e) {
373 						e.printStackTrace();
374 					} catch (IllegalAccessException e) {
375 						e.printStackTrace();
376 					} catch (InvocationTargetException e) {
377 						e.printStackTrace();
378 					}
379 
380 				}
381 			} catch (ClassNotFoundException e) {
382 				e.printStackTrace();
383 			} catch (SecurityException e) {
384 				e.printStackTrace();
385 			} catch (NoSuchMethodException e) {
386 				e.printStackTrace();
387 			}
388 		} finally {
389 			Thread.currentThread().setContextClassLoader(oldCL);
390 		}
391 		return null;
392 	}
393 	
394 	
395 	/**
396 	 * readCorbaTypeFromAnyInputStream
397 	 * 
398 	 * @param paramAny
399 	 * @param typeClass
400 	 * @param classLoader
401 	 * @return
402 	 */
403 	public static Object readCorbaTypeFromAnyInputStream(InputStream paramAny, String typeClass, URLClassLoader classLoader)
404 			 {
405 		
406 		Class typeHelperClass = null;
407 		try {
408 			if(!typeClass.endsWith("Helper")){
409                         typeHelperClass = classLoader.loadClass( typeClass
410 					+ "Helper");
411                         }else{
412                          typeHelperClass = classLoader.loadClass( typeClass);
413                         }                
414 
415 		if (typeHelperClass != null) {
416 			if (LOG.isDebugEnabled()) {
417 				LOG.debug("Using helper for class: " + typeHelperClass);
418 			}
419 			Method method = typeHelperClass.getMethod("read", InputStream.class);
420 			try {
421 				Object obj = method.invoke(null, paramAny);
422 				
423 				return obj;
424 			} catch (IllegalArgumentException e) {
425 				// TODO Auto-generated catch block
426 				e.printStackTrace();
427 			} catch (IllegalAccessException e) {
428 				// TODO Auto-generated catch block
429 				e.printStackTrace();
430 			} catch (InvocationTargetException e) {
431 				// TODO Auto-generated catch block
432 				e.printStackTrace();
433 			}
434 			 
435 		}
436 		} catch (ClassNotFoundException e) {
437 			// TODO Auto-generated catch block
438 			e.printStackTrace();
439 		} catch (SecurityException e) {
440 			// TODO Auto-generated catch block
441 			e.printStackTrace();
442 		} catch (NoSuchMethodException e) {
443 			// TODO Auto-generated catch block
444 			e.printStackTrace();
445 		}
446 		return null;
447 	}
448 	
449 	/**
450 	 * @param desc
451 	 * @return
452 	 */
453 	public static SearchedType isSearchedType(String desc, boolean isArray,
454 			Map allSearchedTypes, TCKind searchType) {
455 
456 		SearchedType st = null;
457 
458 		String type = getTypeFromTypeDescription(desc);
459 
460 		st = (SearchedType) allSearchedTypes.get(type);
461 
462 		return st;
463 	}
464 
465 	/**
466 	 * 
467 	 * @param desc
468 	 * @return
469 	 */
470 	public static String getTypeFromTypeDescription(String desc) {
471 		String type = desc;
472 		if (desc.startsWith("L") && desc.endsWith(";")) {
473 			type = desc.substring(1, desc.length() - 1);
474 		} else if (desc.startsWith("[")) {
475 			type = desc.substring(getArrayDimmentionAsPrefix(desc).length(),
476 					desc.length());
477 			if (type.startsWith("L") && type.endsWith(";")) {
478 				type = type.substring(1, type.length() - 1);
479 			}
480 		}
481 		type = Util.replaceSeparatorWithDot(type);
482 
483 		return type;
484 	}
485 
486 	/**
487 	 * isSearchedTypeAll
488 	 * 
489 	 * @param desc
490 	 * @param isArray
491 	 * @param allSearchedTypes
492 	 * @return
493 	 */
494 	public static SearchedType isSearchedTypeAll(String desc, boolean isArray,
495 			Map allSearchedTypes) {
496 		SearchedType searchedT = null;
497 		if ((desc.length() <= 1 && !isArray) || (desc.length() <= 2 && isArray))
498 			return null; // primitive types
499 
500 		  searchedT = isSearchedType(desc, isArray, allSearchedTypes, INTERFACE);
501 		if (searchedT == null)
502 			searchedT = isSearchedType(desc, isArray, allSearchedTypes, UNION);
503 
504 		if (searchedT == null)
505 			searchedT = isSearchedType(desc, isArray, allSearchedTypes, ANY);
506 
507 		return searchedT;
508 	}
509 
510 	/**
511 	 * getFieldType
512 	 * 
513 	 * @param field
514 	 * @param methods
515 	 * @return
516 	 */
517 	protected Class getFieldType(String field, Method[] methods) {
518 
519 		// determine the union field type using XXX(type param) method where XXX
520 		// is the field name
521 		for (Method method : methods) {
522 			if (method.getName().equals(field)
523 					&& method.getParameterTypes().length == 1) {
524 				return method.getParameterTypes()[0];
525 			}
526 		}
527 		return null;
528 	}
529 
530 	/**
531 	 * invokeMethod - invoke method using reflection
532 	 * 
533 	 * @param clss
534 	 * @param methodName
535 	 * @return
536 	 */
537 	private static Object invokeMethod(Class clss, String methodName, Class...methodParameters){
538 		try {
539 
540 			Method method = clss.getMethod(methodName, methodParameters);
541 			return method.invoke(clss);
542 
543 		} catch (NoSuchMethodException ex) {
544 			throw new Jbi4CorbaRuntimeException(
545 					"UnionTypeUtils - invokeMethod: NoSuchMethodException - "
546 							+ methodName, ex);
547 		} catch (SecurityException ex) {
548 			throw new Jbi4CorbaRuntimeException(
549 					"UnionTypeUtils - invokeMethod: SecurityException - "
550 							+ methodName, ex);
551 		} catch (IllegalAccessException ex) {
552 			throw new Jbi4CorbaRuntimeException(
553 					"UnionTypeUtils - invokeMethod: IllegalAccessException - "
554 							+ methodName, ex);
555 		} catch (IllegalArgumentException ex) {
556 			throw new Jbi4CorbaRuntimeException(
557 					"UnionTypeUtils - invokeMethod: IllegalArgumentException - "
558 							+ methodName, ex);
559 		} catch (InvocationTargetException ex) {
560 			throw new Jbi4CorbaRuntimeException(
561 					"UnionTypeUtils - invokeMethod: InvocationTargetException - "
562 							+ methodName, ex);
563 		}
564 	}
565 
566 	/**
567 	 * getTypeClass
568 	 * 
569 	 * @param className
570 	 * @param classesDir
571 	 * @param interfaceClassType
572 	 * @return
573 	 * @throws it.imolinfo.jbi4corba.exception.ClassGenerationException
574 	 */
575 	protected static Class getTypeClass(String className, String classesDir,
576 			Class interfaceClassType) throws ClassGenerationException {
577 		Class typeClass = null;
578 
579 		// Catch the primitive types:
580 		if (isPrimitive(className)) {
581 			typeClass = (Class) PRIMITIVE_NAME_TYPE_MAP.get(className);
582 		} else {
583 			// otherwise, try to load th class directly
584 			typeClass = Util.classLoadQuiet(classesDir, className);
585 		}
586 
587 		// Try adding the interface package to the type.
588 		if (typeClass == null) {
589 
590 			String classWithInterfacePackage = interfaceClassType.getPackage()
591 					.getName()
592 					+ "." + className;
593 			typeClass = Util.classLoadQuiet(classesDir,
594 					classWithInterfacePackage);
595 		}
596 		if (typeClass != null
597 				&& (typeClass.getName().startsWith("java.lang") || typeClass
598 						.getName().startsWith("java.io"))) {
599 			// LOG.debug("No type class found. Let's try adding the the
600 			// java.lang package");
601 
602 			typeClass = null;
603 		}
604 		return typeClass;
605 	}
606 
607 	/**
608 	 * 
609 	 * @param clsType
610 	 */
611 	protected abstract SearchedType processType(Class clsType);
612 
613 	/**
614 	 * getUnionFields
615 	 * 
616 	 * @param clsUnion
617 	 * @return
618 	 */
619 	protected abstract Map<String, Class> getFields(Class clsUnion);
620 
621 	/**
622 	 * 
623 	 * Add MethodType to methodSignature
624 	 */
625 	protected abstract void setMethodTypes(MethodSignature methodSignature,
626 			Set<String> typeList);
627 
628 	/**
629 	 * isPrimitive
630 	 * 
631 	 * @param type
632 	 * @return
633 	 */
634 	public static boolean isPrimitive(final String type) {
635 		return PRIMITIVE_NAME_TYPE_MAP.containsKey(type);
636 	}
637 
638 	/**
639 	 * isPrimitive
640 	 * 
641 	 * @param type
642 	 * @return
643 	 */
644 	public static boolean isPrimitiveParamSign(final String type) {
645 		String typeNoArray = type;
646 		int idx = -1;
647 		idx = type.lastIndexOf('[');
648 		if (idx >= 0) {
649 			typeNoArray = typeNoArray.substring(idx + 1);
650 		}
651 		return PRIMITIVE_JAVA_PARAMETER_SIGN.contains(typeNoArray);
652 	}
653 
654 	/**
655 	 * getJavaLangClassPrimitive
656 	 * 
657 	 * @param type
658 	 * @return
659 	 */
660 	public static Class getJavaLangClassPrimitive(final String type) {
661 		return PRIMITIVE_JAVA_LANG_TYPE_MAP.get(type);
662 	}
663 
664 	/**
665 	 * isJavaType
666 	 * 
667 	 * @param type
668 	 * @return
669 	 */
670 	public static boolean isJavaType(final String type) {
671 		if (type.startsWith("java.lang") || type.startsWith("java.io"))
672 			return true;
673 
674 		return false;
675 	}
676 
677 	/**
678 	 * getTypeName
679 	 * 
680 	 * @param type
681 	 * @return
682 	 */
683 	public static String getTypeName(Class type) {
684 		if (type.isArray()) {
685 
686 			Class cl = type;
687 			int dimensions = 0;
688 			while (cl.isArray()) {
689 				dimensions++;
690 				cl = cl.getComponentType();
691 			}
692 			StringBuffer sb = new StringBuffer();
693 			sb.append(cl.getName());
694 			for (int i = 0; i < dimensions; i++) {
695 				sb.append("[]");
696 			}
697 			return sb.toString();
698 
699 		}
700 		return type.getName();
701 	}
702 
703 	/**
704 	 * getTypeNameWithoutBrackets
705 	 * 
706 	 * @param type
707 	 * @return
708 	 */
709 	public static String getTypeNameWithoutBrackets(Class type) {
710 		String typeStr = getTypeName(type);
711 		int idx = typeStr.indexOf('[');
712 		if (idx > 0) {
713 			return typeStr.substring(0, idx);
714 		}
715 		return getTypeNameWithoutBrackets(typeStr);
716 	}
717 	
718 	/**
719 	 * getTypeNameWithoutBrackets
720 	 * 
721 	 * @param type
722 	 * @return
723 	 */
724 	public static String getTypeNameWithoutBrackets(String typeStr) {
725 		int idx = typeStr.indexOf('[');
726 		if (idx > 0) {
727 			return typeStr.substring(0, idx);
728 		}
729 		return typeStr;
730 	}	
731 
732 	/**
733 	 * isArray - verify that a type is array
734 	 * 
735 	 * @param type
736 	 * @return
737 	 */
738 	public static boolean isArray(String type) {
739 		if (type.startsWith("[") && type.endsWith(";"))
740 			return true;
741 		return false;
742 	}
743 
744 	/**
745 	 * getObjectDimensions return the dimensions of the respective obj array
746 	 * 
747 	 * @param obj
748 	 * @return
749 	 */
750 	public static int[] getObjectDimensions(Object obj) {
751 		int dimension = 0;
752 		int dimensions[];
753 
754 		Class objClass = obj.getClass();
755 
756 		while (objClass.isArray()) {
757 			dimension++;
758 			objClass = objClass.getComponentType();
759 		}
760 
761 		dimensions = new int[dimension];
762 		for (int i = 0; i < dimension; i++) {
763 			dimensions[i] = 0;
764 		}
765 		int idxArr = 0;
766 		Object tempObj = obj;
767 		while (idxArr < dimension) {
768 			dimensions[idxArr] = Array.getLength(tempObj);
769             LOG.debug("--> Arrays "+Arrays.toString((Object[])tempObj));
770             LOG.debug("--> Arrays "+Arrays.toString(dimensions));
771 			if (dimensions[idxArr] == 0)
772 				break;
773 			tempObj = Array.get(tempObj, 0);
774 			idxArr++;
775 		}
776 
777 		return dimensions;
778 	}
779 
780 	/**
781 	 * extractParameters
782 	 * 
783 	 * @param desc
784 	 * @return
785 	 */
786 	protected static List<String> extractParameters(String desc) {
787 
788 		List<String> listOfParams = new ArrayList<String>();
789 		String params = desc.substring(1, desc.lastIndexOf(")"));
790 
791 		int index = 0;
792 		StringBuffer strBuff = new StringBuffer();
793 		while (index < params.length()) {
794 			int ch = params.charAt(index);
795 
796 			switch (ch) {
797 			case 'B':
798 			case 'C':
799 			case 'D':
800 			case 'F':
801 			case 'I':
802 			case 'J':
803 			case 'S':
804 			case 'Z': {
805 				strBuff.append(params.charAt(index));
806 				listOfParams.add(strBuff.toString());
807 				strBuff = new StringBuffer();
808 				++index;
809 				break;
810 			}
811 			case 'L': {
812 				int semicol = params.indexOf(';', index + 1);
813 				// verify generics
814 				if ((params.length() > semicol + 2)
815 						&& (params.charAt(semicol + 1) == '>')) {
816 					semicol += 2;
817 				}
818 				strBuff.append(params.substring(index, semicol + 1));
819 				listOfParams.add(strBuff.toString());
820 				strBuff = new StringBuffer();
821 				index = semicol + 1;
822 				break;
823 			}
824 			case '[': {
825 				strBuff.append('[');
826 				++index;
827 				break;
828 			}
829 			}
830 
831 		}
832 
833 		return listOfParams;
834 
835 	}
836 
837 	/**
838 	 * extractReturnType
839 	 * 
840 	 * @param desc
841 	 * @return
842 	 */
843 	protected static String extractReturnType(String desc) {
844 
845 		String returnType = desc.substring(desc.indexOf(")") + 1);
846 
847 		return returnType;
848 	}
849 
850 	/**
851 	 * getArrayDimmentionAsPrefix
852 	 * 
853 	 * @param cls
854 	 * @return
855 	 */
856 	public static String getArrayDimmentionAsPrefix(Class cls) {
857 		StringBuffer arrStr = new StringBuffer();
858 		while (cls.isArray()) {
859 			arrStr.append("[");
860 			cls = cls.getComponentType();
861 		}
862 
863 		return arrStr.toString();
864 	}
865 
866 	/**
867 	 * getArrayDimmentionAsPrefix
868 	 * 
869 	 * @param desc
870 	 * @return
871 	 */
872 	public static String getArrayDimmentionAsPrefix(String desc) {
873 		if (desc.startsWith("[")) {
874 			return desc.substring(0, desc.lastIndexOf("[") + 1);
875 		} else
876 			return "";
877 	}
878 
879 	/**
880 	 * isEnumType
881 	 * 
882 	 * @param type
883 	 * @param enumSet
884 	 * @return
885 	 */
886 	public static boolean isEnumType(Class type, Set<String> enumSet) {
887 
888 		String simpleType = getTypeNameWithoutBrackets(type);
889 
890 		if (enumSet.contains(simpleType))
891 			return true;
892 
893 		return false;
894 	}
895 
896 	/**
897 	 * extractTypeFromTypeCode
898 	 *  extract the type of the typeCode object
899 	 * @param tc
900 	 * @return
901 	 */
902 	public static String extractTypeFromTypeCode(TypeCode tc)
903 	{
904             // Due to a idlj bug (or so it seems), we have to support TWO dofferent
905             // types of id:
906             // "IDL:it/imolinfo/jbi4corba/test/testprovidercomplex/EchoComplex:1.0";            
907             // "IDL:it/imolinfo/jbi4corba/test/testprovidercomplex/EchoComplex/EchoComplex:1.0";
908             
909 		try {
910 			String tempType = tc.id();                     
911                         String className = tc.name();                      
912 			int idxStart = tempType.indexOf(':');
913 			int idxEnd = tempType.lastIndexOf('/');
914 			if (idxEnd < 0)
915 			{
916                             idxEnd = tempType.lastIndexOf('\\');
917 			}
918 			tempType = tempType.substring(idxStart + 1, idxEnd);
919                         // For the "first" type of id
920 			if (!tempType.contains(className)) {
921                             tempType = tempType + '/' + className;
922                         }
923                         
924                         return Util.replaceSeparatorWithDot(tempType);
925 		} catch (BadKind e) {
926 			return null;
927 		}
928 	}
929         
930 	
931 
932       
933 }