Skip to content

Commit 22c67fd

Browse files
authored
New section: Redefined annotations
1 parent 99b9703 commit 22c67fd

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

README.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,62 @@ MySpecificAnnotation class>>createContainerForRegistry
162162
^SortedCollection sortBlock: #priority ascending
163163
```
164164

165+
## Redefined annotations
166+
167+
All annotations are collected from methods and cached in default ClassAnnotationRegistry instance.
168+
169+
There is special mechanizm to redefine these instances. When cache is updated all redefined annotations are restored.
170+
171+
To redefine particular annotation use #redefineBy: message with block which will modify properties to original instance.
172+
For example following code allows to redefine shortcut of #browse command in Calypso:
173+
```Smalltalk
174+
(ClySpawnFullBrowserCommand classAnnotationsAt: #browserShortcutActivation)
175+
redefineBy: [:shortcut | shortcut keyCombination: $o meta ].
176+
```
177+
Try evaluate it and press cmd+o on selected item in the browser. It will open new browser window.
178+
You can notice that old shortcut cmd+b is not working anymore.
179+
180+
Try manualy reset annotation cache to see that redefined shortcut is still working:
181+
```Smalltalk
182+
ClassAnnotation resetCache.
183+
(ClySpawnFullBrowserCommand classAnnotationsAt: #browserShortcutActivation) inspect
184+
```
185+
To inspect all redefined annotations ask annotation class:
186+
```Smalltalk
187+
CmdShortcutCommandActivation redefinedInstances
188+
```
189+
Redefined instances are stored in class side variable #redefinedInstances.
190+
It is a dictionary which keys are new redefining annotations and values are original annotations collected from methods.
191+
Notice that key and value are equal objects because annotations define equality using annotated class and declaration selector.
192+
So dictionary items can be accessed using both objects.
193+
194+
To check that annotation is redefined use #isRedefined message:
195+
```Smalltalk
196+
(ClySpawnFullBrowserCommand classAnnotationsAt: #browserShortcutActivation)
197+
isRedefined
198+
```
199+
You can ask what instance was redefined:
200+
```Smalltalk
201+
(ClySpawnFullBrowserCommand classAnnotationsAt: #browserShortcutActivation)
202+
redefinedInstance
203+
```
204+
205+
To revert redefined annotation use #revertRedefinedInstance message:
206+
```Smalltalk
207+
(ClySpawnFullBrowserCommand classAnnotationsAt: #browserShortcutActivation)
208+
revertRedefinedInstance
209+
```
210+
This script will revert back old browse shortcut cmd+b (which is defined in annotation declaration method).
211+
212+
You can also revert all redefined annotations:
213+
```Smalltalk
214+
CmdShortcutCommandActivation revertRedefinedInstances
215+
```
216+
Redefining logic is very suitable mehanizm to override system behavior which depends on annotations without changing the code.
217+
218+
It can be used to manage particular kind of annotation in settings browser.
219+
For example shortcut annotations based on Commander are available in setting browser. Users can explore and edit all shortcuts in the system. All these settings are persistable.
220+
165221
## Annotation registry
166222
The cache of class annotations is managed by default instance of ClassAnnotationRegistry. It is subscribed on system changes and it updates the cache automatically when changes affect class annotations.
167223

@@ -200,7 +256,7 @@ For example nobody forbids to pass the value of class side variable to an annota
200256

201257
In such cases developers should invalidate annotation cache manually. It should add following code in required places:
202258
```Smalltalk
203-
ClassAnnotation resetAll
259+
ClassAnnotation resetCache
204260
```
205261

206262
## Installation

0 commit comments

Comments
 (0)