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 package java.lang.reflect;
26
27 import java.lang.annotation.*;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.Objects;
32 import sun.reflect.annotation.AnnotationSupport;
33
34 /**
35 * Information about method parameters.
36 *
37 * A {@code Parameter} provides information about method parameters,
38 * including its name and modifiers. It also provides an alternate
39 * means of obtaining attributes for the parameter.
40 *
41 * @since 1.8
42 */
43 public final class Parameter implements AnnotatedElement {
44
45 private final String name;
46 private final int modifiers;
47 private final Executable executable;
48 private final int index;
49
50 /**
51 * Package-private constructor for {@code Parameter}.
154 *
155 * @jls 8.4.1 Formal Parameters
156 * @see <a
157 * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
158 * programming language and JVM modeling in core reflection</a>
159 */
160 public int getModifiers() {
161 return modifiers;
162 }
163
164 /**
165 * {@return an unmodifiable set of the {@linkplain AccessFlag
166 * access flags} for the parameter represented by this object,
167 * possibly empty}
168 *
169 * @see #getModifiers()
170 * @jvms 4.7.24 The MethodParameters Attribute
171 * @since 20
172 */
173 public Set<AccessFlag> accessFlags() {
174 return AccessibleObject.reflectionFactory.parseAccessFlags(getModifiers(),
175 AccessFlag.Location.METHOD_PARAMETER, getDeclaringExecutable().getDeclaringClass());
176 }
177
178 /**
179 * Returns the name of the parameter. If the parameter's name is
180 * {@linkplain #isNamePresent() present}, then this method returns
181 * the name provided by the class file. Otherwise, this method
182 * synthesizes a name of the form argN, where N is the index of
183 * the parameter in the descriptor of the method which declares
184 * the parameter.
185 *
186 * @return The name of the parameter, either provided by the class
187 * file or synthesized if the class file does not provide
188 * a name.
189 */
190 public String getName() {
191 // Note: empty strings as parameter names are now outlawed.
192 // The .isEmpty() is for compatibility with current JVM
193 // behavior. It may be removed at some point.
194 if(name == null || name.isEmpty())
195 return "arg" + index;
|
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 package java.lang.reflect;
26
27 import java.lang.annotation.*;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.Objects;
32
33 import jdk.internal.reflect.AccessFlagSet;
34 import sun.reflect.annotation.AnnotationSupport;
35
36 /**
37 * Information about method parameters.
38 *
39 * A {@code Parameter} provides information about method parameters,
40 * including its name and modifiers. It also provides an alternate
41 * means of obtaining attributes for the parameter.
42 *
43 * @since 1.8
44 */
45 public final class Parameter implements AnnotatedElement {
46
47 private final String name;
48 private final int modifiers;
49 private final Executable executable;
50 private final int index;
51
52 /**
53 * Package-private constructor for {@code Parameter}.
156 *
157 * @jls 8.4.1 Formal Parameters
158 * @see <a
159 * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
160 * programming language and JVM modeling in core reflection</a>
161 */
162 public int getModifiers() {
163 return modifiers;
164 }
165
166 /**
167 * {@return an unmodifiable set of the {@linkplain AccessFlag
168 * access flags} for the parameter represented by this object,
169 * possibly empty}
170 *
171 * @see #getModifiers()
172 * @jvms 4.7.24 The MethodParameters Attribute
173 * @since 20
174 */
175 public Set<AccessFlag> accessFlags() {
176 return AccessFlagSet.ofValidated(AccessFlagSet.METHOD_PARAMETER_FLAGS, modifiers);
177 }
178
179 /**
180 * Returns the name of the parameter. If the parameter's name is
181 * {@linkplain #isNamePresent() present}, then this method returns
182 * the name provided by the class file. Otherwise, this method
183 * synthesizes a name of the form argN, where N is the index of
184 * the parameter in the descriptor of the method which declares
185 * the parameter.
186 *
187 * @return The name of the parameter, either provided by the class
188 * file or synthesized if the class file does not provide
189 * a name.
190 */
191 public String getName() {
192 // Note: empty strings as parameter names are now outlawed.
193 // The .isEmpty() is for compatibility with current JVM
194 // behavior. It may be removed at some point.
195 if(name == null || name.isEmpty())
196 return "arg" + index;
|