lahodaj commented on code in PR #8827:
URL: https://github.com/apache/netbeans/pull/8827#discussion_r2368350106
##########
java/java.source.base/src/org/netbeans/api/java/source/GeneratorUtilities.java:
##########
@@ -1187,11 +1206,37 @@ private CompilationUnitTree
addImports(CompilationUnitTree cut, List<? extends I
typeCounts.put((TypeElement)el, cnt);
} else {
pkgCounts.put((PackageElement)el, cnt);
+ if (cnt >= -1 && modCounts != null) {
+ ModuleElement ml;
+ try {
+ ml = elements.getModuleOf(e);
+ } catch (IllegalArgumentException ex) {
Review Comment:
Using the `catch` here seems a bit sub-optimal here. The problematic cases
originate in `ImmutableTreeTranslator`, and eventually in `ElementOverlay`. I
think I would suggest to use `ElementOverlay.moduleOf` in the problematic
cases. Like:
```
diff --git
a/java/java.source.base/src/org/netbeans/api/java/source/GeneratorUtilities.java
b/java/java.source.base/src/org/netbeans/api/java/source/GeneratorUtilities.java
index 59aa8067aa..6bb6462698 100644
---
a/java/java.source.base/src/org/netbeans/api/java/source/GeneratorUtilities.java
+++
b/java/java.source.base/src/org/netbeans/api/java/source/GeneratorUtilities.java
@@ -88,6 +88,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.function.Function;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
@@ -1087,10 +1088,10 @@ public final class GeneratorUtilities {
* @since 0.86
*/
public CompilationUnitTree addImports(CompilationUnitTree cut, Set<?
extends Element> toImport) {
- return addImports(cut, cut.getImports(), toImport);
+ return addImports(cut, cut.getImports(), toImport,
copy.getElements()::getModuleOf);
}
- private CompilationUnitTree addImports(CompilationUnitTree cut, List<?
extends ImportTree> cutImports, Set<? extends Element> toImport) {
+ private CompilationUnitTree addImports(CompilationUnitTree cut, List<?
extends ImportTree> cutImports, Set<? extends Element> toImport,
Function<Element, ModuleElement> moduleOf) {
assert cut != null && toImport != null && !toImport.isEmpty();
ArrayList<Element> elementsToImport = new
ArrayList<>(toImport.size());
@@ -1207,12 +1208,7 @@ public final class GeneratorUtilities {
} else {
pkgCounts.put((PackageElement)el, cnt);
if (cnt >= -1 && modCounts != null) {
- ModuleElement ml;
- try {
- ml = elements.getModuleOf(e);
- } catch (IllegalArgumentException ex) {
- ml = null;
- }
+ ModuleElement ml = moduleOf.apply(e);
if (ml != null) {
modCounts.merge(ml, 1, Integer::sum);
}
@@ -1443,12 +1439,7 @@ public final class GeneratorUtilities {
} else {
int modCount = 0;
if (modCounts != null) {
- ModuleElement m;
- try {
- m =
elements.getModuleOf(currentToImportElement);
- } catch (IllegalArgumentException ex) {
- m = null;
- }
+ ModuleElement m =
moduleOf.apply(currentToImportElement);
Integer mc = m == null ? null : modCounts.get(m);
if (mc != null && mc < 0) {
modCount = mc;
@@ -2407,8 +2398,8 @@ public final class GeneratorUtilities {
static {
GeneratorUtilitiesAccessor.setInstance(new
GeneratorUtilitiesAccessor() {
@Override
- public CompilationUnitTree addImports(GeneratorUtilities gu,
CompilationUnitTree cut, List<? extends ImportTree> cutImports, Set<? extends
Element> toImport) {
- return gu.addImports(cut, cutImports, toImport);
+ public CompilationUnitTree addImports(GeneratorUtilities gu,
CompilationUnitTree cut, List<? extends ImportTree> cutImports, Set<? extends
Element> toImport, Function<Element, ModuleElement> moduleOf) {
+ return gu.addImports(cut, cutImports, toImport, moduleOf);
}
});
}
diff --git
a/java/java.source.base/src/org/netbeans/modules/java/source/GeneratorUtilitiesAccessor.java
b/java/java.source.base/src/org/netbeans/modules/java/source/GeneratorUtilitiesAccessor.java
index 3e96b8d5fd..75041ca8a8 100644
---
a/java/java.source.base/src/org/netbeans/modules/java/source/GeneratorUtilitiesAccessor.java
+++
b/java/java.source.base/src/org/netbeans/modules/java/source/GeneratorUtilitiesAccessor.java
@@ -22,7 +22,9 @@ import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ImportTree;
import java.util.List;
import java.util.Set;
+import java.util.function.Function;
import javax.lang.model.element.Element;
+import javax.lang.model.element.ModuleElement;
import org.netbeans.api.java.source.GeneratorUtilities;
public abstract class GeneratorUtilitiesAccessor {
@@ -60,6 +62,6 @@ public abstract class GeneratorUtilitiesAccessor {
protected GeneratorUtilitiesAccessor() {
}
- public abstract CompilationUnitTree addImports(GeneratorUtilities gu,
CompilationUnitTree cut, List<? extends ImportTree> cutImports, Set<? extends
Element> toImport);
+ public abstract CompilationUnitTree addImports(GeneratorUtilities gu,
CompilationUnitTree cut, List<? extends ImportTree> cutImports, Set<? extends
Element> toImport, Function<Element, ModuleElement> moduleOf);
}
diff --git
a/java/java.source.base/src/org/netbeans/modules/java/source/save/ElementOverlay.java
b/java/java.source.base/src/org/netbeans/modules/java/source/save/ElementOverlay.java
index 3c6144151f..7692df5a45 100644
---
a/java/java.source.base/src/org/netbeans/modules/java/source/save/ElementOverlay.java
+++
b/java/java.source.base/src/org/netbeans/modules/java/source/save/ElementOverlay.java
@@ -432,7 +432,7 @@ public class ElementOverlay {
return (PackageElement) resolve(ast, elements, "", modle);
}
- private ModuleElement moduleOf(Elements elements, Element el) {
+ public ModuleElement moduleOf(Elements elements, Element el) {
if (el instanceof TypeElementWrapper)
return moduleOf(elements, ((TypeElementWrapper) el).delegateTo);
if (el instanceof FakeTypeElement)
diff --git
a/java/java.source.base/src/org/netbeans/modules/java/source/transform/ImmutableTreeTranslator.java
b/java/java.source.base/src/org/netbeans/modules/java/source/transform/ImmutableTreeTranslator.java
index 463f4a5b91..6c074adb42 100644
---
a/java/java.source.base/src/org/netbeans/modules/java/source/transform/ImmutableTreeTranslator.java
+++
b/java/java.source.base/src/org/netbeans/modules/java/source/transform/ImmutableTreeTranslator.java
@@ -651,7 +651,7 @@ public class ImmutableTreeTranslator implements
TreeVisitor<Tree,Object> {
Set<? extends Element> newImports = importAnalysis.getImports();
if (copy != null && newImports != null && !newImports.isEmpty()) {
imps = GeneratorUtilitiesAccessor.getInstance()
-
.addImports(GeneratorUtilities.get(copy), tree, imps, newImports)
+
.addImports(GeneratorUtilities.get(copy), tree, imps, newImports, el ->
overlay.moduleOf(elements, el))
.getImports();
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists