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
05e5c82c
Commit
05e5c82c
authored
Apr 24, 2019
by
Ahmed Ashour
Committed by
skylot
Apr 24, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: remove redundant cast over similar conditions (PR #612)
parent
30fbf4bc
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
42 additions
and
12 deletions
+42
-12
ConditionGen.java
jadx-core/src/main/java/jadx/core/codegen/ConditionGen.java
+2
-1
IfCondition.java
...in/java/jadx/core/dex/regions/conditions/IfCondition.java
+24
-0
PrepareForCodeGen.java
...c/main/java/jadx/core/dex/visitors/PrepareForCodeGen.java
+16
-1
TestConditions16.java
...a/jadx/tests/integration/conditions/TestConditions16.java
+0
-10
No files found.
jadx-core/src/main/java/jadx/core/codegen/ConditionGen.java
View file @
05e5c82c
...
...
@@ -4,6 +4,7 @@ import java.util.Iterator;
import
java.util.LinkedList
;
import
java.util.Queue
;
import
jadx.core.dex.attributes.AFlag
;
import
jadx.core.dex.instructions.ArithNode
;
import
jadx.core.dex.instructions.IfOp
;
import
jadx.core.dex.instructions.InsnType
;
...
...
@@ -155,7 +156,7 @@ public class ConditionGen extends InsnGen {
}
private
boolean
isWrapNeeded
(
IfCondition
condition
)
{
if
(
condition
.
isCompare
())
{
if
(
condition
.
isCompare
()
||
condition
.
contains
(
AFlag
.
DONT_WRAP
)
)
{
return
false
;
}
return
condition
.
getMode
()
!=
Mode
.
NOT
;
...
...
jadx-core/src/main/java/jadx/core/dex/regions/conditions/IfCondition.java
View file @
05e5c82c
...
...
@@ -8,6 +8,9 @@ import java.util.LinkedList;
import
java.util.List
;
import
java.util.Objects
;
import
jadx.core.dex.attributes.AFlag
;
import
jadx.core.dex.attributes.AttributeStorage
;
import
jadx.core.dex.attributes.EmptyAttrStorage
;
import
jadx.core.dex.instructions.ArithNode
;
import
jadx.core.dex.instructions.ArithOp
;
import
jadx.core.dex.instructions.IfNode
;
...
...
@@ -31,6 +34,10 @@ public final class IfCondition {
OR
}
private
static
final
AttributeStorage
EMPTY_ATTR_STORAGE
=
new
EmptyAttrStorage
();
private
AttributeStorage
storage
=
EMPTY_ATTR_STORAGE
;
private
final
Mode
mode
;
private
final
List
<
IfCondition
>
args
;
private
final
Compare
compare
;
...
...
@@ -262,6 +269,23 @@ public final class IfCondition {
return
list
;
}
public
void
add
(
AFlag
flag
)
{
initStorage
().
add
(
flag
);
}
public
boolean
contains
(
AFlag
flag
)
{
return
storage
.
contains
(
flag
);
}
private
AttributeStorage
initStorage
()
{
AttributeStorage
store
=
storage
;
if
(
store
==
EMPTY_ATTR_STORAGE
)
{
store
=
new
AttributeStorage
();
storage
=
store
;
}
return
store
;
}
@Override
public
String
toString
()
{
switch
(
mode
)
{
...
...
jadx-core/src/main/java/jadx/core/dex/visitors/PrepareForCodeGen.java
View file @
05e5c82c
...
...
@@ -11,9 +11,12 @@ import jadx.core.dex.instructions.args.InsnArg;
import
jadx.core.dex.instructions.args.InsnWrapArg
;
import
jadx.core.dex.instructions.args.RegisterArg
;
import
jadx.core.dex.instructions.mods.ConstructorInsn
;
import
jadx.core.dex.instructions.mods.TernaryInsn
;
import
jadx.core.dex.nodes.BlockNode
;
import
jadx.core.dex.nodes.InsnNode
;
import
jadx.core.dex.nodes.MethodNode
;
import
jadx.core.dex.regions.conditions.IfCondition
;
import
jadx.core.dex.regions.conditions.IfCondition.Mode
;
import
jadx.core.dex.visitors.regions.variables.ProcessVariables
;
import
jadx.core.dex.visitors.shrink.CodeShrinkVisitor
;
import
jadx.core.utils.exceptions.JadxException
;
...
...
@@ -104,7 +107,7 @@ public class PrepareForCodeGen extends AbstractVisitor {
}
/**
* Remove parenthesis for wrapped insn
in arith '+' or '-'
* Remove parenthesis for wrapped insn in arith '+' or '-'
* ('(a + b) +c' => 'a + b + c')
*/
private
static
void
removeParenthesis
(
InsnNode
insn
)
{
...
...
@@ -124,6 +127,9 @@ public class PrepareForCodeGen extends AbstractVisitor {
}
}
}
else
{
if
(
insn
.
getType
()
==
InsnType
.
TERNARY
)
{
removeParenthesis
(((
TernaryInsn
)
insn
).
getCondition
());
}
for
(
InsnArg
arg
:
insn
.
getArguments
())
{
if
(
arg
.
isInsnWrap
())
{
InsnNode
wrapInsn
=
((
InsnWrapArg
)
arg
).
getWrapInsn
();
...
...
@@ -133,6 +139,15 @@ public class PrepareForCodeGen extends AbstractVisitor {
}
}
private
static
void
removeParenthesis
(
IfCondition
cond
)
{
Mode
mode
=
cond
.
getMode
();
for
(
IfCondition
c
:
cond
.
getArgs
())
{
if
(
c
.
getMode
()
==
mode
)
{
c
.
add
(
AFlag
.
DONT_WRAP
);
}
}
}
/**
* Replace arithmetic operation with short form
* ('a = a + 2' => 'a += 2')
...
...
jadx-core/src/test/java/jadx/tests/integration/conditions/TestConditions16.java
View file @
05e5c82c
...
...
@@ -2,7 +2,6 @@ package jadx.tests.integration.conditions;
import
org.junit.jupiter.api.Test
;
import
jadx.NotYetImplemented
;
import
jadx.core.dex.nodes.ClassNode
;
import
jadx.tests.api.IntegrationTest
;
...
...
@@ -27,19 +26,10 @@ public class TestConditions16 extends IntegrationTest {
}
@Test
@NotYetImplemented
public
void
test
()
{
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
assertThat
(
code
,
containsOne
(
"return a < 0 || (b % 2 != 0 && a > 28) || b < 0;"
));
}
@Test
public
void
test2
()
{
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
assertThat
(
code
,
containsOne
(
"return a < 0 || ((b % 2 != 0 && a > 28) || b < 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