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
ffc64204
Commit
ffc64204
authored
Dec 18, 2014
by
Skylot
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core: fix type check for loop over iterable.
parent
8de6190a
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
18 deletions
+69
-18
LoopRegionVisitor.java
...ava/jadx/core/dex/visitors/regions/LoopRegionVisitor.java
+26
-18
TestIterableForEach3.java
...va/jadx/tests/integration/loops/TestIterableForEach3.java
+43
-0
No files found.
jadx-core/src/main/java/jadx/core/dex/visitors/regions/LoopRegionVisitor.java
View file @
ffc64204
...
@@ -224,7 +224,8 @@ public class LoopRegionVisitor extends AbstractVisitor implements IRegionVisitor
...
@@ -224,7 +224,8 @@ public class LoopRegionVisitor extends AbstractVisitor implements IRegionVisitor
InsnArg
iterableArg
=
assignInsn
.
getArg
(
0
);
InsnArg
iterableArg
=
assignInsn
.
getArg
(
0
);
InsnNode
hasNextCall
=
useList
.
get
(
0
).
getParentInsn
();
InsnNode
hasNextCall
=
useList
.
get
(
0
).
getParentInsn
();
InsnNode
nextCall
=
useList
.
get
(
1
).
getParentInsn
();
InsnNode
nextCall
=
useList
.
get
(
1
).
getParentInsn
();
if
(!
checkInvoke
(
hasNextCall
,
"java.util.Iterator"
,
"hasNext()Z"
,
0
)
if
(
hasNextCall
==
null
||
nextCall
==
null
||
!
checkInvoke
(
hasNextCall
,
"java.util.Iterator"
,
"hasNext()Z"
,
0
)
||
!
checkInvoke
(
nextCall
,
"java.util.Iterator"
,
"next()Ljava/lang/Object;"
,
0
))
{
||
!
checkInvoke
(
nextCall
,
"java.util.Iterator"
,
"next()Ljava/lang/Object;"
,
0
))
{
return
false
;
return
false
;
}
}
...
@@ -239,7 +240,7 @@ public class LoopRegionVisitor extends AbstractVisitor implements IRegionVisitor
...
@@ -239,7 +240,7 @@ public class LoopRegionVisitor extends AbstractVisitor implements IRegionVisitor
}
else
{
}
else
{
iterVar
=
parentInsn
.
getResult
();
iterVar
=
parentInsn
.
getResult
();
InsnArg
castArg
=
BlockUtils
.
searchWrappedInsnParent
(
mth
,
parentInsn
);
InsnArg
castArg
=
BlockUtils
.
searchWrappedInsnParent
(
mth
,
parentInsn
);
if
(
castArg
!=
null
)
{
if
(
castArg
!=
null
&&
castArg
.
getParentInsn
()
!=
null
)
{
castArg
.
getParentInsn
().
replaceArg
(
castArg
,
iterVar
);
castArg
.
getParentInsn
().
replaceArg
(
castArg
,
iterVar
);
}
else
{
}
else
{
// cast not inlined
// cast not inlined
...
@@ -266,28 +267,35 @@ public class LoopRegionVisitor extends AbstractVisitor implements IRegionVisitor
...
@@ -266,28 +267,35 @@ public class LoopRegionVisitor extends AbstractVisitor implements IRegionVisitor
}
}
private
static
boolean
fixIterableType
(
InsnArg
iterableArg
,
RegisterArg
iterVar
)
{
private
static
boolean
fixIterableType
(
InsnArg
iterableArg
,
RegisterArg
iterVar
)
{
ArgType
type
=
iterableArg
.
getType
();
ArgType
iterableType
=
iterableArg
.
getType
();
if
(
type
.
isGeneric
())
{
ArgType
varType
=
iterVar
.
getType
();
ArgType
[]
genericTypes
=
type
.
getGenericTypes
();
if
(
iterableType
.
isGeneric
())
{
if
(
genericTypes
!=
null
&&
genericTypes
.
length
==
1
)
{
ArgType
[]
genericTypes
=
iterableType
.
getGenericTypes
();
if
(
genericTypes
==
null
||
genericTypes
.
length
!=
1
)
{
return
false
;
}
ArgType
gType
=
genericTypes
[
0
];
ArgType
gType
=
genericTypes
[
0
];
if
(
ArgType
.
isInstanceOf
(
gType
,
iterVar
.
getType
()
))
{
if
(
gType
.
equals
(
varType
))
{
return
true
;
return
true
;
}
else
{
LOG
.
warn
(
"Generic type differs: {} and {}"
,
type
,
iterVar
.
getType
());
}
}
if
(
gType
.
isGenericType
())
{
iterVar
.
setType
(
gType
);
return
true
;
}
if
(
ArgType
.
isInstanceOf
(
gType
,
varType
))
{
return
true
;
}
LOG
.
warn
(
"Generic type differs: {} and {}"
,
gType
,
varType
);
return
false
;
}
}
}
else
{
if
(!
iterableArg
.
isRegister
())
{
if
(!
iterableArg
.
isRegister
())
{
return
true
;
return
true
;
}
}
// TODO: add checks
// TODO: add checks
type
=
ArgType
.
generic
(
type
.
getObject
(),
new
ArgType
[]{
iterVar
.
getType
()
});
iterableType
=
ArgType
.
generic
(
iterableType
.
getObject
(),
new
ArgType
[]{
varType
});
iterableArg
.
setType
(
t
ype
);
iterableArg
.
setType
(
iterableT
ype
);
return
true
;
return
true
;
}
}
return
false
;
}
/**
/**
* Check if instruction is a interface invoke with corresponding parameters.
* Check if instruction is a interface invoke with corresponding parameters.
...
...
jadx-core/src/test/java/jadx/tests/integration/loops/TestIterableForEach3.java
0 → 100644
View file @
ffc64204
package
jadx
.
tests
.
integration
.
loops
;
import
jadx.core.dex.nodes.ClassNode
;
import
jadx.tests.api.IntegrationTest
;
import
java.util.Set
;
import
org.junit.Test
;
import
static
jadx
.
tests
.
api
.
utils
.
JadxMatchers
.
containsOne
;
import
static
org
.
junit
.
Assert
.
assertThat
;
public
class
TestIterableForEach3
extends
IntegrationTest
{
public
static
class
TestCls
<
T
extends
String
>
{
private
Set
<
T
>
a
;
private
Set
<
T
>
b
;
private
void
test
(
T
str
)
{
Set
<
T
>
set
=
str
.
length
()
==
1
?
a
:
b
;
for
(
T
s
:
set
)
{
if
(
s
.
length
()
==
str
.
length
())
{
if
(
str
.
length
()
==
0
)
{
set
.
remove
(
s
);
}
else
{
set
.
add
(
str
);
}
return
;
}
}
}
}
@Test
public
void
test
()
{
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
assertThat
(
code
,
containsOne
(
"for (T s : set) {"
));
assertThat
(
code
,
containsOne
(
"if (str.length() == 0) {"
));
// TODO move return outside 'if'
}
}
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