Skip to content

Refactor InvocationsRecorderClassTransformer #33675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
package org.springframework.aot.agent;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
import java.util.Arrays;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.asm.ClassReader;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
Expand All @@ -43,6 +44,8 @@ class InvocationsRecorderClassTransformer implements ClassFileTransformer {

private final String[] ignoredPackages;

private final Log logger = LogFactory.getLog(getClass());

public InvocationsRecorderClassTransformer(String[] instrumentedPackages, String[] ignoredPackages) {
Assert.notNull(instrumentedPackages, "instrumentedPackages must not be null");
Assert.notNull(ignoredPackages, "ignoredPackages must not be null");
Expand All @@ -57,7 +60,7 @@ private String[] rewriteToAsmFormat(String[] packages) {

@Override
public byte[] transform(@Nullable ClassLoader classLoader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
ProtectionDomain protectionDomain, byte[] classfileBuffer) {

if (isTransformationCandidate(classLoader, className)) {
return attemptClassTransformation(classfileBuffer);
Expand All @@ -71,24 +74,22 @@ private boolean isTransformationCandidate(@Nullable ClassLoader classLoader, Str
return false;
}
// Ignore agent classes and spring-core-test DynamicClassLoader
else if (className.startsWith(AGENT_PACKAGE) || className.equals(AOT_DYNAMIC_CLASSLOADER)) {
if (className.startsWith(AGENT_PACKAGE) || className.equals(AOT_DYNAMIC_CLASSLOADER)) {
return false;
}
// Do not instrument CGlib classes
else if (className.contains("$$")) {
if (className.contains("$$")) {
return false;
}
// Only some packages are instrumented
else {
for (String ignoredPackage : this.ignoredPackages) {
if (className.startsWith(ignoredPackage)) {
return false;
}
for (String ignoredPackage : this.ignoredPackages) {
if (className.startsWith(ignoredPackage)) {
return false;
}
for (String instrumentedPackage : this.instrumentedPackages) {
if (className.startsWith(instrumentedPackage)) {
return true;
}
}
for (String instrumentedPackage : this.instrumentedPackages) {
if (className.startsWith(instrumentedPackage)) {
return true;
}
}
return false;
Expand All @@ -101,7 +102,7 @@ private byte[] attemptClassTransformation(byte[] classfileBuffer) {
fileReader.accept(classVisitor, 0);
}
catch (Exception ex) {
ex.printStackTrace();
logger.error(ex);
return classfileBuffer;
}
if (classVisitor.isTransformed()) {
Expand Down
Loading