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
Hide 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
InsnArg
iterableArg
=
assignInsn
.
getArg
(
0
);
InsnNode
hasNextCall
=
useList
.
get
(
0
).
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
))
{
return
false
;
}
...
...
@@ -239,7 +240,7 @@ public class LoopRegionVisitor extends AbstractVisitor implements IRegionVisitor
}
else
{
iterVar
=
parentInsn
.
getResult
();
InsnArg
castArg
=
BlockUtils
.
searchWrappedInsnParent
(
mth
,
parentInsn
);
if
(
castArg
!=
null
)
{
if
(
castArg
!=
null
&&
castArg
.
getParentInsn
()
!=
null
)
{
castArg
.
getParentInsn
().
replaceArg
(
castArg
,
iterVar
);
}
else
{
// cast not inlined
...
...
@@ -266,27 +267,34 @@ public class LoopRegionVisitor extends AbstractVisitor implements IRegionVisitor
}
private
static
boolean
fixIterableType
(
InsnArg
iterableArg
,
RegisterArg
iterVar
)
{
ArgType
type
=
iterableArg
.
getType
();
if
(
type
.
isGeneric
())
{
ArgType
[]
genericTypes
=
type
.
getGenericTypes
();
if
(
genericTypes
!=
null
&&
genericTypes
.
length
==
1
)
{
ArgType
gType
=
genericTypes
[
0
];
if
(
ArgType
.
isInstanceOf
(
gType
,
iterVar
.
getType
()))
{
return
true
;
}
else
{
LOG
.
warn
(
"Generic type differs: {} and {}"
,
type
,
iterVar
.
getType
());
}
ArgType
iterableType
=
iterableArg
.
getType
();
ArgType
varType
=
iterVar
.
getType
();
if
(
iterableType
.
isGeneric
())
{
ArgType
[]
genericTypes
=
iterableType
.
getGenericTypes
();
if
(
genericTypes
==
null
||
genericTypes
.
length
!=
1
)
{
return
false
;
}
}
else
{
if
(!
iterableArg
.
isRegister
())
{
ArgType
gType
=
genericTypes
[
0
];
if
(
gType
.
equals
(
varType
))
{
return
true
;
}
if
(
gType
.
isGenericType
())
{
iterVar
.
setType
(
gType
);
return
true
;
}
// TODO: add checks
type
=
ArgType
.
generic
(
type
.
getObject
(),
new
ArgType
[]{
iterVar
.
getType
()});
iterableArg
.
setType
(
type
);
if
(
ArgType
.
isInstanceOf
(
gType
,
varType
))
{
return
true
;
}
LOG
.
warn
(
"Generic type differs: {} and {}"
,
gType
,
varType
);
return
false
;
}
if
(!
iterableArg
.
isRegister
())
{
return
true
;
}
return
false
;
// TODO: add checks
iterableType
=
ArgType
.
generic
(
iterableType
.
getObject
(),
new
ArgType
[]{
varType
});
iterableArg
.
setType
(
iterableType
);
return
true
;
}
/**
...
...
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