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
7b70d617
Commit
7b70d617
authored
Sep 19, 2015
by
Skylot
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core: fix variables inline (#86)
parent
261ba464
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
147 additions
and
37 deletions
+147
-37
CodeShrinker.java
...re/src/main/java/jadx/core/dex/visitors/CodeShrinker.java
+31
-37
TestIssue86.java
.../test/java/jadx/tests/integration/inline/TestIssue86.java
+116
-0
No files found.
jadx-core/src/main/java/jadx/core/dex/visitors/CodeShrinker.java
View file @
7b70d617
...
...
@@ -23,8 +23,6 @@ import java.util.List;
import
java.util.ListIterator
;
import
java.util.Set
;
import
org.jetbrains.annotations.Nullable
;
public
class
CodeShrinker
extends
AbstractVisitor
{
@Override
...
...
@@ -38,6 +36,7 @@ public class CodeShrinker extends AbstractVisitor {
}
for
(
BlockNode
block
:
mth
.
getBasicBlocks
())
{
shrinkBlock
(
mth
,
block
);
simplifyMoveInsns
(
block
);
}
}
...
...
@@ -231,53 +230,28 @@ public class CodeShrinker extends AbstractVisitor {
if
(
assignBlock
!=
null
&&
assignInsn
!=
arg
.
getParentInsn
()
&&
canMoveBetweenBlocks
(
assignInsn
,
assignBlock
,
block
,
argsInfo
.
getInsn
()))
{
if
(
inline
(
arg
,
assignInsn
,
assignBlock
,
mth
))
{
InsnList
.
remove
(
assignBlock
,
assignInsn
);
}
inline
(
arg
,
assignInsn
,
assignBlock
);
}
}
}
}
if
(!
wrapList
.
isEmpty
())
{
for
(
WrapInfo
wrapInfo
:
wrapList
)
{
inline
(
wrapInfo
.
getArg
(),
wrapInfo
.
getInsn
(),
block
,
mth
);
}
for
(
WrapInfo
wrapInfo
:
wrapList
)
{
insnList
.
remove
(
wrapInfo
.
getInsn
());
inline
(
wrapInfo
.
getArg
(),
wrapInfo
.
getInsn
(),
block
);
}
}
}
private
static
boolean
inline
(
RegisterArg
arg
,
InsnNode
insn
,
@Nullable
BlockNode
block
,
MethodNode
mth
)
{
private
static
boolean
inline
(
RegisterArg
arg
,
InsnNode
insn
,
BlockNode
block
)
{
InsnNode
parentInsn
=
arg
.
getParentInsn
();
// replace move instruction if needed
if
(
parentInsn
!=
null
)
{
switch
(
parentInsn
.
getType
())
{
case
MOVE:
{
if
(
block
==
null
)
{
block
=
BlockUtils
.
getBlockByInsn
(
mth
,
parentInsn
);
}
if
(
block
!=
null
)
{
int
index
=
InsnList
.
getIndex
(
block
.
getInstructions
(),
parentInsn
);
if
(
index
!=
-
1
)
{
insn
.
setResult
(
parentInsn
.
getResult
());
insn
.
copyAttributesFrom
(
parentInsn
);
insn
.
setOffset
(
parentInsn
.
getOffset
());
block
.
getInstructions
().
set
(
index
,
insn
);
return
true
;
}
}
break
;
}
case
RETURN:
{
parentInsn
.
setSourceLine
(
insn
.
getSourceLine
());
break
;
}
}
if
(
parentInsn
!=
null
&&
parentInsn
.
getType
()
==
InsnType
.
RETURN
)
{
parentInsn
.
setSourceLine
(
insn
.
getSourceLine
());
}
boolean
replaced
=
arg
.
wrapInstruction
(
insn
)
!=
null
;
if
(
replaced
)
{
InsnList
.
remove
(
block
,
insn
);
}
// simple case
return
arg
.
wrapInstruction
(
insn
)
!=
null
;
return
replaced
;
}
private
static
boolean
canMoveBetweenBlocks
(
InsnNode
assignInsn
,
BlockNode
assignBlock
,
...
...
@@ -320,4 +294,24 @@ public class CodeShrinker extends AbstractVisitor {
}
throw
new
JadxRuntimeException
(
"Can't process instruction move : "
+
assignBlock
);
}
private
static
void
simplifyMoveInsns
(
BlockNode
block
)
{
List
<
InsnNode
>
insns
=
block
.
getInstructions
();
int
size
=
insns
.
size
();
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
InsnNode
insn
=
insns
.
get
(
i
);
if
(
insn
.
getType
()
==
InsnType
.
MOVE
)
{
// replace 'move' with wrapped insn
InsnArg
arg
=
insn
.
getArg
(
0
);
if
(
arg
.
isInsnWrap
())
{
InsnNode
wrapInsn
=
((
InsnWrapArg
)
arg
).
getWrapInsn
();
wrapInsn
.
setResult
(
insn
.
getResult
());
wrapInsn
.
copyAttributesFrom
(
insn
);
wrapInsn
.
setOffset
(
insn
.
getOffset
());
wrapInsn
.
remove
(
AFlag
.
WRAPPED
);
block
.
getInstructions
().
set
(
i
,
wrapInsn
);
}
}
}
}
}
jadx-core/src/test/java/jadx/tests/integration/inline/TestIssue86.java
0 → 100644
View file @
7b70d617
package
jadx
.
tests
.
integration
.
inline
;
import
jadx.core.dex.nodes.ClassNode
;
import
jadx.tests.api.IntegrationTest
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.junit.Test
;
import
static
jadx
.
tests
.
api
.
utils
.
JadxMatchers
.
containsOne
;
import
static
org
.
junit
.
Assert
.
assertThat
;
public
class
TestIssue86
extends
IntegrationTest
{
public
static
class
TestCls
{
private
static
final
String
SERVER_ERR
=
"server-err"
;
private
static
final
String
NOT_FOUND
=
"not-found"
;
private
static
final
String
LIST_TAG
=
"list-tag"
;
private
static
final
String
TEMP_TAG
=
"temp-tag"
;
private
static
final
String
MIN_TAG
=
"min-tag"
;
private
static
final
String
MAX_TAG
=
"max-tag"
;
private
static
final
String
MILLIS_TAG
=
"millis-tag"
;
private
static
final
String
WEATHER_TAG
=
"weather-tag"
;
private
static
final
String
DESC_TAG
=
"desc-tag"
;
private
List
<
Day
>
test
(
String
response
)
{
List
<
Day
>
reportList
=
new
ArrayList
<
Day
>();
try
{
System
.
out
.
println
(
response
);
if
(
response
!=
null
&&
(
response
.
startsWith
(
SERVER_ERR
)
||
response
.
startsWith
(
NOT_FOUND
)))
{
return
reportList
;
}
JSONObject
jsonObj
=
new
JSONObject
(
response
);
JSONArray
days
=
jsonObj
.
getJSONArray
(
LIST_TAG
);
for
(
int
i
=
0
;
i
<
days
.
length
();
i
++)
{
JSONObject
c
=
days
.
getJSONObject
(
i
);
long
millis
=
c
.
getLong
(
MILLIS_TAG
);
JSONObject
temp
=
c
.
getJSONObject
(
TEMP_TAG
);
String
max
=
temp
.
getString
(
MAX_TAG
);
String
min
=
temp
.
getString
(
MIN_TAG
);
JSONArray
weather
=
c
.
getJSONArray
(
WEATHER_TAG
);
String
weatherDesc
=
weather
.
getJSONObject
(
0
).
getString
(
DESC_TAG
);
Day
d
=
new
Day
();
d
.
setMilis
(
millis
);
d
.
setMinTmp
(
min
);
d
.
setMaxTmp
(
max
);
d
.
setWeatherDesc
(
weatherDesc
);
reportList
.
add
(
d
);
}
}
catch
(
JSONException
e
)
{
e
.
printStackTrace
();
}
return
reportList
;
}
private
static
class
Day
{
public
void
setMilis
(
long
milis
)
{
}
public
void
setMinTmp
(
String
min
)
{
}
public
void
setMaxTmp
(
String
max
)
{
}
public
void
setWeatherDesc
(
String
weatherDesc
)
{
}
}
private
static
class
JSONObject
{
public
JSONObject
(
String
response
)
{
}
public
JSONArray
getJSONArray
(
String
tag
)
throws
JSONException
{
return
null
;
}
public
JSONObject
getJSONObject
(
String
tag
)
throws
JSONException
{
return
null
;
}
public
String
getString
(
String
tag
)
throws
JSONException
{
return
null
;
}
public
long
getLong
(
String
tag
)
throws
JSONException
{
return
0
;
}
}
private
class
JSONArray
{
public
JSONObject
getJSONObject
(
int
i
)
throws
JSONException
{
return
null
;
}
public
int
length
()
{
return
0
;
}
}
private
class
JSONException
extends
Exception
{
private
static
final
long
serialVersionUID
=
-
4358405506584551910L
;
}
}
@Test
public
void
test
()
{
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
assertThat
(
code
,
containsOne
(
"response.startsWith(NOT_FOUND)"
));
}
}
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