|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Red Hat Inc. and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * which accompanies this distribution, and is available at |
| 5 | + * http://www.eclipse.org/legal/epl-v20.html |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + *******************************************************************************/ |
| 9 | +package com.redhat.qute.jdt; |
| 10 | + |
| 11 | +import java.util.logging.Level; |
| 12 | +import java.util.logging.Logger; |
| 13 | + |
| 14 | +import org.eclipse.core.resources.IProject; |
| 15 | +import org.eclipse.core.resources.IWorkspaceRoot; |
| 16 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 17 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 18 | +import org.eclipse.jdt.core.IAnnotation; |
| 19 | +import org.eclipse.jdt.core.ICompilationUnit; |
| 20 | +import org.eclipse.jdt.core.IJavaProject; |
| 21 | +import org.eclipse.jdt.core.IMethod; |
| 22 | +import org.eclipse.jdt.core.ISourceRange; |
| 23 | +import org.eclipse.jdt.core.IType; |
| 24 | +import org.eclipse.jdt.core.JavaCore; |
| 25 | +import org.eclipse.jdt.core.JavaModelException; |
| 26 | +import org.eclipse.jdt.core.dom.AST; |
| 27 | +import org.eclipse.jdt.core.dom.ASTParser; |
| 28 | +import org.eclipse.jdt.core.dom.CompilationUnit; |
| 29 | + |
| 30 | +import com.redhat.qute.jdt.debug.JavaSourceLocationArguments; |
| 31 | +import com.redhat.qute.jdt.debug.JavaSourceLocationResponse; |
| 32 | +import com.redhat.qute.jdt.utils.IJDTUtils; |
| 33 | + |
| 34 | +/** |
| 35 | + * JDT-side implementation for resolving Java source locations referenced from |
| 36 | + * Qute templates. |
| 37 | + * |
| 38 | + * Supports: - type resolution - method resolution - annotation-based template |
| 39 | + * resolution (@TemplateContents) - text blocks (""" """) |
| 40 | + */ |
| 41 | +public class QuteSupportForDebug { |
| 42 | + |
| 43 | + private static final Logger LOGGER = Logger.getLogger(QuteSupportForDebug.class.getName()); |
| 44 | + |
| 45 | + private static final QuteSupportForDebug INSTANCE = new QuteSupportForDebug(); |
| 46 | + |
| 47 | + public static QuteSupportForDebug getInstance() { |
| 48 | + return INSTANCE; |
| 49 | + } |
| 50 | + |
| 51 | + public JavaSourceLocationResponse resolveJavaSource(JavaSourceLocationArguments args, IJDTUtils utils, |
| 52 | + IProgressMonitor monitor) { |
| 53 | + |
| 54 | + try { |
| 55 | + |
| 56 | + IType type = findType(args.getTypeName(), monitor); |
| 57 | + if (type == null) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + ICompilationUnit cu = type.getCompilationUnit(); |
| 62 | + if (cu == null) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + String javaFileUri = utils.toUri(cu); |
| 67 | + |
| 68 | + // 1️⃣ Annotation-based template (highest priority) |
| 69 | + if (args.getAnnotation() != null) { |
| 70 | + IAnnotation annotation = findAnnotation(type, args.getAnnotation()); |
| 71 | + |
| 72 | + if (annotation != null) { |
| 73 | + int offset = computeTemplateContentOffset(cu, annotation); |
| 74 | + int startLine = computeLine(cu, offset); |
| 75 | + return new JavaSourceLocationResponse(javaFileUri, startLine); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // 2️⃣ Method-based resolution |
| 80 | + if (args.getMethod() != null) { |
| 81 | + IMethod method = findMethod(type, args.getMethod()); |
| 82 | + if (method != null) { |
| 83 | + int offset = method.getNameRange().getOffset(); |
| 84 | + int startLine = computeLine(cu, offset); |
| 85 | + return new JavaSourceLocationResponse(javaFileUri, startLine); |
| 86 | + } |
| 87 | + } |
| 88 | + return null; |
| 89 | + |
| 90 | + } catch (Exception e) { |
| 91 | + LOGGER.log(Level.SEVERE, "Failed to resolve Java source location", e); |
| 92 | + } |
| 93 | + |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + /* ---------------------------------------------------------------------- */ |
| 98 | + /* Resolution helpers */ |
| 99 | + /* ---------------------------------------------------------------------- */ |
| 100 | + |
| 101 | + private static IType findType(String className, IProgressMonitor monitor) { |
| 102 | + try { |
| 103 | + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); |
| 104 | + |
| 105 | + for (IProject project : root.getProjects()) { |
| 106 | + if (!project.isOpen() || !project.hasNature(JavaCore.NATURE_ID)) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + IJavaProject javaProject = JavaCore.create(project); |
| 111 | + |
| 112 | + IType type = javaProject.findType(className, monitor); |
| 113 | + if (type != null) { |
| 114 | + return type; |
| 115 | + } |
| 116 | + |
| 117 | + // Fallback java.lang |
| 118 | + if (!className.contains(".")) { |
| 119 | + type = javaProject.findType("java.lang." + className, monitor); |
| 120 | + if (type != null) { |
| 121 | + return type; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } catch (Exception e) { |
| 126 | + LOGGER.log(Level.SEVERE, "Error while finding type '" + className + "'", e); |
| 127 | + } |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + private static IMethod findMethod(IType type, String methodName) throws JavaModelException { |
| 132 | + |
| 133 | + for (IMethod method : type.getMethods()) { |
| 134 | + if (method.getElementName().equals(methodName)) { |
| 135 | + return method; |
| 136 | + } |
| 137 | + } |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + private static IAnnotation findAnnotation(IType type, String annotationFqn) throws JavaModelException { |
| 142 | + |
| 143 | + String simpleName = simpleName(annotationFqn); |
| 144 | + |
| 145 | + for (IAnnotation ann : type.getAnnotations()) { |
| 146 | + if (ann.getElementName().equals(simpleName)) { |
| 147 | + return ann; |
| 148 | + } |
| 149 | + } |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + private static String simpleName(String fqn) { |
| 154 | + int idx = fqn.lastIndexOf('.'); |
| 155 | + return idx == -1 ? fqn : fqn.substring(idx + 1); |
| 156 | + } |
| 157 | + |
| 158 | + /* ---------------------------------------------------------------------- */ |
| 159 | + /* Offset & line computation */ |
| 160 | + /* ---------------------------------------------------------------------- */ |
| 161 | + |
| 162 | + private static int computeTemplateContentOffset(ICompilationUnit cu, IAnnotation annotation) |
| 163 | + throws JavaModelException { |
| 164 | + |
| 165 | + ISourceRange range = annotation.getSourceRange(); |
| 166 | + String source = cu.getSource(); |
| 167 | + |
| 168 | + int start = range.getOffset(); |
| 169 | + int end = start + range.getLength(); |
| 170 | + |
| 171 | + String annotationSource = source.substring(start, end); |
| 172 | + |
| 173 | + int paren = annotationSource.indexOf('('); |
| 174 | + if (paren == -1) { |
| 175 | + return start; |
| 176 | + } |
| 177 | + |
| 178 | + int offset = start + paren + 1; |
| 179 | + |
| 180 | + // Skip whitespace |
| 181 | + while (offset < source.length() && Character.isWhitespace(source.charAt(offset))) { |
| 182 | + offset++; |
| 183 | + } |
| 184 | + |
| 185 | + // Text block |
| 186 | + if (source.startsWith("\"\"\"", offset)) { |
| 187 | + return offset + 3; |
| 188 | + } |
| 189 | + |
| 190 | + // Classic string |
| 191 | + if (source.charAt(offset) == '"') { |
| 192 | + return offset + 1; |
| 193 | + } |
| 194 | + |
| 195 | + return offset; |
| 196 | + } |
| 197 | + |
| 198 | + private static int computeLine(ICompilationUnit cu, int offset) throws JavaModelException { |
| 199 | + |
| 200 | + CompilationUnit ast = parse(cu); |
| 201 | + return ast.getLineNumber(offset) - 1; // DAP is 0-based |
| 202 | + } |
| 203 | + |
| 204 | + private static CompilationUnit parse(ICompilationUnit cu) { |
| 205 | + ASTParser parser = ASTParser.newParser(AST.JLS_Latest); |
| 206 | + parser.setSource(cu); |
| 207 | + parser.setResolveBindings(false); |
| 208 | + return (CompilationUnit) parser.createAST(null); |
| 209 | + } |
| 210 | +} |
0 commit comments