Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Commit 4029d57

Browse files
committed
Merge pull request #1553 from dodgex/1551_copy_sharedpref_javadoc
Copy @SharedPref javadoc
2 parents f4b732e + ef98641 commit 4029d57

5 files changed

Lines changed: 93 additions & 3 deletions

File tree

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/BaseGeneratedClassHolder.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.List;
2626
import java.util.Map;
2727

28+
import javax.annotation.processing.ProcessingEnvironment;
2829
import javax.lang.model.element.Element;
2930
import javax.lang.model.element.TypeElement;
3031
import javax.lang.model.element.TypeParameterElement;
@@ -101,6 +102,10 @@ public AndroidAnnotationsEnvironment getEnvironment() {
101102
return environment;
102103
}
103104

105+
protected ProcessingEnvironment getProcessingEnvironment() {
106+
return environment.getProcessingEnvironment();
107+
}
108+
104109
protected ProcessHolder.Classes getClasses() {
105110
return environment.getClasses();
106111
}

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/SharedPrefHolder.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,13 @@ private void createEditMethod() {
115115
editMethod.body()._return(editMethodEditorInvocation);
116116
}
117117

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

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

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,10 @@ private JExpression createFieldMethod(SharedPrefHolder holder, ExecutableElement
246246
Annotation annotation = method.getAnnotation(annotationClass);
247247
JExpression defaultValueExpr;
248248

249+
Object value = null;
249250
if (annotation != null && method.getAnnotation(DefaultStringSet.class) == null) {
250-
defaultValueExpr = codeModelHelper.litObject(annotationHelper.extractAnnotationParameter(method, annotationClass.getName(), "value"));
251+
value = annotationHelper.extractAnnotationParameter(method, annotationClass.getName(), "value");
252+
defaultValueExpr = codeModelHelper.litObject(value);
251253
} else if (method.getAnnotation(DefaultRes.class) != null) {
252254
defaultValueExpr = extractResValue(holder, method, resType);
253255
annotationClass = DefaultRes.class;
@@ -276,7 +278,12 @@ private JExpression createFieldMethod(SharedPrefHolder holder, ExecutableElement
276278
keyExpression = holder.getEditorContextField().invoke("getString").arg(idRef);
277279
}
278280

279-
holder.createFieldMethod(prefFieldClass, keyExpression, fieldName, fieldHelperMethodName, defaultValueExpr);
281+
String docComment = getProcessingEnvironment().getElementUtils().getDocComment(method);
282+
String defaultValueStr = value == null ? null : value.toString();
283+
if (defaultValueStr == null) {
284+
defaultValueStr = defaultValue == null ? null : defaultValue.toString();
285+
}
286+
holder.createFieldMethod(prefFieldClass, keyExpression, fieldName, fieldHelperMethodName, defaultValueExpr, docComment, defaultValueStr);
280287
return keyExpression;
281288
}
282289

‎AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/generation/GenerateJavaDocTest.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,34 @@ public void generateJavaDocForServiceAction() throws IOException {
6767
assertGeneratedClassMatches(generatedFile, ".*\\* @param param this is a param");
6868
assertGeneratedClassMatches(generatedFile, ".*\\* @return");
6969
}
70+
71+
@Test
72+
public void generateJavaDocForSharedPref() throws IOException {
73+
CompileResult result = compileFiles(SharedPrefWithJavaDoc.class);
74+
File generatedFile = toGeneratedFile(SharedPrefWithJavaDoc.class);
75+
76+
assertCompilationSuccessful(result);
77+
78+
// CHECKSTYLE:OFF
79+
String[] fieldDoc = { //
80+
" * <p><b>Defaults to</b>: 42</p>", //
81+
" * The Age!", //
82+
" * @return the age as int", //
83+
" * @deprecated use {@link #ageLong()}", //
84+
" * ", //
85+
" */", //
86+
" public IntPrefField age() {", //
87+
};
88+
String[] editorDoc = { //
89+
" * The Age!", //
90+
" * @return the age as int", //
91+
" * @deprecated use {@link #ageLong()}", //
92+
" * ", //
93+
" */", //
94+
" public IntPrefEditorField<SharedPrefWithJavaDoc_.SharedPrefWithJavaDocEditor_> age() {", //
95+
};
96+
// CHECKSTYLE:ON
97+
assertGeneratedClassContains(generatedFile, fieldDoc);
98+
assertGeneratedClassContains(generatedFile, editorDoc);
99+
}
70100
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.generation;
17+
18+
import org.androidannotations.annotations.sharedpreferences.DefaultInt;
19+
import org.androidannotations.annotations.sharedpreferences.DefaultLong;
20+
import org.androidannotations.annotations.sharedpreferences.DefaultString;
21+
import org.androidannotations.annotations.sharedpreferences.SharedPref;
22+
import org.androidannotations.annotations.sharedpreferences.SharedPref.Scope;
23+
24+
@SharedPref(Scope.UNIQUE)
25+
public interface SharedPrefWithJavaDoc {
26+
27+
@DefaultString("John")
28+
String name();
29+
30+
/**
31+
* The Age!
32+
* @return the age as int
33+
* @deprecated use {@link #ageLong()}
34+
*/
35+
@Deprecated
36+
@DefaultInt(42)
37+
int age();
38+
39+
@DefaultLong(42L)
40+
long ageLong();
41+
}

0 commit comments

Comments
 (0)