86 * Maps primitive Java type names to their descriptors. 87 */ 88 private static final Map<String, String> DESCRIPTORS; 89 90 static { 91 DESCRIPTORS = new HashMap<String, String>(); 92 DESCRIPTORS.put("void", "V"); 93 DESCRIPTORS.put("byte", "B"); 94 DESCRIPTORS.put("char", "C"); 95 DESCRIPTORS.put("double", "D"); 96 DESCRIPTORS.put("float", "F"); 97 DESCRIPTORS.put("int", "I"); 98 DESCRIPTORS.put("long", "J"); 99 DESCRIPTORS.put("short", "S"); 100 DESCRIPTORS.put("boolean", "Z"); 101 } 102 103 /** 104 * Creates a new {@link Method}. 105 * 106 * @param name the method's name. 107 * @param desc the method's descriptor. 108 */ 109 public Method(final String name, final String desc) { 110 this.name = name; 111 this.desc = desc; 112 } 113 114 /** 115 * Creates a new {@link Method}. 116 * 117 * @param name the method's name. 118 * @param returnType the method's return type. 119 * @param argumentTypes the method's argument types. 120 */ 121 public Method( 122 final String name, 123 final Type returnType, 124 final Type[] argumentTypes) 125 { 126 this(name, Type.getMethodDescriptor(returnType, argumentTypes)); 127 } 128 129 /** 130 * Creates a new {@link Method}. 131 * 132 * @param m a java.lang.reflect method descriptor 133 * @return a {@link Method} corresponding to the given Java method 134 * declaration. 135 */ 136 public static Method getMethod(java.lang.reflect.Method m) { 137 return new Method(m.getName(), Type.getMethodDescriptor(m)); 138 } 139 140 /** 141 * Creates a new {@link Method}. 142 * 143 * @param c a java.lang.reflect constructor descriptor 144 * @return a {@link Method} corresponding to the given Java constructor 145 * declaration. 146 */ 147 public static Method getMethod(java.lang.reflect.Constructor<?> c) { 148 return new Method("<init>", Type.getConstructorDescriptor(c)); 149 } 150 151 /** 152 * Returns a {@link Method} corresponding to the given Java method 153 * declaration. 154 * 155 * @param method a Java method declaration, without argument names, of the 156 * form "returnType name (argumentType1, ... argumentTypeN)", where 157 * the types are in plain Java (e.g. "int", "float", 158 * "java.util.List", ...). Classes of the java.lang package can be 159 * specified by their unqualified name; all other classes names must 160 * be fully qualified. 161 * @return a {@link Method} corresponding to the given Java method 162 * declaration. 163 * @throws IllegalArgumentException if <code>method</code> could not get 164 * parsed. 165 */ 166 public static Method getMethod(final String method) 167 throws IllegalArgumentException 168 { 169 return getMethod(method, false); 170 } 171 172 /** 173 * Returns a {@link Method} corresponding to the given Java method 174 * declaration. 175 * 176 * @param method a Java method declaration, without argument names, of the 177 * form "returnType name (argumentType1, ... argumentTypeN)", where 178 * the types are in plain Java (e.g. "int", "float", 179 * "java.util.List", ...). Classes of the java.lang package may be 180 * specified by their unqualified name, depending on the 181 * defaultPackage argument; all other classes names must be fully 182 * qualified. 183 * @param defaultPackage true if unqualified class names belong to the 184 * default package, or false if they correspond to java.lang classes. 185 * For instance "Object" means "Object" if this option is true, or 186 * "java.lang.Object" otherwise. 187 * @return a {@link Method} corresponding to the given Java method 188 * declaration. 189 * @throws IllegalArgumentException if <code>method</code> could not get 190 * parsed. 191 */ 192 public static Method getMethod( 193 final String method, 194 final boolean defaultPackage) throws IllegalArgumentException 195 { 196 int space = method.indexOf(' '); 197 int start = method.indexOf('(', space) + 1; 198 int end = method.indexOf(')', start); 199 if (space == -1 || start == -1 || end == -1) { 200 throw new IllegalArgumentException(); 201 } 202 String returnType = method.substring(0, space); 203 String methodName = method.substring(space + 1, start - 1).trim(); 204 StringBuffer sb = new StringBuffer(); 205 sb.append('('); 206 int p; 207 do { 208 String s; 209 p = method.indexOf(',', start); 210 if (p == -1) { 211 s = map(method.substring(start, end).trim(), defaultPackage); 212 } else { 213 s = map(method.substring(start, p).trim(), defaultPackage); 214 start = p + 1; 215 } | 86 * Maps primitive Java type names to their descriptors. 87 */ 88 private static final Map<String, String> DESCRIPTORS; 89 90 static { 91 DESCRIPTORS = new HashMap<String, String>(); 92 DESCRIPTORS.put("void", "V"); 93 DESCRIPTORS.put("byte", "B"); 94 DESCRIPTORS.put("char", "C"); 95 DESCRIPTORS.put("double", "D"); 96 DESCRIPTORS.put("float", "F"); 97 DESCRIPTORS.put("int", "I"); 98 DESCRIPTORS.put("long", "J"); 99 DESCRIPTORS.put("short", "S"); 100 DESCRIPTORS.put("boolean", "Z"); 101 } 102 103 /** 104 * Creates a new {@link Method}. 105 * 106 * @param name 107 * the method's name. 108 * @param desc 109 * the method's descriptor. 110 */ 111 public Method(final String name, final String desc) { 112 this.name = name; 113 this.desc = desc; 114 } 115 116 /** 117 * Creates a new {@link Method}. 118 * 119 * @param name 120 * the method's name. 121 * @param returnType 122 * the method's return type. 123 * @param argumentTypes 124 * the method's argument types. 125 */ 126 public Method(final String name, final Type returnType, 127 final Type[] argumentTypes) { 128 this(name, Type.getMethodDescriptor(returnType, argumentTypes)); 129 } 130 131 /** 132 * Creates a new {@link Method}. 133 * 134 * @param m 135 * a java.lang.reflect method descriptor 136 * @return a {@link Method} corresponding to the given Java method 137 * declaration. 138 */ 139 public static Method getMethod(java.lang.reflect.Method m) { 140 return new Method(m.getName(), Type.getMethodDescriptor(m)); 141 } 142 143 /** 144 * Creates a new {@link Method}. 145 * 146 * @param c 147 * a java.lang.reflect constructor descriptor 148 * @return a {@link Method} corresponding to the given Java constructor 149 * declaration. 150 */ 151 public static Method getMethod(java.lang.reflect.Constructor<?> c) { 152 return new Method("<init>", Type.getConstructorDescriptor(c)); 153 } 154 155 /** 156 * Returns a {@link Method} corresponding to the given Java method 157 * declaration. 158 * 159 * @param method 160 * a Java method declaration, without argument names, of the form 161 * "returnType name (argumentType1, ... argumentTypeN)", where 162 * the types are in plain Java (e.g. "int", "float", 163 * "java.util.List", ...). Classes of the java.lang package can 164 * be specified by their unqualified name; all other classes 165 * names must be fully qualified. 166 * @return a {@link Method} corresponding to the given Java method 167 * declaration. 168 * @throws IllegalArgumentException 169 * if <code>method</code> could not get parsed. 170 */ 171 public static Method getMethod(final String method) 172 throws IllegalArgumentException { 173 return getMethod(method, false); 174 } 175 176 /** 177 * Returns a {@link Method} corresponding to the given Java method 178 * declaration. 179 * 180 * @param method 181 * a Java method declaration, without argument names, of the form 182 * "returnType name (argumentType1, ... argumentTypeN)", where 183 * the types are in plain Java (e.g. "int", "float", 184 * "java.util.List", ...). Classes of the java.lang package may 185 * be specified by their unqualified name, depending on the 186 * defaultPackage argument; all other classes names must be fully 187 * qualified. 188 * @param defaultPackage 189 * true if unqualified class names belong to the default package, 190 * or false if they correspond to java.lang classes. For instance 191 * "Object" means "Object" if this option is true, or 192 * "java.lang.Object" otherwise. 193 * @return a {@link Method} corresponding to the given Java method 194 * declaration. 195 * @throws IllegalArgumentException 196 * if <code>method</code> could not get parsed. 197 */ 198 public static Method getMethod(final String method, 199 final boolean defaultPackage) throws IllegalArgumentException { 200 int space = method.indexOf(' '); 201 int start = method.indexOf('(', space) + 1; 202 int end = method.indexOf(')', start); 203 if (space == -1 || start == -1 || end == -1) { 204 throw new IllegalArgumentException(); 205 } 206 String returnType = method.substring(0, space); 207 String methodName = method.substring(space + 1, start - 1).trim(); 208 StringBuffer sb = new StringBuffer(); 209 sb.append('('); 210 int p; 211 do { 212 String s; 213 p = method.indexOf(',', start); 214 if (p == -1) { 215 s = map(method.substring(start, end).trim(), defaultPackage); 216 } else { 217 s = map(method.substring(start, p).trim(), defaultPackage); 218 start = p + 1; 219 } |