Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java decompilation

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?

like image 783
Jonhnny Weslley Avatar asked Feb 21 '12 14:02

Jonhnny Weslley


People also ask

Can Java be decompiled?

If you are writing Java classes and distributing them over the Internet, you should know that people can reverse-engineer, disassemble, or decompile your classes into Java source code. The most widely used decompiler (at least publicly) is Mocha.

What does a Java decompiler do?

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 do I decompile a Java class?

In order to decompile class file, just open any of your Java projects and go to Maven dependencies of libraries to see the jar files included in your project. Just expand any jar file for which you don't have the source attached in Eclipse and click on the . class file.


2 Answers

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.

like image 100
Jörn Horstmann Avatar answered Sep 21 '22 11:09

Jörn Horstmann


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.

like image 33
Philipp Wendler Avatar answered Sep 21 '22 11:09

Philipp Wendler