Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java source file displaying in IntelliJ as an empty decompiled class file

I have a project with several .java files that I can view the source of perfectly in IntelliJ, except for one.

That file is a .java file like the others, but IntelliJ labels it a "Decompiled .class file" and the source is completely empty. If I open the file in a text editor, I can see the source just fine. Ant builds also work fine with this file.

These are not JDK sources that are missing, where you might expect to see decompiled code. And this is not a compiled .class file I'm opening, either.

Deleting the file from the project and updating from source control to restore it doesn't seem to help.

Here is what it looks like when I try to open the file in IntelliJ:

IntelliJ <code>.java</code> file as Decompiled <code>.class</code> file

What might cause this behavior in IntelliJ and what can I do to fix it?

like image 893
Brandon Essler Avatar asked Nov 19 '18 21:11

Brandon Essler


People also ask

What is decompiled .class file?

A Java Decompiler is a special type of decompiler which takes a class file as input and produces Java source code as output. The decompilation is exactly the reverse process of compilation. Thus, decompiler does not produce a replica of the source code.

How can I get Java source code in IntelliJ?

zip to IntelliJ IDEA. 2.2 Platform Settings -> SDKs -> Select Sourcepath -> plus icon -> select src. zip from the JDK installed path. 2.3 Done.


2 Answers

I did end up solving my issue.

Somehow, I inadvertently added that individual file's name to the Java Class File Types list, and so that file was associated as a decompiled .class files in the same way an actual .class file is.

The fix is going to Settings (Ctrl+Alt+S)EditorFile Types, scrolling down to Java Class in the list of Recognized File Types, selecting the file name in the list of Registered Patterns, and clicking the Remove button.

Settings, Editor, File Types Menu

I did not figure out how it was automatically added to that list. One guess is that I may have accepted a suggestion pop-up or selected the wrong Intention Action, and so the name of the open or active file was added to the .class type list.

like image 119
Brandon Essler Avatar answered Oct 04 '22 17:10

Brandon Essler


If the same class (comparing fully qualified name) is both existing in your project and simultaneously coming from a 3rd party dependency at times IntelliJ will get confused.

This happens also for dependencies if you have the decompiled class open in a editor window and press "Download Sources" action. This results in another editor window opening the just downloaded source code for the same class and IntelliJ navigation going bonkers (Ctrl + Left click jump and other actions).

The only solution I know of is to restart IntelliJ and hope that it works. You can also try File > Invalidate Caches and Restart which will force IntelliJ to re-index the world.

like image 41
Karol Dowbecki Avatar answered Oct 04 '22 18:10

Karol Dowbecki