views:

35

answers:

2

Sometimes, in Eclipse , i press a combination of keys which take me to the editor page that shows contents of my .class file (bytecode). I never seem to be able to remember what that key combination is.

Can someone please let me know?

Or in other words, how can one see own bytecode?

A: 

Well... if the .class is selected in the Navigator View or Package Explorer view, a simple F3 is enough to open a decompiled version of it in the editor.

eclipse class editor

VonC
Or when you have the cursor over the type in Java code (alternatively CTRL+click). Note that Eclipse doesn't actually decompile anything. You just associate a source source with a binary source. The source you see might have absolutely nothing to do with the compiled class file. The source for `BufferManagerWriteCollect` happens to be included in the JDK. Try opening pretty much anything in the sun.** subpackages and you'll see what I mean.
Mark Peters
@Mark: I agree, even if it isn't exactly a... "combination of keys" ;) But again, neither is my <kbd>F3</kbd> suggestion!
VonC
+1  A: 

Eclipse's default class file viewer shows the source (see VonC's answer) if it has been associated with the binaries, otherwise it gives a javap-like view of the class (with an option to attach source). I'm guessing it's the latter that you are looking for.

I've never found a way to cleanly force Eclipse to show that output rather than the linked source. What you probably want is an Eclipse plug-in that provides Javap like functionality (or an interface to javap). Look for plugins stating they "disassemble" (as opposed to "decompile," like jad).

Barring a plug-in, you could probably configure an external tool to perform javap but it might not play nicely with other eclipse features.

Edit: Let me be clear about the above: If you hit F3, Eclipse does the following (generally, the actual procedure might be slightly different):

  1. Resolves the target (if you are selecting a Java file, it will be the Java file itself; if you are selecting or your cursor is over a class name it will be the class declaration, similar for a method declaration, etc).
  2. Searches the build path (same project first) for a Java file containing the target. If found, opens up an writable editor displaying that Java source file.
  3. For class/method declarations, it continues searching references on your build path for a class file that contains the declaration. If it is found, then
    a) If the class file has had source attached to it, open up a read-only editor of the linked Java file. b) If the class file does not have source attached to it, then open up a read-only panel showing the disassembled (javap-like) bytecode of the compiled class file.

My guess would be that you're thinking there's a dedicated key sequence to 3.b), but I don't think there is. But again, I would love to be proven wrong here.

Mark Peters
There is some default key combination that shows bytecode. Excuse me if i was not clear. That is what i was referring to
mac
You were clear, and I understood fine. I just believe you're mistaken, or that maybe that functionality is provided by a non-stock plugin. I would love to be proven wrong since I could use such a shortcut. If you know this to be true, why don't you just go through Preferences->General->Keys, find the binding, and report back here? Thanks.
Mark Peters
And by the way, my answer *does* deal with bytecode. It's what I mean when I say a javap-like view. Javap is the JDK tool for displaying bytecode from a compiled class file.
Mark Peters