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
5f11f12d
Commit
5f11f12d
authored
Jul 17, 2014
by
Skylot
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core: remove redundant spaces for enums
parent
2d189505
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
217 additions
and
31 deletions
+217
-31
ClassGen.java
jadx-core/src/main/java/jadx/core/codegen/ClassGen.java
+54
-20
TestEnums.java
...re/src/test/java/jadx/tests/internal/enums/TestEnums.java
+71
-0
TestEnums2.java
...e/src/test/java/jadx/tests/internal/enums/TestEnums2.java
+54
-0
CountString.java
jadx-core/src/test/java/jadx/tests/utils/CountString.java
+13
-10
JadxMatchers.java
jadx-core/src/test/java/jadx/tests/utils/JadxMatchers.java
+25
-1
No files found.
jadx-core/src/main/java/jadx/core/codegen/ClassGen.java
View file @
5f11f12d
...
...
@@ -44,7 +44,7 @@ public class ClassGen {
private
final
boolean
fallback
;
private
final
Set
<
ClassInfo
>
imports
=
new
HashSet
<
ClassInfo
>();
private
int
clsDeclLine
=
0
;
private
int
clsDeclLine
;
public
ClassGen
(
ClassNode
cls
,
ClassGen
parentClsGen
,
boolean
fallback
)
{
this
.
cls
=
cls
;
...
...
@@ -103,7 +103,9 @@ public class ClassGen {
if
(
af
.
isInterface
())
{
af
=
af
.
remove
(
AccessFlags
.
ACC_ABSTRACT
);
}
else
if
(
af
.
isEnum
())
{
af
=
af
.
remove
(
AccessFlags
.
ACC_FINAL
).
remove
(
AccessFlags
.
ACC_ABSTRACT
);
af
=
af
.
remove
(
AccessFlags
.
ACC_FINAL
)
.
remove
(
AccessFlags
.
ACC_ABSTRACT
)
.
remove
(
AccessFlags
.
ACC_STATIC
);
}
annotationGen
.
addForClass
(
clsCode
);
...
...
@@ -203,29 +205,50 @@ public class ClassGen {
private
void
addInnerClasses
(
CodeWriter
code
,
ClassNode
cls
)
throws
CodegenException
{
for
(
ClassNode
innerCls
:
cls
.
getInnerClasses
())
{
if
(
innerCls
.
contains
(
AFlag
.
DONT_GENERATE
)
||
innerCls
.
isAnonymous
())
{
continue
;
}
ClassGen
inClGen
=
new
ClassGen
(
innerCls
,
getParentGen
(),
fallback
);
code
.
newLine
();
inClGen
.
addClassCode
(
code
);
imports
.
addAll
(
inClGen
.
getImports
());
}
}
private
boolean
isInnerClassesPresents
()
{
for
(
ClassNode
innerCls
:
cls
.
getInnerClasses
())
{
if
(!
innerCls
.
isAnonymous
())
{
ClassGen
inClGen
=
new
ClassGen
(
innerCls
,
getParentGen
(),
fallback
);
code
.
newLine
();
inClGen
.
addClassCode
(
code
);
imports
.
addAll
(
inClGen
.
getImports
());
return
true
;
}
}
return
false
;
}
private
void
addMethods
(
CodeWriter
code
)
{
for
(
MethodNode
mth
:
cls
.
getMethods
())
{
if
(
mth
.
contains
(
AFlag
.
DONT_GENERATE
))
{
continue
;
}
if
(
code
.
getLine
()
!=
clsDeclLine
)
{
code
.
newLine
();
}
try
{
addMethod
(
code
,
mth
);
}
catch
(
Exception
e
)
{
String
msg
=
ErrorsCounter
.
methodError
(
mth
,
"Method generation error"
,
e
);
code
.
startLine
(
"/* "
+
msg
+
CodeWriter
.
NL
+
Utils
.
getStackTrace
(
e
)
+
" */"
);
}
}
}
private
boolean
isMethodsPresents
()
{
for
(
MethodNode
mth
:
cls
.
getMethods
())
{
if
(!
mth
.
contains
(
AFlag
.
DONT_GENERATE
))
{
try
{
if
(
code
.
getLine
()
!=
clsDeclLine
)
{
code
.
newLine
();
}
addMethod
(
code
,
mth
);
}
catch
(
Exception
e
)
{
String
msg
=
ErrorsCounter
.
methodError
(
mth
,
"Method generation error"
,
e
);
code
.
startLine
(
"/* "
+
msg
+
CodeWriter
.
NL
+
Utils
.
getStackTrace
(
e
)
+
" */"
);
}
return
true
;
}
}
return
false
;
}
private
void
addMethod
(
CodeWriter
code
,
MethodNode
mth
)
throws
CodegenException
{
...
...
@@ -284,6 +307,15 @@ public class ClassGen {
}
}
private
boolean
isFieldsPresents
()
{
for
(
FieldNode
field
:
cls
.
getFields
())
{
if
(!
field
.
contains
(
AFlag
.
DONT_GENERATE
))
{
return
true
;
}
}
return
false
;
}
private
void
addEnumFields
(
CodeWriter
code
)
throws
CodegenException
{
EnumClassAttr
enumFields
=
cls
.
get
(
AType
.
ENUM_CLASS
);
if
(
enumFields
==
null
)
{
...
...
@@ -310,21 +342,23 @@ public class ClassGen {
code
.
add
(
')'
);
}
if
(
f
.
getCls
()
!=
null
)
{
code
.
add
(
' '
);
new
ClassGen
(
f
.
getCls
(),
this
,
fallback
).
addClassBody
(
code
);
}
if
(
it
.
hasNext
())
{
code
.
add
(
','
);
}
}
if
(
enumFields
.
getFields
().
isEmpty
())
{
code
.
startLine
();
if
(
isMethodsPresents
()
||
isFieldsPresents
()
||
isInnerClassesPresents
())
{
if
(
enumFields
.
getFields
().
isEmpty
())
{
code
.
startLine
();
}
code
.
add
(
';'
);
}
code
.
add
(
';'
);
code
.
newLine
();
}
public
void
useType
(
CodeWriter
code
,
ArgType
type
)
{
final
PrimitiveType
stype
=
type
.
getPrimitiveType
();
PrimitiveType
stype
=
type
.
getPrimitiveType
();
if
(
stype
==
null
)
{
code
.
add
(
type
.
toString
());
}
else
if
(
stype
==
PrimitiveType
.
OBJECT
)
{
...
...
jadx-core/src/test/java/jadx/tests/internal/enums/TestEnums.java
0 → 100644
View file @
5f11f12d
package
jadx
.
tests
.
internal
.
enums
;
import
jadx.api.InternalJadxTest
;
import
jadx.core.dex.nodes.ClassNode
;
import
org.junit.Test
;
import
static
jadx
.
tests
.
utils
.
JadxMatchers
.
containsLines
;
import
static
org
.
junit
.
Assert
.
assertThat
;
public
class
TestEnums
extends
InternalJadxTest
{
public
static
class
TestCls
{
public
enum
EmptyEnum
{
}
public
enum
EmptyEnum2
{
;
public
static
void
mth
()
{
}
}
public
enum
Direction
{
NORTH
,
SOUTH
,
EAST
,
WEST
}
public
enum
Singleton
{
INSTANCE
;
public
String
test
()
{
return
""
;
}
}
}
@Test
public
void
test
()
{
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
System
.
out
.
println
(
code
);
assertThat
(
code
,
containsLines
(
1
,
"public enum EmptyEnum {"
,
"}"
));
assertThat
(
code
,
containsLines
(
1
,
"public enum EmptyEnum2 {"
,
indent
(
1
)
+
";"
,
""
,
indent
(
1
)
+
"public static void mth() {"
,
indent
(
1
)
+
"}"
,
"}"
));
assertThat
(
code
,
containsLines
(
1
,
"public enum Direction {"
,
indent
(
1
)
+
"NORTH,"
,
indent
(
1
)
+
"SOUTH,"
,
indent
(
1
)
+
"EAST,"
,
indent
(
1
)
+
"WEST"
,
"}"
));
assertThat
(
code
,
containsLines
(
1
,
"public enum Singleton {"
,
indent
(
1
)
+
"INSTANCE;"
,
""
,
indent
(
1
)
+
"public String test() {"
,
indent
(
2
)
+
"return \"\";"
,
indent
(
1
)
+
"}"
,
"}"
));
}
}
jadx-core/src/test/java/jadx/tests/internal/enums/TestEnums2.java
0 → 100644
View file @
5f11f12d
package
jadx
.
tests
.
internal
.
enums
;
import
jadx.api.InternalJadxTest
;
import
jadx.core.dex.nodes.ClassNode
;
import
jadx.tests.utils.JadxMatchers
;
import
org.junit.Test
;
import
static
org
.
junit
.
Assert
.
assertThat
;
public
class
TestEnums2
extends
InternalJadxTest
{
public
static
class
TestCls
{
public
enum
Operation
{
PLUS
{
int
apply
(
int
x
,
int
y
)
{
return
x
+
y
;
}
},
MINUS
{
int
apply
(
int
x
,
int
y
)
{
return
x
-
y
;
}
};
abstract
int
apply
(
int
x
,
int
y
);
}
}
@Test
public
void
test
()
{
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
System
.
out
.
println
(
code
);
assertThat
(
code
,
JadxMatchers
.
containsLines
(
1
,
"public enum Operation {"
,
indent
(
1
)
+
"PLUS {"
,
indent
(
2
)
+
"int apply(int x, int y) {"
,
indent
(
3
)
+
"return x + y;"
,
indent
(
2
)
+
"}"
,
indent
(
1
)
+
"},"
,
indent
(
1
)
+
"MINUS {"
,
indent
(
2
)
+
"int apply(int x, int y) {"
,
indent
(
3
)
+
"return x - y;"
,
indent
(
2
)
+
"}"
,
indent
(
1
)
+
"};"
,
""
,
indent
(
1
)
+
"abstract int apply(int i, int i2);"
,
"}"
));
}
}
jadx-core/src/test/java/jadx/tests/utils/CountString.java
View file @
5f11f12d
package
jadx
.
tests
.
utils
;
import
jadx.api.TestUtils
;
import
org.hamcrest.Description
;
import
org.hamcrest.core.SubstringMatcher
;
public
class
CountString
extends
SubstringMatcher
{
...
...
@@ -13,21 +16,21 @@ public class CountString extends SubstringMatcher {
@Override
protected
boolean
evalSubstringOf
(
String
string
)
{
return
this
.
count
==
count
Str
(
string
,
sub
string
);
return
this
.
count
==
count
(
string
);
}
@Override
protected
String
relationship
()
{
return
"containing "
+
count
+
" occurrence of"
;
return
"containing <"
+
count
+
"> occurrence of"
;
}
@Override
public
void
describeMismatchSafely
(
String
item
,
Description
mismatchDescription
)
{
mismatchDescription
.
appendText
(
"found "
).
appendValue
(
count
(
item
))
.
appendText
(
" in \""
).
appendText
(
item
).
appendText
(
"\""
);
}
private
static
int
countStr
(
String
string
,
String
substring
)
{
int
cnt
=
0
;
int
idx
=
0
;
while
((
idx
=
string
.
indexOf
(
substring
,
idx
))
!=
-
1
)
{
idx
++;
cnt
++;
}
return
cnt
;
private
int
count
(
String
string
)
{
return
TestUtils
.
count
(
string
,
substring
);
}
}
jadx-core/src/test/java/jadx/tests/utils/JadxMatchers.java
View file @
5f11f12d
package
jadx
.
tests
.
utils
;
import
jadx.api.TestUtils
;
import
jadx.core.codegen.CodeWriter
;
import
org.hamcrest.Matcher
;
public
class
JadxMatchers
{
...
...
@@ -9,6 +12,27 @@ public class JadxMatchers {
}
public
static
Matcher
<
String
>
containsOne
(
String
substring
)
{
return
new
CountString
(
1
,
substring
);
return
countString
(
1
,
substring
);
}
public
static
Matcher
<
String
>
containsLines
(
String
...
lines
)
{
StringBuilder
sb
=
new
StringBuilder
();
for
(
String
line
:
lines
)
{
sb
.
append
(
line
).
append
(
CodeWriter
.
NL
);
}
return
countString
(
1
,
sb
.
toString
());
}
public
static
Matcher
<
String
>
containsLines
(
int
commonIndent
,
String
...
lines
)
{
String
indent
=
TestUtils
.
indent
(
commonIndent
);
StringBuilder
sb
=
new
StringBuilder
();
for
(
String
line
:
lines
)
{
if
(!
line
.
isEmpty
())
{
sb
.
append
(
indent
);
sb
.
append
(
line
);
}
sb
.
append
(
CodeWriter
.
NL
);
}
return
countString
(
1
,
sb
.
toString
());
}
}
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