Skip to content

Commit e604e2e

Browse files
committed
allow to provide a default value for @DefaultStringSet
x
1 parent 4029d57 commit e604e2e

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

  • AndroidAnnotations/androidannotations-core

‎AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/annotations/sharedpreferences/DefaultStringSet.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@
5050
@Target(ElementType.METHOD)
5151
public @interface DefaultStringSet {
5252

53+
/**
54+
* The default value of the preference.
55+
*
56+
* @return the default value
57+
*/
58+
String[] value();
59+
5360
/**
5461
* The R.string.* field which refers to the key of the preference.
5562
*

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/SharedPrefHandler.java‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323
import java.lang.annotation.Annotation;
2424
import java.util.ArrayList;
25+
import java.util.Arrays;
2526
import java.util.HashMap;
27+
import java.util.HashSet;
2628
import java.util.List;
2729
import java.util.Map;
2830
import java.util.Set;
@@ -64,6 +66,7 @@
6466
import com.sun.codemodel.JExpr;
6567
import com.sun.codemodel.JExpression;
6668
import com.sun.codemodel.JFieldRef;
69+
import com.sun.codemodel.JInvocation;
6770
import com.sun.codemodel.JMethod;
6871
import com.sun.codemodel.JVar;
6972

@@ -247,14 +250,28 @@ private JExpression createFieldMethod(SharedPrefHolder holder, ExecutableElement
247250
JExpression defaultValueExpr;
248251

249252
Object value = null;
250-
if (annotation != null && method.getAnnotation(DefaultStringSet.class) == null) {
253+
if (annotation != null) {
251254
value = annotationHelper.extractAnnotationParameter(method, annotationClass.getName(), "value");
255+
}
256+
257+
if (annotation != null && method.getAnnotation(DefaultStringSet.class) == null) {
252258
defaultValueExpr = codeModelHelper.litObject(value);
253259
} else if (method.getAnnotation(DefaultRes.class) != null) {
254260
defaultValueExpr = extractResValue(holder, method, resType);
255261
annotationClass = DefaultRes.class;
256262
} else if (method.getAnnotation(DefaultStringSet.class) != null) {
257-
defaultValueExpr = newEmptyStringHashSet();
263+
if (value != null) {
264+
Set<String> arrayValues = new HashSet<>(Arrays.asList((String[]) value));
265+
value = arrayValues;
266+
267+
JInvocation arrayAsList = getClasses().ARRAYS.staticInvoke("asList");
268+
for (String arrayValue : arrayValues) {
269+
arrayAsList.arg(lit(arrayValue));
270+
}
271+
defaultValueExpr = JExpr._new(getClasses().HASH_SET.narrow(getClasses().STRING)).arg(arrayAsList);
272+
} else {
273+
defaultValueExpr = newEmptyStringHashSet();
274+
}
258275
annotationClass = DefaultStringSet.class;
259276
} else {
260277
defaultValueExpr = defaultValue != null ? codeModelHelper.litObject(defaultValue) : newEmptyStringHashSet();

0 commit comments

Comments
 (0)