| 1 |
/* |
| 2 |
* JBoss, Home of Professional Open Source. |
| 3 |
* |
| 4 |
* Copyright 2012 Red Hat, Inc. |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
package org.jboss.classfilewriter.util; |
| 19 |
|
| 20 |
import java.lang.reflect.Constructor; |
| 21 |
import java.lang.reflect.Method; |
| 22 |
import java.util.ArrayList; |
| 23 |
import java.util.List; |
| 24 |
|
| 25 |
/** |
| 26 |
* Utility class for working with method descriptors |
| 27 |
* |
| 28 |
* @author Stuart Douglas |
| 29 |
*/ |
| 30 |
public class DescriptorUtils { |
| 31 |
|
| 32 |
static final String VOID_CLASS_DESCRIPTOR = "V"; |
| 33 |
static final String BYTE_CLASS_DESCRIPTOR = "B"; |
| 34 |
static final String CHAR_CLASS_DESCRIPTOR = "C"; |
| 35 |
static final String DOUBLE_CLASS_DESCRIPTOR = "D"; |
| 36 |
static final String FLOAT_CLASS_DESCRIPTOR = "F"; |
| 37 |
static final String INT_CLASS_DESCRIPTOR = "I"; |
| 38 |
static final String LONG_CLASS_DESCRIPTOR = "J"; |
| 39 |
static final String SHORT_CLASS_DESCRIPTOR = "S"; |
| 40 |
static final String BOOLEAN_CLASS_DESCRIPTOR = "Z"; |
| 41 |
|
| 42 |
/** |
| 43 |
* Changes a class name to the internal form suitable for use in a descriptor string. |
| 44 |
* <p/> |
| 45 |
* e.g. java.lang.String => Ljava/lang/String; |
| 46 |
*/ |
| 47 |
public static String makeDescriptor(String className) { |
| 48 |
String repl = className.replace('.', '/'); |
| 49 |
return 'L' + repl + ';'; |
| 50 |
} |
| 51 |
|
| 52 |
public static String makeDescriptor(Class<?> c) { |
| 53 |
if (void.class.equals(c)) { |
| 54 |
return VOID_CLASS_DESCRIPTOR; |
| 55 |
} else if (byte.class.equals(c)) { |
| 56 |
return BYTE_CLASS_DESCRIPTOR; |
| 57 |
} else if (char.class.equals(c)) { |
| 58 |
return CHAR_CLASS_DESCRIPTOR; |
| 59 |
} else if (double.class.equals(c)) { |
| 60 |
return DOUBLE_CLASS_DESCRIPTOR; |
| 61 |
} else if (float.class.equals(c)) { |
| 62 |
return FLOAT_CLASS_DESCRIPTOR; |
| 63 |
} else if (int.class.equals(c)) { |
| 64 |
return INT_CLASS_DESCRIPTOR; |
| 65 |
} else if (long.class.equals(c)) { |
| 66 |
return LONG_CLASS_DESCRIPTOR; |
| 67 |
} else if (short.class.equals(c)) { |
| 68 |
return SHORT_CLASS_DESCRIPTOR; |
| 69 |
} else if (boolean.class.equals(c)) { |
| 70 |
return BOOLEAN_CLASS_DESCRIPTOR; |
| 71 |
} else if (c.isArray()) { |
| 72 |
return c.getName().replace('.', '/'); |
| 73 |
} else |
| 74 |
// normal object |
| 75 |
{ |
| 76 |
return makeDescriptor(c.getName()); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
public static String makeDescriptor(Constructor<?> c) { |
| 81 |
StringBuilder desc = new StringBuilder("("); |
| 82 |
for (Class<?> p : c.getParameterTypes()) { |
| 83 |
desc.append(DescriptorUtils.makeDescriptor(p)); |
| 84 |
} |
| 85 |
desc.append(")"); |
| 86 |
desc.append("V"); |
| 87 |
return desc.toString(); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* returns an array of String representations of the parameter types. Primitives are returned as their native |
| 92 |
* representations, while clases are returned in the internal descriptor form e.g. Ljava/lang/Integer; |
| 93 |
*/ |
| 94 |
public static String[] parameterDescriptors(String methodDescriptor) { |
| 95 |
int i = 1; // char 0 is a '(' |
| 96 |
List<String> ret = new ArrayList<String>(); |
| 97 |
int arraystart = -1; |
| 98 |
while (methodDescriptor.charAt(i) != ')') { |
| 99 |
String type = null; |
| 100 |
if (methodDescriptor.charAt(i) == '[') { |
| 101 |
if (arraystart == -1) { |
| 102 |
arraystart = i; |
| 103 |
} |
| 104 |
} else { |
| 105 |
if (methodDescriptor.charAt(i) == 'L') { |
| 106 |
int start = i; |
| 107 |
i++; |
| 108 |
while (methodDescriptor.charAt(i) != ';') { |
| 109 |
++i; |
| 110 |
} |
| 111 |
if (arraystart == -1) { |
| 112 |
type = methodDescriptor.substring(start, i + 1); |
| 113 |
} else { |
| 114 |
type = methodDescriptor.substring(arraystart, i + 1); |
| 115 |
} |
| 116 |
} else { |
| 117 |
if (arraystart == -1) { |
| 118 |
type = methodDescriptor.charAt(i) + ""; |
| 119 |
} else { |
| 120 |
type = methodDescriptor.substring(arraystart, i + 1); |
| 121 |
} |
| 122 |
} |
| 123 |
arraystart = -1; |
| 124 |
ret.add(type); |
| 125 |
} |
| 126 |
++i; |
| 127 |
} |
| 128 |
String[] r = new String[ret.size()]; |
| 129 |
for (int j = 0; j < ret.size(); ++j) { |
| 130 |
r[j] = ret.get(j); |
| 131 |
} |
| 132 |
return r; |
| 133 |
} |
| 134 |
|
| 135 |
public static String[] parameterDescriptors(Method m) { |
| 136 |
return parameterDescriptors(m.getParameterTypes()); |
| 137 |
} |
| 138 |
|
| 139 |
public static String[] parameterDescriptors(Class<?>[] parameters) { |
| 140 |
String[] ret = new String[parameters.length]; |
| 141 |
for (int i = 0; i < ret.length; ++i) { |
| 142 |
ret[i] = DescriptorUtils.makeDescriptor(parameters[i]); |
| 143 |
} |
| 144 |
return ret; |
| 145 |
} |
| 146 |
|
| 147 |
public static String returnType(String methodDescriptor) { |
| 148 |
return methodDescriptor.substring(methodDescriptor.lastIndexOf(')') + 1, methodDescriptor.length()); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
/** |
| 153 |
* returns true if the descriptor represents a primitive type |
| 154 |
*/ |
| 155 |
public static boolean isPrimitive(String descriptor) { |
| 156 |
if (descriptor.length() == 1) { |
| 157 |
return true; |
| 158 |
} |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* returns true if the descriptor represents a long or a double |
| 164 |
*/ |
| 165 |
public static boolean isWide(String descriptor) { |
| 166 |
if (!isPrimitive(descriptor)) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
char c = descriptor.charAt(0); |
| 170 |
if (c == 'D' || c == 'J') { |
| 171 |
return true; |
| 172 |
} |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* returns true if the class represents a long or a double |
| 178 |
*/ |
| 179 |
public static boolean isWide(Class<?> cls) { |
| 180 |
return cls == double.class || cls == long.class; |
| 181 |
} |
| 182 |
|
| 183 |
public static String methodDescriptor(Method m) { |
| 184 |
StringBuilder desc = new StringBuilder("("); |
| 185 |
for (Class<?> p : m.getParameterTypes()) { |
| 186 |
desc.append(DescriptorUtils.makeDescriptor(p)); |
| 187 |
} |
| 188 |
desc.append(")"); |
| 189 |
desc.append(DescriptorUtils.makeDescriptor(m.getReturnType())); |
| 190 |
return desc.toString(); |
| 191 |
} |
| 192 |
|
| 193 |
public static String methodDescriptor(String[] parameters, String returnType) { |
| 194 |
StringBuilder desc = new StringBuilder("("); |
| 195 |
for (String p : parameters) { |
| 196 |
desc.append(p); |
| 197 |
} |
| 198 |
desc.append(")"); |
| 199 |
desc.append(returnType); |
| 200 |
return desc.toString(); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* performs basic validation on a descriptor |
| 205 |
*/ |
| 206 |
public static String validateDescriptor(String descriptor) { |
| 207 |
if (descriptor.length() == 0) { |
| 208 |
throw new RuntimeException("descriptors may not be empty"); |
| 209 |
} |
| 210 |
if (descriptor.length() > 1) { |
| 211 |
if (descriptor.startsWith("L")) { |
| 212 |
if (!descriptor.endsWith(";")) { |
| 213 |
throw new RuntimeException(descriptor + " is not a valid descriptor"); |
| 214 |
} |
| 215 |
} else if (descriptor.startsWith("[")) { |
| 216 |
|
| 217 |
} else { |
| 218 |
throw new RuntimeException(descriptor + " is not a valid descriptor"); |
| 219 |
} |
| 220 |
} else { |
| 221 |
char type = descriptor.charAt(0); |
| 222 |
switch (type) { |
| 223 |
case 'I': |
| 224 |
case 'Z': |
| 225 |
case 'S': |
| 226 |
case 'B': |
| 227 |
case 'F': |
| 228 |
case 'D': |
| 229 |
case 'V': |
| 230 |
case 'J': |
| 231 |
case 'C': |
| 232 |
break; |
| 233 |
default: |
| 234 |
throw new RuntimeException(descriptor + " is not a valid descriptor"); |
| 235 |
} |
| 236 |
} |
| 237 |
return descriptor; |
| 238 |
} |
| 239 |
} |