use of org.androidannotations.annotations.sharedpreferences.SharedPref in project androidannotations by androidannotations.
the class SharedPrefHandler method validate.
@Override
public void validate(Element element, ElementValidation validation) {
super.validate(element, validation);
TypeElement typeElement = (TypeElement) element;
validatorHelper.isInterface(typeElement, validation);
List<? extends Element> inheritedMembers = getProcessingEnvironment().getElementUtils().getAllMembers(typeElement);
for (Element memberElement : inheritedMembers) {
if (!memberElement.getEnclosingElement().asType().toString().equals("java.lang.Object")) {
coreValidatorHelper.isPrefMethod(memberElement, validation);
DefaultPrefInfo<?> info;
IdValidatorHelper defaultAnnotationValidatorHelper = null;
if (validation.isValid()) {
info = DEFAULT_PREF_INFOS.get(((ExecutableElement) memberElement).getReturnType().toString());
coreValidatorHelper.hasCorrectDefaultAnnotation((ExecutableElement) memberElement, validation);
if (validation.isValid() && memberElement.getAnnotation(DefaultRes.class) != null) {
defaultAnnotationValidatorHelper = new IdValidatorHelper(new IdAnnotationHelper(getEnvironment(), DefaultRes.class.getName()));
defaultAnnotationValidatorHelper.resIdsExist(memberElement, info.resType, FallbackStrategy.USE_ELEMENT_NAME, validation);
} else if (validation.isValid() && memberElement.getAnnotation(info.annotationClass) != null) {
defaultAnnotationValidatorHelper = new IdValidatorHelper(new IdAnnotationHelper(getEnvironment(), info.annotationClass.getName()));
}
if (validation.isValid() && defaultAnnotationValidatorHelper != null) {
defaultAnnotationValidatorHelper.annotationParameterIsOptionalValidResId(memberElement, IRClass.Res.STRING, "keyRes", validation);
}
}
}
}
SharedPref sharedPrefAnnotation = element.getAnnotation(SharedPref.class);
SharedPref.Scope scope = sharedPrefAnnotation.value();
String name = sharedPrefAnnotation.name();
boolean hasCustomName = !name.trim().isEmpty();
EnumSet<SharedPref.Scope> allowedScopes = EnumSet.of(SharedPref.Scope.ACTIVITY, SharedPref.Scope.UNIQUE);
if (hasCustomName && !allowedScopes.contains(scope)) {
validation.addError("SharedPref#name() is only supported for Scope.ACTIVITY and Scope.UNIQUE.");
}
}
use of org.androidannotations.annotations.sharedpreferences.SharedPref in project androidannotations by androidannotations.
the class SharedPrefHandler method generateConstructor.
private void generateConstructor(Element element, SharedPrefHolder holder) {
SharedPref sharedPrefAnnotation = element.getAnnotation(SharedPref.class);
SharedPref.Scope scope = sharedPrefAnnotation.value();
String name = sharedPrefAnnotation.name();
int mode = sharedPrefAnnotation.mode();
if (name.trim().isEmpty()) {
name = element.getSimpleName().toString();
}
JBlock constructorSuperBlock = holder.getConstructorSuperBlock();
JVar contextParam = holder.getConstructorContextParam();
switch(scope) {
case ACTIVITY_DEFAULT:
{
JMethod getLocalClassName = getLocalClassName(holder);
//
constructorSuperBlock.invoke("super").arg(//
contextParam.invoke("getSharedPreferences").arg(//
invoke(getLocalClassName).arg(contextParam)).arg(lit(mode)));
break;
}
case ACTIVITY:
{
JMethod getLocalClassName = getLocalClassName(holder);
//
constructorSuperBlock.invoke("super").arg(//
contextParam.invoke("getSharedPreferences").arg(//
invoke(getLocalClassName).arg(contextParam).plus(//
lit("_" + name))).arg(lit(mode)));
break;
}
case UNIQUE:
{
//
constructorSuperBlock.invoke("super").arg(//
contextParam.invoke("getSharedPreferences").arg(//
lit(name)).arg(lit(mode)));
break;
}
case APPLICATION_DEFAULT:
{
AbstractJClass preferenceManagerClass = getJClass("android.preference.PreferenceManager");
//
constructorSuperBlock.invoke("super").arg(//
preferenceManagerClass.staticInvoke("getDefaultSharedPreferences").arg(contextParam));
break;
}
}
}
Aggregations