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
1358a05a
Commit
1358a05a
authored
Sep 21, 2013
by
Skylot
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core: omit redundant brackets in conditions
parent
1b0a8990
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
87 additions
and
10 deletions
+87
-10
InsnGen.java
jadx-core/src/main/java/jadx/core/codegen/InsnGen.java
+13
-6
RegionGen.java
jadx-core/src/main/java/jadx/core/codegen/RegionGen.java
+28
-4
TestRedundantBrackets.java
.../test/java/jadx/tests/internal/TestRedundantBrackets.java
+46
-0
No files found.
jadx-core/src/main/java/jadx/core/codegen/InsnGen.java
View file @
1358a05a
...
...
@@ -214,7 +214,7 @@ public class InsnGen {
break
;
case
CHECK_CAST:
case
CAST:
case
CAST:
{
boolean
wrap
=
state
.
contains
(
IGState
.
BODY_ONLY
);
if
(
wrap
)
code
.
add
(
"("
);
...
...
@@ -225,7 +225,7 @@ public class InsnGen {
if
(
wrap
)
code
.
add
(
")"
);
break
;
}
case
ARITH:
makeArith
((
ArithNode
)
insn
,
code
,
state
);
break
;
...
...
@@ -263,11 +263,18 @@ public class InsnGen {
code
.
add
(
String
.
format
(
"(%1$s > %2$s ? 1 : (%1$s == %2$s ? 0 : -1))"
,
arg
(
insn
,
0
),
arg
(
insn
,
1
)));
break
;
case
INSTANCE_OF:
code
.
add
(
'('
).
add
(
arg
(
insn
,
0
)).
add
(
" instanceof "
)
.
add
(
useType
((
ArgType
)
((
IndexInsnNode
)
insn
).
getIndex
())).
add
(
')'
);
case
INSTANCE_OF:
{
boolean
wrap
=
state
.
contains
(
IGState
.
BODY_ONLY
);
if
(
wrap
)
code
.
add
(
"("
);
code
.
add
(
arg
(
insn
,
0
));
code
.
add
(
" instanceof "
);
code
.
add
(
useType
((
ArgType
)
((
IndexInsnNode
)
insn
).
getIndex
()));
if
(
wrap
)
{
code
.
add
(
")"
);
}
break
;
}
case
CONSTRUCTOR:
makeConstructor
((
ConstructorInsn
)
insn
,
code
,
state
);
break
;
...
...
jadx-core/src/main/java/jadx/core/codegen/RegionGen.java
View file @
1358a05a
...
...
@@ -5,10 +5,13 @@ import jadx.core.dex.attributes.AttributeType;
import
jadx.core.dex.attributes.DeclareVariableAttr
;
import
jadx.core.dex.attributes.ForceReturnAttr
;
import
jadx.core.dex.attributes.IAttribute
;
import
jadx.core.dex.instructions.ArithNode
;
import
jadx.core.dex.instructions.IfOp
;
import
jadx.core.dex.instructions.InsnType
;
import
jadx.core.dex.instructions.SwitchNode
;
import
jadx.core.dex.instructions.args.ArgType
;
import
jadx.core.dex.instructions.args.InsnArg
;
import
jadx.core.dex.instructions.args.InsnWrapArg
;
import
jadx.core.dex.instructions.args.LiteralArg
;
import
jadx.core.dex.instructions.args.RegisterArg
;
import
jadx.core.dex.nodes.IBlock
;
...
...
@@ -186,17 +189,38 @@ public class RegionGen extends InsnGen {
&&
secondArg
.
isLiteral
()
&&
secondArg
.
getType
().
equals
(
ArgType
.
BOOLEAN
))
{
LiteralArg
lit
=
(
LiteralArg
)
secondArg
;
if
(
lit
.
getLiteral
()
==
0
)
if
(
lit
.
getLiteral
()
==
0
)
{
op
=
op
.
invert
();
}
if
(
op
==
IfOp
.
EQ
)
{
return
arg
(
firstArg
);
// == true
return
arg
(
firstArg
,
false
);
// == true
}
else
if
(
op
==
IfOp
.
NE
)
{
return
"!"
+
arg
(
firstArg
);
// != true
}
LOG
.
warn
(
ErrorsCounter
.
formatErrorMsg
(
mth
,
"Unsupported boolean condition "
+
op
.
getSymbol
()));
}
return
arg
(
firstArg
)
+
" "
+
op
.
getSymbol
()
+
" "
+
arg
(
secondArg
);
return
arg
(
firstArg
,
isWrapNeeded
(
firstArg
))
+
" "
+
op
.
getSymbol
()
+
" "
+
arg
(
secondArg
,
isWrapNeeded
(
secondArg
));
}
private
boolean
isWrapNeeded
(
InsnArg
arg
)
{
if
(!
arg
.
isInsnWrap
())
{
return
false
;
}
InsnNode
insn
=
((
InsnWrapArg
)
arg
).
getWrapInsn
();
if
(
insn
.
getType
()
==
InsnType
.
ARITH
)
{
ArithNode
arith
=
((
ArithNode
)
insn
);
switch
(
arith
.
getOp
())
{
case
ADD:
case
SUB:
case
MUL:
case
DIV:
case
REM:
return
false
;
}
}
return
true
;
}
private
CodeWriter
makeSwitch
(
SwitchRegion
sw
,
CodeWriter
code
)
throws
CodegenException
{
...
...
jadx-core/src/test/java/jadx/tests/internal/TestRedundantBrackets.java
0 → 100644
View file @
1358a05a
package
jadx
.
tests
.
internal
;
import
jadx.api.InternalJadxTest
;
import
jadx.core.dex.nodes.ClassNode
;
import
org.junit.Test
;
import
static
org
.
hamcrest
.
CoreMatchers
.
containsString
;
import
static
org
.
junit
.
Assert
.
assertThat
;
public
class
TestRedundantBrackets
extends
InternalJadxTest
{
public
static
class
TestCls
{
public
boolean
method
(
String
str
)
{
return
str
.
indexOf
(
'a'
)
!=
-
1
;
}
public
int
method2
(
Object
obj
)
{
if
(
obj
instanceof
String
)
{
return
((
String
)
obj
).
length
();
}
return
0
;
}
public
int
method3
(
int
a
,
int
b
)
{
if
(
a
+
b
<
10
)
{
return
a
;
}
if
((
a
&
b
)
!=
0
)
{
return
a
*
b
;
}
return
b
;
}
}
@Test
public
void
test
()
{
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
// assertThat(code, not(containsString("(-1)")));
assertThat
(
code
,
containsString
(
"if (obj instanceof String)"
));
assertThat
(
code
,
containsString
(
"if (a + b < 10)"
));
assertThat
(
code
,
containsString
(
"if ((a & b) != 0)"
));
}
}
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