1919import static com .sun .codemodel .JExpr .lit ;
2020import static com .sun .codemodel .JMod .PRIVATE ;
2121import static com .sun .codemodel .JMod .STATIC ;
22+ import static org .androidannotations .helper .CanonicalNameConstants .CONTEXT ;
2223
2324import java .util .ArrayList ;
2425import java .util .HashMap ;
2526import java .util .List ;
2627import java .util .Map ;
2728
29+ import javax .annotation .processing .ProcessingEnvironment ;
2830import javax .lang .model .element .Element ;
2931import javax .lang .model .element .ExecutableElement ;
3032import javax .lang .model .element .TypeElement ;
3436import org .androidannotations .annotations .sharedpreferences .DefaultFloat ;
3537import org .androidannotations .annotations .sharedpreferences .DefaultInt ;
3638import org .androidannotations .annotations .sharedpreferences .DefaultLong ;
39+ import org .androidannotations .annotations .sharedpreferences .DefaultRes ;
3740import org .androidannotations .annotations .sharedpreferences .DefaultString ;
3841import org .androidannotations .annotations .sharedpreferences .SharedPref ;
3942import org .androidannotations .annotations .sharedpreferences .SharedPref .Scope ;
5356import org .androidannotations .api .sharedpreferences .StringPrefEditorField ;
5457import org .androidannotations .api .sharedpreferences .StringPrefField ;
5558import org .androidannotations .helper .CanonicalNameConstants ;
59+ import org .androidannotations .helper .IdAnnotationHelper ;
5660import org .androidannotations .helper .ModelConstants ;
61+ import org .androidannotations .rclass .IRClass ;
62+ import org .androidannotations .rclass .IRClass .Res ;
5763
5864import com .sun .codemodel .ClassType ;
5965import com .sun .codemodel .JBlock ;
6268import com .sun .codemodel .JDefinedClass ;
6369import com .sun .codemodel .JExpr ;
6470import com .sun .codemodel .JExpression ;
71+ import com .sun .codemodel .JFieldRef ;
72+ import com .sun .codemodel .JFieldVar ;
6573import com .sun .codemodel .JMethod ;
6674import com .sun .codemodel .JMod ;
6775import com .sun .codemodel .JVar ;
@@ -89,14 +97,19 @@ public EditorFieldHolder(Class<?> fieldClass, String fieldMethodName) {
8997 }
9098 };
9199
100+ private final IdAnnotationHelper annotationHelper ;
101+
102+ public SharedPrefProcessor (ProcessingEnvironment processingEnv , IRClass rClass ) {
103+ annotationHelper = new IdAnnotationHelper (processingEnv , getTarget (), rClass );
104+ }
105+
92106 @ Override
93107 public String getTarget () {
94108 return SharedPref .class .getName ();
95109 }
96110
97111 @ Override
98112 public void process (Element element , JCodeModel codeModel , EBeansHolder eBeansHolder ) throws Exception {
99-
100113 generateApiClass (element , eBeansHolder );
101114
102115 TypeElement typeElement = (TypeElement ) element ;
@@ -105,7 +118,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
105118
106119 String helperQualifiedName = interfaceQualifiedName + ModelConstants .GENERATION_SUFFIX ;
107120 JDefinedClass helperClass = codeModel ._class (JMod .PUBLIC | JMod .FINAL , helperQualifiedName , ClassType .CLASS );
108- eBeansHolder .create (typeElement , SharedPref .class , helperClass );
121+ EBeanHolder eBeanHolder = eBeansHolder .create (typeElement , SharedPref .class , helperClass );
109122
110123 helperClass ._extends (SharedPreferencesHelper .class );
111124
@@ -145,10 +158,9 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
145158 Scope scope = sharedPrefAnnotation .value ();
146159 int mode = sharedPrefAnnotation .mode ();
147160 JMethod constructor = helperClass .constructor (JMod .PUBLIC );
161+ JVar contextParam = constructor .param (contextClass , "context" );
148162 switch (scope ) {
149163 case ACTIVITY_DEFAULT : {
150-
151- JVar contextParam = constructor .param (contextClass , "context" );
152164 JMethod getLocalClassName = getLocalClassName (eBeansHolder , helperClass , codeModel );
153165 constructor .body ().invoke ("super" ) //
154166 .arg (contextParam .invoke ("getSharedPreferences" ) //
@@ -157,7 +169,6 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
157169 break ;
158170 }
159171 case ACTIVITY : {
160- JVar contextParam = constructor .param (contextClass , "context" );
161172 JMethod getLocalClassName = getLocalClassName (eBeansHolder , helperClass , codeModel );
162173 constructor .body ().invoke ("super" ) //
163174 .arg (contextParam .invoke ("getSharedPreferences" ) //
@@ -167,7 +178,6 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
167178 break ;
168179 }
169180 case UNIQUE : {
170- JVar contextParam = constructor .param (contextClass , "context" );
171181 constructor .body () //
172182 .invoke ("super" ) //
173183 .arg (contextParam .invoke ("getSharedPreferences" ) //
@@ -177,7 +187,6 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
177187 }
178188 case APPLICATION_DEFAULT : {
179189 JClass preferenceManagerClass = eBeansHolder .refClass ("android.preference.PreferenceManager" );
180- JVar contextParam = constructor .param (contextClass , "context" );
181190 constructor .body () //
182191 .invoke ("super" ) //
183192 .arg (preferenceManagerClass .staticInvoke ("getDefaultSharedPreferences" ) //
@@ -186,6 +195,10 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
186195 }
187196 }
188197
198+ // Context field
199+ JFieldVar contextField = helperClass .field (JMod .PRIVATE , eBeansHolder .refClass (CONTEXT ), "context" );
200+ constructor .body ().assign (JExpr ._this ().ref (contextField ), contextParam );
201+
189202 // Helper edit method
190203 JMethod editMethod = helperClass .method (JMod .PUBLIC , editorClass , "edit" );
191204 editMethod .body ()._return (JExpr ._new (editorClass ).arg (JExpr .invoke ("getSharedPreferences" )));
@@ -199,6 +212,8 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
199212 DefaultBoolean defaultAnnotation = method .getAnnotation (DefaultBoolean .class );
200213 if (defaultAnnotation != null ) {
201214 defaultValue = JExpr .lit (defaultAnnotation .value ());
215+ } else if (method .getAnnotation (DefaultRes .class ) != null ) {
216+ defaultValue = extractResValue (eBeanHolder , method , contextField , Res .BOOL );
202217 } else {
203218 defaultValue = JExpr .lit (false );
204219 }
@@ -208,6 +223,8 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
208223 DefaultFloat defaultAnnotation = method .getAnnotation (DefaultFloat .class );
209224 if (defaultAnnotation != null ) {
210225 defaultValue = JExpr .lit (defaultAnnotation .value ());
226+ } else if (method .getAnnotation (DefaultRes .class ) != null ) {
227+ defaultValue = extractResValue (eBeanHolder , method , contextField , Res .INTEGER );
211228 } else {
212229 defaultValue = JExpr .lit (0f );
213230 }
@@ -217,6 +234,8 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
217234 DefaultInt defaultAnnotation = method .getAnnotation (DefaultInt .class );
218235 if (defaultAnnotation != null ) {
219236 defaultValue = JExpr .lit (defaultAnnotation .value ());
237+ } else if (method .getAnnotation (DefaultRes .class ) != null ) {
238+ defaultValue = extractResValue (eBeanHolder , method , contextField , Res .INTEGER );
220239 } else {
221240 defaultValue = JExpr .lit (0 );
222241 }
@@ -226,6 +245,8 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
226245 DefaultLong defaultAnnotation = method .getAnnotation (DefaultLong .class );
227246 if (defaultAnnotation != null ) {
228247 defaultValue = JExpr .lit (defaultAnnotation .value ());
248+ } else if (method .getAnnotation (DefaultRes .class ) != null ) {
249+ defaultValue = extractResValue (eBeanHolder , method , contextField , Res .INTEGER );
229250 } else {
230251 defaultValue = JExpr .lit (0l );
231252 }
@@ -235,13 +256,34 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
235256 DefaultString defaultAnnotation = method .getAnnotation (DefaultString .class );
236257 if (defaultAnnotation != null ) {
237258 defaultValue = JExpr .lit (defaultAnnotation .value ());
259+ } else if (method .getAnnotation (DefaultRes .class ) != null ) {
260+ defaultValue = extractResValue (eBeanHolder , method , contextField , Res .STRING );
238261 } else {
239262 defaultValue = JExpr .lit ("" );
240263 }
241264 addFieldHelperMethod (helperClass , fieldName , defaultValue , StringPrefField .class , "stringField" );
242265 }
243266 }
267+ }
244268
269+ private JExpression extractResValue (EBeanHolder eBeanHolder , Element method , JFieldVar contextField , Res res ) {
270+ JFieldRef idRef = annotationHelper .extractOneAnnotationFieldRef (eBeanHolder , method , DefaultRes .class .getCanonicalName (), res , true );
271+
272+ String resourceGetMethodName = null ;
273+ switch (res ) {
274+ case BOOL :
275+ resourceGetMethodName = "getBoolean" ;
276+ break ;
277+ case INTEGER :
278+ resourceGetMethodName = "getInteger" ;
279+ break ;
280+ case STRING :
281+ resourceGetMethodName = "getString" ;
282+ break ;
283+ default :
284+ break ;
285+ }
286+ return contextField .invoke ("getResources" ).invoke (resourceGetMethodName ).arg (idRef );
245287 }
246288
247289 private void addFieldHelperMethod (JDefinedClass helperClass , String fieldName , JExpression defaultValue , Class <?> prefFieldHelperClass , String fieldHelperMethodName ) {
0 commit comments