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

[core] Missing casts and cast optimizations #1336

Closed
nitram84 opened this issue Jan 14, 2022 · 2 comments
Closed

[core] Missing casts and cast optimizations #1336

nitram84 opened this issue Jan 14, 2022 · 2 comments
Labels
bug Core Issues in jadx-core module

Comments

@nitram84
Copy link
Contributor

I made some tests with casts and would like to report some compilation issues (missing casts) and a little feature request (avoid intermediate or unnecessary casts). My tests were made with openjdk8, latest git version, java-input and java-convert (same results):

public class JadxCastTest {
  public void testCasts() {
    testShort((short)0);  // compilation issue, cast missing after decompilation
    testShort((short)getInt()); // ok
    testByte((byte)0);  // ok
    testByte((byte)getInt());  // ok
    testChar((char)0);  // compilation issue, cast missing after decompilation
    testChar((char)getInt()); // ok
    testShort((short)0L);  // compilation issue, cast missing after decompilation
    testShort((short)getLong()); // ok, but no intermediate cast to int necessary
    testByte((byte)0L);  // const becomes an int
    testByte((byte)getLong()); // ok, but no intermediate cast to int necessary
    testChar((char)0L);  // compilation issue, cast missing after decompilation, const becomes an int
    testChar((char)getLong()); // ok, but no intermediate cast to int necessary
    testShort((short)' ');  // compilation issue, cast missing after decompilation, const becomes an int
    testShort((short)getChar());  // ok
    testByte((byte)' ');  // ok, but const becomes an int
    testByte((byte)getChar());  // ok
  }

  private long getLong() {
    return 1L; // const becomes an int
  }

  private char getChar() {
    return ' ';
  }

  private int getInt() {
    return 1;
  }

  private void testChar(char c) {
  }

  private void testByte(byte b) {
  }

  private void testShort(short s) {
  }
}

I observed that jadx converts long and char constants to int. I don't see a problem here as long as the results are the same. But I think cascading casts (long -> int -> short) could be optimized.

This sample can be used for unit tests.

@skylot
Copy link
Owner

skylot commented Jan 15, 2022

@nitram84 I commit a fix for compilation issues and unnecessary cascading casts.
Although, I must note that type of some constants can't be restored during decompilation because java compiler can convert/cast constant at compile time, so in bytecode appear already converted ones, so jadx don't know original type.
In your sample, for example, this happens with char constants: bytecode load them as a byte, so char type info is lost completely.

@skylot skylot closed this as completed Jan 15, 2022
@nitram84
Copy link
Contributor Author

Thank you very much for your fixes and your explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Core Issues in jadx-core module
Projects
None yet
Development

No branches or pull requests

2 participants