From 22242ec9fa5ddcaddc5eb4167720d6f0119126bb Mon Sep 17 00:00:00 2001 From: zdx Date: Mon, 12 Oct 2026 23:08:18 +0800 Subject: [PATCH 1/2] update --- .../core/adapter/IHotCodeReplaceProvider.java | 3 + .../handler/HotCodeReplaceHandler.java | 5 +- .../java/debug/core/protocol/Requests.java | 5 +- .../internal/JavaHotCodeReplaceProvider.java | 125 +++++++++++++++++- .../com.microsoft.java.debug.tp.target | 2 +- 5 files changed, 132 insertions(+), 8 deletions(-) diff --git a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/IHotCodeReplaceProvider.java b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/IHotCodeReplaceProvider.java index 0760c432e..f9a35c996 100644 --- a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/IHotCodeReplaceProvider.java +++ b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/IHotCodeReplaceProvider.java @@ -15,12 +15,15 @@ import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; +import com.microsoft.java.debug.core.protocol.Requests.RedefineClassesArguments; + import io.reactivex.Observable; public interface IHotCodeReplaceProvider extends IProvider { void onClassRedefined(Consumer> consumer); CompletableFuture> redefineClasses(); + void initArgs(RedefineClassesArguments bpArguments); Observable getEventHub(); } diff --git a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/HotCodeReplaceHandler.java b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/HotCodeReplaceHandler.java index fa65e978b..b6e29e3e7 100644 --- a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/HotCodeReplaceHandler.java +++ b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/HotCodeReplaceHandler.java @@ -25,6 +25,7 @@ import com.microsoft.java.debug.core.protocol.Requests; import com.microsoft.java.debug.core.protocol.Requests.Arguments; import com.microsoft.java.debug.core.protocol.Requests.Command; +import com.microsoft.java.debug.core.protocol.Requests.RedefineClassesArguments; import com.microsoft.java.debug.core.protocol.Responses; public class HotCodeReplaceHandler implements IDebugRequestHandler { @@ -56,9 +57,9 @@ public void initialize(IDebugAdapterContext context) { @Override public CompletableFuture handle(Command command, Arguments arguments, Response response, IDebugAdapterContext context) { - + RedefineClassesArguments rcArguments = (RedefineClassesArguments) arguments; IHotCodeReplaceProvider provider = context.getProvider(IHotCodeReplaceProvider.class); - + provider.initArgs(rcArguments); return provider.redefineClasses().thenCompose(classNames -> { response.body = new Responses.RedefineClassesResponse(classNames.toArray(new String[0])); return CompletableFuture.completedFuture(response); diff --git a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/protocol/Requests.java b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/protocol/Requests.java index 1129d230e..fe1033a46 100644 --- a/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/protocol/Requests.java +++ b/com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/protocol/Requests.java @@ -331,7 +331,10 @@ public static class EvaluateArguments extends Arguments { } public static class RedefineClassesArguments extends Arguments { - + public Types.Source source; + public int[] lines = new int[0]; + public Types.SourceBreakpoint[] breakpoints = new Types.SourceBreakpoint[0]; + public boolean sourceModified = false; } public static class RestartFrameArguments extends Arguments { diff --git a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaHotCodeReplaceProvider.java b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaHotCodeReplaceProvider.java index 3a2b2f3df..5becaae5b 100644 --- a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaHotCodeReplaceProvider.java +++ b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaHotCodeReplaceProvider.java @@ -22,6 +22,7 @@ package com.microsoft.java.debug.plugin.internal; +import java.net.URI; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -53,13 +54,24 @@ import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaModelMarker; import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.ToolFactory; +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.ASTParser; +import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; +import org.eclipse.jdt.core.dom.AnonymousClassDeclaration; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.ITypeBinding; +import org.eclipse.jdt.core.dom.NodeFinder; import org.eclipse.jdt.core.util.IClassFileReader; import org.eclipse.jdt.core.util.ISourceAttribute; import org.eclipse.jdt.internal.core.util.Util; import org.eclipse.jdt.ls.core.internal.JobHelpers; import org.eclipse.jdt.ls.core.internal.ProjectUtils; +import org.eclipse.jdt.ls.core.internal.JDTUtils; +import org.eclipse.jdt.ls.core.internal.handlers.JsonRpcHelpers; import com.microsoft.java.debug.core.Configuration; import com.microsoft.java.debug.core.DebugException; @@ -74,7 +86,7 @@ import com.microsoft.java.debug.core.adapter.IDebugAdapterContext; import com.microsoft.java.debug.core.adapter.IHotCodeReplaceProvider; import com.microsoft.java.debug.core.protocol.Events; - +import com.microsoft.java.debug.core.protocol.Requests.RedefineClassesArguments; import com.sun.jdi.IncompatibleThreadStateException; import com.sun.jdi.ReferenceType; import com.sun.jdi.StackFrame; @@ -325,11 +337,115 @@ public void resourceChanged(IResourceChangeEvent event) { public void onClassRedefined(Consumer> consumer) { this.consumers.add(consumer); } - + + public String getFullyQualifiedName(ICompilationUnit unit,int line,int column) { + // 构建AST解析器 + ASTParser parser = ASTParser.newParser(AST.JLS_Latest); + parser.setKind(ASTParser.K_COMPILATION_UNIT); + parser.setSource(unit); // 绑定编译单元 + parser.setResolveBindings(false); // 启用绑定解析 + CompilationUnit astRoot = (CompilationUnit) parser.createAST(null); + + // 定位行列号对应的AST节点 + int offset = astRoot.getPosition(line, column); + + try + { + IJavaElement element = unit.getElementAt(offset); + if (element != null) + { + IType type = (IType) element.getAncestor(IJavaElement.TYPE); + if (type != null ) { + return type.getFullyQualifiedName(); + } + + } + IType[] allTypes = unit.getAllTypes(); + if (allTypes != null && allTypes.length > 0 ) { + return allTypes[0].getFullyQualifiedName(); + } + return ""; + }catch (Exception e) { + return ""; + } + +// // 找到该 offset 处最内层的 AST 节点 +// ASTNode node = NodeFinder.perform(astRoot, position, 0); +// +// // 向上找类型声明 +// ASTNode current = node; +// while (current != null) { +// if (current instanceof AbstractTypeDeclaration) { +// AbstractTypeDeclaration td = (AbstractTypeDeclaration) current; +// String simpleName = td.getName().getIdentifier(); +// return simpleName; +// } +// if (current instanceof AnonymousClassDeclaration) { +// // 匿名类:没有名字,用 binding +// ITypeBinding binding = +// ((AnonymousClassDeclaration) current).resolveBinding(); +// if (binding != null) { +// return binding.getName(); +// } +// } +// current = current.getParent(); +// } +// return ""; + } + + public void initArgs(RedefineClassesArguments rcArguments) { + //String qualifiedname = "com.hylandtec.tfb.logic.pub.DsPublicAction"; + //String resourcepath = "D:/ab9986/home/git/3024_htfb/tfb-web/target/classes/com/hylandtec/tfb/logic/pub/DsPublicAction.class"; + //deltaClassNames.add(qualifiedname); + //IResource resource = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(IPath.fromOSString(resourcepath)); + + //deltaResources.add(resource); + + IResource sres = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(IPath.fromOSString(rcArguments.source.path)); + final ICompilationUnit unit = JDTUtils.resolveCompilationUnit((IFile)sres); + if (unit == null || unit.getResource() == null || !unit.getResource().exists()) { + return; + } + String output_dir = ""; + try { + output_dir = unit.getJavaProject().getOutputLocation().toOSString(); + } catch (Exception e) { + System.out.println("getOutputLocation error: " +e.getMessage()); + } + // 获取包名 + //String packageName = unit.getParent().getElementName(); // 直接获取包片段名称 + //String fileName = unit.getElementName().replace(".java", ""); + String qualifiedname = getFullyQualifiedName(unit,rcArguments.breakpoints[0].line-1,rcArguments.breakpoints[0].column-1); + //String qualifiedname = packageName +"."+className; + + + String resourcepath = output_dir + "\\"+qualifiedname.replace('.', '\\')+ ".class";; +// if (fileName.equalsIgnoreCase(className)) { +// resourcepath = resourcepath + qualifiedname.replace('.', '/')+ ".class"; +// }else { +// resourcepath = resourcepath + packageName.replace('.', '/')+'/'+fileName+'$'+className+ ".class"; +// } + //IResource resource = ResourcesPlugin.getWorkspace().getRoot().getFile(IPath.fromOSString(resourcepath)); + publishEvent(HotCodeReplaceEvent.EventType.STARTING, "hot code QualiFiedName:"+qualifiedname); + publishEvent(HotCodeReplaceEvent.EventType.STARTING, "hot code ClassPath:"+resourcepath); + IResource resource = unit.getJavaProject().getProject().getWorkspace().getRoot().findMember(resourcepath); + if (resource == null) + { + publishEvent(HotCodeReplaceEvent.EventType.STARTING, "hot code Full Classpath:not find"); + return; + } + + publishEvent(HotCodeReplaceEvent.EventType.STARTING, "hot code Full Classpath:"+resource.getFullPath().toOSString()); + + deltaClassNames.add(qualifiedname); + deltaResources.add(resource); + + } + @Override public CompletableFuture> redefineClasses() { - triggerBuildForBspProject(); - JobHelpers.waitForBuildJobs(10 * 1000); + // triggerBuildForBspProject(); + // JobHelpers.waitForBuildJobs(10 * 1000); return CompletableFuture.supplyAsync(() -> { List classNames = new ArrayList<>(); String errorMessage = null; @@ -373,6 +489,7 @@ private String doHotCodeReplace(List resourcesToReplace, List filterNotLoadedTypes(resourcesToReplace, qualifiedNamesToReplace); if (qualifiedNamesToReplace.isEmpty()) { + publishEvent(HotCodeReplaceEvent.EventType.STARTING, "JVM doesn't load classes"); return null; // If none of the changed types are loaded, do nothing. } diff --git a/com.microsoft.java.debug.target/com.microsoft.java.debug.tp.target b/com.microsoft.java.debug.target/com.microsoft.java.debug.tp.target index 31462a770..a3e7667a9 100644 --- a/com.microsoft.java.debug.target/com.microsoft.java.debug.tp.target +++ b/com.microsoft.java.debug.target/com.microsoft.java.debug.tp.target @@ -4,7 +4,7 @@ - + From 6c52eb136efce85ce36ebc456d5cfeb837b0c53d Mon Sep 17 00:00:00 2001 From: zdx Date: Fri, 16 Oct 2026 17:11:47 +0800 Subject: [PATCH 2/2] update --- .../java/debug/plugin/internal/JavaHotCodeReplaceProvider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaHotCodeReplaceProvider.java b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaHotCodeReplaceProvider.java index 5becaae5b..589f4fa26 100644 --- a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaHotCodeReplaceProvider.java +++ b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaHotCodeReplaceProvider.java @@ -419,7 +419,7 @@ public void initArgs(RedefineClassesArguments rcArguments) { //String qualifiedname = packageName +"."+className; - String resourcepath = output_dir + "\\"+qualifiedname.replace('.', '\\')+ ".class";; + String resourcepath = output_dir + "/"+qualifiedname.replace('.', '/')+ ".class";; // if (fileName.equalsIgnoreCase(className)) { // resourcepath = resourcepath + qualifiedname.replace('.', '/')+ ".class"; // }else {