20 ! ! |
20 ! ! |
21 |
21 |
22 !CodeGeneratorTool class methodsFor:'code generation'! |
22 !CodeGeneratorTool class methodsFor:'code generation'! |
23 |
23 |
24 createAccessMethodsFor:aCollectionOfVarNames in:aClass withChange:withChange asValueHolder:asValueHolder readersOnly:readersOnly writersOnly:writersOnly |
24 createAccessMethodsFor:aCollectionOfVarNames in:aClass withChange:withChange asValueHolder:asValueHolder readersOnly:readersOnly writersOnly:writersOnly |
|
25 ^ self |
|
26 createAccessMethodsFor:aCollectionOfVarNames |
|
27 in:aClass |
|
28 withChange:withChange |
|
29 asValueHolder:asValueHolder |
|
30 readersOnly:readersOnly |
|
31 writersOnly:writersOnly |
|
32 lazyInitialization:false |
|
33 ! |
|
34 |
|
35 createAccessMethodsFor:aCollectionOfVarNames in:aClass withChange:withChange asValueHolder:asValueHolder readersOnly:readersOnly writersOnly:writersOnly lazyInitialization:lazyInitialization |
25 "workhorse for creating access methods for instvars." |
36 "workhorse for creating access methods for instvars." |
26 |
37 |
27 |classesClassVars| |
38 |classesClassVars| |
28 |
39 |
29 classesClassVars := aClass theNonMetaclass allClassVarNames. |
40 classesClassVars := aClass theNonMetaclass allClassVarNames. |
30 |
41 |
31 aCollectionOfVarNames do:[:name | |
42 aCollectionOfVarNames do:[:name | |
32 |source varType methodName| |
43 |source varType methodName defaultMethodName| |
33 |
44 |
34 varType := (classesClassVars includes:name) |
45 varType := (classesClassVars includes:name) |
35 ifTrue:['static'] |
46 ifTrue:['static'] |
36 ifFalse:[ |
47 ifFalse:[ |
37 (aClass isMeta ifTrue:['classInstVar'] ifFalse:['instance'])]. |
48 (aClass isMeta ifTrue:['classInstVar'] ifFalse:['instance'])]. |
39 methodName := name. |
50 methodName := name. |
40 name first isUppercase ifTrue:[ |
51 name first isUppercase ifTrue:[ |
41 methodName := methodName asLowercaseFirst. |
52 methodName := methodName asLowercaseFirst. |
42 ]. |
53 ]. |
43 |
54 |
|
55 "/ the GETTER |
44 writersOnly ifFalse:[ |
56 writersOnly ifFalse:[ |
|
57 lazyInitialization ifTrue:[ |
|
58 defaultMethodName := 'default' , name asUppercaseFirst. |
|
59 ]. |
|
60 |
45 "check, if method is not already present" |
61 "check, if method is not already present" |
46 (aClass includesSelector:(methodName asSymbol)) ifFalse:[ |
62 (aClass includesSelector:(methodName asSymbol)) ifFalse:[ |
47 asValueHolder ifTrue:[ |
63 asValueHolder ifTrue:[ |
48 source := methodName |
64 source := methodName |
49 , '\ "return/create the ''%2'' value holder (automatically generated)"\\' |
65 , '\ "return/create the ''%2'' value holder (automatically generated)"\\' |
50 , ' %2 isNil ifTrue:[\' |
66 , ' %2 isNil ifTrue:[\'. |
51 , ' %2 := ValueHolder new.\'. |
67 lazyInitialization ifTrue:[ |
|
68 source := source |
|
69 , ' %2 := self class %3 asValue.\'. |
|
70 ] ifFalse:[ |
|
71 source := source |
|
72 , ' %2 := ValueHolder new.\'. |
|
73 ]. |
|
74 |
52 withChange ifTrue:[ |
75 withChange ifTrue:[ |
53 source := source |
76 source := source |
54 , ' %2 addDependent:self.\'. |
77 , ' %2 addDependent:self.\'. |
55 ]. |
78 ]. |
56 source := source |
79 source := source |
57 , ' ].\' |
80 , ' ].\' |
58 , ' ^ %2'. |
81 , ' ^ %2'. |
59 ] ifFalse:[ |
82 ] ifFalse:[ |
60 source := methodName |
83 source := methodName |
61 , '\ "return the value of the %1 variable ''%2'' (automatically generated)"\\' |
84 , '\ "return the value of the %1 variable ''%2'' (automatically generated)"\\'. |
62 , ' ^ %2'. |
85 lazyInitialization ifTrue:[ |
|
86 source := source |
|
87 , ' %2 isNil ifTrue:[\' |
|
88 , ' %2 := self class %3.\' |
|
89 , ' ].\' |
|
90 , ' ^ %2'. |
|
91 ] ifFalse:[ |
|
92 source := source |
|
93 , ' ^ %2'. |
|
94 ]. |
63 ]. |
95 ]. |
64 source := (source bindWith:varType with:name) withCRs. |
96 source := (source bindWith:varType with:name with:defaultMethodName) withCRs. |
65 self compile:source forClass:aClass inCategory:(asValueHolder ifTrue:['aspects'] ifFalse:['accessing']). |
97 self compile:source forClass:aClass inCategory:(asValueHolder ifTrue:['aspects'] ifFalse:['accessing']). |
66 ] ifTrue:[ |
98 ] ifTrue:[ |
67 Transcript showCR:'method ''', methodName , ''' already present' |
99 Transcript showCR:'method ''', methodName , ''' already present' |
68 ]. |
100 ]. |
69 ]. |
101 |
70 |
102 "/ default for lazy on class side |
|
103 lazyInitialization ifTrue:[ |
|
104 (aClass class includesSelector:(defaultMethodName asSymbol)) ifFalse:[ |
|
105 source := defaultMethodName |
|
106 , '\ "default value for the ''%2'' instance variable (automatically generated)"\\' |
|
107 , ' self halt:''unfinished code''.\' |
|
108 , ' ^ nil.'. |
|
109 source := (source bindWith:varType with:name) withCRs. |
|
110 self compile:source forClass:aClass class inCategory:'defaults'. |
|
111 ]. |
|
112 ]. |
|
113 ]. |
|
114 |
|
115 "/ the SETTER |
71 readersOnly ifFalse:[ |
116 readersOnly ifFalse:[ |
72 (aClass includesSelector:((methodName , ':') asSymbol)) ifFalse:[ |
117 (aClass includesSelector:((methodName , ':') asSymbol)) ifFalse:[ |
73 asValueHolder ifTrue:[ |
118 asValueHolder ifTrue:[ |
74 source := methodName |
119 source := methodName |
75 , ':something\ "set the ''%2'' value holder'. |
120 , ':something\ "set the ''%2'' value holder'. |