This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-dependency-analyzer.git
The following commit(s) were added to refs/heads/master by this push:
new 889828c Add shared class name and artifact key helpers
889828c is described below
commit 889828c29d4ac7bde5e902ef8e3aba3fd1cc6590
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Thu Nov 19 12:30:13 2026 +0100
Add shared class name and artifact key helpers
---
.../shared/dependency/analyzer/ArtifactKeys.java | 44 ++++++++++++++++++++
.../shared/dependency/analyzer/ClassNames.java | 48 ++++++++++++++++++++++
.../analyzer/ProjectDependencyAnalysis.java | 4 +-
.../dependency/analyzer/ArtifactKeysTest.java | 38 +++++++++++++++++
.../shared/dependency/analyzer/ClassNamesTest.java | 43 +++++++++++++++++++
5 files changed, 175 insertions(+), 2 deletions(-)
diff --git
a/src/main/java/org/apache/maven/shared/dependency/analyzer/ArtifactKeys.java
b/src/main/java/org/apache/maven/shared/dependency/analyzer/ArtifactKeys.java
new file mode 100644
index 0000000..c42f9bb
--- /dev/null
+++
b/src/main/java/org/apache/maven/shared/dependency/analyzer/ArtifactKeys.java
@@ -0,0 +1,44 @@
+/*
+ * 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;
+
+import org.apache.maven.artifact.Artifact;
+
+/**
+ * Helpers for producing the keys used to identify artifacts in dependency
analysis results.
+ *
+ * @since 1.18.0
+ */
+public final class ArtifactKeys {
+
+ private ArtifactKeys() {
+ // utility class
+ }
+
+ /**
+ * Returns the group and artifact identifier of an artifact, separated by
a colon. This is the
+ * same key used to report forced dependency usage and to look up declared
dependencies.
+ *
+ * @param artifact the artifact, must not be {@code null}
+ * @return the group-and-artifact key
+ */
+ public static String groupArtifactId(Artifact artifact) {
+ return artifact.getGroupId() + ':' + artifact.getArtifactId();
+ }
+}
diff --git
a/src/main/java/org/apache/maven/shared/dependency/analyzer/ClassNames.java
b/src/main/java/org/apache/maven/shared/dependency/analyzer/ClassNames.java
new file mode 100644
index 0000000..ffe70ea
--- /dev/null
+++ b/src/main/java/org/apache/maven/shared/dependency/analyzer/ClassNames.java
@@ -0,0 +1,48 @@
+/*
+ * 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;
+
+/**
+ * Helpers for converting JVM internal class names to source-style names.
+ *
+ * @since 1.18.0
+ */
+public final class ClassNames {
+
+ private ClassNames() {
+ // utility class
+ }
+
+ /**
+ * Converts an internal class name to its outer class name. Slashes are
replaced with dots and
+ * any inner-class suffix starting at the first {@code $} is removed.
Class resource names
+ * ending in {@code .class} must have that suffix removed by the caller
before calling this method.
+ *
+ * @param name an internal class name, for example {@code
org/apache/Foo$Bar}, or {@code null}
+ * @return the normalized name, for example {@code org.apache.Foo}, or
{@code null} when the input is {@code null}
+ */
+ public static String normalizeInternalName(String name) {
+ if (name == null) {
+ return null;
+ }
+ String normalized = name.replace('/', '.');
+ int innerClass = normalized.indexOf('$');
+ return innerClass < 0 ? normalized : normalized.substring(0,
innerClass);
+ }
+}
diff --git
a/src/main/java/org/apache/maven/shared/dependency/analyzer/ProjectDependencyAnalysis.java
b/src/main/java/org/apache/maven/shared/dependency/analyzer/ProjectDependencyAnalysis.java
index 6ba7630..258f7c9 100644
---
a/src/main/java/org/apache/maven/shared/dependency/analyzer/ProjectDependencyAnalysis.java
+++
b/src/main/java/org/apache/maven/shared/dependency/analyzer/ProjectDependencyAnalysis.java
@@ -330,7 +330,7 @@ public class ProjectDependencyAnalysis {
while (iter.hasNext()) {
Artifact artifact = iter.next();
- if (forced.remove(artifact.getGroupId() + ':' +
artifact.getArtifactId())) {
+ if (forced.remove(ArtifactKeys.groupArtifactId(artifact))) {
// ok, change artifact status from unused-declared to
used-declared
iter.remove();
forcedUsedDeclared.add(artifact);
@@ -341,7 +341,7 @@ public class ProjectDependencyAnalysis {
// 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.keySet()) {
- String id = artifact.getGroupId() + ':' +
artifact.getArtifactId();
+ String id = ArtifactKeys.groupArtifactId(artifact);
if (forced.remove(id)) {
used.add(id);
}
diff --git
a/src/test/java/org/apache/maven/shared/dependency/analyzer/ArtifactKeysTest.java
b/src/test/java/org/apache/maven/shared/dependency/analyzer/ArtifactKeysTest.java
new file mode 100644
index 0000000..9abf7c4
--- /dev/null
+++
b/src/test/java/org/apache/maven/shared/dependency/analyzer/ArtifactKeysTest.java
@@ -0,0 +1,38 @@
+/*
+ * 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;
+
+import org.apache.maven.artifact.Artifact;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class ArtifactKeysTest {
+
+ @Test
+ void formatsGroupArtifactId() {
+ Artifact artifact = mock(Artifact.class);
+ when(artifact.getGroupId()).thenReturn("org.example");
+ when(artifact.getArtifactId()).thenReturn("sample");
+
+ assertEquals("org.example:sample",
ArtifactKeys.groupArtifactId(artifact));
+ }
+}
diff --git
a/src/test/java/org/apache/maven/shared/dependency/analyzer/ClassNamesTest.java
b/src/test/java/org/apache/maven/shared/dependency/analyzer/ClassNamesTest.java
new file mode 100644
index 0000000..13c43fe
--- /dev/null
+++
b/src/test/java/org/apache/maven/shared/dependency/analyzer/ClassNamesTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class ClassNamesTest {
+
+ @Test
+ void normalizesInternalName() {
+ assertEquals("org.apache.maven.Foo",
ClassNames.normalizeInternalName("org/apache/maven/Foo"));
+ }
+
+ @Test
+ void stripsInnerClassSuffix() {
+ assertEquals("org.apache.maven.Foo",
ClassNames.normalizeInternalName("org/apache/maven/Foo$Bar$Baz"));
+ }
+
+ @Test
+ void acceptsDottedNamesAndNull() {
+ assertEquals("org.apache.maven.Foo",
ClassNames.normalizeInternalName("org.apache.maven.Foo"));
+ assertNull(ClassNames.normalizeInternalName(null));
+ }
+}