28
28
29
29
import java .lang .reflect .Field ;
30
30
import java .lang .reflect .Method ;
31
- import java .util .List ;
32
- import java .util .Map ;
31
+ import java .util .*;
33
32
import java .util .stream .Collectors ;
34
33
35
34
import com .github .therapi .runtimejavadoc .ClassJavadoc ;
@@ -56,6 +55,11 @@ public class SpringDocJavadocProvider implements JavadocProvider {
56
55
*/
57
56
private final CommentFormatter formatter = new CommentFormatter ();
58
57
58
+ /**
59
+ * The Class javadoc cache.
60
+ */
61
+ private final Map <Class <?>, ClassJavadoc > classJavadocCache = new HashMap <>();
62
+
59
63
60
64
/**
61
65
* Gets class description.
@@ -65,7 +69,7 @@ public class SpringDocJavadocProvider implements JavadocProvider {
65
69
*/
66
70
@ Override
67
71
public String getClassJavadoc (Class <?> cl ) {
68
- ClassJavadoc classJavadoc = RuntimeJavadoc . getJavadoc (cl );
72
+ ClassJavadoc classJavadoc = getJavadoc (cl );
69
73
return formatter .format (classJavadoc .getComment ());
70
74
}
71
75
@@ -77,7 +81,7 @@ public String getClassJavadoc(Class<?> cl) {
77
81
*/
78
82
@ Override
79
83
public Map <String , String > getRecordClassParamJavadoc (Class <?> cl ) {
80
- ClassJavadoc classJavadoc = RuntimeJavadoc . getJavadoc (cl );
84
+ ClassJavadoc classJavadoc = getJavadoc (cl );
81
85
return classJavadoc .getRecordComponents ().stream ()
82
86
.collect (Collectors .toMap (ParamJavadoc ::getName , recordClass -> formatter .format (recordClass .getComment ())));
83
87
}
@@ -90,7 +94,7 @@ public Map<String, String> getRecordClassParamJavadoc(Class<?> cl) {
90
94
*/
91
95
@ Override
92
96
public String getMethodJavadocDescription (Method method ) {
93
- MethodJavadoc methodJavadoc = RuntimeJavadoc . getJavadoc (method );
97
+ MethodJavadoc methodJavadoc = getJavadoc (method );
94
98
return formatter .format (methodJavadoc .getComment ());
95
99
}
96
100
@@ -102,7 +106,7 @@ public String getMethodJavadocDescription(Method method) {
102
106
*/
103
107
@ Override
104
108
public String getMethodJavadocReturn (Method method ) {
105
- MethodJavadoc methodJavadoc = RuntimeJavadoc . getJavadoc (method );
109
+ MethodJavadoc methodJavadoc = getJavadoc (method );
106
110
return formatter .format (methodJavadoc .getReturns ());
107
111
}
108
112
@@ -113,7 +117,7 @@ public String getMethodJavadocReturn(Method method) {
113
117
* @return the method throws (name-description map)
114
118
*/
115
119
public Map <String , String > getMethodJavadocThrows (Method method ) {
116
- return RuntimeJavadoc . getJavadoc (method )
120
+ return getJavadoc (method )
117
121
.getThrows ()
118
122
.stream ()
119
123
.collect (toMap (ThrowsJavadoc ::getName , javadoc -> formatter .format (javadoc .getComment ())));
@@ -128,7 +132,7 @@ public Map<String, String> getMethodJavadocThrows(Method method) {
128
132
*/
129
133
@ Override
130
134
public String getParamJavadoc (Method method , String name ) {
131
- MethodJavadoc methodJavadoc = RuntimeJavadoc . getJavadoc (method );
135
+ MethodJavadoc methodJavadoc = getJavadoc (method );
132
136
List <ParamJavadoc > paramsDoc = methodJavadoc .getParams ();
133
137
return paramsDoc .stream ().filter (paramJavadoc1 -> name .equals (paramJavadoc1 .getName ())).findAny ()
134
138
.map (paramJavadoc1 -> formatter .format (paramJavadoc1 .getComment ())).orElse (null );
@@ -142,7 +146,7 @@ public String getParamJavadoc(Method method, String name) {
142
146
*/
143
147
@ Override
144
148
public String getFieldJavadoc (Field field ) {
145
- FieldJavadoc fieldJavadoc = RuntimeJavadoc . getJavadoc (field );
149
+ FieldJavadoc fieldJavadoc = getJavadoc (field );
146
150
return formatter .format (fieldJavadoc .getComment ());
147
151
}
148
152
@@ -173,4 +177,38 @@ public String getFirstSentence(String text) {
173
177
}
174
178
return text ;
175
179
}
180
+
181
+ private ClassJavadoc getJavadoc (Class <?> cl ) {
182
+ ClassJavadoc classJavadoc = classJavadocCache .get (cl );
183
+ if (classJavadoc != null ) {
184
+ return classJavadoc ;
185
+ }
186
+ classJavadoc = RuntimeJavadoc .getJavadoc (cl );
187
+ classJavadocCache .put (cl , classJavadoc );
188
+ return classJavadoc ;
189
+ }
190
+
191
+ private MethodJavadoc getJavadoc (Method method ) {
192
+ ClassJavadoc classJavadoc = getJavadoc (method .getDeclaringClass ());
193
+ List <String > paramTypes = Arrays .stream (method .getParameterTypes ())
194
+ .map (Class ::getCanonicalName )
195
+ .toList ();
196
+ return classJavadoc .getMethods ()
197
+ .stream ()
198
+ .filter (it -> Objects .equals (method .getName (), it .getName ()) && Objects .equals (paramTypes , it .getParamTypes ()))
199
+ .findFirst ().orElseGet (() -> MethodJavadoc .createEmpty (method ));
200
+ }
201
+
202
+ private FieldJavadoc getJavadoc (Field field ) {
203
+ ClassJavadoc classJavadoc = getJavadoc (field .getDeclaringClass ());
204
+ return classJavadoc .getFields ()
205
+ .stream ()
206
+ .filter (it -> Objects .equals (field .getName (), it .getName ()))
207
+ .findFirst ().orElseGet (() -> FieldJavadoc .createEmpty (field .getName ()));
208
+ }
209
+
210
+ @ Override
211
+ public void clean () {
212
+ classJavadocCache .clear ();
213
+ }
176
214
}
0 commit comments