22
22
23
23
import java .lang .reflect .Field ;
24
24
import java .lang .reflect .Method ;
25
+ import java .util .ArrayList ;
25
26
import java .util .List ;
27
+ import java .util .stream .Collectors ;
26
28
29
+ import com .github .therapi .runtimejavadoc .ClassJavadoc ;
27
30
import com .github .therapi .runtimejavadoc .CommentFormatter ;
28
31
import com .github .therapi .runtimejavadoc .FieldJavadoc ;
29
32
import com .github .therapi .runtimejavadoc .MethodJavadoc ;
@@ -50,7 +53,7 @@ public class SpringDocJavadocProvider implements JavadocProvider {
50
53
*/
51
54
@ Override
52
55
public String getMethodJavadocDescription (Method method ) {
53
- MethodJavadoc methodJavadoc = RuntimeJavadoc . getJavadoc (method );
56
+ MethodJavadoc methodJavadoc = findMethodJavadoc (method );
54
57
return formatter .format (methodJavadoc .getComment ());
55
58
}
56
59
@@ -62,7 +65,7 @@ public String getMethodJavadocDescription(Method method) {
62
65
*/
63
66
@ Override
64
67
public String getMethodJavadocReturn (Method method ) {
65
- MethodJavadoc methodJavadoc = RuntimeJavadoc . getJavadoc (method );
68
+ MethodJavadoc methodJavadoc = findMethodJavadoc (method );
66
69
return formatter .format (methodJavadoc .getReturns ());
67
70
}
68
71
@@ -75,7 +78,7 @@ public String getMethodJavadocReturn(Method method) {
75
78
*/
76
79
@ Override
77
80
public String getParamJavadoc (Method method , String name ) {
78
- MethodJavadoc methodJavadoc = RuntimeJavadoc . getJavadoc (method );
81
+ MethodJavadoc methodJavadoc = findMethodJavadoc (method );
79
82
List <ParamJavadoc > paramsDoc = methodJavadoc .getParams ();
80
83
return paramsDoc .stream ().filter (paramJavadoc1 -> name .equals (paramJavadoc1 .getName ())).findAny ()
81
84
.map (paramJavadoc1 -> formatter .format (paramJavadoc1 .getComment ())).orElse (null );
@@ -93,4 +96,61 @@ public String getFieldJavadoc(Field field) {
93
96
return formatter .format (fieldJavadoc .getComment ());
94
97
}
95
98
99
+ /**
100
+ * Find method javadoc method javadoc.
101
+ *
102
+ * @param method the method
103
+ * @return the method javadoc
104
+ */
105
+ private MethodJavadoc findMethodJavadoc (Method method ) {
106
+ ClassJavadoc classJavadoc = RuntimeJavadoc .getJavadoc (method .getDeclaringClass ());
107
+ List <MethodJavadoc > methodDocs = classJavadoc .getMethods ();
108
+ // filter by method name
109
+ List <MethodJavadoc > methodDocByMethodName = methodDocs .stream ().filter (methodJavadoc -> methodJavadoc .getName ().equals (method .getName ())).collect (Collectors .toList ());
110
+ if (methodDocByMethodName .size () == 1 )
111
+ return methodDocByMethodName .get (0 );
112
+ // filter by parameters
113
+ if (methodDocByMethodName .size () > 1 ) {
114
+ List <MethodJavadoc > methodDocByParamType = methodDocByMethodName .stream ().filter (methodJavadoc -> paramsMatch (method .getParameterTypes (), methodJavadoc .getParamTypes ())).collect (Collectors .toList ());
115
+ if (methodDocByParamType .size () == 1 )
116
+ return methodDocByParamType .get (0 );
117
+ }
118
+ return MethodJavadoc .createEmpty (method );
119
+ }
120
+
121
+ /**
122
+ * Params match boolean.
123
+ *
124
+ * @param paramTypesClass the param types class
125
+ * @param paramTypes the param types
126
+ * @return the boolean
127
+ */
128
+ private boolean paramsMatch (Class <?>[] paramTypesClass , List <String > paramTypes ) {
129
+ List <String > paramTypesJavadoc = new ArrayList <>();
130
+ for (int i = 0 ; i < paramTypes .size (); i ++) {
131
+ if (paramTypes .get (i ).contains ("::" )) {
132
+ String [] paramTypeArray = paramTypes .get (i ).split ("::" );
133
+ String paramType = paramTypeArray [paramTypeArray .length - 1 ].trim ().replace (")" , "" );
134
+ paramTypesJavadoc .add (paramType );
135
+ }
136
+ else
137
+ paramTypesJavadoc .add (paramTypes .get (i ));
138
+ }
139
+ return getCanonicalNames (paramTypesClass ).equals (paramTypesJavadoc );
140
+ }
141
+
142
+ /**
143
+ * Gets canonical names.
144
+ *
145
+ * @param paramTypes the param types
146
+ * @return the canonical names
147
+ */
148
+ private List <String > getCanonicalNames (Class <?>[] paramTypes ) {
149
+ List <String > methodParamsTypes = new ArrayList <>();
150
+ for (Class <?> aClass : paramTypes ) {
151
+ methodParamsTypes .add (aClass .getCanonicalName ());
152
+ }
153
+ return methodParamsTypes ;
154
+ }
155
+
96
156
}
0 commit comments