View Javadoc
1 /* 2 * The Apache Software License, Version 1.1 3 * 4 * Copyright (c) 2002 The Apache Software Foundation. All rights 5 * reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * 3. The end-user documentation included with the redistribution, 20 * if any, must include the following acknowledgment: 21 * "This product includes software developed by the 22 * Apache Software Foundation (http://www.apache.org/)." 23 * Alternately, this acknowledgment may appear in the software itself, 24 * if and wherever such third-party acknowledgments normally appear. 25 * 26 * 4. The names "Apache" and "Apache Software Foundation" must 27 * not be used to endorse or promote products derived from this 28 * software without prior written permission. For written 29 * permission, please contact apache@apache.org. 30 * 31 * 5. Products derived from this software may not be called "Apache", 32 * nor may "Apache" appear in their name, without prior written 33 * permission of the Apache Software Foundation. 34 * 35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * ==================================================================== 48 * 49 * This software consists of voluntary contributions made by many 50 * individuals on behalf of the Apache Software Foundation. For more 51 * information on the Apache Software Foundation, please see 52 * <http://www.apache.org/>;. 53 */ 54 package net.sf.cglib; 55 56 import java.lang.reflect.*; 57 import java.util.*; 58 59 /*** 60 * @version $Id: ReflectUtils.java,v 1.10 2003/05/13 06:17:51 herbyderby Exp $ 61 */ 62 abstract class ReflectUtils { 63 private static final Map primitives = new HashMap(8); 64 private static final Map transforms = new HashMap(8); 65 private static final ClassLoader defaultLoader = ReflectUtils.class.getClassLoader(); 66 private static final String[] CGLIB_PACKAGES = { "java.lang", "java.lang.reflect", "net.sf.cglib" }; 67 68 static { 69 primitives.put("char", Character.TYPE); 70 primitives.put("double", Double.TYPE); 71 primitives.put("float", Float.TYPE); 72 primitives.put("int", Integer.TYPE); 73 primitives.put("long", Long.TYPE); 74 primitives.put("short", Short.TYPE); 75 primitives.put("boolean", Boolean.TYPE); 76 transforms.put("byte", "B"); 77 transforms.put("char", "C"); 78 transforms.put("double", "D"); 79 transforms.put("float", "F"); 80 transforms.put("int", "I"); 81 transforms.put("long", "J"); 82 transforms.put("short", "S"); 83 transforms.put("boolean", "Z"); 84 } 85 86 public static Method findMethod(String desc) { 87 return findMethod(desc, defaultLoader); 88 } 89 90 public static Method findMethod(String desc, ClassLoader loader) { 91 try { 92 int lparen = desc.indexOf('('); 93 int rparen = desc.indexOf(')', lparen); 94 int dot = desc.lastIndexOf('.', lparen); 95 String className = desc.substring(0, dot).trim(); 96 String methodName = desc.substring(dot + 1, lparen).trim(); 97 List params = new ArrayList(); 98 int start = lparen + 1; 99 for (;;) { 100 int comma = desc.indexOf(',', start); 101 if (comma < 0) { 102 break; 103 } 104 params.add(desc.substring(start, comma).trim()); 105 start = comma + 1; 106 } 107 if (start < rparen) { 108 params.add(desc.substring(start, rparen).trim()); 109 } 110 Class cls = getClass(className, loader); 111 Class[] types = new Class[params.size()]; 112 for (int i = 0; i < types.length; i++) { 113 types[i] = getClass((String)params.get(i), loader); 114 } 115 return cls.getDeclaredMethod(methodName, types); 116 } catch (ClassNotFoundException e) { 117 throw new CodeGenerationException(e); 118 } catch (NoSuchMethodException e) { 119 throw new CodeGenerationException(e); 120 } 121 } 122 123 private static Class getClass(String className, ClassLoader loader) throws ClassNotFoundException { 124 return getClass(className, loader, CGLIB_PACKAGES); 125 } 126 127 private static Class getClass(String className, ClassLoader loader, String[] packages) throws ClassNotFoundException { 128 String save = className; 129 int dimensions = 0; 130 int index = 0; 131 while ((index = className.indexOf("[]", index) + 1) > 0) { 132 dimensions++; 133 } 134 StringBuffer brackets = new StringBuffer(className.length() - dimensions); 135 for (int i = 0; i < dimensions; i++) { 136 brackets.append('['); 137 } 138 className = className.substring(0, className.length() - 2 * dimensions); 139 140 String prefix = (dimensions > 0) ? brackets + "L" : ""; 141 String suffix = (dimensions > 0) ? ";" : ""; 142 try { 143 return Class.forName(prefix + className + suffix, false, loader); 144 } catch (ClassNotFoundException ignore) { } 145 for (int i = 0; i < packages.length; i++) { 146 try { 147 return Class.forName(prefix + packages[i] + '.' + className + suffix, false, loader); 148 } catch (ClassNotFoundException ignore) { } 149 } 150 if (dimensions == 0) { 151 Class c = (Class)primitives.get(className); 152 if (c != null) { 153 return c; 154 } 155 } else { 156 String transform = (String)transforms.get(className); 157 if (transform != null) { 158 try { 159 return Class.forName(brackets + transform, false, loader); 160 } catch (ClassNotFoundException ignore) { } 161 } 162 } 163 throw new ClassNotFoundException(save); 164 } 165 166 167 public static Class forName(String name, ClassLoader loader) { 168 try { 169 return Class.forName(name, false, loader); 170 } catch (ClassNotFoundException e) { 171 throw new CodeGenerationException(e); 172 } 173 } 174 175 public static Object newInstance(Class clazz) { 176 return newInstance(clazz, Constants.TYPES_EMPTY, null); 177 } 178 179 public static Object newInstance(Class clazz, Class[] parameterTypes, Object[] args) { 180 try { 181 return clazz.getConstructor(parameterTypes).newInstance(args); 182 } catch (NoSuchMethodException e) { 183 throw new CodeGenerationException(e); 184 } catch (InstantiationException e) { 185 throw new CodeGenerationException(e); 186 } catch (IllegalAccessException e) { 187 throw new CodeGenerationException(e); 188 } catch (InvocationTargetException e) { 189 throw new CodeGenerationException(e.getTargetException()); 190 } 191 } 192 193 public static Class[] getClasses(Object[] objects) { 194 Class[] classes = new Class[objects.length]; 195 for (int i = 0; i < objects.length; i++) { 196 classes[i] = objects[i].getClass(); 197 } 198 return classes; 199 } 200 201 public static Method findNewInstance(Class iface) { 202 if (!iface.isInterface()) { 203 throw new IllegalArgumentException(iface + " is not an interface"); 204 } 205 Method newInstance = null; 206 Method[] methods = iface.getDeclaredMethods(); 207 for (int i = 0; i < methods.length; i++) { 208 if (methods[i].getName().equals("newInstance")) { 209 if (newInstance != null) { 210 throw new IllegalArgumentException("Multiple newInstance methods"); 211 } 212 newInstance = methods[i]; 213 } 214 } 215 if (newInstance == null) { 216 throw new IllegalArgumentException("Missing newInstance method"); 217 } 218 return newInstance; 219 } 220 221 // getPackage returns null on JDK 1.2 222 public static String getPackageName(Class clazz) { 223 String name = clazz.getName(); 224 int idx = name.lastIndexOf('.'); 225 return (idx < 0) ? "" : name.substring(0, idx); 226 } 227 }

This page was automatically generated by Maven