added:
authorClaus Gittinger <cg@exept.de>
Wed, 14 Oct 2009 15:32:30 +0200
changeset 8987 6ac672f5991f
parent 8986 df99cfbe7aa7
child 8988 8a0410006a9c
added: #codeFor_classInitialize #createClassInitializeMethodIn:
CodeGeneratorTool.st
--- a/CodeGeneratorTool.st	Wed Oct 14 15:32:07 2009 +0200
+++ b/CodeGeneratorTool.st	Wed Oct 14 15:32:30 2009 +0200
@@ -863,6 +863,51 @@
     "Modified: / 27-10-2006 / 10:21:58 / cg"
 !
 
+createClassInitializeMethodIn:aClass
+    "create a #initialize method on the class side (I'm tired of typing)"
+
+    |nonMetaClass metaClass className code initializer bindings|
+
+    nonMetaClass := aClass theNonMetaclass.
+    metaClass := aClass theMetaclass.
+    className := nonMetaClass name.
+
+    self startCollectChanges.
+
+    (metaClass includesSelector:#'initialize') ifFalse:[
+'initialize
+    "Invoked at system start or when the class is dynamically loaded."
+
+    "/ please change as required (and remove this comment)
+'.
+        bindings := Dictionary new.
+        bindings at:'INIT_CLASSINSTVARS' put:(
+            String streamContents:[:s |
+                metaClass instVarNames do:[:eachClassInstVar |
+                    initializer := 'nil'. "/ need more intelligence here (try to guess class from messages sent to it) ...
+                    s nextPutLine:('    "/ %1 := %2.' bindWith:eachClassInstVar with:initializer).
+                ]
+            ]).
+
+        bindings at:'INIT_CLASSVARS' put:(
+            String streamContents:[:s |
+                nonMetaClass classVarNames do:[:eachClassVar |
+                    initializer := 'nil'. "/ need more intelligence here (try to guess class from messages sent to it) ...
+                    s nextPutLine:('    "/ %1 := %2.' bindWith:eachClassVar with:initializer).
+                ]
+            ]).
+
+        code := (self codeFor_classInitialize) expandPlaceholdersWith:bindings.
+
+        self 
+            compile:code
+            forClass:metaClass 
+            inCategory:'initialization'.
+    ].
+
+    self executeCollectedChangesNamed:('Add Class Initializer to ' , className).
+!
+
 createClassResponsibleProtocolFor:aClass
     "create stubs for the required protocol"
 
@@ -2272,6 +2317,25 @@
 
 !CodeGeneratorTool methodsFor:'code templates'!
 
+codeFor_classInitialize
+    generateComments ifFalse:[
+        ^
+'initialize
+%(INIT_CLASSINSTVARS)
+%(INIT_CLASSVARS)
+'.
+    ].
+
+    ^
+'initialize
+    "Invoked at system start or when the class is dynamically loaded."
+
+    "/ please change as required (and remove this comment)
+%(INIT_CLASSINSTVARS)
+%(INIT_CLASSVARS)
+'.
+!
+
 codeFor_closeAccept
     generateComments ifFalse:[
         ^
@@ -2773,9 +2837,9 @@
 !CodeGeneratorTool class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libtool/CodeGeneratorTool.st,v 1.63 2009-10-07 12:11:11 fm Exp $'
+    ^ '$Header: /cvs/stx/stx/libtool/CodeGeneratorTool.st,v 1.64 2009-10-14 13:32:30 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libtool/CodeGeneratorTool.st,v 1.63 2009-10-07 12:11:11 fm Exp $'
+    ^ '$Header: /cvs/stx/stx/libtool/CodeGeneratorTool.st,v 1.64 2009-10-14 13:32:30 cg Exp $'
 ! !