Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
ql04210214
jadx
提交
e2018535
J
jadx
项目概览
ql04210214
/
jadx
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jadx
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e2018535
编写于
8月 19, 2014
作者:
S
Skylot
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
core: add ternary conditions processing
上级
ee56610f
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
133 addition
and
18 deletion
+133
-18
jadx-core/src/main/java/jadx/core/codegen/ConditionGen.java
jadx-core/src/main/java/jadx/core/codegen/ConditionGen.java
+13
-1
jadx-core/src/main/java/jadx/core/dex/instructions/mods/TernaryInsn.java
...ain/java/jadx/core/dex/instructions/mods/TernaryInsn.java
+19
-1
jadx-core/src/main/java/jadx/core/dex/regions/IfCondition.java
...core/src/main/java/jadx/core/dex/regions/IfCondition.java
+17
-1
jadx-core/src/main/java/jadx/core/dex/visitors/SimplifyVisitor.java
...src/main/java/jadx/core/dex/visitors/SimplifyVisitor.java
+3
-1
jadx-core/src/main/java/jadx/core/dex/visitors/regions/IfMakerHelper.java
...in/java/jadx/core/dex/visitors/regions/IfMakerHelper.java
+45
-5
jadx-core/src/test/java/jadx/tests/internal/conditions/TestConditions14.java
...java/jadx/tests/internal/conditions/TestConditions14.java
+0
-9
jadx-core/src/test/java/jadx/tests/internal/conditions/TestTernaryInIf.java
.../java/jadx/tests/internal/conditions/TestTernaryInIf.java
+36
-0
未找到文件。
jadx-core/src/main/java/jadx/core/codegen/ConditionGen.java
浏览文件 @
e2018535
...
@@ -33,6 +33,10 @@ public class ConditionGen extends InsnGen {
...
@@ -33,6 +33,10 @@ public class ConditionGen extends InsnGen {
addCompare
(
code
,
condition
.
getCompare
());
addCompare
(
code
,
condition
.
getCompare
());
break
;
break
;
case
TERNARY:
addTernary
(
code
,
condition
);
break
;
case
NOT:
case
NOT:
addNot
(
code
,
condition
);
addNot
(
code
,
condition
);
break
;
break
;
...
@@ -43,7 +47,7 @@ public class ConditionGen extends InsnGen {
...
@@ -43,7 +47,7 @@ public class ConditionGen extends InsnGen {
break
;
break
;
default
:
default
:
throw
new
JadxRuntimeException
(
"Unknown condition mode: "
+
condition
);
throw
new
JadxRuntimeException
(
"Unknown condition mode: "
+
condition
.
getMode
()
);
}
}
}
}
...
@@ -94,6 +98,14 @@ public class ConditionGen extends InsnGen {
...
@@ -94,6 +98,14 @@ public class ConditionGen extends InsnGen {
addArg
(
code
,
secondArg
,
isArgWrapNeeded
(
secondArg
));
addArg
(
code
,
secondArg
,
isArgWrapNeeded
(
secondArg
));
}
}
private
void
addTernary
(
CodeWriter
code
,
IfCondition
condition
)
throws
CodegenException
{
add
(
code
,
condition
.
first
());
code
.
add
(
" ? "
);
add
(
code
,
condition
.
second
());
code
.
add
(
" : "
);
add
(
code
,
condition
.
third
());
}
private
void
addNot
(
CodeWriter
code
,
IfCondition
condition
)
throws
CodegenException
{
private
void
addNot
(
CodeWriter
code
,
IfCondition
condition
)
throws
CodegenException
{
code
.
add
(
'!'
);
code
.
add
(
'!'
);
wrap
(
code
,
condition
.
getArgs
().
get
(
0
));
wrap
(
code
,
condition
.
getArgs
().
get
(
0
));
...
...
jadx-core/src/main/java/jadx/core/dex/instructions/mods/TernaryInsn.java
浏览文件 @
e2018535
...
@@ -11,7 +11,11 @@ import jadx.core.utils.Utils;
...
@@ -11,7 +11,11 @@ import jadx.core.utils.Utils;
public
class
TernaryInsn
extends
InsnNode
{
public
class
TernaryInsn
extends
InsnNode
{
private
final
IfCondition
condition
;
private
IfCondition
condition
;
public
TernaryInsn
(
IfCondition
condition
,
RegisterArg
result
)
{
this
(
condition
,
result
,
LiteralArg
.
TRUE
,
LiteralArg
.
FALSE
);
}
public
TernaryInsn
(
IfCondition
condition
,
RegisterArg
result
,
InsnArg
th
,
InsnArg
els
)
{
public
TernaryInsn
(
IfCondition
condition
,
RegisterArg
result
,
InsnArg
th
,
InsnArg
els
)
{
super
(
InsnType
.
TERNARY
,
2
);
super
(
InsnType
.
TERNARY
,
2
);
...
@@ -33,6 +37,20 @@ public class TernaryInsn extends InsnNode {
...
@@ -33,6 +37,20 @@ public class TernaryInsn extends InsnNode {
return
condition
;
return
condition
;
}
}
public
void
simplifyCondition
()
{
condition
=
IfCondition
.
simplify
(
condition
);
if
(
condition
.
getMode
()
==
IfCondition
.
Mode
.
NOT
)
{
invert
();
}
}
private
void
invert
()
{
condition
=
IfCondition
.
invert
(
condition
);
InsnArg
tmp
=
getArg
(
0
);
setArg
(
0
,
getArg
(
1
));
setArg
(
1
,
tmp
);
}
@Override
@Override
public
boolean
equals
(
Object
obj
)
{
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
{
if
(
this
==
obj
)
{
...
...
jadx-core/src/main/java/jadx/core/dex/regions/IfCondition.java
浏览文件 @
e2018535
...
@@ -22,6 +22,7 @@ public final class IfCondition {
...
@@ -22,6 +22,7 @@ public final class IfCondition {
public
enum
Mode
{
public
enum
Mode
{
COMPARE
,
COMPARE
,
TERNARY
,
NOT
,
NOT
,
AND
,
AND
,
OR
OR
...
@@ -64,6 +65,10 @@ public final class IfCondition {
...
@@ -64,6 +65,10 @@ public final class IfCondition {
return
new
IfCondition
(
new
Compare
(
insn
));
return
new
IfCondition
(
new
Compare
(
insn
));
}
}
public
static
IfCondition
ternary
(
IfCondition
a
,
IfCondition
b
,
IfCondition
c
)
{
return
new
IfCondition
(
Mode
.
TERNARY
,
Arrays
.
asList
(
a
,
b
,
c
));
}
public
static
IfCondition
merge
(
Mode
mode
,
IfCondition
a
,
IfCondition
b
)
{
public
static
IfCondition
merge
(
Mode
mode
,
IfCondition
a
,
IfCondition
b
)
{
if
(
a
.
getMode
()
==
mode
)
{
if
(
a
.
getMode
()
==
mode
)
{
IfCondition
n
=
new
IfCondition
(
a
);
IfCondition
n
=
new
IfCondition
(
a
);
...
@@ -89,6 +94,10 @@ public final class IfCondition {
...
@@ -89,6 +94,10 @@ public final class IfCondition {
return
args
.
get
(
1
);
return
args
.
get
(
1
);
}
}
public
IfCondition
third
()
{
return
args
.
get
(
2
);
}
public
void
addArg
(
IfCondition
c
)
{
public
void
addArg
(
IfCondition
c
)
{
args
.
add
(
c
);
args
.
add
(
c
);
}
}
...
@@ -106,6 +115,8 @@ public final class IfCondition {
...
@@ -106,6 +115,8 @@ public final class IfCondition {
switch
(
mode
)
{
switch
(
mode
)
{
case
COMPARE:
case
COMPARE:
return
new
IfCondition
(
cond
.
getCompare
().
invert
());
return
new
IfCondition
(
cond
.
getCompare
().
invert
());
case
TERNARY:
return
ternary
(
not
(
cond
.
first
()),
cond
.
third
(),
cond
.
second
());
case
NOT:
case
NOT:
return
cond
.
first
();
return
cond
.
first
();
case
AND:
case
AND:
...
@@ -154,7 +165,10 @@ public final class IfCondition {
...
@@ -154,7 +165,10 @@ public final class IfCondition {
cond
=
new
IfCondition
(
cond
.
getMode
(),
args
);
cond
=
new
IfCondition
(
cond
.
getMode
(),
args
);
}
}
if
(
cond
.
getMode
()
==
Mode
.
NOT
&&
cond
.
first
().
getMode
()
==
Mode
.
NOT
)
{
if
(
cond
.
getMode
()
==
Mode
.
NOT
&&
cond
.
first
().
getMode
()
==
Mode
.
NOT
)
{
cond
=
cond
.
first
().
first
();
cond
=
invert
(
cond
.
first
());
}
if
(
cond
.
getMode
()
==
Mode
.
TERNARY
&&
cond
.
first
().
getMode
()
==
Mode
.
NOT
)
{
cond
=
invert
(
cond
);
}
}
// for condition with a lot of negations => make invert
// for condition with a lot of negations => make invert
...
@@ -216,6 +230,8 @@ public final class IfCondition {
...
@@ -216,6 +230,8 @@ public final class IfCondition {
switch
(
mode
)
{
switch
(
mode
)
{
case
COMPARE:
case
COMPARE:
return
compare
.
toString
();
return
compare
.
toString
();
case
TERNARY:
return
first
()
+
" ? "
+
second
()
+
" : "
+
third
();
case
NOT:
case
NOT:
return
"!"
+
first
();
return
"!"
+
first
();
case
AND:
case
AND:
...
...
jadx-core/src/main/java/jadx/core/dex/visitors/SimplifyVisitor.java
浏览文件 @
e2018535
...
@@ -65,7 +65,7 @@ public class SimplifyVisitor extends AbstractVisitor {
...
@@ -65,7 +65,7 @@ public class SimplifyVisitor extends AbstractVisitor {
simplifyIf
((
IfNode
)
insn
);
simplifyIf
((
IfNode
)
insn
);
break
;
break
;
case
TERNARY:
case
TERNARY:
simplifyTernary
((
TernaryInsn
)
insn
);
simplifyTernary
((
TernaryInsn
)
insn
);
break
;
break
;
case
INVOKE:
case
INVOKE:
...
@@ -117,6 +117,8 @@ public class SimplifyVisitor extends AbstractVisitor {
...
@@ -117,6 +117,8 @@ public class SimplifyVisitor extends AbstractVisitor {
IfCondition
condition
=
insn
.
getCondition
();
IfCondition
condition
=
insn
.
getCondition
();
if
(
condition
.
isCompare
())
{
if
(
condition
.
isCompare
())
{
simplifyIf
(
condition
.
getCompare
().
getInsn
());
simplifyIf
(
condition
.
getCompare
().
getInsn
());
}
else
{
insn
.
simplifyCondition
();
}
}
}
}
...
...
jadx-core/src/main/java/jadx/core/dex/visitors/regions/IfMakerHelper.java
浏览文件 @
e2018535
...
@@ -101,6 +101,11 @@ public class IfMakerHelper {
...
@@ -101,6 +101,11 @@ public class IfMakerHelper {
return
c1
.
size
()
==
c2
.
size
()
&&
c1
.
containsAll
(
c2
);
return
c1
.
size
()
==
c2
.
size
()
&&
c1
.
containsAll
(
c2
);
}
}
static
IfInfo
searchNestedIf
(
IfInfo
info
)
{
IfInfo
tmp
=
mergeNestedIfNodes
(
info
);
return
tmp
!=
null
?
tmp
:
info
;
}
static
IfInfo
mergeNestedIfNodes
(
IfInfo
currentIf
)
{
static
IfInfo
mergeNestedIfNodes
(
IfInfo
currentIf
)
{
BlockNode
curThen
=
currentIf
.
getThenBlock
();
BlockNode
curThen
=
currentIf
.
getThenBlock
();
BlockNode
curElse
=
currentIf
.
getElseBlock
();
BlockNode
curElse
=
currentIf
.
getElseBlock
();
...
@@ -126,12 +131,13 @@ public class IfMakerHelper {
...
@@ -126,12 +131,13 @@ public class IfMakerHelper {
if
(!
RegionMaker
.
isEqualPaths
(
curElse
,
nextIf
.
getElseBlock
())
if
(!
RegionMaker
.
isEqualPaths
(
curElse
,
nextIf
.
getElseBlock
())
&&
!
RegionMaker
.
isEqualPaths
(
curThen
,
nextIf
.
getThenBlock
()))
{
&&
!
RegionMaker
.
isEqualPaths
(
curThen
,
nextIf
.
getThenBlock
()))
{
// complex condition, run additional checks
// complex condition, run additional checks
if
(
checkConditionBranches
(
curThen
,
curElse
)
||
checkConditionBranches
(
curElse
,
curThen
))
{
if
(
checkConditionBranches
(
curThen
,
curElse
)
||
checkConditionBranches
(
curElse
,
curThen
))
{
return
null
;
return
null
;
}
}
BlockNode
otherBranchBlock
=
followThenBranch
?
curElse
:
curThen
;
BlockNode
otherBranchBlock
=
followThenBranch
?
curElse
:
curThen
;
if
(!
isPathExists
(
nextIf
.
getIfBlock
(),
otherBranchBlock
))
{
if
(!
isPathExists
(
nextIf
.
getIfBlock
(),
otherBranchBlock
))
{
return
null
;
return
checkForTernaryInCondition
(
currentIf
)
;
}
}
if
(
isPathExists
(
nextIf
.
getThenBlock
(),
otherBranchBlock
)
if
(
isPathExists
(
nextIf
.
getThenBlock
(),
otherBranchBlock
)
&&
isPathExists
(
nextIf
.
getElseBlock
(),
otherBranchBlock
))
{
&&
isPathExists
(
nextIf
.
getElseBlock
(),
otherBranchBlock
))
{
...
@@ -152,9 +158,43 @@ public class IfMakerHelper {
...
@@ -152,9 +158,43 @@ public class IfMakerHelper {
IfInfo
result
=
mergeIfInfo
(
currentIf
,
nextIf
,
followThenBranch
);
IfInfo
result
=
mergeIfInfo
(
currentIf
,
nextIf
,
followThenBranch
);
// search next nested if block
// search next nested if block
IfInfo
next
=
mergeNestedIfNodes
(
result
);
return
searchNestedIf
(
result
);
if
(
next
!=
null
)
{
}
return
next
;
private
static
IfInfo
checkForTernaryInCondition
(
IfInfo
currentIf
)
{
IfInfo
nextThen
=
getNextIf
(
currentIf
,
currentIf
.
getThenBlock
());
IfInfo
nextElse
=
getNextIf
(
currentIf
,
currentIf
.
getElseBlock
());
if
(
nextThen
==
null
||
nextElse
==
null
)
{
return
null
;
}
if
(!
nextThen
.
getIfBlock
().
getDomFrontier
().
equals
(
nextElse
.
getIfBlock
().
getDomFrontier
()))
{
return
null
;
}
nextThen
=
searchNestedIf
(
nextThen
);
nextElse
=
searchNestedIf
(
nextElse
);
if
(
nextThen
.
getThenBlock
()
==
nextElse
.
getThenBlock
()
&&
nextThen
.
getElseBlock
()
==
nextElse
.
getElseBlock
())
{
return
mergeTernaryConditions
(
currentIf
,
nextThen
,
nextElse
);
}
if
(
nextThen
.
getThenBlock
()
==
nextElse
.
getElseBlock
()
&&
nextThen
.
getElseBlock
()
==
nextElse
.
getThenBlock
())
{
nextElse
=
IfInfo
.
invert
(
nextElse
);
return
mergeTernaryConditions
(
currentIf
,
nextThen
,
nextElse
);
}
return
null
;
}
private
static
IfInfo
mergeTernaryConditions
(
IfInfo
currentIf
,
IfInfo
nextThen
,
IfInfo
nextElse
)
{
IfCondition
newCondition
=
IfCondition
.
ternary
(
currentIf
.
getCondition
(),
nextThen
.
getCondition
(),
nextElse
.
getCondition
());
IfInfo
result
=
new
IfInfo
(
newCondition
,
nextThen
.
getThenBlock
(),
nextThen
.
getElseBlock
());
result
.
setIfBlock
(
currentIf
.
getIfBlock
());
result
.
getMergedBlocks
().
addAll
(
currentIf
.
getMergedBlocks
());
result
.
getMergedBlocks
().
addAll
(
nextThen
.
getMergedBlocks
());
result
.
getMergedBlocks
().
addAll
(
nextElse
.
getMergedBlocks
());
for
(
BlockNode
blockNode
:
result
.
getMergedBlocks
())
{
blockNode
.
add
(
AFlag
.
SKIP
);
}
}
return
result
;
return
result
;
}
}
...
...
jadx-core/src/test/java/jadx/tests/internal/conditions/TestConditions14.java
浏览文件 @
e2018535
...
@@ -20,15 +20,6 @@ public class TestConditions14 extends InternalJadxTest {
...
@@ -20,15 +20,6 @@ public class TestConditions14 extends InternalJadxTest {
System
.
out
.
println
(
"1"
);
System
.
out
.
println
(
"1"
);
return
true
;
return
true
;
}
}
// public static boolean test2(Object a, Object b) {
// if (a == null ? b != null : !a.equals(b)) {
// return false;
// }
// System.out.println("2");
// return true;
// }
}
}
@Test
@Test
...
...
jadx-core/src/test/java/jadx/tests/internal/conditions/TestTernaryInIf.java
0 → 100644
浏览文件 @
e2018535
package
jadx.tests.internal.conditions
;
import
jadx.api.InternalJadxTest
;
import
jadx.core.dex.nodes.ClassNode
;
import
org.junit.Test
;
import
static
jadx
.
tests
.
utils
.
JadxMatchers
.
containsOne
;
import
static
org
.
hamcrest
.
CoreMatchers
.
containsString
;
import
static
org
.
hamcrest
.
CoreMatchers
.
not
;
import
static
org
.
junit
.
Assert
.
assertThat
;
public
class
TestTernaryInIf
extends
InternalJadxTest
{
public
static
class
TestCls
{
public
boolean
test1
(
boolean
a
,
boolean
b
,
boolean
c
)
{
return
a
?
b
:
c
;
}
public
int
test2
(
boolean
a
,
boolean
b
,
boolean
c
)
{
return
(
a
?
b
:
c
)
?
1
:
2
;
}
}
@Test
public
void
test
()
{
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
System
.
out
.
println
(
code
);
assertThat
(
code
,
containsOne
(
"return a ? b : c;"
));
assertThat
(
code
,
containsOne
(
"return (a ? b : c) ? 1 : 2;"
));
assertThat
(
code
,
not
(
containsString
(
"if"
)));
assertThat
(
code
,
not
(
containsString
(
"else"
)));
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录