Java反编译代码的含义

时间:2011-07-19 15:04:16

标签: java decompiling

我最近反编译了一些Java代码,但部分语法我不明白。我已经搜索了这个,但是我看到许多线程说类似的代码依赖于编译器。在java语法检查上,这将返回错误。

public void run()
    {
        try
        {
            final InputStream inputSocketInputStream = inputSocket.getInputStream();
            final OutputStream inputSocketOutputStream = inputSocket.getOutputStream();
            Socket socket = new Socket();
            socket.connect(new InetSocketAddress(APJP.APJP_LOCAL_HTTP_SERVER_ADDRESS, APJP.APJP_LOCAL_HTTP_SERVER_PORT));
            final InputStream outputSocketInputStream = socket.getInputStream();
            final OutputStream outputSocketOutputStream = socket.getOutputStream();
            Thread thread = new Thread() {

                final InputStream val$inputSocketInputStream;
                final OutputStream val$outputSocketOutputStream;
                final HTTPProxyServerWorker this$0;

                public void run()
                {
                    try
                    {
                        byte abyte0[] = new byte[5120];
                        for(int i = 0; (i = inputSocketInputStream.read(abyte0)) != -1;)
                        {
                            outputSocketOutputStream.write(abyte0, 0, i);
                        }

                        outputSocketOutputStream.close();
                    }
                    catch(Exception exception1) { }
                }


            {
                this$0 = HTTPProxyServerWorker.this;
                inputSocketInputStream = inputstream;
                outputSocketOutputStream = outputstream;
                super();
            }
            };
            thread.start();
            /** OMITTED **/
    }

我对此感到困惑:

Thread thread = new Thread() {

                final InputStream val$inputSocketInputStream;
                final OutputStream val$outputSocketOutputStream;
                final HTTPProxyServerWorker this$0;

                public void run()
                {
                    try
                    {
                        byte abyte0[] = new byte[5120];
                        for(int i = 0; (i = inputSocketInputStream.read(abyte0)) != -1;)
                        {
                            outputSocketOutputStream.write(abyte0, 0, i);
                        }

                        outputSocketOutputStream.close();
                    }
                    catch(Exception exception1) { }
                }

            //WHAT IS THIS BELOW? Constructor?
            {
                this$0 = HTTPProxyServerWorker.this; 
                inputSocketInputStream = inputstream;
                outputSocketOutputStream = outputstream;
                super();
            }
            };

任何人都可以解释这段代码应该如何工作吗?

2 个答案:

答案 0 :(得分:4)

这是non static initialization block。反编译器通常在非编译源中转换字节码。恕我直言最佳代码由JD Decompiler制作。这个变量

            final InputStream val$inputSocketInputStream;
            final OutputStream val$outputSocketOutputStream;
            final HTTPProxyServerWorker this$0;

是从非静态内部类访问最终局部变量的解释。变量this$0指的是包含内部类实例的外部类实例。在这种情况下,内部类是匿名的,因此存在初始化块而不是构造函数。我们可以说$的变量是由编译器生成的。实际上这是反编译器对字节码的解释。

答案 1 :(得分:0)

{
  this$0 = HTTPProxyServerWorker.this; 
  inputSocketInputStream = inputstream;
  outputSocketOutputStream = outputstream;
  super();
}

此块初始化内部类隐式变量。

假设您使用(非静态)内部类声明了一个类:

public class Outter {
  private int a = 1;

  class Inner {
    void doSomethingToOutter() {
      a += 1;
    }
  }
}

Inner类会隐含地获得在this构建时初始化为Outter的其他成员。尝试反编译此示例以查看您获得的内容。