Skip to content

Commit 255d9d5

Browse files
committed
Refactor: remove HookScanner dependency on Bosk
1 parent 164d711 commit 255d9d5

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

bosk-core/src/main/java/works/bosk/Bosk.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ private <T> void localRegisterHook(String name, @NotNull Reference<T> scope, @No
640640
}
641641

642642
public void registerHooks(Object receiver) throws InvalidTypeException {
643-
HookScanner.registerHooks(receiver, this);
643+
HookScanner.registerHooks(receiver, this.rootReference(), this.hookRegistrar());
644644
}
645645

646646
@Override

bosk-core/src/main/java/works/bosk/HookScanner.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* Finds methods annotated with {@link Hook} in the given {@code object} and registers them in the given {@link Bosk}.
2222
*/
2323
final class HookScanner {
24-
static <T> void registerHooks(T receiverObject, Bosk<?> bosk) throws InvalidTypeException {
24+
static <T> void registerHooks(T receiverObject, RootReference<?> rootReference, HookRegistrar hookRegistrar) throws InvalidTypeException {
2525
int hookCounter = 0;
2626
for (
2727
Class<?> receiverClass = receiverObject.getClass();
@@ -40,7 +40,7 @@ static <T> void registerHooks(T receiverObject, Bosk<?> bosk) throws InvalidType
4040
}
4141

4242
try {
43-
registerOneHookMethod(receiverObject, bosk, method, Path.parseParameterized(hookAnnotation.value()));
43+
registerOneHookMethod(receiverObject, method, Path.parseParameterized(hookAnnotation.value()), rootReference, hookRegistrar);
4444
hookCounter++;
4545
} catch (InvalidTypeException e) {
4646
throw new InvalidTypeException("Unable to register hook method " + receiverClass.getSimpleName() + "." + method.getName() + ": " + e.getMessage(), e);
@@ -54,23 +54,23 @@ static <T> void registerHooks(T receiverObject, Bosk<?> bosk) throws InvalidType
5454
}
5555
}
5656

57-
private static <T> void registerOneHookMethod(T receiverObject, Bosk<?> bosk, Method method, Path path) throws InvalidTypeException {
58-
Reference<?> plainRef = bosk.rootReference().then(Object.class, path);
57+
private static <T> void registerOneHookMethod(T receiverObject, Method method, Path path, RootReference<?> rootReference, HookRegistrar hookRegistrar) throws InvalidTypeException {
58+
Reference<?> plainRef = rootReference.then(Object.class, path);
5959

6060
// Now substitute one of the handy Reference subtypes where possible
6161
Reference<?> scope;
6262
if (Catalog.class.isAssignableFrom(plainRef.targetClass())) {
63-
scope = bosk.rootReference().thenCatalog(Entity.class, path);
63+
scope = rootReference.thenCatalog(Entity.class, path);
6464
} else if (Listing.class.isAssignableFrom(plainRef.targetClass())) {
65-
scope = bosk.rootReference().thenListing(Entity.class, path);
65+
scope = rootReference.thenListing(Entity.class, path);
6666
} else if (SideTable.class.isAssignableFrom(plainRef.targetClass())) {
67-
scope = bosk.rootReference().thenSideTable(Entity.class, Object.class, path);
67+
scope = rootReference.thenSideTable(Entity.class, Object.class, path);
6868
} else {
6969
scope = plainRef;
7070
}
7171

7272
List<Function<Reference<?>, Object>> argumentFunctions = new ArrayList<>(method.getParameterCount());
73-
argumentFunctions.add(ref -> receiverObject); // The "this" pointer
73+
argumentFunctions.add(_ -> receiverObject); // The "this" pointer
7474
for (Parameter p : method.getParameters()) {
7575
if (Reference.class.isAssignableFrom(p.getType())) {
7676
Type referenceType = ReferenceUtils.parameterType(p.getParameterizedType(), Reference.class, 0);
@@ -92,7 +92,7 @@ private static <T> void registerOneHookMethod(T receiverObject, Bosk<?> bosk, Me
9292
} catch (IllegalAccessException e) {
9393
throw new IllegalArgumentException(e);
9494
}
95-
bosk.hookRegistrar().registerHook(method.getName(), scope, ref -> {
95+
hookRegistrar.registerHook(method.getName(), scope, ref -> {
9696
try {
9797
List<Object> arguments = new ArrayList<>(argumentFunctions.size());
9898
argumentFunctions.forEach(f -> arguments.add(f.apply(ref)));
@@ -103,5 +103,7 @@ private static <T> void registerOneHookMethod(T receiverObject, Bosk<?> bosk, Me
103103
});
104104
}
105105

106+
private HookScanner() {}
107+
106108
private static final Logger LOGGER = LoggerFactory.getLogger(HookScanner.class);
107109
}

0 commit comments

Comments
 (0)