Skip to content

Commit

Permalink
Add constant STATIC_INITIALIZER for allowBlockingCalls
Browse files Browse the repository at this point in the history
This commit introduces a STATIC_INITIALIZER constant in BlockHound, so
it becomes easier to discover that the features relying on the
`String methodName` parameter can work with `<clinit>` for static
initializer.

Additionally, this is documented in the customization examples and
used in the StaticInitTest.

Fixes #178.
  • Loading branch information
simonbasle committed Mar 25, 2021
1 parent d05ed8f commit d635dbf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
10 changes: 9 additions & 1 deletion agent/src/main/java/reactor/blockhound/BlockHound.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@
*/
public class BlockHound {

static final String PREFIX = "$$BlockHound$$_";
/**
* The special methodName that should be used in e.g. {@link Builder#allowBlockingCallsInside(String, String)}
* when one wants to reference the static initializer block rather than a class method.
*/
public static final String STATIC_INITIALIZER = "<clinit>";

static final String PREFIX = "$$BlockHound$$_";

private static final AtomicBoolean INITIALIZED = new AtomicBoolean(false);

Expand Down Expand Up @@ -294,6 +300,7 @@ public Builder markAsBlocking(String className, String methodName, String signat
* @param className class' name (e.g. "java.lang.Thread")
* @param methodName a method name
* @return this
* @see BlockHound#STATIC_INITIALIZER
*/
public Builder allowBlockingCallsInside(String className, String methodName) {
allowances.computeIfAbsent(className, __ -> new HashMap<>()).put(methodName, true);
Expand All @@ -307,6 +314,7 @@ public Builder allowBlockingCallsInside(String className, String methodName) {
* @param className class' name (e.g. "java.lang.Thread")
* @param methodName a method name
* @return this
* @see BlockHound#STATIC_INITIALIZER
*/
public Builder disallowBlockingCallsInside(String className, String methodName) {
allowances.computeIfAbsent(className, __ -> new HashMap<>()).put(methodName, false);
Expand Down
8 changes: 8 additions & 0 deletions docs/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ builder.disallowBlockingCallsInside(
);
```

This will allow blocking method calls inside the static initializer block of `MyClass` down the callstack:
```java
builder.allowBlockingCallsInside(
"com.example.MyClass",
BlockHound.STATIC_INITIALIZER
);
```

## Custom blocking method callback
* `Builder#blockingMethodCallback(Consumer<BlockingMethod> consumer)`

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.STATIC_INITIALIZER);
});
}

Expand Down

0 comments on commit d635dbf

Please sign in to comment.