1 package org.objectweb.celtix.tools.utils; 2 3 import java.io.File ; 4 import java.lang.annotation.Annotation ; 5 import java.lang.reflect.Method ; 6 import java.net.URL ; 7 import java.net.URLClassLoader ; 8 import java.security.AccessController ; 9 import java.security.PrivilegedAction ; 10 import java.util.logging.Logger ; 11 12 import javax.jws.WebParam; 13 import javax.jws.WebResult; 14 15 import org.objectweb.celtix.common.i18n.Message; 16 import org.objectweb.celtix.common.logging.LogUtils; 17 import org.objectweb.celtix.tools.common.ToolException; 18 19 public final class AnnotationUtil { 20 private static final Logger LOG = LogUtils.getL7dLogger(AnnotationUtil.class); 21 private AnnotationUtil() { 22 23 } 24 25 public static <T extends Annotation > T getPrivClassAnnotation(final Class <?> clazz, 26 final Class <T> anoClass) { 27 return AccessController.doPrivileged(new PrivilegedAction <T>() { 28 public T run() { 29 return clazz.getAnnotation(anoClass); 30 } 31 }); 32 } 33 34 public static <T extends Annotation > T getPrivMethodAnnotation(final Method method, 35 final Class <T> anoClass) { 36 return AccessController.doPrivileged(new PrivilegedAction <T>() { 37 public T run() { 38 return method.getAnnotation(anoClass); 39 } 40 }); 41 } 42 43 public static Annotation [][] getPrivParameterAnnotations(final Method method) { 44 return (Annotation [][])AccessController.doPrivileged(new PrivilegedAction <Annotation [][]>() { 45 public Annotation [][] run() { 46 return method.getParameterAnnotations(); 47 } 48 }); 49 } 50 51 public static synchronized URLClassLoader getClassLoader(ClassLoader parent) { 52 URL [] urls = ProcessorUtil.pathToURLs(getClassPath()); 53 return new URLClassLoader (urls, parent); 54 } 55 56 public static synchronized Class loadClass(String className, ClassLoader parent) { 57 Class clazz = null; 58 URL [] urls = ProcessorUtil.pathToURLs(getClassPath()); 59 URLClassLoader classLoader = new URLClassLoader (urls, parent); 60 try { 61 clazz = classLoader.loadClass(className); 62 } catch (Exception e) { 63 Message msg = new Message("FAIL_TO_LOAD_CLASS", LOG, className); 64 throw new ToolException(msg, e); 65 } 66 return clazz; 67 } 68 69 private static String getClassPath() { 70 ClassLoader loader = AnnotationUtil.class.getClassLoader(); 71 StringBuffer classpath = new StringBuffer (System.getProperty("java.class.path")); 72 if (loader instanceof URLClassLoader ) { 73 URLClassLoader urlloader = (URLClassLoader )loader; 74 for (URL url : urlloader.getURLs()) { 75 classpath.append(File.pathSeparatorChar); 76 classpath.append(url.getFile()); 77 } 78 } 79 return classpath.toString(); 80 } 81 82 public static String capitalize(String name) { 83 if (name == null || name.length() == 0) { 84 return name; 85 } 86 char chars[] = name.toCharArray(); 87 chars[0] = Character.toUpperCase(chars[0]); 88 return new String (chars); 89 } 90 91 public static WebParam getWebParam(Method method, String paraName) { 92 93 Annotation [][] anno = getPrivParameterAnnotations(method); 94 int count = method.getParameterTypes().length; 95 for (int i = 0; i < count; i++) { 96 for (Annotation ann : anno[i]) { 97 if (ann.annotationType() == WebParam.class) { 98 WebParam webParam = (WebParam)ann; 99 if (paraName.equals(webParam.name())) { 100 return webParam; 101 } 102 } 103 } 104 } 105 return null; 106 107 } 108 109 public static WebResult getWebResult(Method method) { 110 111 Annotation ann = method.getAnnotation(WebResult.class); 112 if (ann == null) { 113 return null; 114 } else { 115 return (WebResult)ann; 116 } 117 } 118 119 } 120 | Popular Tags |