Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/com/ibm/cldk/CodeAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,19 @@ private void analyzeV2() throws Exception {
modules = L1Extractor.extractAllForApplication(
Paths.get(input), applicationId, dependencyDir, cached,
analysisLevel, graphFieldDepth, l3Engine, entrypointReport);
// One qualified name in several files (vendored copies across source roots, #269). The
// output keeps every copy, so this is not a degradation and does not fail --strict; but a
// consumer that keys types by qualified name cannot tell the copies apart, so every run
// says so on stderr, verbose or not.
Map<String, List<String>> duplicates = L1Extractor.duplicateTypeNames(modules);
if (!duplicates.isEmpty()) {
Map.Entry<String, List<String>> first = duplicates.entrySet().iterator().next();
System.err.println("warning: " + duplicates.size() + " qualified type name(s) are"
+ " declared in more than one file (e.g. " + first.getKey() + " in "
+ String.join(", ", first.getValue()) + "). Each copy is kept; a reference"
+ " binds to the copy in its own source root when there is one. A consumer"
+ " that keys types by qualified name cannot tell the copies apart.");
}
// The WALA L3 engine needs the RTA build; --no-rta suppresses it, so a level-3 wala run with
// --no-rta would silently carry no overlays. Warn rather than degrade without a signal.
if (analysisLevel >= 3 && noRta && "wala".equalsIgnoreCase(l3Engine)) {
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/com/ibm/cldk/neo4j/V2GraphProjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.ibm.cldk.neo4j.GraphRows.NodeRef;
import com.ibm.cldk.schema.Analysis;
import com.ibm.cldk.schema.CanId;
import com.ibm.cldk.schema.FqnIndex;
import com.ibm.cldk.schema.JApplication;
import com.ibm.cldk.schema.JArtifact;
import com.ibm.cldk.schema.JBodyNode;
Expand Down Expand Up @@ -117,8 +118,10 @@ public static GraphRows project(Analysis analysis, String appName) {

// First pass: an in-project index from a type's qualified (dotted) name to its node id and
// owning module id, for resolving extends/implements/import spellings to emitted nodes.
Map<String, String> typeIdByFqn = new LinkedHashMap<>();
Map<String, String> moduleIdByFqn = new LinkedHashMap<>();
// A name may be declared by several types (vendored copies across source roots, #269): each
// reference resolves to the copy nearest the referring module.
FqnIndex typeIdByFqn = new FqnIndex();
FqnIndex moduleIdByFqn = new FqnIndex();
for (Map.Entry<String, JModule> m : symbolTable.entrySet()) {
indexTypes(m.getValue(), m.getValue().getTypes(), null, typeIdByFqn, moduleIdByFqn);
}
Expand Down Expand Up @@ -204,7 +207,7 @@ public static GraphRows project(Analysis analysis, String appName) {
// ------------------------------------------------------------------------------------------

private static void indexTypes(JModule module, Map<String, JType> types, String qualifierFqn,
Map<String, String> typeIdByFqn, Map<String, String> moduleIdByFqn) {
FqnIndex typeIdByFqn, FqnIndex moduleIdByFqn) {
for (Map.Entry<String, JType> e : types.entrySet()) {
String fqn = qualifierFqn != null
? qualifierFqn + "." + e.getKey()
Expand All @@ -221,7 +224,7 @@ private static void indexTypes(JModule module, Map<String, JType> types, String
}

private static void projectType(RowBuilder b, NodeRef parent, String containmentRel, String name,
JType type, JModule module, String fileKey, Map<String, String> typeIdByFqn) {
JType type, JModule module, String fileKey, FqnIndex typeIdByFqn) {
List<String> labels = type.isEntrypointClass()
? Arrays.asList("JSymbol", "JType", "JEntrypoint")
: SYMBOL_TYPE;
Expand All @@ -245,13 +248,13 @@ private static void projectType(RowBuilder b, NodeRef parent, String containment

String pkg = module.getPackageName();
for (String base : type.getBaseTypes()) {
String target = resolveType(base, pkg, typeIdByFqn);
String target = resolveType(base, pkg, typeIdByFqn, module.getId());
if (target != null) {
b.edgeToSymbol("J_EXTENDS", ref, target);
}
}
for (String iface : type.getInterfaces()) {
String target = resolveType(iface, pkg, typeIdByFqn);
String target = resolveType(iface, pkg, typeIdByFqn, module.getId());
if (target != null) {
b.edgeToSymbol("J_IMPLEMENTS", ref, target);
}
Expand Down Expand Up @@ -320,7 +323,7 @@ private static void projectField(RowBuilder b, NodeRef owner, JField field, Stri
// ------------------------------------------------------------------------------------------

private static void projectCallable(RowBuilder b, NodeRef owner, String signature, JCallable c,
JModule module, String fileKey, Map<String, String> typeIdByFqn) {
JModule module, String fileKey, FqnIndex typeIdByFqn) {
List<String> labels = c.isEntrypoint()
? Arrays.asList("JSymbol", "JCallable", "JEntrypoint")
: SYMBOL_CALLABLE;
Expand Down Expand Up @@ -608,7 +611,7 @@ private static boolean isLockArtifact(String path) {
// ------------------------------------------------------------------------------------------

private static void projectImports(RowBuilder b, NodeRef mod, JModule module,
Map<String, String> typeIdByFqn, Map<String, String> moduleIdByFqn) {
FqnIndex typeIdByFqn, FqnIndex moduleIdByFqn) {
// Aggregate per target so one edge carries every spelling importing it.
Map<String, List<JImport>> byTarget = new LinkedHashMap<>();
Map<String, NodeRef> refByTarget = new LinkedHashMap<>();
Expand All @@ -617,7 +620,7 @@ private static void projectImports(RowBuilder b, NodeRef mod, JModule module,
if (path == null || path.isEmpty()) {
continue;
}
String targetModule = imp.isWildcard() ? null : moduleIdByFqn.get(path);
String targetModule = imp.isWildcard() ? null : moduleIdByFqn.nearest(path, module.getId());
String targetKey;
NodeRef target;
if (targetModule != null) {
Expand Down Expand Up @@ -680,16 +683,16 @@ private static void annotate(RowBuilder b, NodeRef owner, List<JDecorator> decor
* in-project type id, trying the spelling as-is then package-qualified. Unresolved spellings
* stay as the string fallback on the node's {@code base_types}/{@code interfaces} props.
*/
private static String resolveType(String spelling, String pkg, Map<String, String> typeIdByFqn) {
private static String resolveType(String spelling, String pkg, FqnIndex typeIdByFqn, String near) {
if (spelling == null || spelling.isEmpty()) {
return null;
}
String direct = typeIdByFqn.get(spelling);
String direct = typeIdByFqn.nearest(spelling, near);
if (direct != null) {
return direct;
}
if (pkg != null && !pkg.isEmpty()) {
return typeIdByFqn.get(pkg + "." + spelling);
return typeIdByFqn.nearest(pkg + "." + spelling, near);
}
return null;
}
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/com/ibm/cldk/schema/FqnIndex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.ibm.cldk.schema;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Node ids by qualified (or binary) type name, where one name may be declared by several types.
*
* <p>Vendored copies of one library across source roots declare the same name in several files
* (#269). A reference to such a name binds to the copy nearest the referring node: the one whose id
* shares the most leading {@code /}-separated segments with it, which puts the referrer's own source
* root first. Ties, and lookups with no referrer, fall back to id order, so the choice is always
* deterministic rather than an artifact of insertion order.
*/
public final class FqnIndex {
private final Map<String, List<String>> ids = new HashMap<>();
private boolean sorted = true;

/** Record that {@code id} declares {@code name}. */
public void put(String name, String id) {
ids.computeIfAbsent(name, k -> new ArrayList<>()).add(id);
sorted = false;
}

public boolean containsKey(String name) {
return ids.containsKey(name);
}

/** Every id declaring {@code name}, nearest {@code near} first ({@code near} may be null); read-only. */
public List<String> ordered(String name, String near) {
sortOnce();
List<String> found = ids.get(name);
if (found == null) {
return List.of();
}
if (near == null || found.size() < 2) {
return Collections.unmodifiableList(found);
}
List<String> ordered = new ArrayList<>(found);
ordered.sort(Comparator.comparingInt((String id) -> -commonSegments(id, near))
.thenComparing(Comparator.naturalOrder()));
return ordered;
}

/** The id declaring {@code name} nearest {@code near}, or null when none does. */
public String nearest(String name, String near) {
List<String> ordered = ordered(name, near);
return ordered.isEmpty() ? null : ordered.get(0);
}

private void sortOnce() {
if (!sorted) {
ids.values().forEach(Collections::sort);
sorted = true;
}
}

/** How many leading {@code /}-separated segments two ids (or two relative paths) share. */
public static int commonSegments(String a, String b) {
String[] x = a.split("/");
String[] y = b.split("/");
int n = 0;
while (n < x.length && n < y.length && x[n].equals(y[n])) {
n++;
}
return n;
}
}
Loading