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
1daf5d10
Commit
1daf5d10
authored
Nov 07, 2014
by
Skylot
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core: fix condition processing (resolve #25)
parent
9d2c0e4a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
87 additions
and
18 deletions
+87
-18
IfMakerHelper.java
...in/java/jadx/core/dex/visitors/regions/IfMakerHelper.java
+2
-0
IfRegionVisitor.java
.../java/jadx/core/dex/visitors/regions/IfRegionVisitor.java
+28
-18
TestNestedIf2.java
...java/jadx/tests/integration/conditions/TestNestedIf2.java
+57
-0
No files found.
jadx-core/src/main/java/jadx/core/dex/visitors/regions/IfMakerHelper.java
View file @
1daf5d10
...
...
@@ -175,6 +175,8 @@ public class IfMakerHelper {
if
(
isInversionNeeded
(
currentIf
,
nextIf
))
{
nextIf
=
IfInfo
.
invert
(
nextIf
);
}
}
else
{
return
currentIf
;
}
}
...
...
jadx-core/src/main/java/jadx/core/dex/visitors/regions/IfRegionVisitor.java
View file @
1daf5d10
...
...
@@ -45,7 +45,7 @@ public class IfRegionVisitor extends AbstractVisitor implements IRegionVisitor,
@Override
public
boolean
visitRegion
(
MethodNode
mth
,
IRegion
region
)
{
if
(
region
instanceof
IfRegion
)
{
return
removeRedundantElseBlock
((
IfRegion
)
region
);
return
removeRedundantElseBlock
(
mth
,
(
IfRegion
)
region
);
}
return
false
;
}
...
...
@@ -136,28 +136,38 @@ public class IfRegionVisitor extends AbstractVisitor implements IRegionVisitor,
}
}
private
static
boolean
removeRedundantElseBlock
(
IfRegion
ifRegion
)
{
if
(
ifRegion
.
getElseRegion
()
!=
null
&&
!
ifRegion
.
contains
(
AFlag
.
ELSE_IF_CHAIN
)
&&
!
ifRegion
.
getElseRegion
().
contains
(
AFlag
.
ELSE_IF_CHAIN
)
&&
hasBranchTerminator
(
ifRegion
)
&&
insnsCount
(
ifRegion
.
getThenRegion
())
<
2
)
{
IRegion
parent
=
ifRegion
.
getParent
();
Region
newRegion
=
new
Region
(
parent
);
if
(
parent
.
replaceSubBlock
(
ifRegion
,
newRegion
))
{
newRegion
.
add
(
ifRegion
);
newRegion
.
add
(
ifRegion
.
getElseRegion
());
ifRegion
.
setElseRegion
(
null
);
return
true
;
}
private
static
boolean
removeRedundantElseBlock
(
MethodNode
mth
,
IfRegion
ifRegion
)
{
if
(
ifRegion
.
getElseRegion
()
==
null
||
ifRegion
.
contains
(
AFlag
.
ELSE_IF_CHAIN
)
||
ifRegion
.
getElseRegion
().
contains
(
AFlag
.
ELSE_IF_CHAIN
))
{
return
false
;
}
if
(!
hasBranchTerminator
(
ifRegion
.
getThenRegion
()))
{
return
false
;
}
// code style check:
// will remove 'return;' from 'then' and 'else' with one instruction
// see #jadx.tests.integration.conditions.TestConditions9
if
(
mth
.
getReturnType
()
==
ArgType
.
VOID
&&
insnsCount
(
ifRegion
.
getThenRegion
())
==
2
&&
insnsCount
(
ifRegion
.
getElseRegion
())
==
2
)
{
return
false
;
}
IRegion
parent
=
ifRegion
.
getParent
();
Region
newRegion
=
new
Region
(
parent
);
if
(
parent
.
replaceSubBlock
(
ifRegion
,
newRegion
))
{
newRegion
.
add
(
ifRegion
);
newRegion
.
add
(
ifRegion
.
getElseRegion
());
ifRegion
.
setElseRegion
(
null
);
return
true
;
}
return
false
;
}
private
static
boolean
hasBranchTerminator
(
I
fRegion
ifR
egion
)
{
private
static
boolean
hasBranchTerminator
(
I
Container
r
egion
)
{
// TODO: check for exception throw
return
RegionUtils
.
hasExitBlock
(
ifRegion
.
getThenRegion
()
)
||
RegionUtils
.
hasBreakInsn
(
ifRegion
.
getThenRegion
()
);
return
RegionUtils
.
hasExitBlock
(
region
)
||
RegionUtils
.
hasBreakInsn
(
region
);
}
private
static
void
invertIfRegion
(
IfRegion
ifRegion
)
{
...
...
jadx-core/src/test/java/jadx/tests/integration/conditions/TestNestedIf2.java
0 → 100644
View file @
1daf5d10
package
jadx
.
tests
.
integration
.
conditions
;
import
jadx.core.dex.nodes.ClassNode
;
import
jadx.tests.api.IntegrationTest
;
import
org.junit.Test
;
import
static
jadx
.
tests
.
api
.
utils
.
JadxMatchers
.
containsOne
;
import
static
org
.
hamcrest
.
CoreMatchers
.
containsString
;
import
static
org
.
hamcrest
.
CoreMatchers
.
not
;
import
static
org
.
junit
.
Assert
.
assertThat
;
public
class
TestNestedIf2
extends
IntegrationTest
{
public
static
class
TestCls
{
static
int
executedCount
=
0
;
static
boolean
finished
=
false
;
static
int
repeatCount
=
2
;
static
boolean
test
(
float
delta
,
Object
object
)
{
if
(
executedCount
!=
repeatCount
&&
isRun
(
delta
,
object
))
{
if
(
finished
)
{
return
true
;
}
if
(
repeatCount
==
-
1
)
{
++
executedCount
;
action
();
return
false
;
}
++
executedCount
;
if
(
executedCount
>=
repeatCount
)
{
return
true
;
}
action
();
}
return
false
;
}
public
static
void
action
()
{
}
public
static
boolean
isRun
(
float
delta
,
Object
object
)
{
return
delta
==
0
;
}
}
@Test
public
void
test
()
{
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
assertThat
(
code
,
containsOne
(
"if (executedCount != repeatCount && isRun(delta, object)) {"
));
assertThat
(
code
,
containsOne
(
"if (finished) {"
));
assertThat
(
code
,
not
(
containsString
(
"else"
)));
}
}
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