@@ -118,7 +118,7 @@ public static JavadocHelper create(JavacTask mainTask, Collection<? extends Path
118118 StandardJavaFileManager fm = compiler .getStandardFileManager (null , null , null );
119119 try {
120120 fm .setLocationFromPaths (StandardLocation .SOURCE_PATH , sourceLocations );
121- return new OnDemandJavadocHelper (mainTask , fm );
121+ return new OnDemandJavadocHelper (mainTask , fm , sourceLocations );
122122 } catch (IOException ex ) {
123123 try {
124124 fm .close ();
@@ -135,6 +135,21 @@ public Element getSourceElement(Element forElement) throws IOException {
135135 }
136136 @ Override
137137 public void close () throws IOException {}
138+
139+ @ Override
140+ public String getResolvedDocComment (StoredElement forElement ) throws IOException {
141+ return null ;
142+ }
143+
144+ @ Override
145+ public StoredElement getHandle (Element forElement ) {
146+ return null ;
147+ }
148+
149+ @ Override
150+ public Collection <? extends Path > getSourceLocations () {
151+ return List .of ();
152+ }
138153 };
139154 }
140155 }
@@ -147,6 +162,7 @@ public void close() throws IOException {}
147162 * @throws IOException if something goes wrong in the search
148163 */
149164 public abstract String getResolvedDocComment (Element forElement ) throws IOException ;
165+ public abstract String getResolvedDocComment (StoredElement forElement ) throws IOException ;
150166
151167 /**Returns an element representing the same given program element, but the returned element will
152168 * be resolved from source, if it can be found. Returns the original element if the source for
@@ -158,23 +174,30 @@ public void close() throws IOException {}
158174 */
159175 public abstract Element getSourceElement (Element forElement ) throws IOException ;
160176
177+ public abstract StoredElement getHandle (Element forElement );
178+ public abstract Collection <? extends Path > getSourceLocations ();
179+
161180 /**Closes the helper.
162181 *
163182 * @throws IOException if something foes wrong during the close
164183 */
165184 @ Override
166185 public abstract void close () throws IOException ;
167186
187+ public record StoredElement (String module , String binaryName , String handle ) {}
188+
168189 private static final class OnDemandJavadocHelper extends JavadocHelper {
169190 private final JavacTask mainTask ;
170191 private final JavaFileManager baseFileManager ;
171192 private final StandardJavaFileManager fm ;
172193 private final Map <String , Pair <JavacTask , TreePath >> signature2Source = new HashMap <>();
194+ private final Collection <? extends Path > sourceLocations ;
173195
174- private OnDemandJavadocHelper (JavacTask mainTask , StandardJavaFileManager fm ) {
196+ private OnDemandJavadocHelper (JavacTask mainTask , StandardJavaFileManager fm , Collection <? extends Path > sourceLocations ) {
175197 this .mainTask = mainTask ;
176198 this .baseFileManager = ((JavacTaskImpl ) mainTask ).getContext ().get (JavaFileManager .class );
177199 this .fm = fm ;
200+ this .sourceLocations = sourceLocations ;
178201 }
179202
180203 @ Override
@@ -187,6 +210,16 @@ public String getResolvedDocComment(Element forElement) throws IOException {
187210 return getResolvedDocComment (sourceElement .fst , sourceElement .snd );
188211 }
189212
213+ @ Override
214+ public String getResolvedDocComment (StoredElement forElement ) throws IOException {
215+ Pair <JavacTask , TreePath > sourceElement = getSourceElement (forElement );
216+
217+ if (sourceElement == null )
218+ return null ;
219+
220+ return getResolvedDocComment (sourceElement .fst , sourceElement .snd );
221+ }
222+
190223 @ Override
191224 public Element getSourceElement (Element forElement ) throws IOException {
192225 Pair <JavacTask , TreePath > sourceElement = getSourceElement (mainTask , forElement );
@@ -202,7 +235,30 @@ public Element getSourceElement(Element forElement) throws IOException {
202235 return result ;
203236 }
204237
205- private String getResolvedDocComment (JavacTask task , TreePath el ) throws IOException {
238+ @ Override
239+ public StoredElement getHandle (Element forElement ) {
240+ TypeElement type = topLevelType (forElement );
241+
242+ if (type == null )
243+ return null ;
244+
245+ Elements elements = mainTask .getElements ();
246+ ModuleElement module = elements .getModuleOf (type );
247+ String moduleName = module == null || module .isUnnamed ()
248+ ? null
249+ : module .getQualifiedName ().toString ();
250+ String binaryName = elements .getBinaryName (type ).toString ();
251+ String handle = elementSignature (forElement );
252+
253+ return new StoredElement (moduleName , binaryName , handle );
254+ }
255+
256+ @ Override
257+ public Collection <? extends Path > getSourceLocations () {
258+ return sourceLocations ;
259+ }
260+
261+ private String getResolvedDocComment (JavacTask task , TreePath el ) throws IOException {
206262 DocTrees trees = DocTrees .instance (task );
207263 Element element = trees .getElement (el );
208264 String docComment = trees .getDocComment (el );
@@ -634,7 +690,7 @@ private Stream<ExecutableElement> superMethodsForInheritDoc(JavacTask task,
634690 .filter (supMethod -> task .getElements ().overrides (method , supMethod , type ));
635691 }
636692
637- /* Find types from which methods in type may inherit javadoc, in the proper order.*/
693+ /* Find types from which methods in binaryName may inherit javadoc, in the proper order.*/
638694 private Stream <Element > superTypeForInheritDoc (JavacTask task , Element type ) {
639695 TypeElement clazz = (TypeElement ) type ;
640696 Stream <Element > result = interfaces (clazz );
@@ -701,6 +757,35 @@ private String getThrownException(JavacTask task, TreePath rootOn, DocCommentTre
701757 return exc != null ? exc .toString () : null ;
702758 }
703759
760+ private Pair <JavacTask , TreePath > getSourceElement (StoredElement el ) throws IOException {
761+ if (el == null ) {
762+ return null ;
763+ }
764+
765+ String handle = el .handle ();
766+ Pair <JavacTask , TreePath > cached = signature2Source .get (handle );
767+
768+ if (cached != null ) {
769+ return cached .fst != null ? cached : null ;
770+ }
771+
772+ Pair <JavacTask , CompilationUnitTree > source = findSource (el .module (), el .binaryName ());
773+
774+ if (source == null )
775+ return null ;
776+
777+ fillElementCache (source .fst , source .snd );
778+
779+ cached = signature2Source .get (handle );
780+
781+ if (cached != null ) {
782+ return cached ;
783+ } else {
784+ signature2Source .put (handle , Pair .of (null , null ));
785+ return null ;
786+ }
787+ }
788+
704789 private Pair <JavacTask , TreePath > getSourceElement (JavacTask origin , Element el ) throws IOException {
705790 String handle = elementSignature (el );
706791 Pair <JavacTask , TreePath > cached = signature2Source .get (handle );
0 commit comments