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