1 /* 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang.reflect; 27 28 import dalvik.annotation.optimization.FastNative; 29 30 import java.lang.annotation.Annotation; 31 import java.util.Objects; 32 import libcore.reflect.AnnotatedElements; 33 import libcore.reflect.GenericSignatureParser; 34 import libcore.reflect.ListOfTypes; 35 import libcore.reflect.Types; 36 import libcore.util.EmptyArray; 37 38 /** 39 * A shared superclass for the common functionality of {@link Method} 40 * and {@link Constructor}. 41 * 42 * @since 1.8 43 */ 44 public abstract class Executable extends AccessibleObject 45 implements Member, GenericDeclaration { 46 47 // Android-changed: Extensive modifications made throughout the class for ART. 48 // Android-removed: Declared vs actual parameter annotation indexes handling. 49 // Android-removed: Type annotations runtime code. Not supported on Android. 50 51 /* 52 * Only grant package-visibility to the constructor. 53 */ Executable()54 Executable() {} 55 56 /** 57 * Does the Executable have generic information. 58 */ hasGenericInformation()59 abstract boolean hasGenericInformation(); 60 equalParamTypes(Class<?>[] params1, Class<?>[] params2)61 boolean equalParamTypes(Class<?>[] params1, Class<?>[] params2) { 62 /* Avoid unnecessary cloning */ 63 if (params1.length == params2.length) { 64 for (int i = 0; i < params1.length; i++) { 65 if (params1[i] != params2[i]) 66 return false; 67 } 68 return true; 69 } 70 return false; 71 } 72 separateWithCommas(Class<?>[] types, StringBuilder sb)73 void separateWithCommas(Class<?>[] types, StringBuilder sb) { 74 for (int j = 0; j < types.length; j++) { 75 sb.append(types[j].getTypeName()); 76 if (j < (types.length - 1)) 77 sb.append(","); 78 } 79 80 } 81 printModifiersIfNonzero(StringBuilder sb, int mask, boolean isDefault)82 void printModifiersIfNonzero(StringBuilder sb, int mask, boolean isDefault) { 83 int mod = getModifiers() & mask; 84 85 if (mod != 0 && !isDefault) { 86 sb.append(Modifier.toString(mod)).append(' '); 87 } else { 88 int access_mod = mod & Modifier.ACCESS_MODIFIERS; 89 if (access_mod != 0) 90 sb.append(Modifier.toString(access_mod)).append(' '); 91 if (isDefault) 92 sb.append("default "); 93 mod = (mod & ~Modifier.ACCESS_MODIFIERS); 94 if (mod != 0) 95 sb.append(Modifier.toString(mod)).append(' '); 96 } 97 } 98 sharedToString(int modifierMask, boolean isDefault, Class<?>[] parameterTypes, Class<?>[] exceptionTypes)99 String sharedToString(int modifierMask, 100 boolean isDefault, 101 Class<?>[] parameterTypes, 102 Class<?>[] exceptionTypes) { 103 try { 104 StringBuilder sb = new StringBuilder(); 105 106 printModifiersIfNonzero(sb, modifierMask, isDefault); 107 specificToStringHeader(sb); 108 109 sb.append('('); 110 separateWithCommas(parameterTypes, sb); 111 sb.append(')'); 112 if (exceptionTypes.length > 0) { 113 sb.append(" throws "); 114 separateWithCommas(exceptionTypes, sb); 115 } 116 return sb.toString(); 117 } catch (Exception e) { 118 return "<" + e + ">"; 119 } 120 } 121 122 /** 123 * Generate toString header information specific to a method or 124 * constructor. 125 */ specificToStringHeader(StringBuilder sb)126 abstract void specificToStringHeader(StringBuilder sb); 127 sharedToGenericString(int modifierMask, boolean isDefault)128 String sharedToGenericString(int modifierMask, boolean isDefault) { 129 try { 130 StringBuilder sb = new StringBuilder(); 131 132 printModifiersIfNonzero(sb, modifierMask, isDefault); 133 134 TypeVariable<?>[] typeparms = getTypeParameters(); 135 if (typeparms.length > 0) { 136 boolean first = true; 137 sb.append('<'); 138 for(TypeVariable<?> typeparm: typeparms) { 139 if (!first) 140 sb.append(','); 141 // Class objects can't occur here; no need to test 142 // and call Class.getName(). 143 sb.append(typeparm.toString()); 144 first = false; 145 } 146 sb.append("> "); 147 } 148 149 specificToGenericStringHeader(sb); 150 151 sb.append('('); 152 Type[] params = getGenericParameterTypes(); 153 for (int j = 0; j < params.length; j++) { 154 String param = params[j].getTypeName(); 155 if (isVarArgs() && (j == params.length - 1)) // replace T[] with T... 156 param = param.replaceFirst("\\[\\]$", "..."); 157 sb.append(param); 158 if (j < (params.length - 1)) 159 sb.append(','); 160 } 161 sb.append(')'); 162 Type[] exceptions = getGenericExceptionTypes(); 163 if (exceptions.length > 0) { 164 sb.append(" throws "); 165 for (int k = 0; k < exceptions.length; k++) { 166 sb.append((exceptions[k] instanceof Class)? 167 ((Class)exceptions[k]).getName(): 168 exceptions[k].toString()); 169 if (k < (exceptions.length - 1)) 170 sb.append(','); 171 } 172 } 173 return sb.toString(); 174 } catch (Exception e) { 175 return "<" + e + ">"; 176 } 177 } 178 179 /** 180 * Generate toGenericString header information specific to a 181 * method or constructor. 182 */ specificToGenericStringHeader(StringBuilder sb)183 abstract void specificToGenericStringHeader(StringBuilder sb); 184 185 /** 186 * Returns the {@code Class} object representing the class or interface 187 * that declares the executable represented by this object. 188 */ getDeclaringClass()189 public abstract Class<?> getDeclaringClass(); 190 191 /** 192 * Returns the name of the executable represented by this object. 193 */ getName()194 public abstract String getName(); 195 196 /** 197 * Returns the Java language {@linkplain Modifier modifiers} for 198 * the executable represented by this object. 199 */ getModifiers()200 public abstract int getModifiers(); 201 202 /** 203 * Returns an array of {@code TypeVariable} objects that represent the 204 * type variables declared by the generic declaration represented by this 205 * {@code GenericDeclaration} object, in declaration order. Returns an 206 * array of length 0 if the underlying generic declaration declares no type 207 * variables. 208 * 209 * @return an array of {@code TypeVariable} objects that represent 210 * the type variables declared by this generic declaration 211 * @throws GenericSignatureFormatError if the generic 212 * signature of this generic declaration does not conform to 213 * the format specified in 214 * <cite>The Java™ Virtual Machine Specification</cite> 215 */ getTypeParameters()216 public abstract TypeVariable<?>[] getTypeParameters(); 217 218 /** 219 * Returns an array of {@code Class} objects that represent the formal 220 * parameter types, in declaration order, of the executable 221 * represented by this object. Returns an array of length 222 * 0 if the underlying executable takes no parameters. 223 * 224 * @return the parameter types for the executable this object 225 * represents 226 */ getParameterTypes()227 public abstract Class<?>[] getParameterTypes(); 228 229 /** 230 * Returns the number of formal parameters (whether explicitly 231 * declared or implicitly declared or neither) for the executable 232 * represented by this object. 233 * 234 * @return The number of formal parameters for the executable this 235 * object represents 236 */ getParameterCount()237 public int getParameterCount() { 238 throw new AbstractMethodError(); 239 } 240 241 /** 242 * Returns an array of {@code Type} objects that represent the formal 243 * parameter types, in declaration order, of the executable represented by 244 * this object. Returns an array of length 0 if the 245 * underlying executable takes no parameters. 246 * 247 * <p>If a formal parameter type is a parameterized type, 248 * the {@code Type} object returned for it must accurately reflect 249 * the actual type parameters used in the source code. 250 * 251 * <p>If a formal parameter type is a type variable or a parameterized 252 * type, it is created. Otherwise, it is resolved. 253 * 254 * @return an array of {@code Type}s that represent the formal 255 * parameter types of the underlying executable, in declaration order 256 * @throws GenericSignatureFormatError 257 * if the generic method signature does not conform to the format 258 * specified in 259 * <cite>The Java™ Virtual Machine Specification</cite> 260 * @throws TypeNotPresentException if any of the parameter 261 * types of the underlying executable refers to a non-existent type 262 * declaration 263 * @throws MalformedParameterizedTypeException if any of 264 * the underlying executable's parameter types refer to a parameterized 265 * type that cannot be instantiated for any reason 266 */ getGenericParameterTypes()267 public Type[] getGenericParameterTypes() { 268 // Android-changed: getGenericParameterTypes() implementation for use with ART. 269 return Types.getTypeArray( 270 getMethodOrConstructorGenericInfoInternal().genericParameterTypes, false); 271 } 272 273 /** 274 * Behaves like {@code getGenericParameterTypes}, but returns type 275 * information for all parameters, including synthetic parameters. 276 */ getAllGenericParameterTypes()277 Type[] getAllGenericParameterTypes() { 278 final boolean genericInfo = hasGenericInformation(); 279 280 // Easy case: we don't have generic parameter information. In 281 // this case, we just return the result of 282 // getParameterTypes(). 283 if (!genericInfo) { 284 return getParameterTypes(); 285 } else { 286 final boolean realParamData = hasRealParameterData(); 287 final Type[] genericParamTypes = getGenericParameterTypes(); 288 final Type[] nonGenericParamTypes = getParameterTypes(); 289 final Type[] out = new Type[nonGenericParamTypes.length]; 290 final Parameter[] params = getParameters(); 291 int fromidx = 0; 292 // If we have real parameter data, then we use the 293 // synthetic and mandate flags to our advantage. 294 if (realParamData) { 295 for (int i = 0; i < out.length; i++) { 296 final Parameter param = params[i]; 297 if (param.isSynthetic() || param.isImplicit()) { 298 // If we hit a synthetic or mandated parameter, 299 // use the non generic parameter info. 300 out[i] = nonGenericParamTypes[i]; 301 } else { 302 // Otherwise, use the generic parameter info. 303 out[i] = genericParamTypes[fromidx]; 304 fromidx++; 305 } 306 } 307 } else { 308 // Otherwise, use the non-generic parameter data. 309 // Without method parameter reflection data, we have 310 // no way to figure out which parameters are 311 // synthetic/mandated, thus, no way to match up the 312 // indexes. 313 return genericParamTypes.length == nonGenericParamTypes.length ? 314 genericParamTypes : nonGenericParamTypes; 315 } 316 return out; 317 } 318 } 319 320 /** 321 * Returns an array of {@code Parameter} objects that represent 322 * all the parameters to the underlying executable represented by 323 * this object. Returns an array of length 0 if the executable 324 * has no parameters. 325 * 326 * <p>The parameters of the underlying executable do not necessarily 327 * have unique names, or names that are legal identifiers in the 328 * Java programming language (JLS 3.8). 329 * 330 * @throws MalformedParametersException if the class file contains 331 * a MethodParameters attribute that is improperly formatted. 332 * @return an array of {@code Parameter} objects representing all 333 * the parameters to the executable this object represents. 334 */ getParameters()335 public Parameter[] getParameters() { 336 // TODO: This may eventually need to be guarded by security 337 // mechanisms similar to those in Field, Method, etc. 338 // 339 // Need to copy the cached array to prevent users from messing 340 // with it. Since parameters are immutable, we can 341 // shallow-copy. 342 return privateGetParameters().clone(); 343 } 344 synthesizeAllParams()345 private Parameter[] synthesizeAllParams() { 346 final int realparams = getParameterCount(); 347 final Parameter[] out = new Parameter[realparams]; 348 for (int i = 0; i < realparams; i++) 349 // TODO: is there a way to synthetically derive the 350 // modifiers? Probably not in the general case, since 351 // we'd have no way of knowing about them, but there 352 // may be specific cases. 353 out[i] = new Parameter("arg" + i, 0, this, i); 354 return out; 355 } 356 verifyParameters(final Parameter[] parameters)357 private void verifyParameters(final Parameter[] parameters) { 358 final int mask = Modifier.FINAL | Modifier.SYNTHETIC | Modifier.MANDATED; 359 360 if (getParameterTypes().length != parameters.length) 361 throw new MalformedParametersException("Wrong number of parameters in MethodParameters attribute"); 362 363 for (Parameter parameter : parameters) { 364 final String name = parameter.getRealName(); 365 final int mods = parameter.getModifiers(); 366 367 if (name != null) { 368 if (name.isEmpty() || name.indexOf('.') != -1 || 369 name.indexOf(';') != -1 || name.indexOf('[') != -1 || 370 name.indexOf('/') != -1) { 371 throw new MalformedParametersException("Invalid parameter name \"" + name + "\""); 372 } 373 } 374 375 if (mods != (mods & mask)) { 376 throw new MalformedParametersException("Invalid parameter modifiers"); 377 } 378 } 379 } 380 privateGetParameters()381 private Parameter[] privateGetParameters() { 382 // Use tmp to avoid multiple writes to a volatile. 383 Parameter[] tmp = parameters; 384 385 if (tmp == null) { 386 387 // Otherwise, go to the JVM to get them 388 try { 389 tmp = getParameters0(); 390 } catch(IllegalArgumentException e) { 391 // Rethrow ClassFormatErrors 392 // Android-changed: Exception changed to be more descriptive. 393 MalformedParametersException e2 = 394 new MalformedParametersException( 395 "Invalid parameter metadata in class file"); 396 e2.initCause(e); 397 throw e2; 398 } 399 400 // If we get back nothing, then synthesize parameters 401 if (tmp == null) { 402 hasRealParameterData = false; 403 tmp = synthesizeAllParams(); 404 } else { 405 hasRealParameterData = true; 406 verifyParameters(tmp); 407 } 408 409 parameters = tmp; 410 } 411 412 return tmp; 413 } 414 hasRealParameterData()415 boolean hasRealParameterData() { 416 // If this somehow gets called before parameters gets 417 // initialized, force it into existence. 418 if (parameters == null) { 419 privateGetParameters(); 420 } 421 return hasRealParameterData; 422 } 423 424 private transient volatile boolean hasRealParameterData; 425 private transient volatile Parameter[] parameters; 426 427 // Android-changed: Added @FastNative to getParameters0() 428 @FastNative getParameters0()429 private native Parameter[] getParameters0(); 430 431 /** 432 * Returns an array of {@code Class} objects that represent the 433 * types of exceptions declared to be thrown by the underlying 434 * executable represented by this object. Returns an array of 435 * length 0 if the executable declares no exceptions in its {@code 436 * throws} clause. 437 * 438 * @return the exception types declared as being thrown by the 439 * executable this object represents 440 */ getExceptionTypes()441 public abstract Class<?>[] getExceptionTypes(); 442 443 /** 444 * Returns an array of {@code Type} objects that represent the 445 * exceptions declared to be thrown by this executable object. 446 * Returns an array of length 0 if the underlying executable declares 447 * no exceptions in its {@code throws} clause. 448 * 449 * <p>If an exception type is a type variable or a parameterized 450 * type, it is created. Otherwise, it is resolved. 451 * 452 * @return an array of Types that represent the exception types 453 * thrown by the underlying executable 454 * @throws GenericSignatureFormatError 455 * if the generic method signature does not conform to the format 456 * specified in 457 * <cite>The Java™ Virtual Machine Specification</cite> 458 * @throws TypeNotPresentException if the underlying executable's 459 * {@code throws} clause refers to a non-existent type declaration 460 * @throws MalformedParameterizedTypeException if 461 * the underlying executable's {@code throws} clause refers to a 462 * parameterized type that cannot be instantiated for any reason 463 */ getGenericExceptionTypes()464 public Type[] getGenericExceptionTypes() { 465 // Android-changed: getGenericExceptionTypes() implementation for use with ART. 466 return Types.getTypeArray( 467 getMethodOrConstructorGenericInfoInternal().genericExceptionTypes, false); 468 } 469 470 /** 471 * Returns a string describing this {@code Executable}, including 472 * any type parameters. 473 * @return a string describing this {@code Executable}, including 474 * any type parameters 475 */ toGenericString()476 public abstract String toGenericString(); 477 478 /** 479 * Returns {@code true} if this executable was declared to take a 480 * variable number of arguments; returns {@code false} otherwise. 481 * 482 * @return {@code true} if an only if this executable was declared 483 * to take a variable number of arguments. 484 */ isVarArgs()485 public boolean isVarArgs() { 486 // Android-changed: isVarArgs() made slightly more efficient. 487 return (accessFlags & Modifier.VARARGS) != 0; 488 } 489 490 /** 491 * Returns {@code true} if this executable is a synthetic 492 * construct; returns {@code false} otherwise. 493 * 494 * @return true if and only if this executable is a synthetic 495 * construct as defined by 496 * <cite>The Java™ Language Specification</cite>. 497 * @jls 13.1 The Form of a Binary 498 */ isSynthetic()499 public boolean isSynthetic() { 500 // Android-changed: isSynthetic() made slightly more efficient. 501 return (accessFlags & Modifier.SYNTHETIC) != 0; 502 } 503 504 /** 505 * Returns an array of arrays of {@code Annotation}s that 506 * represent the annotations on the formal parameters, in 507 * declaration order, of the {@code Executable} represented by 508 * this object. Synthetic and mandated parameters (see 509 * explanation below), such as the outer "this" parameter to an 510 * inner class constructor will be represented in the returned 511 * array. If the executable has no parameters (meaning no formal, 512 * no synthetic, and no mandated parameters), a zero-length array 513 * will be returned. If the {@code Executable} has one or more 514 * parameters, a nested array of length zero is returned for each 515 * parameter with no annotations. The annotation objects contained 516 * in the returned arrays are serializable. The caller of this 517 * method is free to modify the returned arrays; it will have no 518 * effect on the arrays returned to other callers. 519 * 520 * A compiler may add extra parameters that are implicitly 521 * declared in source ("mandated"), as well as parameters that 522 * are neither implicitly nor explicitly declared in source 523 * ("synthetic") to the parameter list for a method. See {@link 524 * java.lang.reflect.Parameter} for more information. 525 * 526 * @see java.lang.reflect.Parameter 527 * @see java.lang.reflect.Parameter#getAnnotations 528 * @return an array of arrays that represent the annotations on 529 * the formal and implicit parameters, in declaration order, of 530 * the executable represented by this object 531 */ getParameterAnnotations()532 public abstract Annotation[][] getParameterAnnotations(); 533 534 /** 535 * {@inheritDoc} 536 * @throws NullPointerException {@inheritDoc} 537 */ getAnnotation(Class<T> annotationClass)538 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { 539 Objects.requireNonNull(annotationClass); 540 // Android-changed: Implemented getAnnotation(Class) natively. 541 return getAnnotationNative(annotationClass); 542 } 543 544 // Android-changed: Implemented getAnnotation(Class) natively. 545 @FastNative getAnnotationNative(Class<T> annotationClass)546 private native <T extends Annotation> T getAnnotationNative(Class<T> annotationClass); 547 548 /** 549 * {@inheritDoc} 550 * @throws NullPointerException {@inheritDoc} 551 */ 552 @Override getAnnotationsByType(Class<T> annotationClass)553 public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) { 554 // Android-changed: getAnnotationsByType(Class), Android uses AnnotatedElements instead. 555 return AnnotatedElements.getDirectOrIndirectAnnotationsByType(this, annotationClass); 556 } 557 558 /** 559 * {@inheritDoc} 560 */ getDeclaredAnnotations()561 public Annotation[] getDeclaredAnnotations() { 562 // Android-changed: Implemented getDeclaredAnnotations() natively. 563 return getDeclaredAnnotationsNative(); 564 } 565 566 // Android-added: Implemented getDeclaredAnnotations() natively. 567 @FastNative getDeclaredAnnotationsNative()568 private native Annotation[] getDeclaredAnnotationsNative(); 569 570 // BEGIN Android-added: Additional ART-related fields and logic. 571 // This code is shared for Method and Constructor. 572 573 /** Bits encoding access (e.g. public, private) as well as other runtime specific flags */ 574 @SuppressWarnings("unused") // set by runtime 575 private int accessFlags; 576 577 /** 578 * The ArtMethod associated with this Executable, required for dispatching due to entrypoints 579 * Classloader is held live by the declaring class. 580 */ 581 @SuppressWarnings("unused") // set by runtime 582 private long artMethod; 583 584 /** Executable's declaring class */ 585 @SuppressWarnings("unused") // set by runtime 586 private Class<?> declaringClass; 587 588 /** 589 * Overriden method's declaring class (same as declaringClass unless declaringClass is a proxy 590 * class). 591 */ 592 @SuppressWarnings("unused") // set by runtime 593 private Class<?> declaringClassOfOverriddenMethod; 594 595 /** The method index of this method within its defining dex file */ 596 @SuppressWarnings("unused") // set by runtime 597 private int dexMethodIndex; 598 599 /** 600 * We insert native method stubs for abstract methods so we don't have to 601 * check the access flags at the time of the method call. This results in 602 * "native abstract" methods, which can't exist. If we see the "abstract" 603 * flag set, clear the "native" flag. 604 * 605 * We also move the DECLARED_SYNCHRONIZED flag into the SYNCHRONIZED 606 * position, because the callers of this function are trying to convey 607 * the "traditional" meaning of the flags to their callers. 608 */ fixMethodFlags(int flags)609 private static int fixMethodFlags(int flags) { 610 if ((flags & Modifier.ABSTRACT) != 0) { 611 flags &= ~Modifier.NATIVE; 612 } 613 flags &= ~Modifier.SYNCHRONIZED; 614 int ACC_DECLARED_SYNCHRONIZED = 0x00020000; 615 if ((flags & ACC_DECLARED_SYNCHRONIZED) != 0) { 616 flags |= Modifier.SYNCHRONIZED; 617 } 618 return flags & 0xffff; // mask out bits not used by Java 619 } 620 getModifiersInternal()621 final int getModifiersInternal() { 622 return fixMethodFlags(accessFlags); 623 } 624 getDeclaringClassInternal()625 final Class<?> getDeclaringClassInternal() { 626 return declaringClass; 627 } 628 629 /** 630 * Returns an array of {@code Class} objects associated with the parameter types of this 631 * Executable. If the Executable was declared with no parameters, {@code null} will be 632 * returned. 633 * 634 * @return the parameter types, or {@code null} if no parameters were declared. 635 */ 636 @FastNative getParameterTypesInternal()637 final native Class<?>[] getParameterTypesInternal(); 638 639 @FastNative getParameterCountInternal()640 final native int getParameterCountInternal(); 641 642 // Android provides a more efficient implementation of this method for Executable than the one 643 // implemented in AnnotatedElement. 644 @Override isAnnotationPresent(Class<? extends Annotation> annotationType)645 public final boolean isAnnotationPresent(Class<? extends Annotation> annotationType) { 646 Objects.requireNonNull(annotationType); 647 return isAnnotationPresentNative(annotationType); 648 } 649 @FastNative isAnnotationPresentNative(Class<? extends Annotation> annotationType)650 private native boolean isAnnotationPresentNative(Class<? extends Annotation> annotationType); 651 getParameterAnnotationsInternal()652 final Annotation[][] getParameterAnnotationsInternal() { 653 Annotation[][] parameterAnnotations = getParameterAnnotationsNative(); 654 if (parameterAnnotations == null) { 655 parameterAnnotations = new Annotation[getParameterTypes().length][0]; 656 } 657 return parameterAnnotations; 658 } 659 @FastNative getParameterAnnotationsNative()660 private native Annotation[][] getParameterAnnotationsNative(); 661 662 /** 663 * @hide - exposed for use by {@link Class}. 664 */ getAccessFlags()665 public final int getAccessFlags() { 666 return accessFlags; 667 } 668 669 /** 670 * @hide - exposed for use by {@code java.lang.invoke.*}. 671 */ getArtMethod()672 public final long getArtMethod() { 673 return artMethod; 674 } 675 676 static final class GenericInfo { 677 final ListOfTypes genericExceptionTypes; 678 final ListOfTypes genericParameterTypes; 679 final Type genericReturnType; 680 final TypeVariable<?>[] formalTypeParameters; 681 GenericInfo(ListOfTypes exceptions, ListOfTypes parameters, Type ret, TypeVariable<?>[] formal)682 GenericInfo(ListOfTypes exceptions, ListOfTypes parameters, Type ret, 683 TypeVariable<?>[] formal) { 684 genericExceptionTypes = exceptions; 685 genericParameterTypes = parameters; 686 genericReturnType = ret; 687 formalTypeParameters = formal; 688 } 689 } 690 hasGenericInformationInternal()691 final boolean hasGenericInformationInternal() { 692 return getSignatureAnnotation() != null; 693 } 694 695 /** 696 * Returns generic information associated with this method/constructor member. 697 */ getMethodOrConstructorGenericInfoInternal()698 final GenericInfo getMethodOrConstructorGenericInfoInternal() { 699 String signatureAttribute = getSignatureAttribute(); 700 Class<?>[] exceptionTypes = this.getExceptionTypes(); 701 GenericSignatureParser parser = 702 new GenericSignatureParser(this.getDeclaringClass().getClassLoader()); 703 if (this instanceof Method) { 704 parser.parseForMethod(this, signatureAttribute, exceptionTypes); 705 } else { 706 parser.parseForConstructor(this, signatureAttribute, exceptionTypes); 707 } 708 return new GenericInfo(parser.exceptionTypes, parser.parameterTypes, 709 parser.returnType, parser.formalTypeParameters); 710 } 711 getSignatureAttribute()712 private String getSignatureAttribute() { 713 String[] annotation = getSignatureAnnotation(); 714 if (annotation == null) { 715 return null; 716 } 717 StringBuilder result = new StringBuilder(); 718 for (String s : annotation) { 719 result.append(s); 720 } 721 return result.toString(); 722 } 723 @FastNative getSignatureAnnotation()724 private native String[] getSignatureAnnotation(); 725 equalNameAndParametersInternal(Method m)726 final boolean equalNameAndParametersInternal(Method m) { 727 return getName().equals(m.getName()) && (compareMethodParametersInternal(m) == 0); 728 } 729 730 @FastNative compareMethodParametersInternal(Method meth)731 native int compareMethodParametersInternal(Method meth); 732 733 @FastNative getMethodNameInternal()734 final native String getMethodNameInternal(); 735 736 @FastNative getMethodReturnTypeInternal()737 final native Class<?> getMethodReturnTypeInternal(); 738 739 /** A cheap implementation for {@link Method#isDefault()}. */ isDefaultMethodInternal()740 final boolean isDefaultMethodInternal() { 741 return (accessFlags & Modifier.DEFAULT) != 0; 742 } 743 744 /** A cheap implementation for {@link Method#isBridge()}. */ isBridgeMethodInternal()745 final boolean isBridgeMethodInternal() { 746 return (accessFlags & Modifier.BRIDGE) != 0; 747 } 748 // END Android-added: Additional ART-related fields and logic. 749 750 } 751