|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2018 Microsoft Corporation and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Microsoft Corporation - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | + |
| 12 | +package com.microsoft.jdtls.ext.core; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.HashSet; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Set; |
| 18 | + |
| 19 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 20 | +import org.eclipse.jdt.core.Flags; |
| 21 | +import org.eclipse.jdt.core.ICompilationUnit; |
| 22 | +import org.eclipse.jdt.core.IImportDeclaration; |
| 23 | +import org.eclipse.jdt.core.IJavaProject; |
| 24 | +import org.eclipse.jdt.core.IPackageFragmentRoot; |
| 25 | +import org.eclipse.jdt.ls.core.internal.JDTUtils; |
| 26 | + |
| 27 | +import com.microsoft.jdtls.ext.core.model.FileImportsResult; |
| 28 | +import com.microsoft.jdtls.ext.core.model.FileImportsResult.ImportEntry; |
| 29 | +import com.microsoft.jdtls.ext.core.model.FileImportsResult.StaticImportEntry; |
| 30 | + |
| 31 | +/** |
| 32 | + * Lightweight command handler for AI context tools. |
| 33 | + * All methods in this class are designed to be non-blocking and fast (< 10ms). |
| 34 | + * They only read AST-level information and do NOT trigger classpath resolution, |
| 35 | + * type resolution, or any expensive JDT operations. |
| 36 | + */ |
| 37 | +public class AiContextCommand { |
| 38 | + |
| 39 | + // Well-known JDK package prefixes |
| 40 | + private static final Set<String> JDK_PREFIXES = new HashSet<>(); |
| 41 | + static { |
| 42 | + JDK_PREFIXES.add("java."); |
| 43 | + JDK_PREFIXES.add("javax."); |
| 44 | + JDK_PREFIXES.add("jdk."); |
| 45 | + JDK_PREFIXES.add("sun."); |
| 46 | + JDK_PREFIXES.add("com.sun."); |
| 47 | + JDK_PREFIXES.add("org.xml."); |
| 48 | + JDK_PREFIXES.add("org.w3c."); |
| 49 | + JDK_PREFIXES.add("jakarta."); // Jakarta EE (post Java EE) |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Get the classified import list of a Java file. |
| 54 | + * This is a lightweight AST-only operation — it reads import declarations |
| 55 | + * without doing any type resolution (findType) or classpath resolution. |
| 56 | + * |
| 57 | + * Typical response time: < 5ms |
| 58 | + * |
| 59 | + * @param arguments List containing the file URI as the first element |
| 60 | + * @param monitor Progress monitor for cancellation support |
| 61 | + * @return FileImportsResult with classified imports |
| 62 | + */ |
| 63 | + public static FileImportsResult getFileImports(List<Object> arguments, IProgressMonitor monitor) { |
| 64 | + FileImportsResult result = new FileImportsResult(); |
| 65 | + result.imports = new ArrayList<>(); |
| 66 | + result.staticImports = new ArrayList<>(); |
| 67 | + |
| 68 | + if (arguments == null || arguments.isEmpty()) { |
| 69 | + result.error = "No arguments provided"; |
| 70 | + return result; |
| 71 | + } |
| 72 | + |
| 73 | + try { |
| 74 | + String fileUri = (String) arguments.get(0); |
| 75 | + if (fileUri == null || fileUri.trim().isEmpty()) { |
| 76 | + result.error = "Invalid file URI"; |
| 77 | + return result; |
| 78 | + } |
| 79 | + |
| 80 | + // Resolve compilation unit from URI — this is fast, just a model lookup |
| 81 | + java.net.URI uri = JDTUtils.toURI(fileUri); |
| 82 | + ICompilationUnit compilationUnit = JDTUtils.resolveCompilationUnit(uri); |
| 83 | + |
| 84 | + if (compilationUnit == null || !compilationUnit.exists()) { |
| 85 | + result.error = "File not found or not a Java file: " + fileUri; |
| 86 | + return result; |
| 87 | + } |
| 88 | + |
| 89 | + // Get project-relative file path (strip the leading project segment |
| 90 | + // from the Eclipse workspace-relative path, e.g. "/my-project/src/Foo.java" → "src/Foo.java") |
| 91 | + IJavaProject javaProject = compilationUnit.getJavaProject(); |
| 92 | + result.file = compilationUnit.getPath().removeFirstSegments(1).toString(); |
| 93 | + |
| 94 | + // Collect project source package names for classification |
| 95 | + Set<String> projectPackages = collectProjectPackages(javaProject); |
| 96 | + |
| 97 | + // Read import declarations — pure AST operation, no type resolution |
| 98 | + IImportDeclaration[] imports = compilationUnit.getImports(); |
| 99 | + if (imports == null || imports.length == 0) { |
| 100 | + return result; // No imports, return empty (not an error) |
| 101 | + } |
| 102 | + |
| 103 | + for (IImportDeclaration imp : imports) { |
| 104 | + if (monitor.isCanceled()) { |
| 105 | + break; |
| 106 | + } |
| 107 | + |
| 108 | + String name = imp.getElementName(); |
| 109 | + boolean isStatic = Flags.isStatic(imp.getFlags()); |
| 110 | + boolean isOnDemand = name.endsWith(".*"); |
| 111 | + |
| 112 | + if (isStatic) { |
| 113 | + StaticImportEntry entry = new StaticImportEntry(); |
| 114 | + entry.name = name; |
| 115 | + entry.memberKind = "unknown"; // Would need findType to know — skip |
| 116 | + entry.source = classifyByPackageName(name, projectPackages); |
| 117 | + result.staticImports.add(entry); |
| 118 | + } else { |
| 119 | + ImportEntry entry = new ImportEntry(); |
| 120 | + entry.name = name; |
| 121 | + entry.kind = "unknown"; // Would need findType to know — skip |
| 122 | + entry.source = classifyByPackageName(name, projectPackages); |
| 123 | + entry.artifact = null; // Would need classpath attributes — skip for now |
| 124 | + entry.isOnDemand = isOnDemand; |
| 125 | + result.imports.add(entry); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return result; |
| 130 | + |
| 131 | + } catch (Exception e) { |
| 132 | + JdtlsExtActivator.logException("Error in getFileImports", e); |
| 133 | + result.error = "Exception: " + e.getMessage(); |
| 134 | + return result; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Classify an import by its package name prefix. |
| 140 | + * This is a heuristic — no type resolution involved. |
| 141 | + * |
| 142 | + * @param qualifiedName the fully qualified name of the import |
| 143 | + * @param projectPackages set of package names found in the project's source roots |
| 144 | + * @return "jdk", "project", or "external" |
| 145 | + */ |
| 146 | + private static String classifyByPackageName(String qualifiedName, Set<String> projectPackages) { |
| 147 | + // Check JDK |
| 148 | + for (String prefix : JDK_PREFIXES) { |
| 149 | + if (qualifiedName.startsWith(prefix)) { |
| 150 | + return "jdk"; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // Check project packages |
| 155 | + String packageName = getPackageName(qualifiedName); |
| 156 | + if (packageName != null && projectPackages.contains(packageName)) { |
| 157 | + return "project"; |
| 158 | + } |
| 159 | + |
| 160 | + // Check if any project package is a prefix of this import |
| 161 | + for (String projPkg : projectPackages) { |
| 162 | + if (qualifiedName.startsWith(projPkg + ".")) { |
| 163 | + return "project"; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return "external"; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Get the package name from a fully qualified name. |
| 172 | + * e.g., "com.example.model.Order" → "com.example.model" |
| 173 | + * "com.example.model.*" → "com.example.model" |
| 174 | + */ |
| 175 | + private static String getPackageName(String qualifiedName) { |
| 176 | + if (qualifiedName == null) { |
| 177 | + return null; |
| 178 | + } |
| 179 | + // Handle wildcard imports |
| 180 | + if (qualifiedName.endsWith(".*")) { |
| 181 | + return qualifiedName.substring(0, qualifiedName.length() - 2); |
| 182 | + } |
| 183 | + int lastDot = qualifiedName.lastIndexOf('.'); |
| 184 | + if (lastDot > 0) { |
| 185 | + return qualifiedName.substring(0, lastDot); |
| 186 | + } |
| 187 | + return null; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Collect all package names that exist in the project's source roots. |
| 192 | + * This uses getPackageFragmentRoots(K_SOURCE) which is fast — it reads |
| 193 | + * the project model, not the filesystem. |
| 194 | + */ |
| 195 | + private static Set<String> collectProjectPackages(IJavaProject javaProject) { |
| 196 | + Set<String> packages = new HashSet<>(); |
| 197 | + if (javaProject == null) { |
| 198 | + return packages; |
| 199 | + } |
| 200 | + |
| 201 | + try { |
| 202 | + IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots(); |
| 203 | + for (IPackageFragmentRoot root : roots) { |
| 204 | + if (root.getKind() == IPackageFragmentRoot.K_SOURCE) { |
| 205 | + org.eclipse.jdt.core.IJavaElement[] children = root.getChildren(); |
| 206 | + for (org.eclipse.jdt.core.IJavaElement child : children) { |
| 207 | + if (child instanceof org.eclipse.jdt.core.IPackageFragment) { |
| 208 | + String pkgName = child.getElementName(); |
| 209 | + if (pkgName != null && !pkgName.isEmpty()) { |
| 210 | + packages.add(pkgName); |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + } catch (Exception e) { |
| 217 | + // Non-critical — fall back to treating everything as external |
| 218 | + JdtlsExtActivator.logException("Error collecting project packages", e); |
| 219 | + } |
| 220 | + |
| 221 | + return packages; |
| 222 | + } |
| 223 | +} |
0 commit comments