[Git][java-team/maven-dependency-analyzer][upstream] 2 commits: New upstream version 1.15.0

Emmanuel Bourg (@ebourg) gitlab at salsa.debian.org
Tue Oct 22 18:30:05 BST 2024



Emmanuel Bourg pushed to branch upstream at Debian Java Maintainers / maven-dependency-analyzer


Commits:
0e1d6d2c by Emmanuel Bourg at 2024-10-22T19:26:19+02:00
New upstream version 1.15.0
- - - - -
050554c8 by Emmanuel Bourg at 2024-10-22T19:26:27+02:00
New upstream version 1.15.1
- - - - -


17 changed files:

- README.md
- pom.xml
- src/it/java8methodRefs/pom.xml
- src/it/java8methodRefs/verify.groovy
- src/main/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzer.java
- src/main/java/org/apache/maven/shared/dependency/analyzer/DependencyAnalyzer.java
- + src/main/java/org/apache/maven/shared/dependency/analyzer/DependencyUsage.java
- src/main/java/org/apache/maven/shared/dependency/analyzer/ProjectDependencyAnalysis.java
- src/main/java/org/apache/maven/shared/dependency/analyzer/asm/ASMDependencyAnalyzer.java
- src/main/java/org/apache/maven/shared/dependency/analyzer/asm/DefaultAnnotationVisitor.java
- src/main/java/org/apache/maven/shared/dependency/analyzer/asm/DefaultClassVisitor.java
- src/main/java/org/apache/maven/shared/dependency/analyzer/asm/DefaultFieldVisitor.java
- src/main/java/org/apache/maven/shared/dependency/analyzer/asm/DefaultMethodVisitor.java
- src/main/java/org/apache/maven/shared/dependency/analyzer/asm/DefaultSignatureVisitor.java
- src/main/java/org/apache/maven/shared/dependency/analyzer/asm/DependencyClassFileVisitor.java
- src/main/java/org/apache/maven/shared/dependency/analyzer/asm/ResultCollector.java
- src/test/java/org/apache/maven/shared/dependency/analyzer/asm/DependencyVisitorTest.java


Changes:

=====================================
README.md
=====================================
@@ -20,7 +20,7 @@ Contributing to [Apache Maven Dependency Analyzer](https://maven.apache.org/shar
 [![ASF Jira](https://img.shields.io/endpoint?url=https%3A%2F%2Fmaven.apache.org%2Fbadges%2Fasf_jira-MSHARED.json)][jira]
 [![Apache License, Version 2.0, January 2004](https://img.shields.io/github/license/apache/maven.svg?label=License)][license]
 [![Maven Central](https://img.shields.io/maven-central/v/org.apache.maven.shared/maven-dependency-analyzer.svg?label=Maven%20Central)](https://search.maven.org/artifact/org.apache.maven.shared/maven-dependency-analyzer)
-[![Reproducible Builds](https://img.shields.io/badge/Reproducible_Builds-ok-green?labelColor=blue)](https://github.com/jvm-repo-rebuild/reproducible-central/blob/master/content/org/apache/maven/shared/maven-dependency-analyzer/README.md)
+[![Reproducible Builds](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/jvm-repo-rebuild/reproducible-central/master/content/org/apache/maven/shared/maven-dependency-analyzer/badge.json)](https://github.com/jvm-repo-rebuild/reproducible-central/blob/master/content/org/apache/maven/shared/maven-dependency-analyzer/README.md)
 [![Jenkins Status](https://img.shields.io/jenkins/s/https/ci-maven.apache.org/job/Maven/job/maven-box/job/maven-dependency-analyzer/job/master.svg)][build]
 [![Jenkins tests](https://img.shields.io/jenkins/t/https/ci-maven.apache.org/job/Maven/job/maven-box/job/maven-dependency-analyzer/job/master.svg)][test-results]
 


=====================================
pom.xml
=====================================
@@ -24,12 +24,12 @@
   <parent>
     <groupId>org.apache.maven.shared</groupId>
     <artifactId>maven-shared-components</artifactId>
-    <version>42</version>
+    <version>43</version>
     <relativePath />
   </parent>
 
   <artifactId>maven-dependency-analyzer</artifactId>
-  <version>1.14.1</version>
+  <version>1.15.1</version>
   <packaging>jar</packaging>
 
   <name>Apache Maven Dependency Analyzer</name>
@@ -38,7 +38,7 @@
   <scm>
     <connection>scm:git:https://gitbox.apache.org/repos/asf/maven-dependency-analyzer.git</connection>
     <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/maven-dependency-analyzer.git</developerConnection>
-    <tag>maven-dependency-analyzer-1.14.1</tag>
+    <tag>maven-dependency-analyzer-1.15.1</tag>
     <url>https://github.com/apache/maven-dependency-analyzer/tree/${project.scm.tag}</url>
   </scm>
   <issueManagement>
@@ -57,9 +57,9 @@
   </distributionManagement>
 
   <properties>
-    <mavenVersion>3.9.6</mavenVersion>
+    <mavenVersion>3.9.9</mavenVersion>
     <javaVersion>8</javaVersion>
-    <project.build.outputTimestamp>2024-05-19T18:45:28Z</project.build.outputTimestamp>
+    <project.build.outputTimestamp>2024-10-22T15:20:49Z</project.build.outputTimestamp>
   </properties>
 
   <dependencyManagement>
@@ -67,7 +67,7 @@
       <dependency>
         <groupId>org.assertj</groupId>
         <artifactId>assertj-bom</artifactId>
-        <version>3.25.3</version>
+        <version>3.26.3</version>
         <type>pom</type>
         <scope>import</scope>
       </dependency>
@@ -104,7 +104,7 @@
     <dependency>
       <groupId>org.ow2.asm</groupId>
       <artifactId>asm</artifactId>
-      <version>9.7</version>
+      <version>9.7.1</version>
     </dependency>
 
     <!-- testing support -->


=====================================
src/it/java8methodRefs/pom.xml
=====================================
@@ -35,7 +35,7 @@
     <dependency>
       <groupId>commons-io</groupId>
       <artifactId>commons-io</artifactId>
-      <version>2.11.0</version>
+      <version>2.17.0</version>
     </dependency>
     <dependency>
       <groupId>commons-lang</groupId>


=====================================
src/it/java8methodRefs/verify.groovy
=====================================
@@ -21,7 +21,7 @@ def analysis = new File( basedir, 'target/analysis.txt' ).text
 
 def expected = '''
 UsedDeclaredArtifacts:
- commons-io:commons-io:jar:2.11.0:compile
+ commons-io:commons-io:jar:2.17.0:compile
  commons-lang:commons-lang:jar:2.6:compile
 
 UsedUndeclaredArtifactsWithClasses:


=====================================
src/main/java/org/apache/maven/shared/dependency/analyzer/DefaultProjectDependencyAnalyzer.java
=====================================
@@ -36,6 +36,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
+import java.util.stream.Collectors;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.project.MavenProject;
@@ -68,17 +69,17 @@ public class DefaultProjectDependencyAnalyzer implements ProjectDependencyAnalyz
             ClassesPatterns excludedClassesPatterns = new ClassesPatterns(excludedClasses);
             Map<Artifact, Set<String>> artifactClassMap = buildArtifactClassMap(project, excludedClassesPatterns);
 
-            Set<String> mainDependencyClasses = buildMainDependencyClasses(project, excludedClassesPatterns);
-            Set<String> testDependencyClasses = buildTestDependencyClasses(project, excludedClassesPatterns);
+            Set<DependencyUsage> mainDependencyClasses = buildMainDependencyClasses(project, excludedClassesPatterns);
+            Set<DependencyUsage> testDependencyClasses = buildTestDependencyClasses(project, excludedClassesPatterns);
 
-            Set<String> dependencyClasses = new HashSet<>();
+            Set<DependencyUsage> dependencyClasses = new HashSet<>();
             dependencyClasses.addAll(mainDependencyClasses);
             dependencyClasses.addAll(testDependencyClasses);
 
-            Set<String> testOnlyDependencyClasses =
+            Set<DependencyUsage> testOnlyDependencyClasses =
                     buildTestOnlyDependencyClasses(mainDependencyClasses, testDependencyClasses);
 
-            Map<Artifact, Set<String>> usedArtifacts = buildUsedArtifacts(artifactClassMap, dependencyClasses);
+            Map<Artifact, Set<DependencyUsage>> usedArtifacts = buildUsedArtifacts(artifactClassMap, dependencyClasses);
             Set<Artifact> mainUsedArtifacts =
                     buildUsedArtifacts(artifactClassMap, mainDependencyClasses).keySet();
 
@@ -90,7 +91,12 @@ public class DefaultProjectDependencyAnalyzer implements ProjectDependencyAnalyz
             Set<Artifact> usedDeclaredArtifacts = new LinkedHashSet<>(declaredArtifacts);
             usedDeclaredArtifacts.retainAll(usedArtifacts.keySet());
 
-            Map<Artifact, Set<String>> usedUndeclaredArtifactsWithClasses = new LinkedHashMap<>(usedArtifacts);
+            Map<Artifact, Set<DependencyUsage>> usedDeclaredArtifactsWithClasses = new LinkedHashMap<>();
+            for (Artifact a : usedDeclaredArtifacts) {
+                usedDeclaredArtifactsWithClasses.put(a, usedArtifacts.get(a));
+            }
+
+            Map<Artifact, Set<DependencyUsage>> usedUndeclaredArtifactsWithClasses = new LinkedHashMap<>(usedArtifacts);
             Set<Artifact> usedUndeclaredArtifacts =
                     removeAll(usedUndeclaredArtifactsWithClasses.keySet(), declaredArtifacts);
             usedUndeclaredArtifactsWithClasses.keySet().retainAll(usedUndeclaredArtifacts);
@@ -101,7 +107,7 @@ public class DefaultProjectDependencyAnalyzer implements ProjectDependencyAnalyz
             Set<Artifact> testArtifactsWithNonTestScope = getTestArtifactsWithNonTestScope(testOnlyArtifacts);
 
             return new ProjectDependencyAnalysis(
-                    usedDeclaredArtifacts, usedUndeclaredArtifactsWithClasses,
+                    usedDeclaredArtifactsWithClasses, usedUndeclaredArtifactsWithClasses,
                     unusedDeclaredArtifacts, testArtifactsWithNonTestScope);
         } catch (IOException exception) {
             throw new ProjectDependencyAnalyzerException("Cannot analyze dependencies", exception);
@@ -149,7 +155,7 @@ public class DefaultProjectDependencyAnalyzer implements ProjectDependencyAnalyz
         return nonTestScopeArtifacts;
     }
 
-    private Map<Artifact, Set<String>> buildArtifactClassMap(MavenProject project, ClassesPatterns excludedClasses)
+    protected Map<Artifact, Set<String>> buildArtifactClassMap(MavenProject project, ClassesPatterns excludedClasses)
             throws IOException {
         Map<Artifact, Set<String>> artifactClassMap = new LinkedHashMap<>();
 
@@ -190,29 +196,33 @@ public class DefaultProjectDependencyAnalyzer implements ProjectDependencyAnalyz
         return artifactClassMap;
     }
 
-    private static Set<String> buildTestOnlyDependencyClasses(
-            Set<String> mainDependencyClasses, Set<String> testDependencyClasses) {
-        Set<String> testOnlyDependencyClasses = new HashSet<>(testDependencyClasses);
-        testOnlyDependencyClasses.removeAll(mainDependencyClasses);
+    private static Set<DependencyUsage> buildTestOnlyDependencyClasses(
+            Set<DependencyUsage> mainDependencyClasses, Set<DependencyUsage> testDependencyClasses) {
+        Set<DependencyUsage> testOnlyDependencyClasses = new HashSet<>(testDependencyClasses);
+        Set<String> mainDepClassNames = mainDependencyClasses.stream()
+                .map(DependencyUsage::getDependencyClass)
+                .collect(Collectors.toSet());
+        testOnlyDependencyClasses.removeIf(u -> mainDepClassNames.contains(u.getDependencyClass()));
         return testOnlyDependencyClasses;
     }
 
-    private Set<String> buildMainDependencyClasses(MavenProject project, ClassesPatterns excludedClasses)
+    private Set<DependencyUsage> buildMainDependencyClasses(MavenProject project, ClassesPatterns excludedClasses)
             throws IOException {
         String outputDirectory = project.getBuild().getOutputDirectory();
         return buildDependencyClasses(outputDirectory, excludedClasses);
     }
 
-    private Set<String> buildTestDependencyClasses(MavenProject project, ClassesPatterns excludedClasses)
+    private Set<DependencyUsage> buildTestDependencyClasses(MavenProject project, ClassesPatterns excludedClasses)
             throws IOException {
         String testOutputDirectory = project.getBuild().getTestOutputDirectory();
         return buildDependencyClasses(testOutputDirectory, excludedClasses);
     }
 
-    private Set<String> buildDependencyClasses(String path, ClassesPatterns excludedClasses) throws IOException {
+    private Set<DependencyUsage> buildDependencyClasses(String path, ClassesPatterns excludedClasses)
+            throws IOException {
         URL url = new File(path).toURI().toURL();
 
-        return dependencyAnalyzer.analyze(url, excludedClasses);
+        return dependencyAnalyzer.analyzeUsages(url, excludedClasses);
     }
 
     private static Set<Artifact> buildDeclaredArtifacts(MavenProject project) {
@@ -225,20 +235,20 @@ public class DefaultProjectDependencyAnalyzer implements ProjectDependencyAnalyz
         return declaredArtifacts;
     }
 
-    private static Map<Artifact, Set<String>> buildUsedArtifacts(
-            Map<Artifact, Set<String>> artifactClassMap, Set<String> dependencyClasses) {
-        Map<Artifact, Set<String>> usedArtifacts = new HashMap<>();
+    private static Map<Artifact, Set<DependencyUsage>> buildUsedArtifacts(
+            Map<Artifact, Set<String>> artifactClassMap, Set<DependencyUsage> dependencyClasses) {
+        Map<Artifact, Set<DependencyUsage>> usedArtifacts = new HashMap<>();
 
-        for (String className : dependencyClasses) {
-            Artifact artifact = findArtifactForClassName(artifactClassMap, className);
+        for (DependencyUsage classUsage : dependencyClasses) {
+            Artifact artifact = findArtifactForClassName(artifactClassMap, classUsage.getDependencyClass());
 
             if (artifact != null) {
-                Set<String> classesFromArtifact = usedArtifacts.get(artifact);
+                Set<DependencyUsage> classesFromArtifact = usedArtifacts.get(artifact);
                 if (classesFromArtifact == null) {
                     classesFromArtifact = new HashSet<>();
                     usedArtifacts.put(artifact, classesFromArtifact);
                 }
-                classesFromArtifact.add(className);
+                classesFromArtifact.add(classUsage);
             }
         }
 


=====================================
src/main/java/org/apache/maven/shared/dependency/analyzer/DependencyAnalyzer.java
=====================================
@@ -21,6 +21,7 @@ package org.apache.maven.shared.dependency.analyzer;
 import java.io.IOException;
 import java.net.URL;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 /**
  * Gets the set of classes referenced by a library given either as a jar file or an exploded directory.
@@ -48,5 +49,19 @@ public interface DependencyAnalyzer {
      * @return the set of class names referenced by the library
      * @throws IOException if an error occurs reading a JAR or .class file
      */
-    Set<String> analyze(URL url, ClassesPatterns excludeClasses) throws IOException;
+    default Set<String> analyze(URL url, ClassesPatterns excludeClasses) throws IOException {
+        return analyzeUsages(url, excludeClasses).stream()
+                .map(DependencyUsage::getDependencyClass)
+                .collect(Collectors.toSet());
+    }
+
+    /**
+     * <p>analyzeUsages.</p>
+     *
+     * @param url the JAR file or directory to analyze
+     * @return the set of class names referenced by the library, paired with the
+     * classes declaring those references.
+     * @throws IOException if an error occurs reading a JAR or .class file
+     */
+    Set<DependencyUsage> analyzeUsages(URL url, ClassesPatterns excludeClasses) throws IOException;
 }


=====================================
src/main/java/org/apache/maven/shared/dependency/analyzer/DependencyUsage.java
=====================================
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.shared.dependency.analyzer;
+
+/**
+ * Usage of a dependency class by a project class.
+ *
+ * @author <a href="mailto:hijon89 at gmail.com">Jonathan Haber</a>
+ */
+public class DependencyUsage {
+
+    private final String dependencyClass;
+
+    private final String usedBy;
+
+    public DependencyUsage(String dependencyClass, String usedBy) {
+        this.dependencyClass = dependencyClass;
+        this.usedBy = usedBy;
+    }
+
+    /**
+     * @return the dependency class used by the project class
+     */
+    public String getDependencyClass() {
+        return dependencyClass;
+    }
+
+    /**
+     * @return the project class using the dependency class
+     */
+    public String getUsedBy() {
+        return usedBy;
+    }
+
+    /*
+     * @see java.lang.Object#hashCode()
+     */
+    public int hashCode() {
+        int hashCode = dependencyClass.hashCode();
+        hashCode = (hashCode * 37) + usedBy.hashCode();
+
+        return hashCode;
+    }
+
+    /*
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    public boolean equals(Object object) {
+        if (object instanceof DependencyUsage) {
+            DependencyUsage usage = (DependencyUsage) object;
+
+            return getDependencyClass().equals(usage.getDependencyClass())
+                    && getUsedBy().equals(usage.getUsedBy());
+        }
+
+        return false;
+    }
+
+    /*
+     * @see java.lang.Object#toString()
+     */
+    public String toString() {
+        StringBuilder buffer = new StringBuilder();
+
+        buffer.append("dependencyClass=").append(getDependencyClass());
+        buffer.append(",");
+        buffer.append("usedBy=").append(getUsedBy());
+
+        buffer.insert(0, "[");
+        buffer.insert(0, getClass().getName());
+
+        buffer.append("]");
+
+        return buffer.toString();
+    }
+}


=====================================
src/main/java/org/apache/maven/shared/dependency/analyzer/ProjectDependencyAnalysis.java
=====================================
@@ -23,9 +23,11 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 import org.apache.maven.artifact.Artifact;
 
@@ -37,9 +39,9 @@ import org.apache.maven.artifact.Artifact;
 public class ProjectDependencyAnalysis {
     // fields -----------------------------------------------------------------
 
-    private final Set<Artifact> usedDeclaredArtifacts;
+    private final Map<Artifact, Set<DependencyUsage>> usedDeclaredArtifacts;
 
-    private final Map<Artifact, Set<String>> usedUndeclaredArtifacts;
+    private final Map<Artifact, Set<DependencyUsage>> usedUndeclaredArtifacts;
 
     private final Set<Artifact> unusedDeclaredArtifacts;
 
@@ -49,7 +51,7 @@ public class ProjectDependencyAnalysis {
      * <p>Constructor for ProjectDependencyAnalysis.</p>
      */
     public ProjectDependencyAnalysis() {
-        this(null, (Map<Artifact, Set<String>>) null, null, null);
+        this(null, (Map<Artifact, Set<DependencyUsage>>) null, null, null);
     }
 
     /**
@@ -80,15 +82,15 @@ public class ProjectDependencyAnalysis {
             Set<Artifact> unusedDeclaredArtifacts,
             Set<Artifact> testArtifactsWithNonTestScope) {
         this(
-                usedDeclaredArtifacts,
+                mapWithKeys(usedDeclaredArtifacts),
                 mapWithKeys(usedUndeclaredArtifacts),
                 unusedDeclaredArtifacts,
                 testArtifactsWithNonTestScope);
     }
 
     public ProjectDependencyAnalysis(
-            Set<Artifact> usedDeclaredArtifacts,
-            Map<Artifact, Set<String>> usedUndeclaredArtifacts,
+            Map<Artifact, Set<DependencyUsage>> usedDeclaredArtifacts,
+            Map<Artifact, Set<DependencyUsage>> usedUndeclaredArtifacts,
             Set<Artifact> unusedDeclaredArtifacts,
             Set<Artifact> testArtifactsWithNonTestScope) {
         this.usedDeclaredArtifacts = safeCopy(usedDeclaredArtifacts);
@@ -103,6 +105,15 @@ public class ProjectDependencyAnalysis {
      * @return artifacts both used and declared
      */
     public Set<Artifact> getUsedDeclaredArtifacts() {
+        return safeCopy(usedDeclaredArtifacts.keySet());
+    }
+
+    /**
+     * Returns artifacts both used and declared.
+     *
+     * @return artifacts both used and declared
+     */
+    public Map<Artifact, Set<DependencyUsage>> getUsedDeclaredArtifactsWithUsages() {
         return safeCopy(usedDeclaredArtifacts);
     }
 
@@ -121,6 +132,20 @@ public class ProjectDependencyAnalysis {
      * @return artifacts used but not declared
      */
     public Map<Artifact, Set<String>> getUsedUndeclaredArtifactsWithClasses() {
+        Map<Artifact, Set<String>> usedUndeclaredArtifactsWithClasses = new HashMap<>();
+
+        for (Map.Entry<Artifact, Set<DependencyUsage>> entry : usedUndeclaredArtifacts.entrySet()) {
+            usedUndeclaredArtifactsWithClasses.put(
+                    entry.getKey(),
+                    entry.getValue().stream()
+                            .map(DependencyUsage::getDependencyClass)
+                            .collect(Collectors.toSet()));
+        }
+
+        return usedUndeclaredArtifactsWithClasses;
+    }
+
+    public Map<Artifact, Set<DependencyUsage>> getUsedUndeclaredArtifactsWithUsages() {
         return safeCopy(usedUndeclaredArtifacts);
     }
 
@@ -173,7 +198,7 @@ public class ProjectDependencyAnalysis {
         Set<String> forced = new HashSet<>(Arrays.asList(forceUsedDependencies));
 
         Set<Artifact> forcedUnusedDeclared = new HashSet<>(unusedDeclaredArtifacts);
-        Set<Artifact> forcedUsedDeclared = new HashSet<>(usedDeclaredArtifacts);
+        Set<Artifact> forcedUsedDeclared = new HashSet<>(usedDeclaredArtifacts.keySet());
 
         Iterator<Artifact> iter = forcedUnusedDeclared.iterator();
         while (iter.hasNext()) {
@@ -189,7 +214,7 @@ public class ProjectDependencyAnalysis {
         if (!forced.isEmpty()) {
             // trying to force dependencies as used-declared which were not declared or already detected as used
             Set<String> used = new HashSet<>();
-            for (Artifact artifact : usedDeclaredArtifacts) {
+            for (Artifact artifact : usedDeclaredArtifacts.keySet()) {
                 String id = artifact.getGroupId() + ':' + artifact.getArtifactId();
                 if (forced.remove(id)) {
                     used.add(id);
@@ -210,7 +235,10 @@ public class ProjectDependencyAnalysis {
         }
 
         return new ProjectDependencyAnalysis(
-                forcedUsedDeclared, usedUndeclaredArtifacts, forcedUnusedDeclared, testArtifactsWithNonTestScope);
+                mapWithKeys(forcedUsedDeclared),
+                usedUndeclaredArtifacts,
+                forcedUnusedDeclared,
+                testArtifactsWithNonTestScope);
     }
 
     /**
@@ -294,29 +322,29 @@ public class ProjectDependencyAnalysis {
         return (set == null) ? Collections.emptySet() : Collections.unmodifiableSet(new LinkedHashSet<>(set));
     }
 
-    private static Map<Artifact, Set<String>> safeCopy(Map<Artifact, Set<String>> origMap) {
+    private static Map<Artifact, Set<DependencyUsage>> safeCopy(Map<Artifact, Set<DependencyUsage>> origMap) {
         if (origMap == null) {
             return Collections.emptyMap();
         }
 
-        Map<Artifact, Set<String>> map = new HashMap<>();
+        Map<Artifact, Set<DependencyUsage>> map = new LinkedHashMap<>();
 
-        for (Map.Entry<Artifact, Set<String>> e : origMap.entrySet()) {
+        for (Map.Entry<Artifact, Set<DependencyUsage>> e : origMap.entrySet()) {
             map.put(e.getKey(), Collections.unmodifiableSet(new LinkedHashSet<>(e.getValue())));
         }
 
         return map;
     }
 
-    private static Map<Artifact, Set<String>> mapWithKeys(Set<Artifact> keys) {
+    private static Map<Artifact, Set<DependencyUsage>> mapWithKeys(Set<Artifact> keys) {
         if (keys == null) {
             return Collections.emptyMap();
         }
 
-        Map<Artifact, Set<String>> map = new HashMap<>();
+        Map<Artifact, Set<DependencyUsage>> map = new LinkedHashMap<>();
 
         for (Artifact k : keys) {
-            map.put(k, Collections.<String>emptySet());
+            map.put(k, Collections.<DependencyUsage>emptySet());
         }
 
         return map;


=====================================
src/main/java/org/apache/maven/shared/dependency/analyzer/asm/ASMDependencyAnalyzer.java
=====================================
@@ -28,6 +28,7 @@ import java.util.Set;
 import org.apache.maven.shared.dependency.analyzer.ClassFileVisitorUtils;
 import org.apache.maven.shared.dependency.analyzer.ClassesPatterns;
 import org.apache.maven.shared.dependency.analyzer.DependencyAnalyzer;
+import org.apache.maven.shared.dependency.analyzer.DependencyUsage;
 
 /**
  * ASMDependencyAnalyzer
@@ -39,11 +40,11 @@ import org.apache.maven.shared.dependency.analyzer.DependencyAnalyzer;
 public class ASMDependencyAnalyzer implements DependencyAnalyzer {
 
     @Override
-    public Set<String> analyze(URL url, ClassesPatterns excludeClasses) throws IOException {
+    public Set<DependencyUsage> analyzeUsages(URL url, ClassesPatterns excludeClasses) throws IOException {
         DependencyClassFileVisitor visitor = new DependencyClassFileVisitor(excludeClasses);
 
         ClassFileVisitorUtils.accept(url, visitor);
 
-        return visitor.getDependencies();
+        return visitor.getDependencyUsages();
     }
 }


=====================================
src/main/java/org/apache/maven/shared/dependency/analyzer/asm/DefaultAnnotationVisitor.java
=====================================
@@ -31,34 +31,37 @@ import org.objectweb.asm.Type;
 public class DefaultAnnotationVisitor extends AnnotationVisitor {
     private final ResultCollector resultCollector;
 
+    private final String usedByClass;
+
     /**
      * <p>Constructor for DefaultAnnotationVisitor.</p>
      *
      * @param resultCollector a {@link org.apache.maven.shared.dependency.analyzer.asm.ResultCollector} object.
      */
-    public DefaultAnnotationVisitor(ResultCollector resultCollector) {
+    public DefaultAnnotationVisitor(ResultCollector resultCollector, String usedByClass) {
         super(Opcodes.ASM9);
         this.resultCollector = resultCollector;
+        this.usedByClass = usedByClass;
     }
 
     /** {@inheritDoc} */
     @Override
     public void visit(final String name, final Object value) {
         if (value instanceof Type) {
-            resultCollector.addType((Type) value);
+            resultCollector.addType(usedByClass, (Type) value);
         }
     }
 
     /** {@inheritDoc} */
     @Override
     public void visitEnum(final String name, final String desc, final String value) {
-        resultCollector.addDesc(desc);
+        resultCollector.addDesc(usedByClass, desc);
     }
 
     /** {@inheritDoc} */
     @Override
     public AnnotationVisitor visitAnnotation(final String name, final String desc) {
-        resultCollector.addDesc(desc);
+        resultCollector.addDesc(usedByClass, desc);
 
         return this;
     }


=====================================
src/main/java/org/apache/maven/shared/dependency/analyzer/asm/DefaultClassVisitor.java
=====================================
@@ -44,6 +44,8 @@ public class DefaultClassVisitor extends ClassVisitor {
 
     private final MethodVisitor methodVisitor;
 
+    private final String usedByClass;
+
     /**
      * <p>Constructor for DefaultClassVisitor.</p>
      *
@@ -58,13 +60,15 @@ public class DefaultClassVisitor extends ClassVisitor {
             AnnotationVisitor annotationVisitor,
             FieldVisitor fieldVisitor,
             MethodVisitor methodVisitor,
-            ResultCollector resultCollector) {
+            ResultCollector resultCollector,
+            String usedByClass) {
         super(Opcodes.ASM9);
         this.signatureVisitor = signatureVisitor;
         this.annotationVisitor = annotationVisitor;
         this.fieldVisitor = fieldVisitor;
         this.methodVisitor = methodVisitor;
         this.resultCollector = resultCollector;
+        this.usedByClass = usedByClass;
     }
 
     /**
@@ -86,8 +90,8 @@ public class DefaultClassVisitor extends ClassVisitor {
             final String superName,
             final String[] interfaces) {
         if (signature == null) {
-            resultCollector.addName(superName);
-            resultCollector.addNames(interfaces);
+            resultCollector.addName(usedByClass, superName);
+            resultCollector.addNames(usedByClass, interfaces);
         } else {
             addSignature(signature);
         }
@@ -96,7 +100,7 @@ public class DefaultClassVisitor extends ClassVisitor {
     /** {@inheritDoc} */
     @Override
     public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) {
-        resultCollector.addDesc(desc);
+        resultCollector.addDesc(usedByClass, desc);
 
         return annotationVisitor;
     }
@@ -106,13 +110,13 @@ public class DefaultClassVisitor extends ClassVisitor {
     public FieldVisitor visitField(
             final int access, final String name, final String desc, final String signature, final Object value) {
         if (signature == null) {
-            resultCollector.addDesc(desc);
+            resultCollector.addDesc(usedByClass, desc);
         } else {
             addTypeSignature(signature);
         }
 
         if (value instanceof Type) {
-            resultCollector.addType((Type) value);
+            resultCollector.addType(usedByClass, (Type) value);
         }
 
         return fieldVisitor;
@@ -132,12 +136,12 @@ public class DefaultClassVisitor extends ClassVisitor {
     public MethodVisitor visitMethod(
             final int access, final String name, final String desc, final String signature, final String[] exceptions) {
         if (signature == null) {
-            resultCollector.addMethodDesc(desc);
+            resultCollector.addMethodDesc(usedByClass, desc);
         } else {
             addSignature(signature);
         }
 
-        resultCollector.addNames(exceptions);
+        resultCollector.addNames(usedByClass, exceptions);
 
         return methodVisitor;
     }
@@ -145,13 +149,13 @@ public class DefaultClassVisitor extends ClassVisitor {
     /** {@inheritDoc} */
     @Override
     public void visitNestHost(final String nestHost) {
-        resultCollector.addName(nestHost);
+        resultCollector.addName(usedByClass, nestHost);
     }
 
     /** {@inheritDoc} */
     @Override
     public void visitNestMember(final String nestMember) {
-        resultCollector.addName(nestMember);
+        resultCollector.addName(usedByClass, nestMember);
     }
 
     private void addSignature(final String signature) {


=====================================
src/main/java/org/apache/maven/shared/dependency/analyzer/asm/DefaultFieldVisitor.java
=====================================
@@ -33,22 +33,26 @@ public class DefaultFieldVisitor extends FieldVisitor {
 
     private final ResultCollector resultCollector;
 
+    private final String usedByClass;
+
     /**
      * <p>Constructor for DefaultFieldVisitor.</p>
      *
      * @param annotationVisitor a {@link org.objectweb.asm.AnnotationVisitor} object.
      * @param resultCollector a {@link org.apache.maven.shared.dependency.analyzer.asm.ResultCollector} object.
      */
-    public DefaultFieldVisitor(AnnotationVisitor annotationVisitor, ResultCollector resultCollector) {
+    public DefaultFieldVisitor(
+            AnnotationVisitor annotationVisitor, ResultCollector resultCollector, String usedByClass) {
         super(Opcodes.ASM9);
         this.annotationVisitor = annotationVisitor;
         this.resultCollector = resultCollector;
+        this.usedByClass = usedByClass;
     }
 
     /** {@inheritDoc} */
     @Override
     public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) {
-        resultCollector.addDesc(desc);
+        resultCollector.addDesc(usedByClass, desc);
 
         return annotationVisitor;
     }


=====================================
src/main/java/org/apache/maven/shared/dependency/analyzer/asm/DefaultMethodVisitor.java
=====================================
@@ -43,6 +43,8 @@ public class DefaultMethodVisitor extends MethodVisitor {
 
     private final ResultCollector resultCollector;
 
+    private final String usedByClass;
+
     /**
      * <p>Constructor for DefaultMethodVisitor.</p>
      *
@@ -51,17 +53,21 @@ public class DefaultMethodVisitor extends MethodVisitor {
      * @param resultCollector a {@link org.apache.maven.shared.dependency.analyzer.asm.ResultCollector} object.
      */
     public DefaultMethodVisitor(
-            AnnotationVisitor annotationVisitor, SignatureVisitor signatureVisitor, ResultCollector resultCollector) {
+            AnnotationVisitor annotationVisitor,
+            SignatureVisitor signatureVisitor,
+            ResultCollector resultCollector,
+            String usedByClass) {
         super(Opcodes.ASM9);
         this.annotationVisitor = annotationVisitor;
         this.signatureVisitor = signatureVisitor;
         this.resultCollector = resultCollector;
+        this.usedByClass = usedByClass;
     }
 
     /** {@inheritDoc} */
     @Override
     public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) {
-        resultCollector.addDesc(desc);
+        resultCollector.addDesc(usedByClass, desc);
 
         return annotationVisitor;
     }
@@ -69,7 +75,7 @@ public class DefaultMethodVisitor extends MethodVisitor {
     /** {@inheritDoc} */
     @Override
     public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) {
-        resultCollector.addDesc(desc);
+        resultCollector.addDesc(usedByClass, desc);
 
         return annotationVisitor;
     }
@@ -77,7 +83,7 @@ public class DefaultMethodVisitor extends MethodVisitor {
     /** {@inheritDoc} */
     @Override
     public AnnotationVisitor visitParameterAnnotation(final int parameter, final String desc, final boolean visible) {
-        resultCollector.addDesc(desc);
+        resultCollector.addDesc(usedByClass, desc);
 
         return annotationVisitor;
     }
@@ -86,7 +92,7 @@ public class DefaultMethodVisitor extends MethodVisitor {
     @Override
     public AnnotationVisitor visitLocalVariableAnnotation(
             int typeRef, TypePath typePath, Label[] start, Label[] end, int[] index, String desc, boolean visible) {
-        resultCollector.addDesc(desc);
+        resultCollector.addDesc(usedByClass, desc);
 
         return annotationVisitor;
     }
@@ -95,16 +101,16 @@ public class DefaultMethodVisitor extends MethodVisitor {
     @Override
     public void visitTypeInsn(final int opcode, final String desc) {
         if (desc.charAt(0) == '[') {
-            resultCollector.addDesc(desc);
+            resultCollector.addDesc(usedByClass, desc);
         } else {
-            resultCollector.addName(desc);
+            resultCollector.addName(usedByClass, desc);
         }
     }
 
     /** {@inheritDoc} */
     @Override
     public void visitFieldInsn(final int opcode, final String owner, final String name, final String desc) {
-        resultCollector.addName(owner);
+        resultCollector.addName(usedByClass, owner);
         /*
          * NOTE: Merely accessing a field does not impose a direct dependency on its type. For example, the code line
          * <code>java.lang.Object var = bean.field;</code> does not directly depend on the type of the field. A direct
@@ -116,30 +122,32 @@ public class DefaultMethodVisitor extends MethodVisitor {
     /** {@inheritDoc} */
     @Override
     public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
-        resultCollector.addName(owner);
+        resultCollector.addName(usedByClass, owner);
     }
 
     /** {@inheritDoc} */
     @Override
     public void visitLdcInsn(final Object cst) {
         if (cst instanceof Type) {
-            resultCollector.addType((Type) cst);
+            resultCollector.addType(usedByClass, (Type) cst);
         }
     }
 
     /** {@inheritDoc} */
     @Override
     public void visitMultiANewArrayInsn(final String desc, final int dims) {
-        resultCollector.addDesc(desc);
+        resultCollector.addDesc(usedByClass, desc);
     }
 
     /** {@inheritDoc} */
     @Override
     public void visitTryCatchBlock(final Label start, final Label end, final Label handler, final String type) {
-        resultCollector.addName(type);
+        resultCollector.addName(usedByClass, type);
     }
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public void visitLocalVariable(
             final String name,
@@ -149,7 +157,7 @@ public class DefaultMethodVisitor extends MethodVisitor {
             final Label end,
             final int index) {
         if (signature == null) {
-            resultCollector.addDesc(desc);
+            resultCollector.addDesc(usedByClass, desc);
         } else {
             addTypeSignature(signature);
         }
@@ -167,6 +175,6 @@ public class DefaultMethodVisitor extends MethodVisitor {
         Arrays.stream(bootstrapMethodArguments)
                 .filter(Type.class::isInstance)
                 .map(Type.class::cast)
-                .forEach(resultCollector::addType);
+                .forEach(t -> resultCollector.addType(usedByClass, t));
     }
 }


=====================================
src/main/java/org/apache/maven/shared/dependency/analyzer/asm/DefaultSignatureVisitor.java
=====================================
@@ -29,26 +29,28 @@ import org.objectweb.asm.signature.SignatureVisitor;
  */
 public class DefaultSignatureVisitor extends SignatureVisitor {
     private final ResultCollector resultCollector;
+    private final String usedByClass;
 
     /**
      * <p>Constructor for DefaultSignatureVisitor.</p>
      *
      * @param resultCollector a {@link org.apache.maven.shared.dependency.analyzer.asm.ResultCollector} object.
      */
-    public DefaultSignatureVisitor(ResultCollector resultCollector) {
+    public DefaultSignatureVisitor(ResultCollector resultCollector, String usedByClass) {
         super(Opcodes.ASM9);
         this.resultCollector = resultCollector;
+        this.usedByClass = usedByClass;
     }
 
     /** {@inheritDoc} */
     @Override
     public void visitClassType(final String name) {
-        resultCollector.addName(name);
+        resultCollector.addName(usedByClass, name);
     }
 
     /** {@inheritDoc} */
     @Override
     public void visitInnerClassType(final String name) {
-        resultCollector.addName(name);
+        resultCollector.addName(usedByClass, name);
     }
 }


=====================================
src/main/java/org/apache/maven/shared/dependency/analyzer/asm/DependencyClassFileVisitor.java
=====================================
@@ -26,6 +26,7 @@ import java.util.Set;
 
 import org.apache.maven.shared.dependency.analyzer.ClassFileVisitor;
 import org.apache.maven.shared.dependency.analyzer.ClassesPatterns;
+import org.apache.maven.shared.dependency.analyzer.DependencyUsage;
 import org.objectweb.asm.AnnotationVisitor;
 import org.objectweb.asm.ClassReader;
 import org.objectweb.asm.ClassVisitor;
@@ -76,15 +77,16 @@ public class DependencyClassFileVisitor implements ClassFileVisitor {
 
             final Set<String> constantPoolClassRefs = ConstantPoolParser.getConstantPoolClassReferences(byteCode);
             for (String string : constantPoolClassRefs) {
-                resultCollector.addName(string);
+                resultCollector.addName(className, string);
             }
 
-            AnnotationVisitor annotationVisitor = new DefaultAnnotationVisitor(resultCollector);
-            SignatureVisitor signatureVisitor = new DefaultSignatureVisitor(resultCollector);
-            FieldVisitor fieldVisitor = new DefaultFieldVisitor(annotationVisitor, resultCollector);
-            MethodVisitor mv = new DefaultMethodVisitor(annotationVisitor, signatureVisitor, resultCollector);
-            ClassVisitor classVisitor =
-                    new DefaultClassVisitor(signatureVisitor, annotationVisitor, fieldVisitor, mv, resultCollector);
+            AnnotationVisitor annotationVisitor = new DefaultAnnotationVisitor(resultCollector, className);
+            SignatureVisitor signatureVisitor = new DefaultSignatureVisitor(resultCollector, className);
+            FieldVisitor fieldVisitor = new DefaultFieldVisitor(annotationVisitor, resultCollector, className);
+            MethodVisitor mv =
+                    new DefaultMethodVisitor(annotationVisitor, signatureVisitor, resultCollector, className);
+            ClassVisitor classVisitor = new DefaultClassVisitor(
+                    signatureVisitor, annotationVisitor, fieldVisitor, mv, resultCollector, className);
 
             reader.accept(classVisitor, 0);
         } catch (IOException exception) {
@@ -116,4 +118,14 @@ public class DependencyClassFileVisitor implements ClassFileVisitor {
     public Set<String> getDependencies() {
         return resultCollector.getDependencies();
     }
+
+    /**
+     * <p>getDependencyUsages.</p>
+     *
+     * @return the set of classes referenced by visited class files, paired with
+     * classes declaring the references.
+     */
+    public Set<DependencyUsage> getDependencyUsages() {
+        return resultCollector.getDependencyUsages();
+    }
 }


=====================================
src/main/java/org/apache/maven/shared/dependency/analyzer/asm/ResultCollector.java
=====================================
@@ -20,7 +20,9 @@ package org.apache.maven.shared.dependency.analyzer.asm;
 
 import java.util.HashSet;
 import java.util.Set;
+import java.util.stream.Collectors;
 
+import org.apache.maven.shared.dependency.analyzer.DependencyUsage;
 import org.objectweb.asm.Type;
 
 /**
@@ -30,7 +32,7 @@ import org.objectweb.asm.Type;
  */
 public class ResultCollector {
 
-    private final Set<String> classes = new HashSet<>();
+    private final Set<DependencyUsage> classUsages = new HashSet<>();
 
     /**
      * <p>getDependencies.</p>
@@ -38,7 +40,18 @@ public class ResultCollector {
      * @return a {@link java.util.Set} object.
      */
     public Set<String> getDependencies() {
-        return classes;
+        return getDependencyUsages().stream()
+                .map(DependencyUsage::getDependencyClass)
+                .collect(Collectors.toSet());
+    }
+
+    /**
+     * <p>getDependencyUsages.</p>
+     *
+     * @return a {@link java.util.Set} object.
+     */
+    public Set<DependencyUsage> getDependencyUsages() {
+        return classUsages;
     }
 
     /**
@@ -46,7 +59,7 @@ public class ResultCollector {
      *
      * @param name a {@link java.lang.String} object.
      */
-    public void addName(String name) {
+    public void addName(final String usedByClass, String name) {
         if (name == null) {
             return;
         }
@@ -65,25 +78,25 @@ public class ResultCollector {
         }
 
         // decode internal representation
-        add(name.replace('/', '.'));
+        add(usedByClass, name.replace('/', '.'));
     }
 
-    void addDesc(final String desc) {
-        addType(Type.getType(desc));
+    void addDesc(final String usedByClass, final String desc) {
+        addType(usedByClass, Type.getType(desc));
     }
 
-    void addType(final Type t) {
+    void addType(final String usedByClass, final Type t) {
         switch (t.getSort()) {
             case Type.ARRAY:
-                addType(t.getElementType());
+                addType(usedByClass, t.getElementType());
                 break;
 
             case Type.METHOD:
-                addMethodDesc(t.getDescriptor());
+                addMethodDesc(usedByClass, t.getDescriptor());
                 break;
 
             case Type.OBJECT:
-                addName(t.getClassName());
+                addName(usedByClass, t.getClassName());
                 break;
             default:
         }
@@ -94,30 +107,30 @@ public class ResultCollector {
      *
      * @param name a {@link java.lang.String} object.
      */
-    public void add(String name) {
+    public void add(final String usedByClass, final String name) {
         // inner classes have equivalent compilation requirement as container class
         if (name.indexOf('$') < 0) {
-            classes.add(name);
+            classUsages.add(new DependencyUsage(name, usedByClass));
         }
     }
 
-    void addNames(final String[] names) {
+    void addNames(final String usedByClass, final String[] names) {
         if (names == null) {
             return;
         }
 
         for (String name : names) {
-            addName(name);
+            addName(usedByClass, name);
         }
     }
 
-    void addMethodDesc(final String desc) {
-        addType(Type.getReturnType(desc));
+    void addMethodDesc(final String usedByClass, final String desc) {
+        addType(usedByClass, Type.getReturnType(desc));
 
         Type[] types = Type.getArgumentTypes(desc);
 
         for (Type type : types) {
-            addType(type);
+            addType(usedByClass, type);
         }
     }
 }


=====================================
src/test/java/org/apache/maven/shared/dependency/analyzer/asm/DependencyVisitorTest.java
=====================================
@@ -41,13 +41,16 @@ class DependencyVisitorTest {
     private DefaultClassVisitor visitor;
     private MethodVisitor mv;
 
+    private String usedByClass = "com.example.MyClass";
+
     @BeforeEach
     void setUp() {
-        AnnotationVisitor annotationVisitor = new DefaultAnnotationVisitor(resultCollector);
-        SignatureVisitor signatureVisitor = new DefaultSignatureVisitor(resultCollector);
-        FieldVisitor fieldVisitor = new DefaultFieldVisitor(annotationVisitor, resultCollector);
-        mv = new DefaultMethodVisitor(annotationVisitor, signatureVisitor, resultCollector);
-        visitor = new DefaultClassVisitor(signatureVisitor, annotationVisitor, fieldVisitor, mv, resultCollector);
+        AnnotationVisitor annotationVisitor = new DefaultAnnotationVisitor(resultCollector, usedByClass);
+        SignatureVisitor signatureVisitor = new DefaultSignatureVisitor(resultCollector, usedByClass);
+        FieldVisitor fieldVisitor = new DefaultFieldVisitor(annotationVisitor, resultCollector, usedByClass);
+        mv = new DefaultMethodVisitor(annotationVisitor, signatureVisitor, resultCollector, usedByClass);
+        visitor = new DefaultClassVisitor(
+                signatureVisitor, annotationVisitor, fieldVisitor, mv, resultCollector, usedByClass);
     }
 
     @Test



View it on GitLab: https://salsa.debian.org/java-team/maven-dependency-analyzer/-/compare/4797ac3689afacf20b003013d9a64df253eb223a...050554c8cb6b17cb351b6975993d28ad2e531833

-- 
View it on GitLab: https://salsa.debian.org/java-team/maven-dependency-analyzer/-/compare/4797ac3689afacf20b003013d9a64df253eb223a...050554c8cb6b17cb351b6975993d28ad2e531833
You're receiving this email because of your account on salsa.debian.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/pkg-java-commits/attachments/20241022/89b8b2dc/attachment.htm>


More information about the pkg-java-commits mailing list