View Javadoc
1   /*
2    * Copyright (c) 2006 JMockit developers
3    * This file is subject to the terms of the MIT license (see LICENSE.txt).
4    */
5   package mockit.internal.state;
6   
7   import edu.umd.cs.findbugs.annotations.NonNull;
8   import edu.umd.cs.findbugs.annotations.Nullable;
9   
10  import java.util.HashMap;
11  import java.util.Map;
12  
13  import mockit.internal.util.TestMethod;
14  
15  import org.checkerframework.checker.index.qual.NonNegative;
16  
17  public final class ParameterNames {
18      private static final Map<String, Map<String, String[]>> classesToMethodsToParameters = new HashMap<>();
19  
20      private ParameterNames() {
21      }
22  
23      public static boolean hasNamesForClass(@NonNull String classDesc) {
24          return classesToMethodsToParameters.containsKey(classDesc);
25      }
26  
27      public static void register(@NonNull String classDesc, @NonNull String memberName, @NonNull String memberDesc,
28              @NonNull String[] names) {
29          Map<String, String[]> methodsToParameters = classesToMethodsToParameters.get(classDesc);
30  
31          if (methodsToParameters == null) {
32              methodsToParameters = new HashMap<>();
33              classesToMethodsToParameters.put(classDesc, methodsToParameters);
34          }
35  
36          String methodKey = memberName + memberDesc;
37          methodsToParameters.put(methodKey, names);
38      }
39  
40      @NonNull
41      public static String getName(@NonNull TestMethod method, @NonNegative int index) {
42          String name = getName(method.testClassDesc, method.testMethodDesc, index);
43          return name == null ? "param" + index : name;
44      }
45  
46      @Nullable
47      public static String getName(@NonNull String classDesc, @NonNull String methodDesc, @NonNegative int index) {
48          Map<String, String[]> methodsToParameters = classesToMethodsToParameters.get(classDesc);
49  
50          if (methodsToParameters == null) {
51              return null;
52          }
53  
54          String[] parameterNames = methodsToParameters.get(methodDesc);
55          return parameterNames[index];
56      }
57  }