Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Map;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.TypeParameterElement;
Expand Down Expand Up @@ -101,6 +102,10 @@ public AndroidAnnotationsEnvironment getEnvironment() {
return environment;
}

protected ProcessingEnvironment getProcessingEnvironment() {
return environment.getProcessingEnvironment();
}

protected ProcessHolder.Classes getClasses() {
return environment.getClasses();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,13 @@ private void createEditMethod() {
editMethod.body()._return(editMethodEditorInvocation);
}

public void createFieldMethod(Class<?> prefFieldHelperClass, JExpression keyExpression, String fieldName, String fieldHelperMethodName, JExpression defaultValue) {
public void createFieldMethod(Class<?> prefFieldHelperClass, JExpression keyExpression, String fieldName, String fieldHelperMethodName, JExpression defaultValue,
String docComment, String defaultValueStr) {
JMethod fieldMethod = generatedClass.method(PUBLIC, prefFieldHelperClass, fieldName);
if (defaultValueStr != null) {
fieldMethod.javadoc().append("<p><b>Defaults to</b>: " + defaultValueStr + "</p>\n");
}
codeModelHelper.addTrimmedDocComment(fieldMethod, docComment);
fieldMethod.body()._return(JExpr.invoke(fieldHelperMethodName).arg(keyExpression).arg(defaultValue));
}

Expand All @@ -126,6 +131,8 @@ public void createEditorFieldMethods(ExecutableElement method, JExpression keyEx
JClass editorFieldClass = getJClass(editorFieldHolder.fieldClass);
String fieldName = method.getSimpleName().toString();
JMethod editorFieldMethod = editorClass.method(PUBLIC, editorFieldClass.narrow(editorClass), fieldName);
String docComment = getProcessingEnvironment().getElementUtils().getDocComment(method);
codeModelHelper.addTrimmedDocComment(editorFieldMethod, docComment);
editorFieldMethod.body()._return(JExpr.invoke(editorFieldHolder.fieldMethodName).arg(keyExpression));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,10 @@ private JExpression createFieldMethod(SharedPrefHolder holder, ExecutableElement
Annotation annotation = method.getAnnotation(annotationClass);
JExpression defaultValueExpr;

Object value = null;
if (annotation != null && method.getAnnotation(DefaultStringSet.class) == null) {
defaultValueExpr = codeModelHelper.litObject(annotationHelper.extractAnnotationParameter(method, annotationClass.getName(), "value"));
value = annotationHelper.extractAnnotationParameter(method, annotationClass.getName(), "value");
defaultValueExpr = codeModelHelper.litObject(value);
} else if (method.getAnnotation(DefaultRes.class) != null) {
defaultValueExpr = extractResValue(holder, method, resType);
annotationClass = DefaultRes.class;
Expand Down Expand Up @@ -276,7 +278,12 @@ private JExpression createFieldMethod(SharedPrefHolder holder, ExecutableElement
keyExpression = holder.getEditorContextField().invoke("getString").arg(idRef);
}

holder.createFieldMethod(prefFieldClass, keyExpression, fieldName, fieldHelperMethodName, defaultValueExpr);
String docComment = getProcessingEnvironment().getElementUtils().getDocComment(method);
String defaultValueStr = value == null ? null : value.toString();
if (defaultValueStr == null) {
defaultValueStr = defaultValue == null ? null : defaultValue.toString();
}
holder.createFieldMethod(prefFieldClass, keyExpression, fieldName, fieldHelperMethodName, defaultValueExpr, docComment, defaultValueStr);
return keyExpression;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,34 @@ public void generateJavaDocForServiceAction() throws IOException {
assertGeneratedClassMatches(generatedFile, ".*\\* @param param this is a param");
assertGeneratedClassMatches(generatedFile, ".*\\* @return");
}

@Test
public void generateJavaDocForSharedPref() throws IOException {
CompileResult result = compileFiles(SharedPrefWithJavaDoc.class);
File generatedFile = toGeneratedFile(SharedPrefWithJavaDoc.class);

assertCompilationSuccessful(result);

// CHECKSTYLE:OFF
String[] fieldDoc = { //
" * <p><b>Defaults to</b>: 42</p>", //
" * The Age!", //
" * @return the age as int", //
" * @deprecated use {@link #ageLong()}", //

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add the method signatures as well

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

" * ", //
" */", //
" public IntPrefField age() {", //
};
String[] editorDoc = { //
" * The Age!", //
" * @return the age as int", //
" * @deprecated use {@link #ageLong()}", //
" * ", //
" */", //
" public IntPrefEditorField<SharedPrefWithJavaDoc_.SharedPrefWithJavaDocEditor_> age() {", //
};
// CHECKSTYLE:ON
assertGeneratedClassContains(generatedFile, fieldDoc);
assertGeneratedClassContains(generatedFile, editorDoc);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.androidannotations.generation;

import org.androidannotations.annotations.sharedpreferences.DefaultInt;
import org.androidannotations.annotations.sharedpreferences.DefaultLong;
import org.androidannotations.annotations.sharedpreferences.DefaultString;
import org.androidannotations.annotations.sharedpreferences.SharedPref;
import org.androidannotations.annotations.sharedpreferences.SharedPref.Scope;

@SharedPref(Scope.UNIQUE)
public interface SharedPrefWithJavaDoc {

@DefaultString("John")
String name();

/**
* The Age!
* @return the age as int
* @deprecated use {@link #ageLong()}
*/
@Deprecated
@DefaultInt(42)
int age();

@DefaultLong(42L)
long ageLong();
}