|
| 1 | +package javaextractor |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/enola-labs/enola/internal/facts" |
| 11 | +) |
| 12 | + |
| 13 | +// JavaExtractor extracts architectural facts from Java source code using |
| 14 | +// tree-sitter AST parsing (see java_ast.go for the walker and spring.go for |
| 15 | +// Spring/JPA/Dubbo framework specialization). |
| 16 | +type JavaExtractor struct{} |
| 17 | + |
| 18 | +// New creates a new JavaExtractor. |
| 19 | +func New() *JavaExtractor { |
| 20 | + return &JavaExtractor{} |
| 21 | +} |
| 22 | + |
| 23 | +func (e *JavaExtractor) Name() string { |
| 24 | + return "java" |
| 25 | +} |
| 26 | + |
| 27 | +// Detect returns true if the repository looks like a Java project: a Maven project |
| 28 | +// (pom.xml), or any actual .java source file. A Gradle build file alone is not |
| 29 | +// sufficient — Gradle is equally used by Kotlin, Android, and Groovy projects, so |
| 30 | +// detecting on it would wrongly claim pure-Kotlin repos. Requiring real .java |
| 31 | +// sources keeps the Java extractor off non-Java JVM projects. |
| 32 | +func (e *JavaExtractor) Detect(repoPath string) (bool, error) { |
| 33 | + if _, err := os.Stat(filepath.Join(repoPath, "pom.xml")); err == nil { |
| 34 | + return true, nil |
| 35 | + } |
| 36 | + return containsJavaSource(repoPath, 8), nil |
| 37 | +} |
| 38 | + |
| 39 | +// Extract parses Java files and emits architectural facts. |
| 40 | +// |
| 41 | +// Two passes: pass 1 walks each file's AST (extractFileAST) to emit declaration, |
| 42 | +// import, route, storage and call-graph facts while indexing every declared type by |
| 43 | +// its fully-qualified name. Pass 2 (canonicalizeTargets) rewrites type-reference |
| 44 | +// edge targets (implements/instantiates/injects) and import targets from FQNs to |
| 45 | +// canonical "<dir>.<Type>" / module-dir names so reverse traversal connects |
| 46 | +// dependents. Module facts are emitted per directory. |
| 47 | +func (e *JavaExtractor) Extract(ctx context.Context, repoPath string, files []string) ([]facts.Fact, error) { |
| 48 | + var allFacts []facts.Fact |
| 49 | + modules := make(map[string]bool) |
| 50 | + |
| 51 | + for _, relFile := range files { |
| 52 | + select { |
| 53 | + case <-ctx.Done(): |
| 54 | + return allFacts, ctx.Err() |
| 55 | + default: |
| 56 | + } |
| 57 | + |
| 58 | + if !isJavaFile(relFile) { |
| 59 | + continue |
| 60 | + } |
| 61 | + |
| 62 | + absFile := filepath.Join(repoPath, relFile) |
| 63 | + src, err := os.ReadFile(absFile) |
| 64 | + if err != nil { |
| 65 | + log.Printf("[java-extractor] error reading %s: %v", relFile, err) |
| 66 | + continue |
| 67 | + } |
| 68 | + |
| 69 | + allFacts = append(allFacts, extractFileAST(src, relFile)...) |
| 70 | + modules[filepath.Dir(relFile)] = true |
| 71 | + } |
| 72 | + |
| 73 | + canonicalizeTargets(allFacts) |
| 74 | + resolveTableConstants(allFacts) |
| 75 | + |
| 76 | + for dir := range modules { |
| 77 | + allFacts = append(allFacts, facts.Fact{ |
| 78 | + Kind: facts.KindModule, |
| 79 | + Name: dir, |
| 80 | + File: dir, |
| 81 | + Props: map[string]any{ |
| 82 | + "language": "java", |
| 83 | + }, |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | + return allFacts, nil |
| 88 | +} |
| 89 | + |
| 90 | +// canonicalizeTargets resolves FQN-based edge targets to canonical fact names. |
| 91 | +// |
| 92 | +// - implements/instantiates/injects targets that match a declared type's FQN are |
| 93 | +// rewritten to that type's "<dir>.<Type>" fact name; unresolved targets (external |
| 94 | +// libraries) are left as written. |
| 95 | +// - import dependency facts whose target FQN resolves to a declared type — or whose |
| 96 | +// value names a known source package — are marked source="internal" and pointed at |
| 97 | +// the owning module dir. |
| 98 | +func canonicalizeTargets(allFacts []facts.Fact) { |
| 99 | + typeIndex := make(map[string]string) // FQN -> "<dir>.<Type>" canonical name |
| 100 | + typeDir := make(map[string]string) // FQN -> dir |
| 101 | + packageDir := make(map[string]string) |
| 102 | + for _, f := range allFacts { |
| 103 | + if f.Kind != facts.KindSymbol { |
| 104 | + continue |
| 105 | + } |
| 106 | + switch f.Props["symbol_kind"] { |
| 107 | + case facts.SymbolClass, facts.SymbolInterface, facts.SymbolEnum: |
| 108 | + fqn, _ := f.Props["fqn"].(string) |
| 109 | + if fqn == "" { |
| 110 | + continue |
| 111 | + } |
| 112 | + dir := f.File |
| 113 | + if i := strings.LastIndex(dir, "/"); i >= 0 { |
| 114 | + dir = dir[:i] |
| 115 | + } else { |
| 116 | + dir = "." |
| 117 | + } |
| 118 | + typeIndex[fqn] = f.Name |
| 119 | + typeDir[fqn] = dir |
| 120 | + if pkg := parentName(fqn); pkg != "" { |
| 121 | + packageDir[pkg] = dir |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + for i := range allFacts { |
| 127 | + f := &allFacts[i] |
| 128 | + if f.Kind == facts.KindDependency { |
| 129 | + resolveImport(f, typeDir, packageDir) |
| 130 | + continue |
| 131 | + } |
| 132 | + for j := range f.Relations { |
| 133 | + r := &f.Relations[j] |
| 134 | + switch r.Kind { |
| 135 | + case facts.RelImplements, facts.RelInstantiates, facts.RelInjects: |
| 136 | + if canon, ok := typeIndex[r.Target]; ok { |
| 137 | + r.Target = canon |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func resolveImport(f *facts.Fact, typeDir, packageDir map[string]string) { |
| 145 | + imp, _ := f.Props["import"].(string) |
| 146 | + if imp == "" { |
| 147 | + return |
| 148 | + } |
| 149 | + var dir string |
| 150 | + var ok bool |
| 151 | + if dir, ok = typeDir[imp]; !ok { |
| 152 | + // Wildcard / package import (e.g. "com.example.foo"). |
| 153 | + dir, ok = packageDir[imp] |
| 154 | + } |
| 155 | + if !ok { |
| 156 | + return // external dependency |
| 157 | + } |
| 158 | + f.Props["source"] = "internal" |
| 159 | + for j := range f.Relations { |
| 160 | + if f.Relations[j].Kind == facts.RelImports { |
| 161 | + f.Relations[j].Target = dir |
| 162 | + } |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +// resolveTableConstants rewrites storage facts whose "table" prop names a string |
| 167 | +// constant (e.g. @Table(name = ADMIN_SETTINGS_TABLE_NAME)) to that constant's |
| 168 | +// literal value. Constants are indexed by simple name across all files, since the |
| 169 | +// table-name constants typically live in a shared ModelConstants class. When the |
| 170 | +// same simple name maps to conflicting values it is left unresolved (ambiguous). |
| 171 | +func resolveTableConstants(allFacts []facts.Fact) { |
| 172 | + values := make(map[string]string) |
| 173 | + ambiguous := make(map[string]bool) |
| 174 | + for _, f := range allFacts { |
| 175 | + if f.Kind != facts.KindSymbol { |
| 176 | + continue |
| 177 | + } |
| 178 | + v, ok := f.Props["value"].(string) |
| 179 | + if !ok { |
| 180 | + continue |
| 181 | + } |
| 182 | + simple := f.Name |
| 183 | + if i := strings.LastIndex(simple, "."); i >= 0 { |
| 184 | + simple = simple[i+1:] |
| 185 | + } |
| 186 | + if existing, seen := values[simple]; seen && existing != v { |
| 187 | + ambiguous[simple] = true |
| 188 | + continue |
| 189 | + } |
| 190 | + values[simple] = v |
| 191 | + } |
| 192 | + |
| 193 | + for i := range allFacts { |
| 194 | + f := &allFacts[i] |
| 195 | + if f.Kind != facts.KindStorage { |
| 196 | + continue |
| 197 | + } |
| 198 | + tbl, ok := f.Props["table"].(string) |
| 199 | + if !ok { |
| 200 | + continue |
| 201 | + } |
| 202 | + if ambiguous[tbl] { |
| 203 | + continue |
| 204 | + } |
| 205 | + if v, ok := values[tbl]; ok { |
| 206 | + f.Props["table"] = v |
| 207 | + f.Props["table_constant"] = tbl |
| 208 | + } |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +func parentName(fqn string) string { |
| 213 | + if i := strings.LastIndex(fqn, "."); i >= 0 { |
| 214 | + return fqn[:i] |
| 215 | + } |
| 216 | + return "" |
| 217 | +} |
| 218 | + |
| 219 | +func isJavaFile(path string) bool { |
| 220 | + return strings.HasSuffix(strings.ToLower(path), ".java") |
| 221 | +} |
| 222 | + |
| 223 | +// containsJavaSource reports whether any .java file exists under root within |
| 224 | +// maxDepth directory levels. It returns on the first match and skips hidden and |
| 225 | +// common build/dependency directories so it stays cheap on large repos. |
| 226 | +func containsJavaSource(root string, maxDepth int) bool { |
| 227 | + var search func(dir string, depth int) bool |
| 228 | + search = func(dir string, depth int) bool { |
| 229 | + if depth > maxDepth { |
| 230 | + return false |
| 231 | + } |
| 232 | + entries, err := os.ReadDir(dir) |
| 233 | + if err != nil { |
| 234 | + return false |
| 235 | + } |
| 236 | + for _, entry := range entries { |
| 237 | + name := entry.Name() |
| 238 | + if entry.IsDir() { |
| 239 | + if strings.HasPrefix(name, ".") || name == "build" || |
| 240 | + name == "target" || name == "node_modules" { |
| 241 | + continue |
| 242 | + } |
| 243 | + if search(filepath.Join(dir, name), depth+1) { |
| 244 | + return true |
| 245 | + } |
| 246 | + } else if isJavaFile(name) { |
| 247 | + return true |
| 248 | + } |
| 249 | + } |
| 250 | + return false |
| 251 | + } |
| 252 | + return search(root, 0) |
| 253 | +} |
0 commit comments