Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
J
jadx
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
jadx
Commits
632cc3ec
Commit
632cc3ec
authored
Apr 15, 2019
by
Ahmed Ashour
Committed by
skylot
Apr 15, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: add primitive cast in ternary for byte and short (PR #601)
parent
bcfed5b3
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
82 additions
and
0 deletions
+82
-0
InsnGen.java
jadx-core/src/main/java/jadx/core/codegen/InsnGen.java
+21
-0
TestCast.java
...test/java/jadx/tests/integration/conditions/TestCast.java
+61
-0
No files found.
jadx-core/src/main/java/jadx/core/codegen/InsnGen.java
View file @
632cc3ec
...
...
@@ -862,6 +862,7 @@ public class InsnGen {
}
else
{
condGen
.
wrap
(
code
,
insn
.
getCondition
());
code
.
add
(
" ? "
);
addCastIfNeeded
(
code
,
first
,
second
);
addArg
(
code
,
first
,
false
);
code
.
add
(
" : "
);
addArg
(
code
,
second
,
false
);
...
...
@@ -871,6 +872,26 @@ public class InsnGen {
}
}
private
void
addCastIfNeeded
(
CodeWriter
code
,
InsnArg
first
,
InsnArg
second
)
{
if
(
first
.
isLiteral
()
&&
second
.
isLiteral
())
{
if
(
first
.
getType
()
==
ArgType
.
BYTE
)
{
long
lit1
=
((
LiteralArg
)
first
).
getLiteral
();
long
lit2
=
((
LiteralArg
)
second
).
getLiteral
();
if
(
lit1
!=
Byte
.
MAX_VALUE
&&
lit1
!=
Byte
.
MIN_VALUE
&&
lit2
!=
Byte
.
MAX_VALUE
&&
lit2
!=
Byte
.
MIN_VALUE
)
{
code
.
add
(
"(byte) "
);
}
}
else
if
(
first
.
getType
()
==
ArgType
.
SHORT
)
{
long
lit1
=
((
LiteralArg
)
first
).
getLiteral
();
long
lit2
=
((
LiteralArg
)
second
).
getLiteral
();
if
(
lit1
!=
Short
.
MAX_VALUE
&&
lit1
!=
Short
.
MIN_VALUE
&&
lit2
!=
Short
.
MAX_VALUE
&&
lit2
!=
Short
.
MIN_VALUE
)
{
code
.
add
(
"(short) "
);
}
}
}
}
private
void
makeArith
(
ArithNode
insn
,
CodeWriter
code
,
Set
<
Flags
>
state
)
throws
CodegenException
{
if
(
insn
.
contains
(
AFlag
.
ARITH_ONEARG
))
{
makeArithOneArg
(
insn
,
code
);
...
...
jadx-core/src/test/java/jadx/tests/integration/conditions/TestCast.java
0 → 100644
View file @
632cc3ec
package
jadx
.
tests
.
integration
.
conditions
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
static
org
.
hamcrest
.
Matchers
.
containsString
;
import
org.junit.jupiter.api.Test
;
import
jadx.core.dex.nodes.ClassNode
;
import
jadx.tests.api.IntegrationTest
;
public
class
TestCast
extends
IntegrationTest
{
public
static
class
TestCls
{
byte
myByte
;
short
myShort
;
public
void
test1
(
boolean
a
)
{
write
(
a
?
(
byte
)
0
:
1
);
}
public
void
test2
(
boolean
a
)
{
write
(
a
?
0
:
myByte
);
}
public
void
test3
(
boolean
a
)
{
write
(
a
?
0
:
Byte
.
MAX_VALUE
);
}
public
void
test4
(
boolean
a
)
{
write
(
a
?
(
short
)
0
:
1
);
}
public
void
test5
(
boolean
a
)
{
write
(
a
?
myShort
:
0
);
}
public
void
test6
(
boolean
a
)
{
write
(
a
?
Short
.
MIN_VALUE
:
0
);
}
public
void
write
(
byte
b
)
{
}
public
void
write
(
short
b
)
{
}
}
@Test
public
void
test
()
{
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
assertThat
(
code
,
containsString
(
"write(a ? (byte) 0 : 1);"
));
assertThat
(
code
,
containsString
(
"write(a ? 0 : this.myByte);"
));
assertThat
(
code
,
containsString
(
"write(a ? 0 : Byte.MAX_VALUE);"
));
assertThat
(
code
,
containsString
(
"write(a ? (short) 0 : 1);"
));
assertThat
(
code
,
containsString
(
"write(a ? this.myShort : 0);"
));
assertThat
(
code
,
containsString
(
"write(a ? Short.MIN_VALUE : 0);"
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment