-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathBytecodeNotification.java
More file actions
81 lines (73 loc) · 2.49 KB
/
BytecodeNotification.java
File metadata and controls
81 lines (73 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package org.python.core;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Notifies registered callbacks if new bytecode is loaded.
*/
public class BytecodeNotification {
/**
* Interface for callbacks.
* Notifies the name of the loaded class, raw bytes of the class,
* and the Java class object.
*/
public interface Callback {
public void notify(String name, byte[] bytes, Class c);
}
/**
* The following list stores register callback objects.
* The list is shared among the PySystemState objects
* if there are multiple instances.
*/
private static List<Callback> callbacks = new CopyOnWriteArrayList();
static {
// Maintain legacy behavior
register(new Callback() {
@Override
public void notify(String name, byte[] bytes, Class c) {
if (Options.proxyDebugDirectory == null ||
(!name.startsWith("org.python.pycode.") &&
!name.startsWith("org.python.proxies."))) {
return;
}
ByteArrayOutputStream ostream = new ByteArrayOutputStream(bytes.length);
ostream.write(bytes, 0, bytes.length);
Py.saveClassFile(name, ostream);
}
});
}
/**
* Registers the class as a callback
*
* @param n the callback object
*/
public static void register(Callback n) { callbacks.add(n); }
/**
* Unregisters the callback object
*
* @param n the callback object
* @return true if successfully removed and
* false if the callback object was not registered
*/
public static boolean unregister(Callback n) { return callbacks.remove(n); }
/**
* Clears all the registered callbacks
*/
public static void clear() { callbacks.clear(); }
/**
* Notifies that the new bytecode to the registered callbacks
*
* @param name the name of the class of the new bytecode
* @param data raw byte data of the class
* @param klass Java class object of the new bytecode
*/
public static void notify(String name, byte[] data, Class klass) {
for (Callback c : callbacks) {
try {
c.notify(name, data, klass);
} catch (Exception e) {
Py.writeWarning("BytecodeNotification", "Exception from callback:" + e);
}
}
}
}