Skip to content

Commit

Permalink
Merge pull request #25019 from stuartwdouglas/24943
Browse files Browse the repository at this point in the history
Don't fail if we can't transform test classes
  • Loading branch information
gsmet authored Apr 20, 2022
2 parents 32afb53 + af5949b commit 34ac54b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public final class BytecodeTransformerBuildItem extends MultiBuildItem {

final int classReaderOptions;

final boolean continueOnFailure;

public BytecodeTransformerBuildItem(String classToTransform,
BiFunction<String, ClassVisitor, ClassVisitor> visitorFunction) {
this(classToTransform, visitorFunction, null);
Expand Down Expand Up @@ -78,6 +80,7 @@ public BytecodeTransformerBuildItem(boolean eager, String classToTransform,
this.cacheable = cacheable;
this.inputTransformer = null;
this.classReaderOptions = 0;
this.continueOnFailure = false;
}

public BytecodeTransformerBuildItem(Builder builder) {
Expand All @@ -88,6 +91,7 @@ public BytecodeTransformerBuildItem(Builder builder) {
this.cacheable = builder.cacheable;
this.inputTransformer = builder.inputTransformer;
this.classReaderOptions = builder.classReaderOptions;
this.continueOnFailure = builder.continueOnFailure;
if (visitorFunction == null && inputTransformer == null) {
throw new IllegalArgumentException("One of either visitorFunction or inputTransformer must be set");
}
Expand Down Expand Up @@ -121,15 +125,25 @@ public BiFunction<String, byte[], byte[]> getInputTransformer() {
return inputTransformer;
}

public boolean isContinueOnFailure() {
return continueOnFailure;
}

public static class Builder {
public BiFunction<String, byte[], byte[]> inputTransformer;
public boolean continueOnFailure;
private String classToTransform;
private BiFunction<String, ClassVisitor, ClassVisitor> visitorFunction;
private Set<String> requireConstPoolEntry = null;
private boolean eager = false;
private boolean cacheable = false;
private int classReaderOptions = 0;

public Builder setContinueOnFailure(boolean continueOnFailure) {
this.continueOnFailure = continueOnFailure;
return this;
}

public Builder setInputTransformer(BiFunction<String, byte[], byte[]> inputTransformer) {
this.inputTransformer = inputTransformer;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,18 @@ public void instrumentTestClasses(CombinedIndexBuildItem combinedIndexBuildItem,
for (ClassInfo clazz : combinedIndexBuildItem.getIndex().getKnownClasses()) {
String theClassName = clazz.name().toString();
if (isAppClass(theClassName)) {
transformerProducer.produce(new BytecodeTransformerBuildItem(false, theClassName,
new BiFunction<String, ClassVisitor, ClassVisitor>() {
@Override
public ClassVisitor apply(String s, ClassVisitor classVisitor) {
return new TracingClassVisitor(classVisitor, theClassName);
}
}, true));
transformerProducer.produce(new BytecodeTransformerBuildItem.Builder().setEager(false)
.setClassToTransform(theClassName)
.setVisitorFunction(
new BiFunction<String, ClassVisitor, ClassVisitor>() {
@Override
public ClassVisitor apply(String s, ClassVisitor classVisitor) {
return new TracingClassVisitor(classVisitor, theClassName);
}
})
.setCacheable(true)
.setContinueOnFailure(true)
.build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ public byte[] apply(String className, byte[] originalBytes) {
if (classTransformers == null) {
return originalBytes;
}
boolean continueOnFailure = classTransformers.stream()
.filter(a -> !a.isContinueOnFailure())
.findAny().isEmpty();
List<BiFunction<String, ClassVisitor, ClassVisitor>> visitors = classTransformers.stream()
.map(BytecodeTransformerBuildItem::getVisitorFunction).filter(Objects::nonNull)
.collect(Collectors.toList());
Expand Down Expand Up @@ -154,6 +157,17 @@ public byte[] apply(String className, byte[] originalBytes) {
} else {
return originalBytes;
}
} catch (Throwable e) {
if (continueOnFailure) {
if (log.isDebugEnabled()) {
log.errorf(e, "Failed to transform %s", className);
} else {
log.errorf("Failed to transform %s", className);
}
return originalBytes;
} else {
throw e;
}
} finally {
Thread.currentThread().setContextClassLoader(old);
}
Expand Down Expand Up @@ -184,6 +198,10 @@ public byte[] apply(String className, byte[] originalBytes) {
entry.getKey());
continue;
}

boolean continueOnFailure = entry.getValue().stream()
.filter(a -> !a.isContinueOnFailure())
.findAny().isEmpty();
List<BiFunction<String, ClassVisitor, ClassVisitor>> visitors = entry.getValue().stream()
.map(BytecodeTransformerBuildItem::getVisitorFunction).filter(Objects::nonNull)
.collect(Collectors.toList());
Expand All @@ -196,9 +214,9 @@ public byte[] apply(String className, byte[] originalBytes) {
public TransformedClassesBuildItem.TransformedClass call() throws Exception {
ClassLoader old = Thread.currentThread().getContextClassLoader();
try {
byte[] classData = classPathElement.getResource(classFileName).getData();
Thread.currentThread().setContextClassLoader(transformCl);
Set<String> constValues = constScanning.get(className);
byte[] classData = classPathElement.getResource(classFileName).getData();
if (constValues != null && !noConstScanning.contains(className)) {
if (!ConstPoolScanner.constPoolEntryPresent(classData, constValues)) {
return null;
Expand All @@ -214,6 +232,17 @@ public TransformedClassesBuildItem.TransformedClass call() throws Exception {
transformedClassesCache.put(className, transformedClass);
}
return transformedClass;
} catch (Throwable e) {
if (continueOnFailure) {
if (log.isDebugEnabled()) {
log.errorf(e, "Failed to transform %s", className);
} else {
log.errorf("Failed to transform %s", className);
}
return null;
} else {
throw e;
}
} finally {
Thread.currentThread().setContextClassLoader(old);
}
Expand Down

0 comments on commit 34ac54b

Please sign in to comment.