Skip to content

Commit

Permalink
Add method to patch with explicit differential matcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Aug 13, 2023
1 parent 0f1d715 commit 405af61
Showing 1 changed file with 89 additions and 8 deletions.
Expand Up @@ -783,6 +783,25 @@ Ignored ignore(ElementMatcher<? super TypeDescription> typeMatcher,
*/
ResettableClassFileTransformer patchOn(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer);

/**
* <p>
* Creates and installs a {@link ResettableClassFileTransformer} that implements the configuration of
* this agent builder with a given {@link java.lang.instrument.Instrumentation}. If retransformation is enabled,
* the installation also causes all loaded types to be retransformed which have changed compared to the previous
* class file transformer that is provided as an argument. Without specification, {@link PatchMode#OVERLAP} is used.
* </p>
* <p>
* In order to assure the correct handling of the {@link InstallationListener}, an uninstallation should be applied
* via the {@link ResettableClassFileTransformer}'s {@code reset} methods.
* </p>
*
* @param instrumentation The instrumentation on which this agent builder's configuration is to be installed.
* @param classFileTransformer The class file transformer that is being patched.
* @param differentialMatcher The differential matcher to decide what types need retransformation.
* @return The installed class file transformer.
*/
ResettableClassFileTransformer patchOn(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, RawMatcher differentialMatcher);

/**
* <p>
* Creates and installs a {@link ResettableClassFileTransformer} that implements the configuration of
Expand All @@ -802,6 +821,26 @@ Ignored ignore(ElementMatcher<? super TypeDescription> typeMatcher,
*/
ResettableClassFileTransformer patchOn(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, PatchMode patchMode);

/**
* <p>
* Creates and installs a {@link ResettableClassFileTransformer} that implements the configuration of
* this agent builder with a given {@link java.lang.instrument.Instrumentation}. If retransformation is enabled,
* the installation also causes all loaded types to be retransformed which have changed compared to the previous
* class file transformer that is provided as an argument.
* </p>
* <p>
* In order to assure the correct handling of the {@link InstallationListener}, an uninstallation should be applied
* via the {@link ResettableClassFileTransformer}'s {@code reset} methods.
* </p>
*
* @param instrumentation The instrumentation on which this agent builder's configuration is to be installed.
* @param classFileTransformer The class file transformer that is being patched.
* @param differentialMatcher The differential matcher to decide what types need retransformation.
* @param patchMode The patch mode to apply.
* @return The installed class file transformer.
*/
ResettableClassFileTransformer patchOn(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, RawMatcher differentialMatcher, PatchMode patchMode);

/**
* <p>
* Creates and installs a {@link ResettableClassFileTransformer} that implements the configuration of
Expand Down Expand Up @@ -9931,6 +9970,18 @@ protected Handler toHandler(ResettableClassFileTransformer classFileTransformer)
}
};

/**
* Resolves a default patch mode for a given {@link ResettableClassFileTransformer}.
*
* @param classFileTransformer The class file transformer to consider.
* @return A meaningful default patch mode.
*/
protected static PatchMode of(ResettableClassFileTransformer classFileTransformer) {
return classFileTransformer instanceof ResettableClassFileTransformer.Substitutable
? PatchMode.SUBSTITUTE
: PatchMode.OVERLAP;
}

/**
* Resolves this strategy to a handler.
*
Expand Down Expand Up @@ -10710,7 +10761,9 @@ public AgentBuilder with(LocationStrategy locationStrategy) {
transformations);
}

@Override
/**
* {@inheritDoc}
*/
public AgentBuilder with(ClassFileLocator classFileLocator) {
return new Default(byteBuddy,
listener,
Expand Down Expand Up @@ -11328,18 +11381,30 @@ public ResettableClassFileTransformer installOnByteBuddyAgent() {
* {@inheritDoc}
*/
public ResettableClassFileTransformer patchOn(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer) {
return patchOn(instrumentation, classFileTransformer, classFileTransformer instanceof ResettableClassFileTransformer.Substitutable
? PatchMode.SUBSTITUTE
: PatchMode.OVERLAP);
return patchOn(instrumentation, classFileTransformer, PatchMode.of(classFileTransformer));
}

/**
* {@inheritDoc}
*/
public ResettableClassFileTransformer patchOn(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, RawMatcher differentialMatcher) {
return patchOn(instrumentation, classFileTransformer, differentialMatcher, PatchMode.of(classFileTransformer));
}

/**
* {@inheritDoc}
*/
public ResettableClassFileTransformer patchOn(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, PatchMode patchMode) {
return doInstall(instrumentation, new Transformation.DifferentialMatcher(ignoreMatcher, transformations, classFileTransformer instanceof ResettableClassFileTransformer.Substitutable
? ((ResettableClassFileTransformer.Substitutable) classFileTransformer).unwrap()
: classFileTransformer), patchMode.toHandler(classFileTransformer));
return patchOn(instrumentation, classFileTransformer, new Transformation.DifferentialMatcher(ignoreMatcher, transformations, classFileTransformer instanceof ResettableClassFileTransformer.Substitutable
? ((ResettableClassFileTransformer.Substitutable) classFileTransformer).unwrap()
: classFileTransformer), patchMode);
}

/**
* {@inheritDoc}
*/
public ResettableClassFileTransformer patchOn(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, RawMatcher differentialMatcher, PatchMode patchMode) {
return doInstall(instrumentation, differentialMatcher, patchMode.toHandler(classFileTransformer));
}

/**
Expand Down Expand Up @@ -12876,7 +12941,9 @@ public AgentBuilder with(LocationStrategy locationStrategy) {
return materialize().with(locationStrategy);
}

@Override
/**
* {@inheritDoc}
*/
public AgentBuilder with(ClassFileLocator classFileLocator) {
return materialize().with(classFileLocator);
}
Expand Down Expand Up @@ -13109,13 +13176,27 @@ public ResettableClassFileTransformer patchOn(Instrumentation instrumentation, R
return materialize().patchOn(instrumentation, classFileTransformer);
}

/**
* {@inheritDoc}
*/
public ResettableClassFileTransformer patchOn(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, RawMatcher differentialMatcher) {
return materialize().patchOn(instrumentation, classFileTransformer, differentialMatcher);
}

/**
* {@inheritDoc}
*/
public ResettableClassFileTransformer patchOn(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, PatchMode patchMode) {
return materialize().patchOn(instrumentation, classFileTransformer, patchMode);
}

/**
* {@inheritDoc}
*/
public ResettableClassFileTransformer patchOn(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, RawMatcher differentialMatcher, PatchMode patchMode) {
return materialize().patchOn(instrumentation, classFileTransformer, differentialMatcher, patchMode);
}

/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit 405af61

Please sign in to comment.