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
95e9da36
Commit
95e9da36
authored
Sep 24, 2013
by
Skylot
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core: simplify conditions, omit redundant parenthesis
parent
9bf7270b
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
66 additions
and
20 deletions
+66
-20
InsnGen.java
jadx-core/src/main/java/jadx/core/codegen/InsnGen.java
+1
-1
RegionGen.java
jadx-core/src/main/java/jadx/core/codegen/RegionGen.java
+1
-1
IfCondition.java
...core/src/main/java/jadx/core/dex/regions/IfCondition.java
+29
-16
RegionMaker.java
...main/java/jadx/core/dex/visitors/regions/RegionMaker.java
+4
-2
TestRedundantBrackets.java
.../test/java/jadx/tests/internal/TestRedundantBrackets.java
+15
-0
TestConditions.java
jadx-samples/src/main/java/jadx/samples/TestConditions.java
+16
-0
No files found.
jadx-core/src/main/java/jadx/core/codegen/InsnGen.java
View file @
95e9da36
...
@@ -309,7 +309,7 @@ public class InsnGen {
...
@@ -309,7 +309,7 @@ public class InsnGen {
break
;
break
;
case
APUT:
case
APUT:
code
.
add
(
arg
(
insn
,
0
)).
add
(
'['
).
add
(
arg
(
insn
,
1
)).
add
(
"] = "
).
add
(
arg
(
insn
,
2
));
code
.
add
(
arg
(
insn
,
0
)).
add
(
'['
).
add
(
arg
(
insn
.
getArg
(
1
),
false
)).
add
(
"] = "
).
add
(
arg
(
insn
.
getArg
(
2
),
false
));
break
;
break
;
case
IGET:
{
case
IGET:
{
...
...
jadx-core/src/main/java/jadx/core/codegen/RegionGen.java
View file @
95e9da36
...
@@ -167,7 +167,7 @@ public class RegionGen extends InsnGen {
...
@@ -167,7 +167,7 @@ public class RegionGen extends InsnGen {
return
"!"
+
makeCondition
(
condition
.
getArgs
().
get
(
0
));
return
"!"
+
makeCondition
(
condition
.
getArgs
().
get
(
0
));
case
AND:
case
AND:
case
OR:
case
OR:
String
mode
=
condition
.
getMode
()
==
IfCondition
.
M
ODE
.
AND
?
" && "
:
" || "
;
String
mode
=
condition
.
getMode
()
==
IfCondition
.
M
ode
.
AND
?
" && "
:
" || "
;
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
for
(
IfCondition
arg
:
condition
.
getArgs
())
{
for
(
IfCondition
arg
:
condition
.
getArgs
())
{
if
(
sb
.
length
()
!=
0
)
{
if
(
sb
.
length
()
!=
0
)
{
...
...
jadx-core/src/main/java/jadx/core/dex/regions/IfCondition.java
View file @
95e9da36
...
@@ -5,6 +5,7 @@ import jadx.core.dex.instructions.IfOp;
...
@@ -5,6 +5,7 @@ import jadx.core.dex.instructions.IfOp;
import
jadx.core.dex.instructions.args.InsnArg
;
import
jadx.core.dex.instructions.args.InsnArg
;
import
jadx.core.dex.nodes.BlockNode
;
import
jadx.core.dex.nodes.BlockNode
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.List
;
...
@@ -21,16 +22,18 @@ public final class IfCondition {
...
@@ -21,16 +22,18 @@ public final class IfCondition {
return
new
IfCondition
(
new
Compare
(
insn
));
return
new
IfCondition
(
new
Compare
(
insn
));
}
}
public
static
IfCondition
not
(
IfCondition
a
)
{
public
static
IfCondition
merge
(
Mode
mode
,
IfCondition
a
,
IfCondition
b
)
{
return
new
IfCondition
(
MODE
.
NOT
,
Arrays
.
asList
(
a
));
if
(
a
.
getMode
()
==
mode
)
{
IfCondition
n
=
new
IfCondition
(
a
);
n
.
addArg
(
b
);
return
n
;
}
else
if
(
b
.
getMode
()
==
mode
)
{
IfCondition
n
=
new
IfCondition
(
b
);
n
.
addArg
(
a
);
return
n
;
}
else
{
return
new
IfCondition
(
mode
,
Arrays
.
asList
(
a
,
b
));
}
}
public
static
IfCondition
and
(
IfCondition
a
,
IfCondition
b
)
{
return
new
IfCondition
(
MODE
.
AND
,
Arrays
.
asList
(
a
,
b
));
}
public
static
IfCondition
or
(
IfCondition
a
,
IfCondition
b
)
{
return
new
IfCondition
(
MODE
.
OR
,
Arrays
.
asList
(
a
,
b
));
}
}
public
static
final
class
Compare
{
public
static
final
class
Compare
{
...
@@ -61,30 +64,36 @@ public final class IfCondition {
...
@@ -61,30 +64,36 @@ public final class IfCondition {
}
}
}
}
public
static
enum
M
ODE
{
public
static
enum
M
ode
{
COMPARE
,
COMPARE
,
NOT
,
NOT
,
AND
,
AND
,
OR
OR
}
}
private
final
M
ODE
mode
;
private
final
M
ode
mode
;
private
final
List
<
IfCondition
>
args
;
private
final
List
<
IfCondition
>
args
;
private
final
Compare
compare
;
private
final
Compare
compare
;
private
IfCondition
(
Compare
compare
)
{
private
IfCondition
(
Compare
compare
)
{
this
.
mode
=
M
ODE
.
COMPARE
;
this
.
mode
=
M
ode
.
COMPARE
;
this
.
compare
=
compare
;
this
.
compare
=
compare
;
this
.
args
=
null
;
this
.
args
=
null
;
}
}
private
IfCondition
(
M
ODE
mode
,
List
<
IfCondition
>
args
)
{
private
IfCondition
(
M
ode
mode
,
List
<
IfCondition
>
args
)
{
this
.
mode
=
mode
;
this
.
mode
=
mode
;
this
.
args
=
args
;
this
.
args
=
args
;
this
.
compare
=
null
;
this
.
compare
=
null
;
}
}
public
MODE
getMode
()
{
private
IfCondition
(
IfCondition
c
)
{
this
.
mode
=
c
.
mode
;
this
.
compare
=
c
.
compare
;
this
.
args
=
new
ArrayList
<
IfCondition
>(
c
.
args
);
}
public
Mode
getMode
()
{
return
mode
;
return
mode
;
}
}
...
@@ -92,8 +101,12 @@ public final class IfCondition {
...
@@ -92,8 +101,12 @@ public final class IfCondition {
return
args
;
return
args
;
}
}
public
void
addArg
(
IfCondition
c
)
{
args
.
add
(
c
);
}
public
boolean
isCompare
()
{
public
boolean
isCompare
()
{
return
mode
==
M
ODE
.
COMPARE
;
return
mode
==
M
ode
.
COMPARE
;
}
}
public
Compare
getCompare
()
{
public
Compare
getCompare
()
{
...
...
jadx-core/src/main/java/jadx/core/dex/visitors/regions/RegionMaker.java
View file @
95e9da36
...
@@ -40,6 +40,7 @@ import java.util.Set;
...
@@ -40,6 +40,7 @@ import java.util.Set;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
static
jadx
.
core
.
dex
.
regions
.
IfCondition
.
Mode
;
import
static
jadx
.
core
.
utils
.
BlockUtils
.
getBlockByOffset
;
import
static
jadx
.
core
.
utils
.
BlockUtils
.
getBlockByOffset
;
import
static
jadx
.
core
.
utils
.
BlockUtils
.
isPathExists
;
import
static
jadx
.
core
.
utils
.
BlockUtils
.
isPathExists
;
import
static
jadx
.
core
.
utils
.
BlockUtils
.
selectOther
;
import
static
jadx
.
core
.
utils
.
BlockUtils
.
selectOther
;
...
@@ -400,6 +401,7 @@ public class RegionMaker {
...
@@ -400,6 +401,7 @@ public class RegionMaker {
IfCondition
condition
;
IfCondition
condition
;
boolean
inverted
=
false
;
boolean
inverted
=
false
;
IfCondition
nestedCondition
=
IfCondition
.
fromIfNode
(
nestedIfInsn
);
if
(
isPathExists
(
bElse
,
nestedIfBlock
))
{
if
(
isPathExists
(
bElse
,
nestedIfBlock
))
{
// else branch
// else branch
if
(
bThen
!=
nbThen
)
{
if
(
bThen
!=
nbThen
)
{
...
@@ -409,7 +411,7 @@ public class RegionMaker {
...
@@ -409,7 +411,7 @@ public class RegionMaker {
nestedIfInsn
.
invertOp
(
nbElse
.
getStartOffset
());
nestedIfInsn
.
invertOp
(
nbElse
.
getStartOffset
());
inverted
=
true
;
inverted
=
true
;
}
}
condition
=
IfCondition
.
or
(
ifRegion
.
getCondition
(),
IfCondition
.
fromIfNode
(
nestedIfInsn
)
);
condition
=
IfCondition
.
merge
(
Mode
.
OR
,
ifRegion
.
getCondition
(),
nestedCondition
);
}
else
{
}
else
{
// then branch
// then branch
if
(
bElse
!=
nbElse
)
{
if
(
bElse
!=
nbElse
)
{
...
@@ -419,7 +421,7 @@ public class RegionMaker {
...
@@ -419,7 +421,7 @@ public class RegionMaker {
nestedIfInsn
.
invertOp
(
nbElse
.
getStartOffset
());
nestedIfInsn
.
invertOp
(
nbElse
.
getStartOffset
());
inverted
=
true
;
inverted
=
true
;
}
}
condition
=
IfCondition
.
and
(
ifRegion
.
getCondition
(),
IfCondition
.
fromIfNode
(
nestedIfInsn
)
);
condition
=
IfCondition
.
merge
(
Mode
.
AND
,
ifRegion
.
getCondition
(),
nestedCondition
);
}
}
ifRegion
.
setCondition
(
condition
);
ifRegion
.
setCondition
(
condition
);
nestedIfBlock
.
getAttributes
().
add
(
AttributeFlag
.
SKIP
);
nestedIfBlock
.
getAttributes
().
add
(
AttributeFlag
.
SKIP
);
...
...
jadx-core/src/test/java/jadx/tests/internal/TestRedundantBrackets.java
View file @
95e9da36
...
@@ -31,6 +31,17 @@ public class TestRedundantBrackets extends InternalJadxTest {
...
@@ -31,6 +31,17 @@ public class TestRedundantBrackets extends InternalJadxTest {
}
}
return
b
;
return
b
;
}
}
public
void
method4
(
int
num
)
{
if
(
num
==
4
||
num
==
6
||
num
==
8
||
num
==
10
)
{
method2
(
null
);
}
}
public
void
method5
(
int
a
[],
int
n
)
{
a
[
1
]
=
n
*
2
;
a
[
n
-
1
]
=
1
;
}
}
}
@Test
@Test
...
@@ -42,5 +53,9 @@ public class TestRedundantBrackets extends InternalJadxTest {
...
@@ -42,5 +53,9 @@ public class TestRedundantBrackets extends InternalJadxTest {
assertThat
(
code
,
containsString
(
"if (obj instanceof String)"
));
assertThat
(
code
,
containsString
(
"if (obj instanceof String)"
));
assertThat
(
code
,
containsString
(
"if (a + b < 10)"
));
assertThat
(
code
,
containsString
(
"if (a + b < 10)"
));
assertThat
(
code
,
containsString
(
"if ((a & b) != 0)"
));
assertThat
(
code
,
containsString
(
"if ((a & b) != 0)"
));
assertThat
(
code
,
containsString
(
"if (num == 4 || num == 6 || num == 8 || num == 10)"
));
assertThat
(
code
,
containsString
(
"a[1] = n * 2;"
));
assertThat
(
code
,
containsString
(
"a[n - 1] = 1;"
));
}
}
}
}
jadx-samples/src/main/java/jadx/samples/TestConditions.java
View file @
95e9da36
...
@@ -50,6 +50,16 @@ public class TestConditions extends AbstractTest {
...
@@ -50,6 +50,16 @@ public class TestConditions extends AbstractTest {
test1
(
0
);
test1
(
0
);
}
}
public
void
test4
(
int
num
)
{
if
(
num
==
4
||
num
==
6
||
num
==
8
||
num
==
10
)
{
accept
(
"a"
);
}
}
public
boolean
test5
(
int
num
)
{
return
num
>
5
&&
(
num
<
10
||
num
==
7
);
}
public
boolean
accept
(
String
name
)
{
public
boolean
accept
(
String
name
)
{
return
name
.
startsWith
(
"Test"
)
&&
name
.
endsWith
(
".class"
)
&&
!
name
.
contains
(
"$"
);
return
name
.
startsWith
(
"Test"
)
&&
name
.
endsWith
(
".class"
)
&&
!
name
.
contains
(
"$"
);
}
}
...
@@ -71,6 +81,12 @@ public class TestConditions extends AbstractTest {
...
@@ -71,6 +81,12 @@ public class TestConditions extends AbstractTest {
assertTrue
(
accept
(
"Test.class"
));
assertTrue
(
accept
(
"Test.class"
));
test3
(
false
,
false
);
test3
(
false
,
false
);
assertFalse
(
test5
(
4
));
assertFalse
(
test5
(
11
));
assertTrue
(
test5
(
6
));
assertTrue
(
test5
(
7
));
assertTrue
(
test5
(
8
));
return
true
;
return
true
;
}
}
...
...
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