Example #1
0
  @Nullable
  public InstanceFactory findInstanceFactory(@Nonnull Type mockedType) {
    InstanceFactory instanceFactory = mockedTypesAndInstances.get(mockedType);

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

    Class<?> mockedClass = getClassType(mockedType);
    //noinspection ReuseOfLocalVariable
    instanceFactory = mockedTypesAndInstances.get(mockedClass);

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

    boolean abstractType = mockedClass.isInterface() || isAbstract(mockedClass.getModifiers());

    for (Entry<Type, InstanceFactory> entry : mockedTypesAndInstances.entrySet()) {
      Type registeredMockedType = entry.getKey();
      Class<?> registeredMockedClass = getClassType(registeredMockedType);

      if (abstractType) {
        registeredMockedClass = getMockedClassOrInterfaceType(registeredMockedClass);
      }

      if (mockedClass.isAssignableFrom(registeredMockedClass)) {
        instanceFactory = entry.getValue();
        break;
      }
    }

    return instanceFactory;
  }
Example #2
0
  void restoreRedefinedClasses(@Nonnull Map<?, byte[]> previousDefinitions) {
    if (redefinedClasses.isEmpty()) {
      return;
    }

    RedefinitionEngine redefinitionEngine = new RedefinitionEngine();
    Iterator<Entry<Class<?>, byte[]>> itr = redefinedClasses.entrySet().iterator();

    while (itr.hasNext()) {
      Entry<Class<?>, byte[]> entry = itr.next();
      Class<?> redefinedClass = entry.getKey();
      byte[] currentDefinition = entry.getValue();
      byte[] previousDefinition = previousDefinitions.get(redefinedClass);

      //noinspection ArrayEquality
      if (currentDefinition != previousDefinition) {
        redefinitionEngine.restoreDefinition(redefinedClass, previousDefinition);

        if (previousDefinition == null) {
          restoreDefinition(redefinedClass);
          discardStateForCorrespondingMockClassIfAny(redefinedClass);
          itr.remove();
        } else {
          entry.setValue(previousDefinition);
        }
      }
    }
  }
Example #3
0
  private void restoreAndRemoveTransformedClasses(
      @Nonnull Set<ClassIdentification> classesToRestore) {
    RedefinitionEngine redefinitionEngine = new RedefinitionEngine();

    for (ClassIdentification transformedClassId : classesToRestore) {
      byte[] definitionToRestore = transformedClasses.get(transformedClassId);
      redefinitionEngine.restoreToDefinition(
          transformedClassId.getLoadedClass(), definitionToRestore);
    }

    transformedClasses.keySet().removeAll(classesToRestore);
  }