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
017c6b4d
Commit
017c6b4d
authored
Sep 22, 2014
by
Skylot
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core tests: compile decompiled code
parent
d55cd5fb
Hide whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
242 additions
and
11 deletions
+242
-11
ClassGen.java
jadx-core/src/main/java/jadx/core/codegen/ClassGen.java
+5
-0
IntegrationTest.java
jadx-core/src/test/java/jadx/tests/api/IntegrationTest.java
+59
-1
CharSequenceJavaFileObject.java
...a/jadx/tests/api/compiler/CharSequenceJavaFileObject.java
+19
-0
ClassFileManager.java
...c/test/java/jadx/tests/api/compiler/ClassFileManager.java
+37
-0
DynamicCompiler.java
...rc/test/java/jadx/tests/api/compiler/DynamicCompiler.java
+72
-0
JavaClassObject.java
...rc/test/java/jadx/tests/api/compiler/JavaClassObject.java
+25
-0
TestWrongCode.java
...e/src/test/java/jadx/tests/integration/TestWrongCode.java
+1
-0
TestFieldIncrement2.java
...ava/jadx/tests/integration/arith/TestFieldIncrement2.java
+4
-4
TestArrayFill2.java
...st/java/jadx/tests/integration/arrays/TestArrayFill2.java
+4
-3
TestSwitchOverEnum.java
...java/jadx/tests/integration/enums/TestSwitchOverEnum.java
+9
-3
TestAnonymousClass3.java
...ava/jadx/tests/integration/inner/TestAnonymousClass3.java
+1
-0
TestArrayForEachNegative.java
...adx/tests/integration/loops/TestArrayForEachNegative.java
+1
-0
TestSequentialLoops.java
...ava/jadx/tests/integration/loops/TestSequentialLoops.java
+1
-0
TestIssue13a.java
...test/java/jadx/tests/integration/others/TestIssue13a.java
+1
-0
TestTryCatch4.java
...t/java/jadx/tests/integration/trycatch/TestTryCatch4.java
+1
-0
TestTryCatch5.java
...t/java/jadx/tests/integration/trycatch/TestTryCatch5.java
+1
-0
TestConstructor.java
...-core/src/test/java/jadx/tests/smali/TestConstructor.java
+1
-0
No files found.
jadx-core/src/main/java/jadx/core/codegen/ClassGen.java
View file @
017c6b4d
...
...
@@ -116,6 +116,11 @@ public class ClassGen {
.
remove
(
AccessFlags
.
ACC_STATIC
);
}
// 'static' modifier not allowed for top classes (not inner)
if
(!
cls
.
getClassInfo
().
isInner
())
{
af
=
af
.
remove
(
AccessFlags
.
ACC_STATIC
);
}
annotationGen
.
addForClass
(
clsCode
);
insertSourceFileInfo
(
clsCode
,
cls
);
clsCode
.
startLine
(
af
.
makeString
());
...
...
jadx-core/src/test/java/jadx/tests/api/IntegrationTest.java
View file @
017c6b4d
package
jadx
.
tests
.
api
;
import
jadx.api.JadxInternalAccess
;
import
jadx.api.DefaultJadxArgs
;
import
jadx.api.JadxDecompiler
;
import
jadx.api.JadxInternalAccess
;
import
jadx.core.Jadx
;
import
jadx.core.dex.attributes.AFlag
;
import
jadx.core.dex.attributes.AType
;
...
...
@@ -12,11 +12,13 @@ import jadx.core.dex.visitors.DepthTraversal;
import
jadx.core.dex.visitors.IDexTreeVisitor
;
import
jadx.core.utils.exceptions.JadxException
;
import
jadx.core.utils.files.FileUtils
;
import
jadx.tests.api.compiler.DynamicCompiler
;
import
jadx.tests.api.utils.TestUtils
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.lang.reflect.Method
;
import
java.net.URISyntaxException
;
import
java.net.URL
;
import
java.util.ArrayList
;
...
...
@@ -39,6 +41,9 @@ public abstract class IntegrationTest extends TestUtils {
protected
String
outDir
=
"test-out-tmp"
;
protected
boolean
compile
=
true
;
private
DynamicCompiler
dynamicCompiler
;
public
ClassNode
getClassNode
(
Class
<?>
clazz
)
{
try
{
File
jar
=
getJarForClass
(
clazz
);
...
...
@@ -67,6 +72,7 @@ public abstract class IntegrationTest extends TestUtils {
// don't unload class
checkCode
(
cls
);
compile
(
cls
);
return
cls
;
}
...
...
@@ -109,6 +115,52 @@ public abstract class IntegrationTest extends TestUtils {
return
null
;
}
void
compile
(
ClassNode
cls
)
{
if
(!
compile
)
{
return
;
}
try
{
dynamicCompiler
=
new
DynamicCompiler
(
cls
);
boolean
result
=
dynamicCompiler
.
compile
();
assertTrue
(
"Compilation failed on code: \n\n"
+
cls
.
getCode
()
+
"\n"
,
result
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
fail
(
e
.
getMessage
());
}
}
public
Object
invoke
(
String
method
)
throws
Exception
{
return
invoke
(
method
,
new
Class
[
0
]);
}
public
Object
invoke
(
String
method
,
Class
[]
types
,
Object
...
args
)
{
Method
mth
=
getReflectMethod
(
method
,
types
);
return
invoke
(
mth
,
args
);
}
public
Method
getReflectMethod
(
String
method
,
Class
...
types
)
{
assertNotNull
(
"dynamicCompiler not ready"
,
dynamicCompiler
);
try
{
return
dynamicCompiler
.
getMethod
(
method
,
types
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
fail
(
e
.
getMessage
());
}
return
null
;
}
public
Object
invoke
(
Method
mth
,
Object
...
args
)
{
assertNotNull
(
"dynamicCompiler not ready"
,
dynamicCompiler
);
assertNotNull
(
"unknown method"
,
mth
);
try
{
return
dynamicCompiler
.
invoke
(
mth
,
args
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
fail
(
e
.
getMessage
());
}
return
null
;
}
public
File
getJarForClass
(
Class
<?>
cls
)
throws
IOException
{
String
path
=
cls
.
getPackage
().
getName
().
replace
(
'.'
,
'/'
);
List
<
File
>
list
=
getClassFilesWithInners
(
cls
);
...
...
@@ -159,6 +211,12 @@ public abstract class IntegrationTest extends TestUtils {
return
list
;
}
// Try to make test class compilable
@Deprecated
public
void
disableCompilation
()
{
this
.
compile
=
false
;
}
// Use only for debug purpose
@Deprecated
protected
void
setOutputCFG
()
{
...
...
jadx-core/src/test/java/jadx/tests/api/compiler/CharSequenceJavaFileObject.java
0 → 100644
View file @
017c6b4d
package
jadx
.
tests
.
api
.
compiler
;
import
javax.tools.SimpleJavaFileObject
;
import
java.net.URI
;
public
class
CharSequenceJavaFileObject
extends
SimpleJavaFileObject
{
private
CharSequence
content
;
public
CharSequenceJavaFileObject
(
String
className
,
CharSequence
content
)
{
super
(
URI
.
create
(
"string:///"
+
className
.
replace
(
'.'
,
'/'
)
+
Kind
.
SOURCE
.
extension
),
Kind
.
SOURCE
);
this
.
content
=
content
;
}
@Override
public
CharSequence
getCharContent
(
boolean
ignoreEncodingErrors
)
{
return
content
;
}
}
jadx-core/src/test/java/jadx/tests/api/compiler/ClassFileManager.java
0 → 100644
View file @
017c6b4d
package
jadx
.
tests
.
api
.
compiler
;
import
javax.tools.FileObject
;
import
javax.tools.ForwardingJavaFileManager
;
import
javax.tools.JavaFileObject
;
import
javax.tools.StandardJavaFileManager
;
import
java.io.IOException
;
import
java.security.SecureClassLoader
;
import
static
javax
.
tools
.
JavaFileObject
.
Kind
;
public
class
ClassFileManager
extends
ForwardingJavaFileManager
<
StandardJavaFileManager
>
{
private
JavaClassObject
jClsObject
;
public
ClassFileManager
(
StandardJavaFileManager
standardManager
)
{
super
(
standardManager
);
}
@Override
public
JavaFileObject
getJavaFileForOutput
(
Location
location
,
String
className
,
Kind
kind
,
FileObject
sibling
)
throws
IOException
{
jClsObject
=
new
JavaClassObject
(
className
,
kind
);
return
jClsObject
;
}
@Override
public
ClassLoader
getClassLoader
(
Location
location
)
{
return
new
SecureClassLoader
()
{
@Override
protected
Class
<?>
findClass
(
String
name
)
throws
ClassNotFoundException
{
byte
[]
clsBytes
=
jClsObject
.
getBytes
();
return
super
.
defineClass
(
name
,
clsBytes
,
0
,
clsBytes
.
length
);
}
};
}
}
jadx-core/src/test/java/jadx/tests/api/compiler/DynamicCompiler.java
0 → 100644
View file @
017c6b4d
package
jadx
.
tests
.
api
.
compiler
;
import
jadx.core.dex.nodes.ClassNode
;
import
javax.tools.JavaCompiler
;
import
javax.tools.JavaFileManager
;
import
javax.tools.JavaFileObject
;
import
javax.tools.ToolProvider
;
import
java.lang.reflect.Method
;
import
java.util.ArrayList
;
import
java.util.List
;
import
static
javax
.
tools
.
JavaCompiler
.
CompilationTask
;
public
class
DynamicCompiler
{
private
final
ClassNode
clsNode
;
private
JavaFileManager
fileManager
;
private
Object
instance
;
public
DynamicCompiler
(
ClassNode
clsNode
)
{
this
.
clsNode
=
clsNode
;
}
public
boolean
compile
()
throws
Exception
{
String
fullName
=
clsNode
.
getFullName
();
String
code
=
clsNode
.
getCode
().
toString
();
JavaCompiler
compiler
=
ToolProvider
.
getSystemJavaCompiler
();
fileManager
=
new
ClassFileManager
(
compiler
.
getStandardFileManager
(
null
,
null
,
null
));
List
<
JavaFileObject
>
jFiles
=
new
ArrayList
<
JavaFileObject
>(
1
);
jFiles
.
add
(
new
CharSequenceJavaFileObject
(
fullName
,
code
));
CompilationTask
compilerTask
=
compiler
.
getTask
(
null
,
fileManager
,
null
,
null
,
null
,
jFiles
);
return
Boolean
.
TRUE
.
equals
(
compilerTask
.
call
());
}
private
void
makeInstance
()
throws
Exception
{
String
fullName
=
clsNode
.
getFullName
();
instance
=
fileManager
.
getClassLoader
(
null
).
loadClass
(
fullName
).
newInstance
();
if
(
instance
==
null
)
{
throw
new
NullPointerException
(
"Instantiation failed"
);
}
}
private
Object
getInstance
()
throws
Exception
{
if
(
instance
==
null
)
{
makeInstance
();
}
return
instance
;
}
public
Method
getMethod
(
String
method
,
Class
[]
types
)
throws
Exception
{
return
getInstance
().
getClass
().
getMethod
(
method
,
types
);
}
public
Object
invoke
(
Method
mth
,
Object
...
args
)
throws
Exception
{
return
mth
.
invoke
(
getInstance
(),
args
);
}
public
Object
invoke
(
String
method
)
throws
Exception
{
return
invoke
(
method
,
new
Class
[
0
]);
}
public
Object
invoke
(
String
method
,
Class
[]
types
,
Object
...
args
)
throws
Exception
{
Method
mth
=
getMethod
(
method
,
types
);
return
invoke
(
mth
,
args
);
}
}
jadx-core/src/test/java/jadx/tests/api/compiler/JavaClassObject.java
0 → 100644
View file @
017c6b4d
package
jadx
.
tests
.
api
.
compiler
;
import
javax.tools.SimpleJavaFileObject
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.net.URI
;
public
class
JavaClassObject
extends
SimpleJavaFileObject
{
protected
final
ByteArrayOutputStream
bos
=
new
ByteArrayOutputStream
();
public
JavaClassObject
(
String
name
,
Kind
kind
)
{
super
(
URI
.
create
(
"string:///"
+
name
.
replace
(
'.'
,
'/'
)
+
kind
.
extension
),
kind
);
}
public
byte
[]
getBytes
()
{
return
bos
.
toByteArray
();
}
@Override
public
OutputStream
openOutputStream
()
throws
IOException
{
return
bos
;
}
}
jadx-core/src/test/java/jadx/tests/integration/TestWrongCode.java
View file @
017c6b4d
...
...
@@ -26,6 +26,7 @@ public class TestWrongCode extends IntegrationTest {
@Test
public
void
test
()
{
disableCompilation
();
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
System
.
out
.
println
(
code
);
...
...
jadx-core/src/test/java/jadx/tests/integration/arith/TestFieldIncrement2.java
View file @
017c6b4d
...
...
@@ -10,11 +10,11 @@ import static org.junit.Assert.assertThat;
public
class
TestFieldIncrement2
extends
IntegrationTest
{
class
A
{
int
f
=
5
;
}
public
static
class
TestCls
{
private
static
class
A
{
int
f
=
5
;
}
public
A
a
;
public
void
test1
(
int
n
)
{
...
...
jadx-core/src/test/java/jadx/tests/integration/arrays/TestArrayFill2.java
View file @
017c6b4d
...
...
@@ -16,9 +16,10 @@ public class TestArrayFill2 extends IntegrationTest {
return
new
int
[]{
1
,
a
+
1
,
2
};
}
public
int
[]
test2
(
int
a
)
{
return
new
int
[]{
1
,
a
++,
a
*
2
};
}
// TODO
// public int[] test2(int a) {
// return new int[]{1, a++, a * 2};
// }
}
@Test
...
...
jadx-core/src/test/java/jadx/tests/integration/enums/TestSwitchOverEnum.java
View file @
017c6b4d
package
jadx
.
tests
.
integration
.
enums
;
import
jadx.tests.api.IntegrationTest
;
import
jadx.core.dex.nodes.ClassNode
;
import
jadx.tests.api.IntegrationTest
;
import
java.lang.reflect.Method
;
import
org.junit.Test
;
import
static
jadx
.
tests
.
api
.
utils
.
JadxMatchers
.
countString
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertThat
;
public
class
TestSwitchOverEnum
extends
IntegrationTest
{
...
...
@@ -20,8 +23,6 @@ public class TestSwitchOverEnum extends IntegrationTest {
return
1
;
case
TWO:
return
2
;
case
THREE:
return
3
;
}
return
0
;
}
...
...
@@ -35,5 +36,10 @@ public class TestSwitchOverEnum extends IntegrationTest {
assertThat
(
code
,
countString
(
1
,
"synthetic"
));
assertThat
(
code
,
countString
(
2
,
"switch (c) {"
));
assertThat
(
code
,
countString
(
2
,
"case ONE:"
));
Method
mth
=
getReflectMethod
(
"testEnum"
,
Count
.
class
);
assertEquals
(
1
,
invoke
(
mth
,
Count
.
ONE
));
assertEquals
(
2
,
invoke
(
mth
,
Count
.
TWO
));
assertEquals
(
0
,
invoke
(
mth
,
Count
.
THREE
));
}
}
jadx-core/src/test/java/jadx/tests/integration/inner/TestAnonymousClass3.java
View file @
017c6b4d
...
...
@@ -40,6 +40,7 @@ public class TestAnonymousClass3 extends IntegrationTest {
@Test
public
void
test
()
{
disableCompilation
();
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
System
.
out
.
println
(
code
);
...
...
jadx-core/src/test/java/jadx/tests/integration/loops/TestArrayForEachNegative.java
View file @
017c6b4d
...
...
@@ -46,6 +46,7 @@ public class TestArrayForEachNegative extends IntegrationTest {
@Test
public
void
test
()
{
disableCompilation
();
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
System
.
out
.
println
(
code
);
...
...
jadx-core/src/test/java/jadx/tests/integration/loops/TestSequentialLoops.java
View file @
017c6b4d
...
...
@@ -35,6 +35,7 @@ public class TestSequentialLoops extends IntegrationTest {
@Test
public
void
test
()
{
disableCompilation
();
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
System
.
out
.
println
(
code
);
...
...
jadx-core/src/test/java/jadx/tests/integration/others/TestIssue13a.java
View file @
017c6b4d
...
...
@@ -88,6 +88,7 @@ public class TestIssue13a extends IntegrationTest {
@Test
public
void
test
()
{
disableCompilation
();
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
System
.
out
.
println
(
code
);
...
...
jadx-core/src/test/java/jadx/tests/integration/trycatch/TestTryCatch4.java
View file @
017c6b4d
...
...
@@ -30,6 +30,7 @@ public class TestTryCatch4 extends IntegrationTest {
@Test
public
void
test
()
{
disableCompilation
();
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
System
.
out
.
println
(
code
);
...
...
jadx-core/src/test/java/jadx/tests/integration/trycatch/TestTryCatch5.java
View file @
017c6b4d
...
...
@@ -43,6 +43,7 @@ public class TestTryCatch5 extends IntegrationTest {
@Test
public
void
test
()
{
disableCompilation
();
ClassNode
cls
=
getClassNode
(
TestCls
.
class
);
String
code
=
cls
.
getCode
().
toString
();
System
.
out
.
println
(
code
);
...
...
jadx-core/src/test/java/jadx/tests/smali/TestConstructor.java
View file @
017c6b4d
...
...
@@ -14,6 +14,7 @@ public class TestConstructor extends SmaliTest {
@Test
public
void
test
()
{
disableCompilation
();
ClassNode
cls
=
getClassNodeFromSmali
(
"TestConstructor"
);
String
code
=
cls
.
getCode
().
toString
();
System
.
out
.
println
(
code
);
...
...
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