|
13 | 13 |
|
14 | 14 | public class BasicMethodRegistry implements MethodRegistry { |
15 | 15 |
|
16 | | - private static String formKey(String alias, Class<?> klass) { |
17 | | - return alias + "#" + klass.getName().replaceAll("\\$", "."); |
18 | | - } |
| 16 | + private static String formKey(String alias, Class<?> klass) { |
| 17 | + return alias + "#" + klass.getName().replaceAll("\\$", "."); |
| 18 | + } |
19 | 19 |
|
20 | | - private static class RegistryItem { |
21 | | - private String methodName; |
22 | | - private Class<?> klass; |
23 | | - private Class<?>[] args; |
24 | | - } |
| 20 | + private static class RegistryItem { |
| 21 | + private String methodName; |
| 22 | + private Class<?> klass; |
| 23 | + private Class<?>[] args; |
| 24 | + } |
25 | 25 |
|
26 | | - private Map<String, RegistryItem> registry = new HashMap<>(); |
27 | | - private Map<String, RegistryItem> globalRegistry = new HashMap<>(); |
| 26 | + private Map<String, RegistryItem> registry = new HashMap<String, RegistryItem>(); |
| 27 | + private Map<String, RegistryItem> globalRegistry = new HashMap<String, RegistryItem>(); |
28 | 28 |
|
29 | | - /* (non-Javadoc) |
30 | | - * @see com.scriptbasic.executors.MethodRegistry#getJavaMethod(java.lang.Class, java.lang.String) |
31 | | - */ |
32 | | - @Override |
33 | | - public Method getJavaMethod(Class<?> klass, String alias) |
34 | | - throws ExecutionException { |
35 | | - Method method = null; |
36 | | - if (klass == null) { |
37 | | - method = getJavaMethod(alias); |
38 | | - } else { |
39 | | - RegistryItem item = registry.get(formKey(alias, klass)); |
40 | | - if (item != null) { |
41 | | - try { |
42 | | - method = item.klass.getMethod(item.methodName, item.args); |
43 | | - } catch (NoSuchMethodException | SecurityException e) { |
44 | | - throw new BasicRuntimeException("Method '" |
45 | | - + item.methodName + "' from class '" + item.klass |
46 | | - + "' can not be accessed", e); |
47 | | - } |
48 | | - } |
49 | | - } |
50 | | - return method; |
51 | | - } |
| 29 | + /* |
| 30 | + * (non-Javadoc) |
| 31 | + * |
| 32 | + * @see |
| 33 | + * com.scriptbasic.executors.MethodRegistry#getJavaMethod(java.lang.Class, |
| 34 | + * java.lang.String) |
| 35 | + */ |
| 36 | + @Override |
| 37 | + public Method getJavaMethod(Class<?> klass, String alias) |
| 38 | + throws ExecutionException { |
| 39 | + Method method = null; |
| 40 | + if (klass == null) { |
| 41 | + method = getJavaMethod(alias); |
| 42 | + } else { |
| 43 | + RegistryItem item = registry.get(formKey(alias, klass)); |
| 44 | + if (item != null) { |
| 45 | + try { |
| 46 | + method = item.klass.getMethod(item.methodName, item.args); |
| 47 | + } catch (Exception e) { |
| 48 | + throw new BasicRuntimeException("Method '" |
| 49 | + + item.methodName + "' from class '" + item.klass |
| 50 | + + "' can not be accessed", e); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + return method; |
| 55 | + } |
52 | 56 |
|
53 | | - private Method getJavaMethod(String alias) throws ExecutionException { |
54 | | - RegistryItem item = globalRegistry.get(alias); |
55 | | - Method method = null; |
56 | | - if (item != null) { |
57 | | - try { |
58 | | - method = item.klass.getMethod(item.methodName, item.args); |
59 | | - } catch (NoSuchMethodException | SecurityException e) { |
60 | | - throw new BasicRuntimeException("Method '" + item.methodName |
61 | | - + "' from class '" + item.klass |
62 | | - + "' can not be accessed", e); |
63 | | - } |
64 | | - } |
65 | | - return method; |
66 | | - } |
| 57 | + private Method getJavaMethod(String alias) throws ExecutionException { |
| 58 | + RegistryItem item = globalRegistry.get(alias); |
| 59 | + Method method = null; |
| 60 | + if (item != null) { |
| 61 | + try { |
| 62 | + method = item.klass.getMethod(item.methodName, item.args); |
| 63 | + } catch (Exception e) { |
| 64 | + throw new BasicRuntimeException("Method '" + item.methodName |
| 65 | + + "' from class '" + item.klass |
| 66 | + + "' can not be accessed", e); |
| 67 | + } |
| 68 | + } |
| 69 | + return method; |
| 70 | + } |
67 | 71 |
|
68 | | - /* |
69 | | - * Register an item in the global registry so that the program can use it |
70 | | - * without specifying the class that the method belongs to. This makes it |
71 | | - * possible to use these methods as simple functions in the BASIC program. |
72 | | - * On the other hand this mechanism does not protect from name collisions. |
73 | | - * If two different methods from two different classes use the same alias |
74 | | - * then they can not be used as functions at the same time. In that case the |
75 | | - * class alias also have to be used in the BASIC program. |
76 | | - * <p> |
77 | | - * This method registers the item containing the name of the method and also |
78 | | - * the class and the argument types if a function with that name was not |
79 | | - * registered yet. If there was already a function with the name registered |
80 | | - * then it removes the previous registry from the store and registers |
81 | | - * {@code null} as item, so that none of the methods can later be used as |
82 | | - * simple functions. |
83 | | - * <p> |
84 | | - * Even though the functionality of evaluation of function calls is not |
85 | | - * implemented in class or in this package if is good to note that if a |
86 | | - * BASIC subroutine is defined with the same name as an alias then that will |
87 | | - * rule. |
88 | | - * |
89 | | - * @param alias |
90 | | - * @param item |
91 | | - */ |
92 | | - private void registerGlobal(String alias, RegistryItem item) { |
93 | | - if (globalRegistry.containsKey(alias)) { |
94 | | - globalRegistry.put(alias, null); |
95 | | - } else { |
96 | | - globalRegistry.put(alias, item); |
97 | | - } |
98 | | - } |
| 72 | + /* |
| 73 | + * Register an item in the global registry so that the program can use it |
| 74 | + * without specifying the class that the method belongs to. This makes it |
| 75 | + * possible to use these methods as simple functions in the BASIC program. |
| 76 | + * On the other hand this mechanism does not protect from name collisions. |
| 77 | + * If two different methods from two different classes use the same alias |
| 78 | + * then they can not be used as functions at the same time. In that case the |
| 79 | + * class alias also have to be used in the BASIC program. <p> This method |
| 80 | + * registers the item containing the name of the method and also the class |
| 81 | + * and the argument types if a function with that name was not registered |
| 82 | + * yet. If there was already a function with the name registered then it |
| 83 | + * removes the previous registry from the store and registers {@code null} |
| 84 | + * as item, so that none of the methods can later be used as simple |
| 85 | + * functions. <p> Even though the functionality of evaluation of function |
| 86 | + * calls is not implemented in class or in this package if is good to note |
| 87 | + * that if a BASIC subroutine is defined with the same name as an alias then |
| 88 | + * that will rule. |
| 89 | + * |
| 90 | + * @param alias |
| 91 | + * |
| 92 | + * @param item |
| 93 | + */ |
| 94 | + private void registerGlobal(String alias, RegistryItem item) { |
| 95 | + if (globalRegistry.containsKey(alias)) { |
| 96 | + globalRegistry.put(alias, null); |
| 97 | + } else { |
| 98 | + globalRegistry.put(alias, item); |
| 99 | + } |
| 100 | + } |
99 | 101 |
|
100 | | - /* (non-Javadoc) |
101 | | - * @see com.scriptbasic.executors.MethodRegistry#registerJavaMethod(java.lang.String, java.lang.Class, java.lang.String, java.lang.Class) |
102 | | - */ |
103 | | - @Override |
104 | | - public void registerJavaMethod(String alias, Class<?> klass, |
105 | | - String methodName, Class<?>[] argumentTypes) { |
106 | | - RegistryItem item = new RegistryItem(); |
107 | | - item.methodName = methodName; |
108 | | - item.klass = klass; |
109 | | - item.args = argumentTypes.clone(); |
110 | | - registry.put(formKey(alias, klass), item); |
111 | | - registerGlobal(alias, item); |
112 | | - } |
| 102 | + /* |
| 103 | + * (non-Javadoc) |
| 104 | + * |
| 105 | + * @see |
| 106 | + * com.scriptbasic.executors.MethodRegistry#registerJavaMethod(java.lang |
| 107 | + * .String, java.lang.Class, java.lang.String, java.lang.Class) |
| 108 | + */ |
| 109 | + @Override |
| 110 | + public void registerJavaMethod(String alias, Class<?> klass, |
| 111 | + String methodName, Class<?>[] argumentTypes) { |
| 112 | + RegistryItem item = new RegistryItem(); |
| 113 | + item.methodName = methodName; |
| 114 | + item.klass = klass; |
| 115 | + item.args = argumentTypes.clone(); |
| 116 | + registry.put(formKey(alias, klass), item); |
| 117 | + registerGlobal(alias, item); |
| 118 | + } |
113 | 119 |
|
114 | 120 | } |
0 commit comments