Skip to content
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

fix #178 by introducing an enum for special allowance of static initializer #184

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
37 changes: 36 additions & 1 deletion 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 reference 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 @@ -289,17 +309,32 @@ public Builder markAsBlocking(String className, String methodName, String signat

/**
* Allows blocking calls inside any method of a class with name identified by the provided className
* and which name matches the provided methodName.
* and which name matches the provided methodName. For special methods, use {@link TargetType} instead.
*
* @param className class' name (e.g. "java.lang.Thread")
* @param methodName a method name
* @return this
* @see #allowBlockingCallsInside(String, TargetType)
*/
public Builder allowBlockingCallsInside(String className, String methodName) {
allowances.computeIfAbsent(className, __ -> new HashMap<>()).put(methodName, true);
return this;
}

/**
* Allows blocking calls inside a special kind of method for 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
* @see TargetType
* @see #allowBlockingCallsInside(String, String)
*/
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