Esempio n. 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;
  }
Esempio n. 2
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);
  }
Esempio n. 3
0
  void removeMockedClasses(@Nonnull List<Class<?>> previousMockedClasses) {
    int currentMockedClassCount = mockedClasses.size();

    if (currentMockedClassCount > 0) {
      int previousMockedClassCount = previousMockedClasses.size();

      if (previousMockedClassCount == 0) {
        mockedClasses.clear();
        mockedTypesAndInstances.clear();
      } else if (previousMockedClassCount < currentMockedClassCount) {
        mockedClasses.retainAll(previousMockedClasses);
        mockedTypesAndInstances.keySet().retainAll(previousMockedClasses);
      }
    }
  }
Esempio n. 4
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);
        }
      }
    }
  }
Esempio n. 5
0
  void restoreTransformedClasses(@Nonnull Set<ClassIdentification> previousTransformedClasses) {
    if (!transformedClasses.isEmpty()) {
      Set<ClassIdentification> classesToRestore;

      if (previousTransformedClasses.isEmpty()) {
        classesToRestore = transformedClasses.keySet();
      } else {
        classesToRestore = getTransformedClasses();
        classesToRestore.removeAll(previousTransformedClasses);
      }

      if (!classesToRestore.isEmpty()) {
        restoreAndRemoveTransformedClasses(classesToRestore);
      }
    }
  }
Esempio n. 6
0
  public boolean isStillMocked(@Nullable Object instance, @Nonnull String classDesc) {
    Class<?> targetClass;

    if (instance == null) {
      targetClass = ClassLoad.loadByInternalName(classDesc);
      return isClassAssignableTo(mockedClasses, targetClass);
    }

    targetClass = instance.getClass();
    return mockedTypesAndInstances.containsKey(targetClass) || isInstanceOfMockedClass(instance);
  }
Esempio n. 7
0
 public void addTransformedClass(
     @Nonnull ClassIdentification classId, @Nonnull byte[] pretransformClassfile) {
   transformedClasses.put(classId, pretransformClassfile);
 }
Esempio n. 8
0
 @Nonnull
 Set<ClassIdentification> getTransformedClasses() {
   return transformedClasses.isEmpty()
       ? Collections.<ClassIdentification>emptySet()
       : new HashSet<ClassIdentification>(transformedClasses.keySet());
 }
Esempio n. 9
0
 private void removeMockedClass(@Nonnull Class<?> mockedClass) {
   mockedTypesAndInstances.remove(mockedClass);
   mockedClasses.remove(mockedClass);
 }
Esempio n. 10
0
 public void registerInstanceFactoryForMockedType(
     @Nonnull Class<?> mockedType, @Nonnull InstanceFactory mockedInstanceFactory) {
   registerMockedClass(mockedType);
   mockedTypesAndInstances.put(mockedType, mockedInstanceFactory);
 }