• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4 package paramnames;
5 
6 import java.lang.reflect.Constructor;
7 import java.lang.reflect.Method;
8 import java.lang.reflect.Parameter;
9 
10 public class ParameterNames {
11 
12   private static final int MODIFIER_NONE = 0;
13   private static final int MODIFIER_FINAL = 0X10;
14 
ParameterNames(int a, final int b)15   public ParameterNames(int a, final int b) {
16   }
17 
check(String expected, String checked)18   public static void check(String expected, String checked) {
19     if (!expected.equals(checked)) {
20       throw new RuntimeException("Found '" + checked + "' while expecting '" + expected + "'");
21     }
22   }
23 
check(int expected, int checked)24   public static void check(int expected, int checked) {
25     if (expected != checked) {
26       throw new RuntimeException("Found '" + checked + "' while expecting '" + expected + "'");
27     }
28   }
29 
myMethod(int a, final int b)30   public static void myMethod(int a, final int b) throws NoSuchMethodException {
31     Class<ParameterNames> clazz = ParameterNames.class;
32     Method myMethod = clazz.getDeclaredMethod("myMethod", int.class, int.class);
33     Parameter[] parameters = myMethod.getParameters();
34     check(2, parameters.length);
35     check("a", parameters[0].getName());
36     check("b", parameters[1].getName());
37     check(MODIFIER_NONE, parameters[0].getModifiers());
38     check(MODIFIER_FINAL, parameters[1].getModifiers());
39   }
40 
myConstructor()41   public static void myConstructor() throws NoSuchMethodException {
42     Class<ParameterNames> clazz = ParameterNames.class;
43     Constructor<?> myConstructor = clazz.getDeclaredConstructor(int.class, int.class);
44     Parameter[] parameters = myConstructor.getParameters();
45     check(2, parameters.length);
46     check("a", parameters[0].getName());
47     check("b", parameters[1].getName());
48     check(MODIFIER_NONE, parameters[0].getModifiers());
49     check(MODIFIER_FINAL, parameters[1].getModifiers());
50   }
51 
main(String[] args)52   public static void main(String[] args) throws NoSuchMethodException {
53     myMethod(0, 1);
54     myConstructor();
55   }
56 }
57