Skip to content

Commit

Permalink
fix #178 by introducing an enum for special allowance of static initi…
Browse files Browse the repository at this point in the history
…alizer
  • Loading branch information
simonbasle committed Mar 29, 2021
1 parent 7dd8d30 commit d5f25ca
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions agent/src/main/java/reactor/blockhound/BlockHound.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ private BlockHound() {

}

/**
* Represents special methods that can be used when configuring BlockHound via the {@link Builder}.
*/
public enum TargetType {

/**
* Easily references the static initializer block of a class.
*/
STATIC_INITIALIZER("<clinit>");

/**
* The {@link String} representation of the special method, as in the JVM specification.
*/
public final String methodName;

TargetType(String methodName) {
this.methodName = methodName;
}
}

private static final class BlockHoundPoolStrategy implements PoolStrategy {

public static final PoolStrategy INSTANCE = new BlockHoundPoolStrategy();
Expand Down Expand Up @@ -300,6 +320,18 @@ public Builder allowBlockingCallsInside(String className, String methodName) {
return this;
}

/**
* Allows blocking calls inside a special kind of method of a class with name identified by
* the provided className. The special method is defined by the provided {@link TargetType}.
*
* @param className class' name (e.g. "java.lang.Thread")
* @param method a {@link TargetType}
* @return this
*/
public Builder allowBlockingCallsInside(String className, TargetType method) {
return allowBlockingCallsInside(className, method.methodName);
}

/**
* Disallows blocking calls inside any method of a class with name identified by the provided className
* and which name matches the provided methodName.
Expand Down
2 changes: 1 addition & 1 deletion example/src/test/java/com/example/StaticInitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class StaticInitTest {

static {
BlockHound.install(b -> {
b.allowBlockingCallsInside(ClassWithStaticInit.class.getName(), "<clinit>");
b.allowBlockingCallsInside(ClassWithStaticInit.class.getName(), BlockHound.TargetType.STATIC_INITIALIZER);
});
}

Expand Down

0 comments on commit d5f25ca

Please sign in to comment.