Skip to content

Commit 5a85a92

Browse files
committed
Script canceling cleanup
1 parent 79aea2d commit 5a85a92

15 files changed

Lines changed: 1059 additions & 155 deletions

File tree

recaf-core/src/main/java/software/coley/recaf/services/plugin/CdiClassAllocator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public <T> T instance(@Nonnull Class<T> cls) throws AllocationException {
3535
try {
3636
// Create bean
3737
Bean<T> bean = (Bean<T>) classBeanMap.computeIfAbsent(cls, c -> {
38-
// TODO bugged.
39-
// Equivalence check is based on the class name and does not include the loader.
38+
// TODO something here is bugged
39+
// bean.create(creationalContext).getClass().getClassLoader() == cls.getClassLoader()
40+
// - Evaluates to false
41+
// - In AnnotatedTypeIdentifier#forBackedAnnotatedType the ClassLoader is not considered, just the class's name
4042
AnnotatedType<T> annotatedClass = beanManager.createAnnotatedType((Class<T>) c);
4143
BeanAttributes<T> attributes = beanManager.createBeanAttributes(annotatedClass);
4244
InjectionTargetFactory<T> factory = beanManager.getInjectionTargetFactory(annotatedClass);

recaf-core/src/main/java/software/coley/recaf/services/script/CancellationSingleton.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,41 @@
44

55
/**
66
* Cancellation singleton.
7+
* <p>
78
* Injected into generated scripts.
8-
* Do not use in normal code.
9+
* <b>Do not use in normal code</b>.
910
*
1011
* @author xDark
12+
* @see InsertCancelSignalVisitor
13+
* @see GenerateResult#requestStop()
1114
*/
1215
public final class CancellationSingleton {
1316
private static volatile boolean shouldStop;
1417

15-
private CancellationSingleton() {
16-
}
18+
private CancellationSingleton() {}
1719

18-
// Names and descriptors are known to the visitor/runtime.
19-
// See InsertCancelSignalVisitor.
20-
// See GenerateResult.
20+
/**
21+
* Schedules cancellation of the script.
22+
*/
2123
public static void stop() {
2224
shouldStop = true;
2325
}
2426

27+
/**
28+
* Clears any prior cancellation request.
29+
*/
30+
public static void reset() {
31+
shouldStop = false;
32+
}
33+
34+
/**
35+
* Polls for cancellation.
36+
*
37+
* @throws CancelSignal
38+
* If cancellation has been requested.
39+
*/
2540
public static void poll() {
26-
if (shouldStop) {
41+
if (shouldStop)
2742
throw CancelSignal.get();
28-
}
2943
}
3044
}

recaf-core/src/main/java/software/coley/recaf/services/script/GenerateResult.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,40 @@ public boolean wasSuccess() {
2626
}
2727

2828
/**
29-
* Attempts to stop the script. If the generation failed,
30-
* this method will do nothing.
29+
* Attempts to stop the script. If the generation failed, this method will do nothing.
30+
* <p>
31+
* Cancellation is scoped to the generated class loader that owns {@link #cls()}.
32+
* Callers that wish to stop running scripts must use {@link ScriptEngine#run(GenerateResult)}
33+
* and track the returned instance for stopping.
3134
*
32-
* @throws IllegalStateException If something went wrong.
35+
* @throws IllegalStateException
36+
* If something went wrong.
3337
*/
3438
public void requestStop() {
39+
invokeCancellationSingleton("stop");
40+
}
41+
42+
/**
43+
* Clears any prior request to stop the script. If the generation failed, this method will do nothing.
44+
*
45+
* @throws IllegalStateException
46+
* If something went wrong.
47+
*/
48+
public void resetStop() {
49+
invokeCancellationSingleton("reset");
50+
}
51+
52+
private void invokeCancellationSingleton(@Nonnull String methodName) {
3553
Class<?> cls = this.cls;
36-
if (cls == null) return;
54+
if (cls == null)
55+
return;
3756
try {
3857
Class<?> cancellationSingleton = cls.getClassLoader().loadClass(CancellationSingleton.class.getName());
39-
cancellationSingleton.getDeclaredMethod("stop").invoke(null);
58+
cancellationSingleton.getDeclaredMethod(methodName).invoke(null);
4059
} catch (InvocationTargetException ex) {
4160
throw new IllegalStateException(ex.getTargetException());
4261
} catch (Exception ex) {
4362
throw new IllegalStateException(ex);
4463
}
4564
}
46-
}
65+
}

0 commit comments

Comments
 (0)