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

mockit.internal.util.ConstructorReflection 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.util;

import java.lang.reflect.*;

import org.jetbrains.annotations.*;

import static mockit.internal.util.ParameterReflection.*;

public final class ConstructorReflection
{
   @NotNull
   public static  T newInstance(
      @NotNull Class aClass, @NotNull Class[] parameterTypes, @NotNull Object... initArgs)
   {
      Constructor constructor = findSpecifiedConstructor(aClass, parameterTypes);
      return invoke(constructor, initArgs);
   }

   @NotNull
   private static  Constructor findSpecifiedConstructor(
      @NotNull Class theClass, @NotNull Class[] paramTypes)
   {
      for (Constructor declaredConstructor : theClass.getDeclaredConstructors()) {
         Class[] declaredParameterTypes = declaredConstructor.getParameterTypes();
         int firstRealParameter = indexOfFirstRealParameter(declaredParameterTypes, paramTypes);

         if (
            firstRealParameter >= 0 &&
            matchesParameterTypes(declaredParameterTypes, paramTypes, firstRealParameter)
         ) {
            //noinspection unchecked
            return (Constructor) declaredConstructor;
         }
      }

      String paramTypesDesc = getParameterTypesDescription(paramTypes);

      throw new IllegalArgumentException(
         "Specified constructor not found: " + theClass.getSimpleName() + paramTypesDesc);
   }

   @NotNull public static  T invoke(@NotNull Constructor constructor, @NotNull Object... initArgs)
   {
      Utilities.ensureThatMemberIsAccessible(constructor);

      try {
         return constructor.newInstance(initArgs);
      }
      catch (InstantiationException e) {
         throw new RuntimeException(e);
      }
      catch (IllegalAccessException e) {
         throw new RuntimeException(e);
      }
      catch (InvocationTargetException e) {
         Throwable cause = e.getCause();

         if (cause instanceof Error) {
            throw (Error) cause;
         }
         else if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
         }
         else {
            ThrowOfCheckedException.doThrow((Exception) cause);
            throw new IllegalStateException("Should never get here");
         }
      }
   }

   @NotNull
   public static  T newInstance(
      @NotNull String className, @NotNull Class[] parameterTypes, @NotNull Object... initArgs)
   {
      Class theClass = ClassLoad.loadClass(className);
      return newInstance(theClass, parameterTypes, initArgs);
   }

   @NotNull
   public static  T newInstance(@NotNull String className, @NotNull Object... nonNullArgs)
   {
      Class[] argTypes = getArgumentTypesFromArgumentValues(nonNullArgs);
      Class theClass = ClassLoad.loadClass(className);
      Constructor constructor = findCompatibleConstructor(theClass, argTypes);
      return invoke(constructor, nonNullArgs);
   }

   @NotNull
   private static  Constructor findCompatibleConstructor(@NotNull Class theClass, @NotNull Class[] argTypes)
   {
      Constructor found = null;
      Class[] foundParameters = null;
      Constructor[] declaredConstructors = theClass.getDeclaredConstructors();

      for (Constructor declaredConstructor : declaredConstructors) {
         Class[] declaredParamTypes = declaredConstructor.getParameterTypes();
         int firstRealParameter = indexOfFirstRealParameter(declaredParamTypes, argTypes);

         if (
            firstRealParameter >= 0 &&
            (matchesParameterTypes(declaredParamTypes, argTypes, firstRealParameter) ||
             acceptsArgumentTypes(declaredParamTypes, argTypes, firstRealParameter)) &&
            (found == null || hasMoreSpecificTypes(declaredParamTypes, foundParameters))
         ) {
            //noinspection unchecked
            found = (Constructor) declaredConstructor;
            foundParameters = declaredParamTypes;
         }
      }

      if (found != null) {
         return found;
      }

      Class declaringClass = theClass.getDeclaringClass();
      Class[] paramTypes = declaredConstructors[0].getParameterTypes();

      if (paramTypes[0] == declaringClass && paramTypes.length > argTypes.length) {
         throw new IllegalArgumentException("Invalid instantiation of inner class; use newInnerInstance instead");
      }

      String argTypesDesc = getParameterTypesDescription(argTypes);
      throw new IllegalArgumentException("No compatible constructor found: " + theClass.getSimpleName() + argTypesDesc);
   }

   @NotNull
   public static  T newInstance(@NotNull Class aClass, @NotNull Object... nonNullArgs)
   {
      Class[] argTypes = getArgumentTypesFromArgumentValues(nonNullArgs);
      Constructor constructor = findCompatibleConstructor(aClass, argTypes);
      return invoke(constructor, nonNullArgs);
   }

   @NotNull public static  T newInstance(@NotNull Class aClass)
   {
      return newInstance(aClass, NO_PARAMETERS);
   }

   @NotNull public static  T newInstanceUsingDefaultConstructor(@NotNull Class aClass)
   {
      try {
         //noinspection ClassNewInstance
         return aClass.newInstance();
      }
      catch (InstantiationException ie) {
         throw new RuntimeException(ie);
      }
      catch (IllegalAccessException ignore) {
         return newInstance(aClass);
      }
   }

   @NotNull
   public static  T newInnerInstance(
      @NotNull Class innerClass, @NotNull Object outerInstance, @NotNull Object... nonNullArgs)
   {
      Object[] initArgs = argumentsWithExtraFirstValue(nonNullArgs, outerInstance);
      return newInstance(innerClass, initArgs);
   }

   @NotNull
   public static  T newInnerInstance(
      @NotNull String innerClassName, @NotNull Object outerInstance, @NotNull Object... nonNullArgs)
   {
      String className = outerInstance.getClass().getName() + '$' + innerClassName;
      Class innerClass = ClassLoad.loadClass(className);

      return newInnerInstance(innerClass, outerInstance, nonNullArgs);
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy