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
2b9c0927
Commit
2b9c0927
authored
Aug 01, 2015
by
Skylot
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core: fix field initialization extract from try/catch block (fix #78)
parent
bc73010d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
118 additions
and
6 deletions
+118
-6
InsnNode.java
jadx-core/src/main/java/jadx/core/dex/nodes/InsnNode.java
+15
-0
ExtractFieldInit.java
...rc/main/java/jadx/core/dex/visitors/ExtractFieldInit.java
+16
-6
TestFieldInitInTryCatch.java
...adx/tests/integration/others/TestFieldInitInTryCatch.java
+87
-0
No files found.
jadx-core/src/main/java/jadx/core/dex/nodes/InsnNode.java
View file @
2b9c0927
...
...
@@ -205,6 +205,21 @@ public class InsnNode extends LineAttrNode {
}
}
public
boolean
canReorderRecursive
()
{
if
(!
canReorder
())
{
return
false
;
}
for
(
InsnArg
arg
:
this
.
getArguments
())
{
if
(
arg
.
isInsnWrap
())
{
InsnNode
wrapInsn
=
((
InsnWrapArg
)
arg
).
getWrapInsn
();
if
(!
wrapInsn
.
canReorderRecursive
())
{
return
false
;
}
}
}
return
true
;
}
@Override
public
String
toString
()
{
return
InsnUtils
.
formatOffset
(
offset
)
+
": "
...
...
jadx-core/src/main/java/jadx/core/dex/visitors/ExtractFieldInit.java
View file @
2b9c0927
...
...
@@ -6,6 +6,8 @@ import jadx.core.dex.info.AccessInfo;
import
jadx.core.dex.info.FieldInfo
;
import
jadx.core.dex.instructions.IndexInsnNode
;
import
jadx.core.dex.instructions.InsnType
;
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.nodes.BlockNode
;
import
jadx.core.dex.nodes.ClassNode
;
...
...
@@ -189,14 +191,22 @@ public class ExtractFieldInit extends AbstractVisitor {
}
private
static
boolean
checkInsn
(
InsnNode
insn
)
{
InsnArg
arg
=
insn
.
getArg
(
0
);
if
(
arg
.
isInsnWrap
())
{
InsnNode
wrapInsn
=
((
InsnWrapArg
)
arg
).
getWrapInsn
();
if
(!
wrapInsn
.
canReorderRecursive
()
&&
insn
.
contains
(
AType
.
CATCH_BLOCK
))
{
return
false
;
}
}
else
{
return
arg
.
isLiteral
()
||
arg
.
isThis
();
}
Set
<
RegisterArg
>
regs
=
new
HashSet
<
RegisterArg
>();
insn
.
getRegisterArgs
(
regs
);
if
(
regs
.
isEmpty
())
{
return
true
;
}
for
(
RegisterArg
reg
:
regs
)
{
if
(!
reg
.
isThis
())
{
return
false
;
if
(!
regs
.
isEmpty
())
{
for
(
RegisterArg
reg
:
regs
)
{
if
(!
reg
.
isThis
())
{
return
false
;
}
}
}
return
true
;
...
...
jadx-core/src/test/java/jadx/tests/integration/others/TestFieldInitInTryCatch.java
0 → 100644
View file @
2b9c0927
package
jadx
.
tests
.
integration
.
others
;
import
jadx.core.dex.nodes.ClassNode
;
import
jadx.tests.api.IntegrationTest
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
org.junit.Test
;
import
static
jadx
.
tests
.
api
.
utils
.
JadxMatchers
.
containsLines
;
import
static
jadx
.
tests
.
api
.
utils
.
JadxMatchers
.
containsOne
;
import
static
org
.
junit
.
Assert
.
assertThat
;
public
class
TestFieldInitInTryCatch
extends
IntegrationTest
{
public
static
class
TestCls
{
private
static
final
URL
a
;
static
{
try
{
a
=
new
URL
(
"http://www.example.com/"
);
}
catch
(
MalformedURLException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
public
static
class
TestCls2
{
private
static
final
URL
[]
a
;
static
{
try
{
a
=
new
URL
[]{
new
URL
(
"http://www.example.com/"
)};
}
catch
(
MalformedURLException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
public
static
class
TestCls3
{
private
static
final
String
[]
a
;
static
{
try
{
a
=
new
String
[]{
"a"
};
// Note: follow code will not be extracted:
// a = new String[]{new String("a")};
new
URL
(
"http://www.example.com/"
);
}
catch
(
MalformedURLException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
@Test
public
void
test
()
{
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
assertThat
(
code
,
containsOne
(
"private static final URL a;"
));
assertThat
(
code
,
containsOne
(
"a = new URL(\"http://www.example.com/\");"
));
assertThat
(
code
,
containsLines
(
2
,
"try {"
,
indent
(
1
)
+
"a = new URL(\"http://www.example.com/\");"
,
"} catch (MalformedURLException e) {"
));
}
@Test
public
void
test2
()
{
ClassNode
cls
=
getClassNode
(
TestCls2
.
class
);
String
code
=
cls
.
getCode
().
toString
();
assertThat
(
code
,
containsLines
(
2
,
"try {"
,
indent
(
1
)
+
"a = new URL[]{new URL(\"http://www.example.com/\")};"
,
"} catch (MalformedURLException e) {"
));
}
@Test
public
void
test3
()
{
ClassNode
cls
=
getClassNode
(
TestCls3
.
class
);
String
code
=
cls
.
getCode
().
toString
();
assertThat
(
code
,
containsOne
(
"private static final String[] a = new String[]{\"a\"};"
));
}
}
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