Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try with resource statement is not decompiled properly #51

Open
sarulmurugan opened this issue Oct 30, 2020 · 0 comments
Open

Try with resource statement is not decompiled properly #51

sarulmurugan opened this issue Oct 30, 2020 · 0 comments

Comments

@sarulmurugan
Copy link

sarulmurugan commented Oct 30, 2020

This code is not decompiled properly. It is an example code that was provided by you.

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.jd.core.v1.ClassFileToJavaSourceDecompiler;
import org.jd.core.v1.api.loader.Loader;
import org.jd.core.v1.api.loader.LoaderException;
import org.jd.core.v1.api.printer.Printer;

//$Id$

public class Test {

public static void main(String[] args) throws Exception {
	
	Loader loader = new Loader() {
	    @Override
	    public byte[] load(String internalName) throws LoaderException {
	        InputStream is = this.getClass().getResourceAsStream("/" + internalName + ".class");

	        if (is == null) {
	            return null;
	        } else {
	            try (InputStream in=is; ByteArrayOutputStream out=new ByteArrayOutputStream()) {
	                byte[] buffer = new byte[1024];
	                int read = in.read(buffer);

	                while (read > 0) {
	                    out.write(buffer, 0, read);		     
	                    read = in.read(buffer);
	                }

	                return out.toByteArray();
	            } catch (IOException e) {
	                throw new LoaderException(e);
	            }
	        }
	    }

	    @Override
	    public boolean canLoad(String internalName) {
	        return this.getClass().getResource("/" + internalName + ".class") != null;
	    }
	};
	
	Printer printer = new Printer() {
	    protected static final String TAB = "  ";
	    protected static final String NEWLINE = "\n";

	    protected int indentationCount = 0;
	    protected StringBuilder sb = new StringBuilder();

	    @Override public String toString() { return sb.toString(); }

	    @Override public void start(int maxLineNumber, int majorVersion, int minorVersion) {}
	    @Override public void end() {}

	    @Override public void printText(String text) { 
	    	sb.append(text); 
	    	}
	    @Override public void printNumericConstant(String constant) {
	    	sb.append(constant); }
	    @Override public void printStringConstant(String constant, String ownerInternalName) {
	    	sb.append(constant); }
	    @Override public void printKeyword(String keyword) { 
	    	sb.append(keyword); }
	    @Override public void printDeclaration(int type, String internalTypeName, String name, String descriptor) {
	    	sb.append(name); }
	    @Override public void printReference(int type, String internalTypeName, String name, String descriptor, String ownerInternalName) { 
	    	sb.append(name); 
	    }

	    @Override public void indent() { this.indentationCount++; }
	    @Override public void unindent() { this.indentationCount--; }

	    @Override public void startLine(int lineNumber) { 
	    	for (int i=0; i<indentationCount; i++) 
	    		sb.append(TAB);
	    	}
	    @Override public void endLine() { 
	    	sb.append(NEWLINE);
	    }
	    @Override public void extraLine(int count) { while (count-- > 0) sb.append(NEWLINE); }

	    @Override public void startMarker(int type) {}
	    @Override public void endMarker(int type) {}
	};
	
	ClassFileToJavaSourceDecompiler decompiler = new ClassFileToJavaSourceDecompiler();

	Map<String,Object> conf = new ConcurrentHashMap<String, Object>();
	
	
	decompiler.decompile(loader, printer, "Test$1",conf);

	String source = printer.toString();
	
	System.out.println(source);	
}

}

Result after decompiling Test$2 is

import java.io.IOException;
import java.io.InputStream;
import org.jd.core.v1.api.loader.Loader;
import org.jd.core.v1.api.loader.LoaderException;

class null implements Loader {
public byte[] load(String internalName) throws LoaderException {
is = getClass().getResourceAsStream("/" + internalName + ".class");
if (is == null)
return null;
try {
throwable1 = null;
throwable2 = null;
try {

  } finally {
    throwable2 = null;
    if (throwable1 == null) {
      throwable1 = throwable2;
    } else if (throwable1 != throwable2) {
      throwable1.addSuppressed(throwable2);
    } 
  } 
} catch (IOException e) {
  throw new LoaderException(e);
} 

}

public boolean canLoad(String internalName) { return (getClass().getResource("/" + internalName + ".class") != null); }
}

yaojieno1 pushed a commit to yaojieno1/jd-core that referenced this issue Feb 20, 2024
* Removed search loop start

* Removed replace loop switch break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant