8

When decompiling a specific jar using java decompiler (http://java.decompiler.free.fr/) I got some strange code I cannot identify what is. can someone help me? the code is something like:

Foo.access$004(Foo.this);

or this

Bar.access$006(Bar.this);

or else

Baz.access$102(Baz.this, true)

What are these methods access$004, access$006 and access$102?

Jonhnny Weslley
  • 1,080
  • 2
  • 13
  • 24
  • 3
    A similar question was asked and got some good responses [here](http://stackoverflow.com/questions/5524358/what-is-this-referencing). – cutchin Feb 21 '12 at 14:18
  • 1
    Do you want to use this decompiler, or is it an option to use JAD instead? http://www.varaneckas.com/jad – Gergely Bacso Feb 21 '12 at 14:19
  • You do not know why, Gergely? :) Jörn is right. JD does not Recognize all code patterns for synthetic methods. – Emmanuel Dupuy Feb 21 '12 at 21:45
  • Thanks guys. I was a heavy jad user, but I had switched to JD because jad does not decompile try/catch blocks properly. But now, I'm using both JD and jad to decompile java bytecode. :( – Jonhnny Weslley Feb 22 '12 at 12:33

3 Answers3

16

Synthetic methods like this get created to support acessing private methods of inner classes. Since inner classes were not part of the initial jvm version, the access modifiers could not really handle this case. The solution was to create additional package-visible methods that delegate to the private implementation.

public class Example {
    private static class Inner {
         private void innerMethod() { ... }
    }

    public void test() {
        Inner inner = ...
        inner.innerMethod():
    }
}

The compile would create a new method of the Inner class like this:

static void access$000(Inner inner) {
    inner.innerMethod();
}

And replace the call in the test method like this:

Inner.access$000(inner);

The static access$000 is package visible and so accessible from the outer class, and being inside the same Inner class it can delegate to the private innerMethod.

Jörn Horstmann
  • 33,639
  • 11
  • 75
  • 118
2

These are auto-generated methods which are created by the compiler in some cases (for example when accessing private fields of another class directly, e.g., in case of nested classes).

See also What is the meaning of "static synthetic"? and Synthetic Class in Java.

Community
  • 1
  • 1
Philipp Wendler
  • 11,184
  • 7
  • 52
  • 87
0

If you get the relevant .class file (run jar through unzip), and run the .class file through JAD

JAD MyClass.class

then you may find that the output JAD file has decompiled that particular line in a more meaningful way, e.g.

Baz.access$102(Baz.this, true)

shows up in the JAD output as simply

myMemberVaiable = true

where myMemberVaiable is a member of class Baz that you will recognise.

Andy Joiner
  • 5,932
  • 3
  • 45
  • 72