1 /* 2 * Copyright (C) 2016 The Android Open Source Project 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 dalvik.annotation; 18 19 import java.lang.annotation.ElementType; 20 import java.lang.annotation.Retention; 21 import java.lang.annotation.RetentionPolicy; 22 import java.lang.annotation.Target; 23 import java.lang.reflect.Parameter; 24 25 /** 26 * A system annotation that can optionally be used to provide parameter metadata such as 27 * parameter names and modifiers. 28 * 29 * <p>The annotation can be omitted from a method / constructor safely when the parameter metadata 30 * is not needed / desired at runtime. {@link Parameter#isNamePresent()} can be used to check 31 * whether metadata is present for a parameter, and the associated reflection methods like 32 * {@link java.lang.reflect.Parameter#getName()} will fall back to default behavior at runtime if 33 * the information is not present. 34 * 35 * <p>When including parameter metadata, compilers should include parameter metadata for generated 36 * classes like enums, since the parameter metadata includes whether or not a parameter is 37 * synthetic or mandated. 38 * 39 * <p>MethodParameters currently only describes individual method parameters and there is no 40 * mechanism to detect whether parameter method data is <em>generally</em> present for an 41 * {@link java.lang.reflect.Executable}. Therefore, it is code-size and runtime efficient to omit 42 * the annotation entirely for constructors and methods that have no parameters. 43 */ 44 @Retention(RetentionPolicy.RUNTIME) 45 @Target({ElementType.CONSTRUCTOR, ElementType.METHOD}) 46 @interface MethodParameters { 47 48 /* 49 * This annotation is never used in source code; it is expected to be generated in .dex 50 * files by tools like compilers. Commented definitions for the annotation members expected 51 * by the runtime / reflection code can be found below for reference. 52 * 53 * The arrays documented below must be the same size as for the method_id_item dex structure 54 * associated with the method otherwise a java.lang.reflect.MalformedParametersException will 55 * be thrown at runtime. 56 * 57 * That is: method_id_item.proto_idx -> proto_id_item.parameters_off -> type_list.size must 58 * be the same as names().length and accessFlags().length. 59 * 60 * Because MethodParameters describes all formal method parameters, even those not explicitly 61 * or implicitly declared in source code, the size of the arrays may differ from the Signature 62 * or other metadata information that can be based only on explicit parameters declared in 63 * source code. MethodParameters will also not include any information about type annotation 64 * receiver parameters that do not exist in the actual method signature. 65 */ 66 67 68 /* 69 * The names of formal parameters for the associated method. The array cannot be null, but can 70 * be empty if there are no formal parameters. A value in the array can be null if the formal 71 * parameter with that index has no name. 72 * 73 * If parameter name Strings are empty or contain '.', ';', '[' or '/' then a 74 * java.lang.reflect.MalformedParametersException will be thrown at runtime. 75 */ 76 // String[] names(); 77 78 /* 79 * The access flags of the formal parameters for the associated method. The array cannot be 80 * null, but can be empty if there are no formal parameters. 81 * 82 * The value is a bit mask with the follow values: 83 * 0x0010 : final, the parameter was declared final 84 * 0x1000 : synthetic, the parameter was introduced by the compiler. 85 * 0x8000 : mandated, the parameter is synthetic but also implied by the language 86 * specification. 87 * 88 * If any bits are set outside of this set then a java.lang.reflect.MalformedParametersException 89 * will be thrown at runtime. 90 */ 91 // int[] accessFlags(); 92 } 93 94