-
Notifications
You must be signed in to change notification settings - Fork 337
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
navigate to decompiled class if no source #3411
Closed
Closed
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 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 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 |
---|---|---|
|
@@ -3,9 +3,7 @@ package scala.meta.internal.metals | |
import java.io.ByteArrayOutputStream | ||
import java.io.PrintStream | ||
import java.net.URI | ||
import java.net.URLDecoder | ||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.Paths | ||
import javax.annotation.Nullable | ||
|
||
import scala.annotation.tailrec | ||
|
@@ -141,13 +139,18 @@ final class FileDecoderProvider( | |
* | ||
* jar: | ||
* metalsDecode:jar:file:///somePath/someFile-sources.jar!/somePackage/someFile.java | ||
* | ||
* jar + cfr: | ||
* metalsDecode:jar:file:///somePath/someFile.jar!/somePackage/someFile.class.cfr | ||
* | ||
* build target: | ||
* metalsDecode:file:///workspacePath/buildTargetName.metals-buildtarget | ||
*/ | ||
def decodedFileContents(uriAsStr: String): Future[DecoderResponse] = { | ||
Try(URI.create(uriAsStr)) match { | ||
case Success(uri) => | ||
uri.getScheme() match { | ||
case "jar" => Future { decodeJar(uri) } | ||
case "file" => decodeMetalsFile(uri) | ||
case "jar" | "file" => decodeMetalsFile(uri) | ||
case "metalsDecode" => | ||
decodedFileContents(uri.getSchemeSpecificPart()) | ||
case _ => | ||
|
@@ -165,21 +168,6 @@ final class FileDecoderProvider( | |
} | ||
} | ||
|
||
private def decodeJar(uri: URI): DecoderResponse = { | ||
Try { | ||
// jar file system cannot cope with a heavily encoded uri | ||
// hence the roundabout way of creating an AbsolutePath | ||
// must have "jar:file:"" instead of "jar:file%3A" | ||
val decodedUriStr = URLDecoder.decode(uri.toString(), "UTF-8") | ||
val decodedUri = URI.create(decodedUriStr) | ||
val path = AbsolutePath(Paths.get(decodedUri)) | ||
FileIO.slurp(path, StandardCharsets.UTF_8) | ||
} match { | ||
case Failure(exception) => DecoderResponse.failed(uri, exception) | ||
case Success(value) => DecoderResponse.success(uri, value) | ||
} | ||
} | ||
|
||
private def decodeMetalsFile( | ||
uri: URI | ||
): Future[DecoderResponse] = { | ||
|
@@ -213,20 +201,41 @@ final class FileDecoderProvider( | |
} | ||
} | ||
} else | ||
Future.successful(DecoderResponse.failed(uri, "Unsupported extension")) | ||
additionalExtension match { | ||
case "java" | "scala" => | ||
Future.successful( | ||
toFile(uri.toString).map(strippedURI => { | ||
FileIO.slurp(strippedURI, StandardCharsets.UTF_8) | ||
}) match { | ||
case Left(value) => value | ||
case Right(value) => DecoderResponse.success(uri, value) | ||
} | ||
) | ||
case _ => | ||
Future.successful( | ||
DecoderResponse.failed(uri, "Unsupported extension") | ||
) | ||
} | ||
} | ||
|
||
private def toFile( | ||
uri: URI, | ||
suffixToRemove: String | ||
): Either[DecoderResponse, AbsolutePath] = | ||
toFile(uri.toString.stripSuffix(suffixToRemove)) | ||
|
||
private def toFile( | ||
uriAsStr: String | ||
): Either[DecoderResponse, AbsolutePath] = { | ||
val strippedURI = uri.toString.stripSuffix(suffixToRemove) | ||
Try { | ||
strippedURI.toAbsolutePath | ||
}.filter(_.exists) | ||
val uri = uriAsStr.toURI | ||
val path = Try { | ||
uri.toAbsolutePath | ||
} | ||
path | ||
.filter(_.exists) | ||
.toOption | ||
.toRight( | ||
DecoderResponse.failed(uri, s"File $strippedURI doesn't exist") | ||
DecoderResponse.failed(uri, s"File $uriAsStr doesn't exist") | ||
) | ||
} | ||
|
||
|
@@ -482,7 +491,18 @@ final class FileDecoderProvider( | |
): Future[DecoderResponse] = { | ||
val cfrDependency = Dependency.of("org.benf", "cfr", "0.151") | ||
val cfrMain = "org.benf.cfr.reader.Main" | ||
val args = List("--analyseas", "CLASS", s"""${path.toNIO.toString}""") | ||
val (parent, className) = | ||
if (path.isJarFileSystem) | ||
(workspace, path.toNIO.toString.stripPrefix("\\").stripPrefix("/")) | ||
else (path.parent, path.toNIO.toString) | ||
val extraClassPath = path.jarPath | ||
.map(jarPath => List("--extraclasspath", jarPath.toString)) | ||
.getOrElse(List.empty) | ||
val args = extraClassPath ::: List( | ||
"--analyseas", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No - although it does look it. The options is meant to mean ANALYSE_AS. Either |
||
"CLASS", | ||
s"""$className""" | ||
) | ||
val sbOut = new StringBuilder() | ||
val sbErr = new StringBuilder() | ||
|
||
|
@@ -491,7 +511,7 @@ final class FileDecoderProvider( | |
.runJava( | ||
cfrDependency, | ||
cfrMain, | ||
path.parent, | ||
parent, | ||
args, | ||
redirectErrorOutput = false, | ||
s => { | ||
|
This file contains 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
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.
This might not work for some more complex Scala symbols such as inner classes 🤔
In that case we might want to use ClasspathSymbols and find the class that contains the symbol.