-
Notifications
You must be signed in to change notification settings - Fork 324
Add support of Kotlin's SourceDebugExtension #10448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/symbol/SourceRemapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package com.datadog.debugger.symbol; | ||
|
|
||
| import com.datadog.debugger.util.JvmLanguage; | ||
| import datadog.trace.agent.tooling.stratum.SourceMap; | ||
| import datadog.trace.agent.tooling.stratum.StratumExt; | ||
| import datadog.trace.api.Pair; | ||
|
|
||
| public interface SourceRemapper { | ||
|
|
||
| int remapSourceLine(int line); | ||
|
|
||
| static SourceRemapper getSourceRemapper(String sourceFile, SourceMap sourceMap) { | ||
| JvmLanguage jvmLanguage = JvmLanguage.of(sourceFile); | ||
| switch (jvmLanguage) { | ||
| case KOTLIN: | ||
| StratumExt stratum = sourceMap.getStratum("KotlinDebug"); | ||
| if (stratum == null) { | ||
| throw new IllegalArgumentException("No stratum found for KotlinDebug"); | ||
| } | ||
| return new KotlinSourceRemapper(stratum); | ||
| default: | ||
| return NOOP_REMAPPER; | ||
| } | ||
| } | ||
|
|
||
| SourceRemapper NOOP_REMAPPER = new NoopSourceRemapper(); | ||
|
|
||
| class NoopSourceRemapper implements SourceRemapper { | ||
| @Override | ||
| public int remapSourceLine(int line) { | ||
| return line; | ||
| } | ||
| } | ||
|
|
||
| class KotlinSourceRemapper implements SourceRemapper { | ||
| private final StratumExt stratum; | ||
|
|
||
| public KotlinSourceRemapper(StratumExt stratum) { | ||
| this.stratum = stratum; | ||
| } | ||
|
|
||
| @Override | ||
| public int remapSourceLine(int line) { | ||
| Pair<String, Integer> pair = stratum.getInputLine(line); | ||
| if (pair == null || pair.getRight() == null) { | ||
| return line; | ||
| } | ||
| return pair.getRight(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/util/JvmLanguage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.datadog.debugger.util; | ||
|
|
||
| public enum JvmLanguage { | ||
| JAVA, | ||
| KOTLIN, | ||
| SCALA, | ||
| GROOVY, | ||
| UNKNOWN; | ||
|
|
||
| public static JvmLanguage of(String sourceFile) { | ||
| if (sourceFile == null) { | ||
| return UNKNOWN; | ||
| } | ||
| if (sourceFile.endsWith(".java")) { | ||
| return JAVA; | ||
| } | ||
| if (sourceFile.endsWith(".kt")) { | ||
| return KOTLIN; | ||
| } | ||
| if (sourceFile.endsWith(".scala")) { | ||
| return SCALA; | ||
| } | ||
| if (sourceFile.endsWith(".groovy")) { | ||
| return GROOVY; | ||
| } | ||
| return UNKNOWN; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/agent/KotlinHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package com.datadog.debugger.agent; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.net.URL; | ||
| import java.net.URLClassLoader; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Collections; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import org.jetbrains.kotlin.cli.common.ExitCode; | ||
| import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments; | ||
| import org.jetbrains.kotlin.cli.common.messages.MessageRenderer; | ||
| import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector; | ||
| import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler; | ||
| import org.jetbrains.kotlin.config.Services; | ||
|
|
||
| public class KotlinHelper { | ||
| public static Class<?> compileAndLoad( | ||
| String className, String sourceFileName, List<File> outputFilesToDelete) { | ||
| K2JVMCompiler compiler = new K2JVMCompiler(); | ||
| K2JVMCompilerArguments args = compiler.createArguments(); | ||
| args.setFreeArgs(Collections.singletonList(sourceFileName)); | ||
| String compilerOutputDir = "/tmp/" + CapturedSnapshotTest.class.getSimpleName() + "-kotlin"; | ||
| args.setDestination(compilerOutputDir); | ||
| args.setClasspath(System.getProperty("java.class.path")); | ||
| ExitCode exitCode = | ||
| compiler.exec( | ||
| new PrintingMessageCollector(System.out, MessageRenderer.WITHOUT_PATHS, true), | ||
| Services.EMPTY, | ||
| args); | ||
|
|
||
| if (exitCode.getCode() != 0) { | ||
| throw new RuntimeException("Kotlin compilation failed"); | ||
| } | ||
| File compileOutputDirFile = new File(compilerOutputDir); | ||
| try { | ||
| URLClassLoader urlClassLoader = | ||
| new URLClassLoader(new URL[] {compileOutputDirFile.toURI().toURL()}); | ||
| return urlClassLoader.loadClass(className); | ||
| } catch (Exception ex) { | ||
| throw new RuntimeException(ex); | ||
| } finally { | ||
| registerFilesToDeleteDir(compileOutputDirFile, outputFilesToDelete); | ||
| } | ||
| } | ||
|
|
||
| public static void registerFilesToDeleteDir(File dir, List<File> outputFilesToDelete) { | ||
| if (!dir.exists()) { | ||
| return; | ||
| } | ||
| try { | ||
| Files.walk(dir.toPath()) | ||
| .sorted(Comparator.reverseOrder()) | ||
| .map(Path::toFile) | ||
| .forEach(outputFilesToDelete::add); | ||
| } catch (IOException ex) { | ||
| ex.printStackTrace(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
odd that this is the sole change in this file...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it used in this file and before it was an inner protected class from an inherited class (
Instrumenter) so before no need an import.not trivial, I agree 😁