2929import com .github .javaparser .ast .body .EnumDeclaration ;
3030import com .github .javaparser .ast .body .Parameter ;
3131import com .github .javaparser .ast .body .TypeDeclaration ;
32+ import com .github .javaparser .ast .expr .NameExpr ;
3233import com .github .javaparser .ast .nodeTypes .NodeWithAnnotations ;
3334import com .github .javaparser .ast .nodeTypes .modifiers .NodeWithAccessModifiers ;
3435import com .github .javaparser .ast .type .ArrayType ;
@@ -660,4 +661,64 @@ public Optional<TypeRef> resolveScopeName(String simpleName, Node usageSite) {
660661 return Optional .of (new UnresolvedTypeRef (simpleName ));
661662 }
662663
664+ public Optional <TypeRef > resolveScope (NameExpr scopeExpr , Node usageSite ) {
665+ try {
666+ var valueDecl = scopeExpr .resolve (); // ResolvedValueDeclaration
667+ var valueType = valueDecl .getType (); // ResolvedType
668+ return resolveResolvedTypeToTypeRef (valueType , usageSite );
669+ } catch (Exception e ) {
670+ // fallback(s) below
671+ }
672+
673+ // Fallback 1: sometimes NameExpr is actually a type name used as qualifier (static call)
674+ String name = scopeExpr .getNameAsString ();
675+ TypeDeclaration <?> indexed = getByFqn (name );
676+ if (indexed != null ) return Optional .of (new DeclaredTypeRef (indexed ));
677+
678+ // Fallback 2: return unresolved *type* ref? I'd avoid emitting a dependency to a bare variable name:
679+ return Optional .empty (); // <- prevents "A ..> b"
680+ }
681+
682+ private Optional <TypeRef > resolveResolvedTypeToTypeRef (
683+ ResolvedType rt ,
684+ Node usageSite
685+ ) {
686+ // 1) Primitives → no dependency
687+ if (rt .isPrimitive ()) {
688+ return Optional .empty ();
689+ }
690+
691+ // 2) Void → no dependency
692+ if (rt .isVoid ()) {
693+ return Optional .empty ();
694+ }
695+
696+ // 3) Reference types (classes, interfaces, enums)
697+ if (rt .isReferenceType ()) {
698+ var rrt = rt .asReferenceType ();
699+ var qname = rrt .getQualifiedName (); // dot-qualified, not JVM $
700+
701+ // a) Declared in our parsed sources
702+ TypeDeclaration <?> td = getByFqn (qname );
703+ if (td != null ) {
704+ return Optional .of (new DeclaredTypeRef (td ));
705+ }
706+
707+ // b) Known but external (JDK, libs)
708+ return Optional .of (new ExternalTypeRef (qname ));
709+ }
710+
711+ // 4) Arrays → dependency on component type
712+ if (rt .isArray ()) {
713+ return resolveResolvedTypeToTypeRef (
714+ rt .asArrayType ().getComponentType (),
715+ usageSite
716+ );
717+ }
718+
719+ // 5) Type variables, wildcards, etc.
720+ // Usually not diagram-worthy on their own
721+ return Optional .empty ();
722+ }
723+
663724}
0 commit comments