Class ByteBuddyCrossClassLoaderSerializationSupport.MockitoMockObjectInputStream
- All Implemented Interfaces:
Closeable,DataInput,ObjectInput,ObjectStreamConstants,AutoCloseable
- Enclosing class:
ByteBuddyCrossClassLoaderSerializationSupport
ObjectInputStream that will resolve the Mockito proxy class.
This specifically crafted ObjectInoutStream has the most important role to resolve the Mockito generated
class. It is doing so via the resolveClass(ObjectStreamClass) which looks in the stream
for a Mockito marker. If this marker is found it will try to resolve the mockito class otherwise it
delegates class resolution to the default super behavior.
The mirror method used for serializing the mock is ByteBuddyCrossClassLoaderSerializationSupport.MockitoMockObjectOutputStream.annotateClass(Class).
When this marker is found, ByteBuddyMockMaker.createMockType(MockCreationSettings) methods are being used
to create the mock class.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.io.ObjectInputStream
ObjectInputStream.GetField -
Field Summary
FieldsFields inherited from interface java.io.ObjectStreamConstants
baseWireHandle, PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, SC_BLOCK_DATA, SC_ENUM, SC_EXTERNALIZABLE, SC_SERIALIZABLE, SC_WRITE_METHOD, SERIAL_FILTER_PERMISSION, STREAM_MAGIC, STREAM_VERSION, SUBCLASS_IMPLEMENTATION_PERMISSION, SUBSTITUTION_PERMISSION, TC_ARRAY, TC_BASE, TC_BLOCKDATA, TC_BLOCKDATALONG, TC_CLASS, TC_CLASSDESC, TC_ENDBLOCKDATA, TC_ENUM, TC_EXCEPTION, TC_LONGSTRING, TC_MAX, TC_NULL, TC_OBJECT, TC_PROXYCLASSDESC, TC_REFERENCE, TC_RESET, TC_STRING -
Constructor Summary
ConstructorsConstructorDescriptionMockitoMockObjectInputStream(InputStream in, Class<?> typeToMock, Set<Class<?>> extraInterfaces) -
Method Summary
Modifier and TypeMethodDescriptionprivate voidhackClassNameToMatchNewlyCreatedClass(ObjectStreamClass descInstance, Class<?> proxyClass) Hack thenamefield of the givenObjectStreamClasswith thenewProxyClass.private booleannotMarkedAsAMockitoMock(Object marker) Read the stream class annotation and identify it as a Mockito mock or not.protected Class<?> Resolve the Mockito proxy class if it is marked as such.Methods inherited from class java.io.ObjectInputStream
available, close, defaultReadObject, enableResolveObject, getObjectInputFilter, read, read, readBoolean, readByte, readChar, readClassDescriptor, readDouble, readFields, readFloat, readFully, readFully, readInt, readLine, readLong, readObject, readObjectOverride, readShort, readStreamHeader, readUnshared, readUnsignedByte, readUnsignedShort, readUTF, registerValidation, resolveObject, resolveProxyClass, setObjectInputFilter, skipBytesMethods inherited from class java.io.InputStream
mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferToMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.io.ObjectInput
read, skip
-
Field Details
-
typeToMock
-
extraInterfaces
-
-
Constructor Details
-
MockitoMockObjectInputStream
public MockitoMockObjectInputStream(InputStream in, Class<?> typeToMock, Set<Class<?>> extraInterfaces) throws IOException - Throws:
IOException
-
-
Method Details
-
resolveClass
Resolve the Mockito proxy class if it is marked as such.Uses the fields
typeToMockandextraInterfacesto create the Mockito proxy class as theObjectStreamClassdoesn't carry useful information for this purpose.- Overrides:
resolveClassin classObjectInputStream- Parameters:
desc- Description of the class in the stream, not used.- Returns:
- The class that will be used to deserialize the instance mock.
- Throws:
IOExceptionClassNotFoundException
-
hackClassNameToMatchNewlyCreatedClass
private void hackClassNameToMatchNewlyCreatedClass(ObjectStreamClass descInstance, Class<?> proxyClass) throws ObjectStreamException Hack thenamefield of the givenObjectStreamClasswith thenewProxyClass. The parent ObjectInputStream will check the name of the class in the stream matches the name of the one that is created in this method. The CGLIB classes uses a hash of the classloader and/or maybe some other data that allow them to be relatively unique in a JVM. When names differ, which happens when the mock is deserialized in another ClassLoader, ajava.io.InvalidObjectExceptionis thrown, so this part of the code is hacking through the givenObjectStreamClassto change the name with the newly created class.- Parameters:
descInstance- TheObjectStreamClassthat will be hacked.proxyClass- The proxy class whose name will be applied.- Throws:
InvalidObjectExceptionObjectStreamException
-
notMarkedAsAMockitoMock
Read the stream class annotation and identify it as a Mockito mock or not.- Parameters:
marker- The marker to identify.- Returns:
trueif not marked as a Mockito,falseif the class annotation marks a Mockito mock.
-