Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) OSGi Alliance (2009, 2010). All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package aQute.bnd.compatibility; |
| 18 | |
| 19 | import java.lang.reflect.*; |
| 20 | import java.util.*; |
| 21 | |
| 22 | /** |
| 23 | * This class is compiled against 1.5 or later to provide access to the generic |
| 24 | * signatures. It can convert a Class, Field, Method or constructor to a generic |
| 25 | * signature and it can normalize a signature. Both are methods. Normalized |
| 26 | * signatures can be string compared and match even if the type variable names |
| 27 | * differ. |
| 28 | * |
| 29 | * @version $Id$ |
| 30 | */ |
| 31 | public class Signatures { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 32 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 33 | /** |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 34 | * Check if the environment has generics, i.e. later than Java 5 VM. |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 35 | * |
| 36 | * @return true if generics are supported |
| 37 | * @throws Exception |
| 38 | */ |
| 39 | public boolean hasGenerics() throws Exception { |
| 40 | try { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 41 | call(Signatures.class, "getGenericSuperClass"); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 42 | return true; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 43 | } |
| 44 | catch (NoSuchMethodException mnfe) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 45 | return false; |
| 46 | } |
| 47 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 48 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 49 | /** |
| 50 | * Helper class to track an index in a string. |
| 51 | */ |
| 52 | static class Rover { |
| 53 | final String s; |
| 54 | int i; |
| 55 | |
| 56 | public Rover(String s) { |
| 57 | this.s = s; |
| 58 | i = 0; |
| 59 | } |
| 60 | |
| 61 | char peek() { |
| 62 | return s.charAt(i); |
| 63 | } |
| 64 | |
| 65 | char take() { |
| 66 | return s.charAt(i++); |
| 67 | } |
| 68 | |
| 69 | char take(char c) { |
| 70 | char x = s.charAt(i++); |
| 71 | if (c != x) |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 72 | throw new IllegalStateException("get() expected " + c + " but got + " + x); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 73 | return x; |
| 74 | } |
| 75 | |
| 76 | public String upTo(String except) { |
| 77 | int start = i; |
| 78 | while (except.indexOf(peek()) < 0) |
| 79 | take(); |
| 80 | return s.substring(start, i); |
| 81 | } |
| 82 | |
| 83 | public boolean isEOF() { |
| 84 | return i >= s.length(); |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Calculate the generic signature of a Class,Method,Field, or Constructor. |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 91 | * |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 92 | * @param f |
| 93 | * @return |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 94 | * @throws Exception |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 95 | */ |
| 96 | public String getSignature(Object c) throws Exception { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 97 | if (c instanceof Class< ? >) |
| 98 | return getSignature((Class< ? >) c); |
| 99 | if (c instanceof Constructor< ? >) |
| 100 | return getSignature((Constructor< ? >) c); |
| 101 | if (c instanceof Method) |
| 102 | return getSignature((Method) c); |
| 103 | if (c instanceof Field) |
| 104 | return getSignature((Field) c); |
| 105 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 106 | throw new IllegalArgumentException(c.toString()); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Calculate the generic signature of a Class. A Class consists of: |
| 111 | * |
| 112 | * <pre> |
| 113 | * class ::= declaration? reference reference* |
| 114 | * </pre> |
| 115 | * |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 116 | * @param f |
| 117 | * @return |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 118 | * @throws Exception |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 119 | */ |
| 120 | public String getSignature(Class< ? > c) throws Exception { |
| 121 | StringBuilder sb = new StringBuilder(); |
| 122 | declaration(sb, c); |
| 123 | reference(sb, call(c, "getGenericSuperclass")); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 124 | for (Object type : (Object[]) call(c, "getGenericInterfaces")) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 125 | reference(sb, type); |
| 126 | } |
| 127 | return sb.toString(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Calculate the generic signature of a Method. A Method consists of: |
| 132 | * |
| 133 | * <pre> |
| 134 | * method ::= declaration? '(' reference* ')' reference |
| 135 | * </pre> |
| 136 | * |
| 137 | * @param c |
| 138 | * @return |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 139 | * @throws Exception |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 140 | */ |
| 141 | public String getSignature(Method m) throws Exception { |
| 142 | StringBuilder sb = new StringBuilder(); |
| 143 | declaration(sb, m); |
| 144 | sb.append('('); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 145 | for (Object type : (Object[]) call(m, "getGenericParameterTypes")) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 146 | reference(sb, type); |
| 147 | } |
| 148 | sb.append(')'); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 149 | reference(sb, call(m, "getGenericReturnType")); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 150 | return sb.toString(); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Calculate the generic signature of a Constructor. A Constructor consists |
| 155 | * of: |
| 156 | * |
| 157 | * <pre> |
| 158 | * constructor ::= declaration? '(' reference* ')V' |
| 159 | * </pre> |
| 160 | * |
| 161 | * @param c |
| 162 | * @return |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 163 | * @throws Exception |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 164 | */ |
| 165 | public String getSignature(Constructor< ? > c) throws Exception { |
| 166 | StringBuilder sb = new StringBuilder(); |
| 167 | declaration(sb, c); |
| 168 | sb.append('('); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 169 | for (Object type : (Object[]) call(c, "getGenericParameterTypes")) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 170 | reference(sb, type); |
| 171 | } |
| 172 | sb.append(')'); |
| 173 | reference(sb, void.class); |
| 174 | return sb.toString(); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Calculate the generic signature of a Field. A Field consists of: |
| 179 | * |
| 180 | * <pre> |
| 181 | * constructor ::= reference |
| 182 | * </pre> |
| 183 | * |
| 184 | * @param c |
| 185 | * @return |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 186 | * @throws Exception |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 187 | */ |
| 188 | public String getSignature(Field f) throws Exception { |
| 189 | StringBuilder sb = new StringBuilder(); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 190 | Object t = call(f, "getGenericType"); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 191 | reference(sb, t); |
| 192 | return sb.toString(); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Classes, Methods, or Constructors can have a declaration that provides |
| 197 | * nested a scope for type variables. A Method/Constructor inherits |
| 198 | * the type variables from its class and a class inherits its type variables |
| 199 | * from its outer class. The declaration consists of the following |
| 200 | * syntax: |
| 201 | * <pre> |
| 202 | * declarations ::= '<' declaration ( ',' declaration )* '>' |
| 203 | * declaration ::= identifier ':' declare |
| 204 | * declare ::= types | variable |
| 205 | * types ::= ( 'L' class ';' )? ( ':' 'L' interface ';' )* |
| 206 | * variable ::= 'T' id ';' |
| 207 | * </pre> |
| 208 | * |
| 209 | * @param sb |
| 210 | * @param gd |
| 211 | * @throws Exception |
| 212 | */ |
| 213 | private void declaration(StringBuilder sb, Object gd) throws Exception { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 214 | Object[] typeParameters = (Object[]) call(gd, "getTypeParameters"); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 215 | if (typeParameters.length > 0) { |
| 216 | sb.append('<'); |
| 217 | for (Object tv : typeParameters) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 218 | sb.append(call(tv, "getName")); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 219 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 220 | Object[] bounds = (Object[]) call(tv, "getBounds"); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 221 | if (bounds.length > 0 && isInterface(bounds[0])) { |
| 222 | sb.append(':'); |
| 223 | } |
| 224 | for (int i = 0; i < bounds.length; i++) { |
| 225 | sb.append(':'); |
| 226 | reference(sb, bounds[i]); |
| 227 | } |
| 228 | } |
| 229 | sb.append('>'); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Verify that the type is an interface. |
| 235 | * |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 236 | * @param type |
| 237 | * the type to check. |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 238 | * @return true if this is a class that is an interface or a Parameterized |
| 239 | * Type that is an interface |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 240 | * @throws Exception |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 241 | */ |
| 242 | private boolean isInterface(Object type) throws Exception { |
| 243 | if (type instanceof Class) |
| 244 | return (((Class< ? >) type).isInterface()); |
| 245 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 246 | if (isInstance(type.getClass(), "java.lang.reflect.ParameterizedType")) |
| 247 | return isInterface(call(type, "getRawType")); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 248 | |
| 249 | return false; |
| 250 | } |
| 251 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 252 | /** |
| 253 | * This is the heart of the signature builder. A reference is used |
| 254 | * in a lot of places. It referes to another type. |
| 255 | * <pre> |
| 256 | * reference ::= array | class | primitive | variable |
| 257 | * array ::= '[' reference |
| 258 | * class ::= 'L' body ( '.' body )* ';' |
| 259 | * body ::= id ( '<' ( wildcard | reference )* '>' )? |
| 260 | * variable ::= 'T' id ';' |
| 261 | * primitive ::= PRIMITIVE |
| 262 | * </pre> |
| 263 | * |
| 264 | * @param sb |
| 265 | * @param t |
| 266 | * @throws Exception |
| 267 | */ |
| 268 | private void reference(StringBuilder sb, Object t) throws Exception { |
| 269 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 270 | if (isInstance(t.getClass(), "java.lang.reflect.ParameterizedType")) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 271 | sb.append('L'); |
| 272 | parameterizedType(sb, t); |
| 273 | sb.append(';'); |
| 274 | return; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 275 | } else if (isInstance(t.getClass(), "java.lang.reflect.GenericArrayType")) { |
| 276 | sb.append('['); |
| 277 | reference(sb, call(t, "getGenericComponentType")); |
| 278 | } else if (isInstance(t.getClass(), "java.lang.reflect.WildcardType")) { |
| 279 | Object[] lowerBounds = (Object[]) call(t, "getLowerBounds"); |
| 280 | Object[] upperBounds = (Object[]) call(t, "getUpperBounds"); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 281 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 282 | if (upperBounds.length == 1 && upperBounds[0] == Object.class) |
| 283 | upperBounds = new Object[0]; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 284 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 285 | if (upperBounds.length != 0) { |
| 286 | // extend |
| 287 | for (Object upper : upperBounds) { |
| 288 | sb.append('+'); |
| 289 | reference(sb, upper); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 290 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 291 | } else if (lowerBounds.length != 0) { |
| 292 | // super, can only be one by the language |
| 293 | for (Object lower : lowerBounds) { |
| 294 | sb.append('-'); |
| 295 | reference(sb, lower); |
| 296 | } |
| 297 | } else |
| 298 | sb.append('*'); |
| 299 | } else if (isInstance(t.getClass(), "java.lang.reflect.TypeVariable")) { |
| 300 | sb.append('T'); |
| 301 | sb.append(call(t, "getName")); |
| 302 | sb.append(';'); |
| 303 | } else if (t instanceof Class< ? >) { |
| 304 | Class< ? > c = (Class< ? >) t; |
| 305 | if (c.isPrimitive()) { |
| 306 | sb.append(primitive(c)); |
| 307 | } else { |
| 308 | sb.append('L'); |
| 309 | String name = c.getName().replace('.', '/'); |
| 310 | sb.append(name); |
| 311 | sb.append(';'); |
| 312 | } |
| 313 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /** |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 317 | * Creates the signature for a Parameterized Type. A Parameterized Type has |
| 318 | * a raw class and a set of type variables. |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 319 | * |
| 320 | * @param sb |
| 321 | * @param pt |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 322 | * @throws Exception |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 323 | */ |
| 324 | private void parameterizedType(StringBuilder sb, Object pt) throws Exception { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 325 | Object owner = call(pt, "getOwnerType"); |
| 326 | String name = ((Class< ? >) call(pt, "getRawType")).getName().replace('.', '/'); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 327 | if (owner != null) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 328 | if (isInstance(owner.getClass(), "java.lang.reflect.ParameterizedType")) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 329 | parameterizedType(sb, owner); |
| 330 | else |
| 331 | sb.append(((Class< ? >) owner).getName().replace('.', '/')); |
| 332 | sb.append('.'); |
| 333 | int n = name.lastIndexOf('$'); |
| 334 | name = name.substring(n + 1); |
| 335 | } |
| 336 | sb.append(name); |
| 337 | |
| 338 | sb.append('<'); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 339 | for (Object parameterType : (Object[]) call(pt, "getActualTypeArguments")) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 340 | reference(sb, parameterType); |
| 341 | } |
| 342 | sb.append('>'); |
| 343 | |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Handle primitives, these need to be translated to a single char. |
| 348 | * |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 349 | * @param type |
| 350 | * the primitive class |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 351 | * @return the single char associated with the primitive |
| 352 | */ |
| 353 | private char primitive(Class< ? > type) { |
| 354 | if (type == byte.class) |
| 355 | return 'B'; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 356 | else if (type == char.class) |
| 357 | return 'C'; |
| 358 | else if (type == double.class) |
| 359 | return 'D'; |
| 360 | else if (type == float.class) |
| 361 | return 'F'; |
| 362 | else if (type == int.class) |
| 363 | return 'I'; |
| 364 | else if (type == long.class) |
| 365 | return 'J'; |
| 366 | else if (type == short.class) |
| 367 | return 'S'; |
| 368 | else if (type == boolean.class) |
| 369 | return 'Z'; |
| 370 | else if (type == void.class) |
| 371 | return 'V'; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 372 | else |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 373 | throw new IllegalArgumentException("Unknown primitive type " + type); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Normalize a signature to make sure the name of the variables are always |
| 378 | * the same. We change the names of the type variables to _n, where n is an |
| 379 | * integer. n is incremented for every new name and already used names are |
| 380 | * replaced with the _n name. |
| 381 | * |
| 382 | * @return a normalized signature |
| 383 | */ |
| 384 | |
| 385 | public String normalize(String signature) { |
| 386 | StringBuilder sb = new StringBuilder(); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 387 | Map<String,String> map = new HashMap<String,String>(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 388 | Rover rover = new Rover(signature); |
| 389 | declare(sb, map, rover); |
| 390 | |
| 391 | if (rover.peek() == '(') { |
| 392 | // method or constructor |
| 393 | sb.append(rover.take('(')); |
| 394 | while (rover.peek() != ')') { |
| 395 | reference(sb, map, rover, true); |
| 396 | } |
| 397 | sb.append(rover.take(')')); |
| 398 | reference(sb, map, rover, true); // return type |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 399 | } else { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 400 | // field or class |
| 401 | reference(sb, map, rover, true); // field type or super class |
| 402 | while (!rover.isEOF()) { |
| 403 | reference(sb, map, rover, true); // interfaces |
| 404 | } |
| 405 | } |
| 406 | return sb.toString(); |
| 407 | } |
| 408 | |
| 409 | /** |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 410 | * The heart of the routine. Handle a reference to a type. Can be an array, |
| 411 | * a class, a type variable, or a primitive. |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 412 | * |
| 413 | * @param sb |
| 414 | * @param map |
| 415 | * @param rover |
| 416 | * @param primitivesAllowed |
| 417 | */ |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 418 | private void reference(StringBuilder sb, Map<String,String> map, Rover rover, boolean primitivesAllowed) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 419 | |
| 420 | char type = rover.take(); |
| 421 | sb.append(type); |
| 422 | |
| 423 | if (type == '[') { |
| 424 | reference(sb, map, rover, true); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 425 | } else if (type == 'L') { |
| 426 | String fqnb = rover.upTo("<;."); |
| 427 | sb.append(fqnb); |
| 428 | body(sb, map, rover); |
| 429 | while (rover.peek() == '.') { |
| 430 | sb.append(rover.take('.')); |
| 431 | sb.append(rover.upTo("<;.")); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 432 | body(sb, map, rover); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 433 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 434 | sb.append(rover.take(';')); |
| 435 | } else if (type == 'T') { |
| 436 | String name = rover.upTo(";"); |
| 437 | name = assign(map, name); |
| 438 | sb.append(name); |
| 439 | sb.append(rover.take(';')); |
| 440 | } else { |
| 441 | if (!primitivesAllowed) |
| 442 | throw new IllegalStateException("Primitives are not allowed without an array"); |
| 443 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /** |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 447 | * Because classes can be nested the body handles the part that can be |
| 448 | * nested, the reference handles the enclosing L ... ; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 449 | * |
| 450 | * @param sb |
| 451 | * @param map |
| 452 | * @param rover |
| 453 | */ |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 454 | private void body(StringBuilder sb, Map<String,String> map, Rover rover) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 455 | if (rover.peek() == '<') { |
| 456 | sb.append(rover.take('<')); |
| 457 | while (rover.peek() != '>') { |
| 458 | switch (rover.peek()) { |
| 459 | case 'L' : |
| 460 | case '[' : |
| 461 | reference(sb, map, rover, false); |
| 462 | break; |
| 463 | |
| 464 | case 'T' : |
| 465 | String name; |
| 466 | sb.append(rover.take('T')); // 'T' |
| 467 | name = rover.upTo(";"); |
| 468 | sb.append(assign(map, name)); |
| 469 | sb.append(rover.take(';')); |
| 470 | break; |
| 471 | |
| 472 | case '+' : // extends |
| 473 | case '-' : // super |
| 474 | sb.append(rover.take()); |
| 475 | reference(sb, map, rover, false); |
| 476 | break; |
| 477 | |
| 478 | case '*' : // wildcard |
| 479 | sb.append(rover.take()); |
| 480 | break; |
| 481 | |
| 482 | } |
| 483 | } |
| 484 | sb.append(rover.take('>')); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Handle the declaration part. |
| 490 | * |
| 491 | * @param sb |
| 492 | * @param map |
| 493 | * @param rover |
| 494 | */ |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 495 | private void declare(StringBuilder sb, Map<String,String> map, Rover rover) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 496 | char c = rover.peek(); |
| 497 | if (c == '<') { |
| 498 | sb.append(rover.take('<')); |
| 499 | |
| 500 | while (rover.peek() != '>') { |
| 501 | String name = rover.upTo(":"); |
| 502 | name = assign(map, name); |
| 503 | sb.append(name); |
| 504 | typeVar: while (rover.peek() == ':') { |
| 505 | sb.append(rover.take(':')); |
| 506 | switch (rover.peek()) { |
| 507 | case ':' : // empty class cases |
| 508 | continue typeVar; |
| 509 | |
| 510 | default : |
| 511 | reference(sb, map, rover, false); |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | sb.append(rover.take('>')); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | /** |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 521 | * Handles the assignment of type variables to index names so that we have a |
| 522 | * normalized name for each type var. |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 523 | * |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 524 | * @param map |
| 525 | * the map with variables. |
| 526 | * @param name |
| 527 | * The name of the variable |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 528 | * @return the index name, like _1 |
| 529 | */ |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 530 | private String assign(Map<String,String> map, String name) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 531 | if (map.containsKey(name)) |
| 532 | return map.get(name); |
| 533 | else { |
| 534 | int n = map.size(); |
| 535 | map.put(name, "_" + n); |
| 536 | return "_" + n; |
| 537 | } |
| 538 | } |
| 539 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 540 | private boolean isInstance(Class< ? > type, String string) { |
| 541 | if (type == null) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 542 | return false; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 543 | |
| 544 | if (type.getName().equals(string)) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 545 | return true; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 546 | |
| 547 | if (isInstance(type.getSuperclass(), string)) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 548 | return true; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 549 | |
| 550 | for (Class< ? > intf : type.getInterfaces()) { |
| 551 | if (isInstance(intf, string)) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 552 | return true; |
| 553 | } |
| 554 | return false; |
| 555 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame^] | 556 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 557 | private Object call(Object gd, String string) throws Exception { |
| 558 | Method m = gd.getClass().getMethod(string); |
| 559 | return m.invoke(gd); |
| 560 | } |
| 561 | |
| 562 | } |