public abstract class ThirdPartyMethodReplacementClass extends Object implements MethodReplacementClass
There is a problem though :( The replaced methods might have inputs or outputs from the third-party library. To write replacements, we need to create the right method signatures, and those must be available at compilation time. A previous attempt to solve this issue was to include those dependencies, but with "provided" scope (so they will not be included in the uber jar). Unfortunately, this does NOT work, as the classloader that loads the instrumentation might be different from the one used for the SUT. This is the case for example for Spring applications when using External Driver.
The current solution is to use reflection, and have such 3rd-party library NOT on the classpath. They can be in "test" scope when running tests (eg to check validity of string constants), though.
Still, this leaves issue with method signatures. For return types using 3rd-party objects, must put Object as return type, with actual type specified in "castTo". A forced casting is automatically then done at instrumentation time. For input parameters, will need to use the ThirdPartyCast annotation.
There is still the issue of which classloader to use for reflection. For MR of non-static methods, can use classloader of the original caller. For the other cases (eg, static methods and constructors), need to retrieve appropriate classloader from UnitInfoRecorder.
CONSUME_INSTANCE_METHOD_NAME| Modifier | Constructor and Description |
|---|---|
protected |
ThirdPartyMethodReplacementClass() |
| Modifier and Type | Method and Description |
|---|---|
protected static Object |
getField(Object object,
String fieldName) |
protected abstract String |
getNameOfThirdPartyTargetClass() |
static Method |
getOriginal(ThirdPartyMethodReplacementClass singleton,
String id,
Object obj) |
static Constructor |
getOriginalConstructor(ThirdPartyMethodReplacementClass singleton,
String id) |
static Method |
getOriginalStaticMethod(ThirdPartyMethodReplacementClass singleton,
String id) |
Class<?> |
getTargetClass() |
Class<?> |
getTargetClass(ClassLoader loader)
The target class this class provides replacements for.
|
String |
getTargetClassName() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitisAvailableprotected abstract String getNameOfThirdPartyTargetClass()
public static Method getOriginalStaticMethod(ThirdPartyMethodReplacementClass singleton, String id)
public static Method getOriginal(ThirdPartyMethodReplacementClass singleton, String id, Object obj)
singleton - a reference to an instance of the subclass. As reflection is expensive,
we suggest to create it only once, and save it in final static fieldid - of a replacement methodpublic static Constructor getOriginalConstructor(ThirdPartyMethodReplacementClass singleton, String id)
singleton - a reference to an instance of the subclass. As reflection is expensive,
we suggest to create it only once, and save it in final static fieldid - of a replacement methodpublic final Class<?> getTargetClass()
getTargetClass in interface MethodReplacementClasspublic Class<?> getTargetClass(ClassLoader loader)
MethodReplacementClassgetTargetClass in interface MethodReplacementClasspublic final String getTargetClassName()
getTargetClassName in interface MethodReplacementClassCopyright © 2016–2024. All rights reserved.