0

Look at this question. When you open .class file with scala plugin enabled (Intellij Idea) it shows you scala code, bu when it is turned off java decompile plugin shows you a decompiled java code. Note that .class files which are compiled by javac decompiles even when scala plugin is enabled. That means that scala plugin "look at" some marker inside class files and intercept file content showing.

What the marker it actually uses? Is there a way to open .class file and change compiler (and/or) other infomartion to make those classes looks like they are compied by javac?

Cherry
  • 31,309
  • 66
  • 224
  • 364
  • 1
    You haven't given much detail (like which IDE, like how you attached the dependency to your project, like whether the jar file has a "source jar" attached to it). Searching on the Internet, it doesn't look like there even is a scala decompiler, so most likely the Scala plugin in your IDE knows to look in a source jar file for source files with the scala file extension rather than the java file extension. – Erwin Bolwidt Jan 19 '18 at 06:40
  • Look at original question. The IDE is intellij idea (question updated), the jar file contains compiled `.class` files only. There is **no** any files with `java` or `scala` extension in it. – Cherry Jan 19 '18 at 07:30
  • The "source" that you see with the scala plugin enabled, does it contain *any* comments at all? A copyright disclaimer, javadoc, multi-line comments, in-line comments? If so, it's not decompiled, because there is no decompiler in existence that that can do that as comments are not stored in the .class file. – Erwin Bolwidt Jan 19 '18 at 09:21

2 Answers2

2

Each class file can have a SourceFile attribute which contains the name of the source code file. Since this is an arbitrary string, it’s a bit about conventions, e.g. for Java source code, it usually contains the file name only, without any package specific directories.

So there still is bit of interpretation of the information, e.g. if the specified name ends with .java, an IDE has to look up the known source tree for a matching file in sub directories matching the actual package.

Determining that the source file is not Java is as simple as recognizing that it has a different file name ending, then, whatever convention is used for the particular language may be used, if a plugin knowing it has been installed. Otherwise, most IDEs will simply look for any text file of that name and display it. There might be LineNumberTable attributes, telling how bytecode instructions map to source line numbers, allowing debuggers to step through the code even without under­standing the source code syntax. I already stepped through code compiled from an XSLT file that way.

Of course, the pattern of the specified source code file name may also be used to decide which decompiler to use when the source file has not been found.

Holger
  • 285,553
  • 42
  • 434
  • 765
  • really? you can do that? – Eugene Jan 19 '18 at 10:03
  • @Eugene I also debugged downported Java 8 code with Eclipse back then, when it had no Java 8 support. The debugging simply worked through the file name and line number association. It even tried to highlight the code, but you had to ignore the red underlining… – Holger Jan 19 '18 at 10:08
  • how many lives have u lived? I'm even afraid to try that now, so used with "out-f-the-box" things... – Eugene Jan 19 '18 at 10:10
  • 2
    @Eugene in case of the XSLT debugging, it wasn’t even intentional. I debugged the Java code using the xslt processor (xalan, most likely) and suddenly I jumped into the code generated by the xslt processor, ending up at the relevant point in my XSLT file, even being able to recognize the applied rule chain from the stack trace. – Holger Jan 19 '18 at 10:13
0

Intellij Idea

Intellij IDEA (assuming your question is about this IDE), just figures out from which source file a binary was compiled, and then displays the corresponding source.

It does not extract or produce the source code from the binary. It is just able to find the source for a given binary. You can do this by matching file names and paths. Intellij does probably a bit more, though.

In general

In general, there is almost certainly no good decompiler that can produce Scala sources from class files. Also the source code is not embedded, so all you can achieve is trying to match source code files with binary files.

The Java code you get from decompilation is what you get shown when the IDE could not find the corresponding source file.

ziggystar
  • 28,410
  • 9
  • 72
  • 124
  • "just figures out from which source file a binary was compiled" and... How it doing this? There must be some information in `class`. That's the question. – Cherry Jan 19 '18 at 07:33
  • @Cherry If you have the source files, you know pretty well how the generated classes will be named. So starting from the source code, you can build a mapping and reverse it. The point is that you can do this without having any kind of "tag" within the class file. You just need to know how the build process works. And an IDE should know this. – ziggystar Jan 19 '18 at 08:27