@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; }
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); }
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); } } }
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); } } } }
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); } } }
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); }
public void addTransformedClass( @Nonnull ClassIdentification classId, @Nonnull byte[] pretransformClassfile) { transformedClasses.put(classId, pretransformClassfile); }
@Nonnull Set<ClassIdentification> getTransformedClasses() { return transformedClasses.isEmpty() ? Collections.<ClassIdentification>emptySet() : new HashSet<ClassIdentification>(transformedClasses.keySet()); }
private void removeMockedClass(@Nonnull Class<?> mockedClass) { mockedTypesAndInstances.remove(mockedClass); mockedClasses.remove(mockedClass); }
public void registerInstanceFactoryForMockedType( @Nonnull Class<?> mockedType, @Nonnull InstanceFactory mockedInstanceFactory) { registerMockedClass(mockedType); mockedTypesAndInstances.put(mockedType, mockedInstanceFactory); }