This is an automated email from the ASF dual-hosted git repository.

dbalek pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 9020a0af72 Issue #3886: Unused in package misbehavior - fix. (#4204)
9020a0af72 is described below

commit 9020a0af7231059788076a18fb8df9f157f612c7
Author: Dusan Balek <[email protected]>
AuthorDate: Fri Jun 24 18:49:51 2022 +0200

    Issue #3886: Unused in package misbehavior - fix. (#4204)
---
 java/java.editor.base/nbproject/project.xml        | 27 ++++++++++
 .../base/semantic/SemanticHighlighterBase.java     |  2 +-
 .../java/editor/base/semantic/UnusedDetector.java  | 63 +++++++++++++++-------
 .../editor/base/semantic/UnusedDetectorTest.java   |  2 +-
 .../netbeans/modules/java/hints/bugs/Unused.java   |  2 +-
 5 files changed, 73 insertions(+), 23 deletions(-)

diff --git a/java/java.editor.base/nbproject/project.xml 
b/java/java.editor.base/nbproject/project.xml
index 74fddcaf4b..d1ffc48275 100644
--- a/java/java.editor.base/nbproject/project.xml
+++ b/java/java.editor.base/nbproject/project.xml
@@ -34,6 +34,15 @@
                         <specification-version>1.25</specification-version>
                     </run-dependency>
                 </dependency>
+                <dependency>
+                    
<code-name-base>org.netbeans.api.java.classpath</code-name-base>
+                    <build-prerequisite/>
+                    <compile-dependency/>
+                    <run-dependency>
+                        <release-version>1</release-version>
+                        <specification-version>1.71</specification-version>
+                    </run-dependency>
+                </dependency>
                 <dependency>
                     <code-name-base>org.netbeans.libs.javacapi</code-name-base>
                     <build-prerequisite/>
@@ -69,6 +78,15 @@
                         <specification-version>1.34</specification-version>
                     </run-dependency>
                 </dependency>
+                <dependency>
+                    
<code-name-base>org.netbeans.modules.java.project</code-name-base>
+                    <build-prerequisite/>
+                    <compile-dependency/>
+                    <run-dependency>
+                        <release-version>1</release-version>
+                        <specification-version>1.89</specification-version>
+                    </run-dependency>
+                </dependency>
                 <dependency>
                     
<code-name-base>org.netbeans.modules.java.source.base</code-name-base>
                     <build-prerequisite/>
@@ -95,6 +113,15 @@
                         <specification-version>9.2</specification-version>
                     </run-dependency>
                 </dependency>
+                <dependency>
+                    
<code-name-base>org.netbeans.modules.projectapi</code-name-base>
+                    <build-prerequisite/>
+                    <compile-dependency/>
+                    <run-dependency>
+                        <release-version>1</release-version>
+                        <specification-version>1.87</specification-version>
+                    </run-dependency>
+                </dependency>
                 <dependency>
                     <code-name-base>org.openide.filesystems</code-name-base>
                     <build-prerequisite/>
diff --git 
a/java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/SemanticHighlighterBase.java
 
b/java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/SemanticHighlighterBase.java
index af076375a8..2bc7bf8f8b 100644
--- 
a/java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/SemanticHighlighterBase.java
+++ 
b/java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/SemanticHighlighterBase.java
@@ -182,7 +182,7 @@ public abstract class SemanticHighlighterBase extends 
JavaParserResultTask {
             }
         }
         
-        Map<Element, List<UnusedDescription>> element2Unused = 
UnusedDetector.findUnused(info) //XXX: unnecessarily ugly
+        Map<Element, List<UnusedDescription>> element2Unused = 
UnusedDetector.findUnused(info, () -> cancel.get()) //XXX: unnecessarily ugly
                                                                              
.stream()
                                                                              
.collect(Collectors.groupingBy(ud -> ud.unusedElement));
         for (Map.Entry<Element, List<Use>> entry : v.type2Uses.entrySet()) {
diff --git 
a/java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/UnusedDetector.java
 
b/java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/UnusedDetector.java
index 3e19f4f321..f2c8ca0686 100644
--- 
a/java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/UnusedDetector.java
+++ 
b/java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/UnusedDetector.java
@@ -31,8 +31,6 @@ import com.sun.source.tree.Tree;
 import com.sun.source.tree.Tree.Kind;
 import com.sun.source.tree.VariableTree;
 import com.sun.source.util.TreePath;
-import com.sun.source.util.TreePathScanner;
-import java.io.IOException;
 import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -44,6 +42,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
+import java.util.concurrent.Callable;
 import java.util.concurrent.atomic.AtomicBoolean;
 import javax.lang.model.element.Element;
 import javax.lang.model.element.ElementKind;
@@ -52,11 +51,19 @@ import javax.lang.model.element.Modifier;
 import javax.lang.model.element.TypeElement;
 import javax.lang.model.type.TypeKind;
 import javax.lang.model.type.TypeMirror;
+import org.netbeans.api.java.classpath.ClassPath;
+import org.netbeans.api.java.project.JavaProjectConstants;
 import org.netbeans.api.java.source.ClassIndex;
+import org.netbeans.api.java.source.ClasspathInfo;
 import org.netbeans.api.java.source.CompilationInfo;
 import org.netbeans.api.java.source.ElementHandle;
 import org.netbeans.api.java.source.JavaSource;
 import org.netbeans.api.java.source.support.ErrorAwareTreePathScanner;
+import org.netbeans.api.project.FileOwnerQuery;
+import org.netbeans.api.project.Project;
+import org.netbeans.api.project.ProjectUtils;
+import org.netbeans.api.project.SourceGroup;
+import org.netbeans.spi.java.classpath.support.ClassPathSupport;
 import org.openide.filesystems.FileObject;
 import org.openide.util.Exceptions;
 
@@ -92,7 +99,7 @@ public class UnusedDetector {
 
     }
 
-    public static List<UnusedDescription> findUnused(CompilationInfo info) {
+    public static List<UnusedDescription> findUnused(CompilationInfo info, 
Callable<Boolean> cancel) {
         List<UnusedDescription> cached = (List<UnusedDescription>) 
info.getCachedValue(UnusedDetector.class);
         if (cached != null) {
             return cached;
@@ -122,26 +129,26 @@ public class UnusedDetector {
                     boolean isWritten = uses.contains(UseTypes.WRITTEN);
                     boolean isRead = uses.contains(UseTypes.READ);
                     if (!isWritten && !isRead) {
-                        if (isPrivate || isUnusedInPkg(info, el)) {
+                        if (isPrivate || isUnusedInPkg(info, el, cancel)) {
                             result.add(new UnusedDescription(el, declaration, 
UnusedReason.NOT_WRITTEN_READ));
                         }
                     } else if (!isWritten) {
                         result.add(new UnusedDescription(el, declaration, 
UnusedReason.NOT_WRITTEN));
                     } else if (!isRead) {
-                        if (isPrivate || isUnusedInPkg(info, el)) {
+                        if (isPrivate || isUnusedInPkg(info, el, cancel)) {
                             result.add(new UnusedDescription(el, declaration, 
UnusedReason.NOT_READ));
                         }
                     }
                 }
             } else if ((el.getKind() == ElementKind.CONSTRUCTOR || 
el.getKind() == ElementKind.METHOD) && (isPrivate || isPkgPrivate)) {
                 if (!isSerializationMethod(info, (ExecutableElement)el) && 
!uses.contains(UseTypes.USED)) {
-                    if (isPrivate || isUnusedInPkg(info, el)) {
+                    if (isPrivate || isUnusedInPkg(info, el, cancel)) {
                         result.add(new UnusedDescription(el, declaration, 
UnusedReason.NOT_USED));
                     }
                 }
             } else if ((el.getKind().isClass() || el.getKind().isInterface()) 
&& (isPrivate || isPkgPrivate)) {
                 if (!uses.contains(UseTypes.USED)) {
-                    if (isPrivate || isUnusedInPkg(info, el)) {
+                    if (isPrivate || isUnusedInPkg(info, el, cancel)) {
                         result.add(new UnusedDescription(el, declaration, 
UnusedReason.NOT_USED));
                     }
                 }
@@ -272,7 +279,7 @@ public class UnusedDetector {
                LOCAL_VARIABLES.contains(el.getKind());
     }
 
-    private static boolean isUnusedInPkg(CompilationInfo info, Element el) {
+    private static boolean isUnusedInPkg(CompilationInfo info, Element el, 
Callable<Boolean> cancel) {
         TypeElement typeElement;
         Set<? extends String> packageSet = 
Collections.singleton(info.getElements().getPackageOf(el).getQualifiedName().toString());
         Set<ClassIndex.SearchKind> searchKinds;
@@ -318,19 +325,35 @@ public class UnusedDetector {
                 return true;
         }
         ElementHandle eh = ElementHandle.create(el);
-        Set<FileObject> res = 
info.getClasspathInfo().getClassIndex().getResources(ElementHandle.create(typeElement),
 searchKinds, scope);
+        Project prj = FileOwnerQuery.getOwner(info.getFileObject());
+        ClasspathInfo cpInfo;
+        if (prj != null) {
+            SourceGroup[] sourceGroups = 
ProjectUtils.getSources(prj).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
+            FileObject[] roots = new FileObject[sourceGroups.length];
+            for (int i = 0; i < sourceGroups.length; i++) {
+                SourceGroup sourceGroup = sourceGroups[i];
+                roots[i] = sourceGroup.getRootFolder();
+            }
+            cpInfo = ClasspathInfo.create(ClassPath.EMPTY, ClassPath.EMPTY, 
ClassPathSupport.createClassPath(roots));
+        } else {
+            cpInfo = info.getClasspathInfo();
+        }
+        Set<FileObject> res = 
cpInfo.getClassIndex().getResources(ElementHandle.create(typeElement), 
searchKinds, scope);
         if (res != null) {
             for (FileObject fo : res) {
-                if (fo != info.getFileObject()) {
-                    JavaSource js = JavaSource.forFileObject(fo);
-                    if (js == null) {
+                try {
+                    if (Boolean.TRUE.equals(cancel.call())) {
                         return false;
                     }
-                    AtomicBoolean found = new AtomicBoolean();
-                    try {
+                    if (fo != info.getFileObject()) {
+                        JavaSource js = JavaSource.forFileObject(fo);
+                        if (js == null) {
+                            return false;
+                        }
+                        AtomicBoolean found = new AtomicBoolean();
                         js.runUserActionTask(cc -> {
                             cc.toPhase(JavaSource.Phase.RESOLVED);
-                            new TreePathScanner<Void, Element>() {
+                            new ErrorAwareTreePathScanner<Void, Element>() {
                                 @Override
                                 public Void scan(Tree tree, Element p) {
                                     if (!found.get() && tree != null) {
@@ -344,12 +367,12 @@ public class UnusedDetector {
                                 }
                             }.scan(new TreePath(cc.getCompilationUnit()), el);
                         }, true);
-                    } catch (IOException ex) {
-                        Exceptions.printStackTrace(ex);
-                    }
-                    if (found.get()) {
-                        return false;
+                        if (found.get()) {
+                            return false;
+                        }
                     }
+                } catch (Exception ex) {
+                    Exceptions.printStackTrace(ex);
                 }
             }
             return true;
diff --git 
a/java/java.editor.base/test/unit/src/org/netbeans/modules/java/editor/base/semantic/UnusedDetectorTest.java
 
b/java/java.editor.base/test/unit/src/org/netbeans/modules/java/editor/base/semantic/UnusedDetectorTest.java
index 7e5612f8b7..a94df908b7 100644
--- 
a/java/java.editor.base/test/unit/src/org/netbeans/modules/java/editor/base/semantic/UnusedDetectorTest.java
+++ 
b/java/java.editor.base/test/unit/src/org/netbeans/modules/java/editor/base/semantic/UnusedDetectorTest.java
@@ -344,7 +344,7 @@ public class UnusedDetectorTest extends NbTestCase {
                 try {
                     parameter.toPhase(JavaSource.Phase.UP_TO_DATE);
 
-                    Set<String> result = UnusedDetector.findUnused(parameter)
+                    Set<String> result = UnusedDetector.findUnused(parameter, 
() -> false)
                                                        .stream()
                                                        .map(ud -> 
parameter.getCompilationUnit().getLineMap().getLineNumber(parameter.getTrees().getSourcePositions().getStartPosition(ud.unusedElementPath.getCompilationUnit(),
 ud.unusedElementPath.getLeaf())) + ":" + ud.unusedElement.getSimpleName() + 
":" + ud.reason.name())
                                                        
.collect(Collectors.toSet());
diff --git 
a/java/java.hints/src/org/netbeans/modules/java/hints/bugs/Unused.java 
b/java/java.hints/src/org/netbeans/modules/java/hints/bugs/Unused.java
index 28b96bd8d0..7cb214e5cc 100644
--- a/java/java.hints/src/org/netbeans/modules/java/hints/bugs/Unused.java
+++ b/java/java.hints/src/org/netbeans/modules/java/hints/bugs/Unused.java
@@ -46,7 +46,7 @@ public class Unused {
 
     @TriggerTreeKind(Kind.COMPILATION_UNIT)
     public static List<ErrorDescription> unused(HintContext ctx) {
-         return UnusedDetector.findUnused(ctx.getInfo())
+         return UnusedDetector.findUnused(ctx.getInfo(), () -> 
ctx.isCanceled())
                              .stream()
                              .map(ud -> convertUnused(ctx, ud))
                              .filter(err -> err != null)


---------------------------------------------------------------------
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

Reply via email to