开发者

Java decompiled code meaning

i recently decompiled a bit of Java code, but parts are in a syntax I don't understand. I've searched for this but many threads I see say that code similar to this is compiler dependant. On java syntax checks, this returns errors.

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;
                inputSocketIn开发者_如何学运维putStream = inputstream;
                outputSocketOutputStream = outputstream;
                super();
            }
            };
            thread.start();
            /** OMITTED **/
    }

I am confused about this bit:

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();
            }
            };

Can anyone explain how this bit of code is supposed to work?


This is non static initialization block. It is common that decompiler converts bytecode in non-compiling source. IMHO best code is produced by JD Decompiler. This variables

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

are interpretation of accessing final local variables from nonstatic inner classes. Variable this$0 refers to outer class instance containing inner class instance. In this case the inner class is anonymous so there is initialization block instead of constructor. We can said that variables with $ are generated by compiler. In fact this is decompilers' interpretation of the bytecode.


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

This block initialises the inner class implicit variables.

Suppose you declare a class with an (non-static) inner class:

public class Outter {
  private int a = 1;

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

The Inner class will implicitely get additional members that will get initialised to this when Outter is constructed. Try to decompile this sample to see what you get.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜