All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mockit.internal.state.ParameterNames Maven / Gradle / Ivy

Go to download

JMockit is a Java toolkit for developer (unit/integration) testing. It contains mocking APIs and other tools, supporting both JUnit and TestNG. The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested in isolation from selected dependencies.

There is a newer version: 1.7
Show newest version
/*
 * Copyright (c) 2006-2013 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.internal.state;

import java.lang.reflect.*;
import java.util.*;

import org.jetbrains.annotations.*;

import mockit.external.asm4.Type;

public final class ParameterNames
{
   private static final Map> classesToMethodsToParameters =
      new HashMap>();
   private static final String[] NO_PARAMETERS = new String[0];

   public static boolean hasNamesForClass(@NotNull String classDesc)
   {
      return classesToMethodsToParameters.containsKey(classDesc);
   }

   public static void registerName(
      @NotNull String classDesc, int methodAccess, @NotNull String methodName, @NotNull String methodDesc,
      @NotNull String name, int index)
   {
      if ("this".equals(name)) {
         return;
      }

      Map methodsToParameters = classesToMethodsToParameters.get(classDesc);

      if (methodsToParameters == null) {
         methodsToParameters = new HashMap();
         classesToMethodsToParameters.put(classDesc, methodsToParameters);
      }

      String methodKey = methodName + methodDesc;
      String[] parameterNames = methodsToParameters.get(methodKey);

      if (parameterNames == null) {
         int numParameters = Type.getArgumentTypes(methodDesc).length;
         parameterNames = numParameters == 0 ? NO_PARAMETERS : new String[numParameters];
         methodsToParameters.put(methodKey, parameterNames);
      }

      if (!Modifier.isStatic(methodAccess)) {
         index--;
      }

      if (index < parameterNames.length) {
         parameterNames[index] = name;
      }
   }

   @Nullable public static String getName(@NotNull String classDesc, @NotNull String methodDesc, int index)
   {
      Map methodsToParameters = classesToMethodsToParameters.get(classDesc);

      if (methodsToParameters == null) {
         return null;
      }

      String[] parameterNames = methodsToParameters.get(methodDesc);
      return parameterNames == null ? null : parameterNames[index];
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy