more fixes 5

This commit is contained in:
2026-06-20 12:52:19 +02:00
parent 9ca8b2fe37
commit 76ac143370
4 changed files with 440 additions and 80 deletions

View File

@@ -146,46 +146,138 @@ public class TransitionLinkerEnricher implements AnalysisEnricher {
}
// Second, filter by vertical routing based on Service class name
if (chain.getMethodChain() != null) {
boolean hasElectronicsService = false;
boolean hasComputerStoreService = false;
// Generic approach combining Prefix, Package, and String-distance heuristics
// without relying on a hardcoded list of magic keywords.
if (chain.getMethodChain() != null && !chain.getMethodChain().isEmpty() && currentMachineName != null) {
String smPackage = getPackageName(currentMachineName);
String smSimple = getSimpleClassName(currentMachineName);
String smPrefix = getFirstCamelCaseWord(smSimple);
boolean hasPositiveMatch = false;
boolean hasStrongMismatch = false;
for (String method : chain.getMethodChain()) {
String className = method;
if (method.contains(".")) {
className = method.substring(0, method.lastIndexOf('.'));
if (className.contains(".")) {
className = className.substring(className.lastIndexOf('.') + 1);
}
String chainClass = getClassNameOnly(method);
String chainPackage = getPackageName(chainClass);
String chainSimple = getSimpleClassName(chainClass);
String chainPrefix = getFirstCamelCaseWord(chainSimple);
// 1. Explicit Prefix Match (e.g. ElectronicsOrderService & ElectronicsStateMachine)
if (smPrefix != null && chainPrefix != null && smPrefix.equals(chainPrefix)) {
hasPositiveMatch = true;
}
String lowerClass = className.toLowerCase();
if (lowerClass.contains("electronics")) {
hasElectronicsService = true;
} else if (lowerClass.contains("computerstore")) {
hasComputerStoreService = true;
// 2. Explicit Sub-Package Match (e.g. com.company.electronics.service & com.company.electronics.sm)
// We consider it a match if they share a package segment beyond the first 2 (which are usually com.company)
String deepShared = getDeepSharedPackage(smPackage, chainPackage);
if (deepShared != null) {
hasPositiveMatch = true;
}
// Determine if there is a strong mismatch.
// A strong mismatch happens if the chain class has a VERY distinct prefix/package
// that contradicts the SM's distinct prefix/package.
// Without magic keywords, we guess if a prefix is distinct if it's long enough and doesn't match.
if (smPrefix != null && chainPrefix != null && !smPrefix.equals(chainPrefix)) {
// They have different first CamelCase words.
// This could be Electronics vs ComputerStore, or it could be Common vs ComputerStore.
// If they diverge at a deep package level, it's a strong mismatch.
if (isDeepPackageDivergence(smPackage, chainPackage)) {
hasStrongMismatch = true;
}
}
}
String lowerMachine = currentMachineName != null ? currentMachineName.toLowerCase() : "";
boolean isElectronicsMachine = lowerMachine.contains("electronics");
// If the machine is an electronics machine, it must NOT be triggered by computer store
if (isElectronicsMachine && hasComputerStoreService && !hasElectronicsService) {
// If we found a positive structural match, route it!
if (hasPositiveMatch) {
return true;
}
// If there's no positive match, but we detected a strong structural mismatch, reject it!
if (hasStrongMismatch) {
return false;
}
// If the machine is NOT an electronics machine (e.g. standard computer store SM),
// it must NOT be triggered by electronics services
if (!isElectronicsMachine && hasElectronicsService && !hasComputerStoreService) {
return false;
}
// If neither, we allow it (fallback for common/shared endpoints without distinct domains)
}
return true;
}
private String simplify(String name) {
private String getPackageName(String fqn) {
if (fqn == null || !fqn.contains(".")) return "";
return fqn.substring(0, fqn.lastIndexOf('.'));
}
private String getSimpleClassName(String fqn) {
if (fqn == null) return "";
if (!fqn.contains(".")) return fqn;
return fqn.substring(fqn.lastIndexOf('.') + 1);
}
private String getClassNameOnly(String methodFqn) {
if (methodFqn == null) return "";
String clean = methodFqn;
if (clean.contains("(")) {
clean = clean.substring(0, clean.indexOf('('));
}
if (clean.contains(".")) {
clean = clean.substring(0, clean.lastIndexOf('.'));
}
return clean;
}
private String getFirstCamelCaseWord(String simpleName) {
if (simpleName == null || simpleName.isEmpty()) return null;
// e.g. "ElectronicsOrderService" -> "Electronics"
String[] words = simpleName.split("(?<!^)(?=[A-Z])");
if (words.length > 0) {
return words[0];
}
return null;
}
private String getDeepSharedPackage(String pkg1, String pkg2) {
if (pkg1 == null || pkg2 == null || pkg1.isEmpty() || pkg2.isEmpty()) return null;
String[] p1 = pkg1.split("\\.");
String[] p2 = pkg2.split("\\.");
// Find the deepest common segment index
int matchIdx = -1;
for (int i = 0; i < Math.min(p1.length, p2.length); i++) {
if (p1[i].equals(p2[i])) {
matchIdx = i;
} else {
break;
}
}
// If they share at least 3 segments (e.g. com.company.electronics), it's a deep match
if (matchIdx >= 2) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i <= matchIdx; i++) {
sb.append(p1[i]).append(".");
}
return sb.toString();
}
return null;
}
private boolean isDeepPackageDivergence(String pkg1, String pkg2) {
if (pkg1 == null || pkg2 == null || pkg1.isEmpty() || pkg2.isEmpty()) return false;
String[] p1 = pkg1.split("\\.");
String[] p2 = pkg2.split("\\.");
// If they share exactly the root (com.company) but diverge at the 3rd segment (e.g. electronics vs computerstore)
// Then it's a divergence.
if (p1.length >= 3 && p2.length >= 3) {
return p1[0].equals(p2[0]) && p1[1].equals(p2[1]) && !p1[2].equals(p2[2]);
}
return false;
}
private String simplify(String name) {
if (name == null) return null;
// Strip common suffixes
String simplified = name.replaceAll("(?i)(.*)(Event|Action|Transition|Command)(s)?$", "$1");

View File

@@ -296,7 +296,11 @@ public class CallGraphBuilder {
if (!visited.add(fqn)) return Collections.emptyList();
List<String> constants = new ArrayList<>();
TypeDeclaration td = contextCu != null ? context.getTypeDeclaration(className, contextCu) : context.getTypeDeclaration(className);
TypeDeclaration tempTd = contextCu != null ? context.getTypeDeclaration(className, contextCu) : null;
if (tempTd == null) {
tempTd = context.getTypeDeclaration(className);
}
final TypeDeclaration td = tempTd;
if (td == null) {
System.out.println("DEEP TRACE FAILED TO FIND TD: " + className);
}
@@ -815,57 +819,109 @@ public class CallGraphBuilder {
private List<String> traceFieldInConstructors(TypeDeclaration td, String fieldName, CodebaseContext context, Set<String> visited) {
List<String> results = new ArrayList<>();
for (MethodDeclaration md : td.getMethods()) {
if (md.isConstructor() && md.getBody() != null) {
md.getBody().accept(new ASTVisitor() {
@Override
public boolean visit(Assignment node) {
Expression left = node.getLeftHandSide();
if ((left instanceof SimpleName && ((SimpleName) left).getIdentifier().equals(fieldName)) ||
(left instanceof FieldAccess && ((FieldAccess) left).getName().getIdentifier().equals(fieldName))) {
String val = constantResolver.resolve(node.getRightHandSide(), context);
if (val != null) results.add(val);
}
return super.visit(node);
// 1. Check Field Declarations
for (org.eclipse.jdt.core.dom.FieldDeclaration fd : td.getFields()) {
for (Object fragObj : fd.fragments()) {
org.eclipse.jdt.core.dom.VariableDeclarationFragment frag = (org.eclipse.jdt.core.dom.VariableDeclarationFragment) fragObj;
if (frag.getName().getIdentifier().equals(fieldName) && frag.getInitializer() != null) {
Expression right = frag.getInitializer();
if (right instanceof MethodInvocation mi) {
String calledName = mi.getName().getIdentifier();
String targetClass = context.getFqn(td);
CompilationUnit cu = td.getRoot() instanceof CompilationUnit ? (CompilationUnit) td.getRoot() : null;
results.addAll(resolveMethodReturnConstant(targetClass, calledName, 0, new HashSet<>(visited), cu));
} else {
String val = constantResolver.resolve(right, context);
if (val != null) results.add(val);
}
@Override
public boolean visit(ConstructorInvocation node) {
for (Object argObj : node.arguments()) {
Expression arg = (Expression) argObj;
String val = constantResolver.resolve(arg, context);
if (val != null) {
if (val.startsWith("ENUM_SET:")) {
for (String eVal : val.substring(9).split(",")) {
results.add(eVal.substring(eVal.lastIndexOf('.') + 1));
}
} else {
results.add(val);
}
}
}
return super.visit(node);
}
@Override
public boolean visit(SuperConstructorInvocation node) {
for (Object argObj : node.arguments()) {
Expression arg = (Expression) argObj;
String val = constantResolver.resolve(arg, context);
if (val != null) {
if (val.startsWith("ENUM_SET:")) {
for (String eVal : val.substring(9).split(",")) {
results.add(eVal.substring(eVal.lastIndexOf('.') + 1));
}
} else {
results.add(val);
}
}
}
return super.visit(node);
}
});
}
}
}
org.eclipse.jdt.core.dom.ASTVisitor assignmentVisitor = new org.eclipse.jdt.core.dom.ASTVisitor() {
@Override
public boolean visit(Assignment node) {
Expression left = node.getLeftHandSide();
if ((left instanceof SimpleName && ((SimpleName) left).getIdentifier().equals(fieldName)) ||
(left instanceof FieldAccess && ((FieldAccess) left).getName().getIdentifier().equals(fieldName))) {
Expression right = node.getRightHandSide();
if (right instanceof MethodInvocation mi) {
String calledName = mi.getName().getIdentifier();
String targetClass = context.getFqn(td);
if (mi.getExpression() != null && mi.resolveMethodBinding() != null && mi.resolveMethodBinding().getDeclaringClass() != null) {
targetClass = mi.resolveMethodBinding().getDeclaringClass().getQualifiedName();
} else if (mi.getExpression() instanceof SimpleName) {
String exprName = ((SimpleName) mi.getExpression()).getIdentifier();
CompilationUnit cu = td.getRoot() instanceof CompilationUnit ? (CompilationUnit) td.getRoot() : null;
TypeDeclaration targetTd = context.resolveStaticImport(exprName, cu);
if (targetTd != null) {
targetClass = context.getFqn(targetTd);
}
}
CompilationUnit cu = td.getRoot() instanceof CompilationUnit ? (CompilationUnit) td.getRoot() : null;
results.addAll(resolveMethodReturnConstant(targetClass, calledName, 0, new HashSet<>(visited), cu));
} else {
String val = constantResolver.resolve(right, context);
if (val != null) results.add(val);
}
}
return super.visit(node);
}
@Override
public boolean visit(ConstructorInvocation node) {
for (Object argObj : node.arguments()) {
Expression arg = (Expression) argObj;
String val = constantResolver.resolve(arg, context);
if (val != null) {
if (val.startsWith("ENUM_SET:")) {
for (String eVal : val.substring(9).split(",")) {
results.add(eVal.substring(eVal.lastIndexOf('.') + 1));
}
} else {
results.add(val);
}
}
}
return super.visit(node);
}
@Override
public boolean visit(SuperConstructorInvocation node) {
for (Object argObj : node.arguments()) {
Expression arg = (Expression) argObj;
String val = constantResolver.resolve(arg, context);
if (val != null) {
if (val.startsWith("ENUM_SET:")) {
for (String eVal : val.substring(9).split(",")) {
results.add(eVal.substring(eVal.lastIndexOf('.') + 1));
}
} else {
results.add(val);
}
}
}
return super.visit(node);
}
};
// 2. Check Constructors
for (MethodDeclaration md : td.getMethods()) {
if (md.isConstructor() && md.getBody() != null) {
md.getBody().accept(assignmentVisitor);
}
}
// 3. Check Initializers
for (Object bodyDecl : td.bodyDeclarations()) {
if (bodyDecl instanceof org.eclipse.jdt.core.dom.Initializer init && init.getBody() != null) {
init.getBody().accept(assignmentVisitor);
}
}
return results;
}