Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
S
SandHook
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
Administrator
SandHook
Commits
92f2647b
Commit
92f2647b
authored
Jan 16, 2019
by
swift_gan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix hook for ctor
parent
3be93512
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
12 deletions
+33
-12
HookWrapper.java
...src/main/java/com/swift/sandhook/wrapper/HookWrapper.java
+33
-12
No files found.
app/src/main/java/com/swift/sandhook/wrapper/HookWrapper.java
View file @
92f2647b
...
@@ -2,6 +2,8 @@ package com.swift.sandhook.wrapper;
...
@@ -2,6 +2,8 @@ package com.swift.sandhook.wrapper;
import
com.swift.sandhook.SandHook
;
import
com.swift.sandhook.SandHook
;
import
java.lang.reflect.Constructor
;
import
java.lang.reflect.Member
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Modifier
;
import
java.lang.reflect.Modifier
;
import
java.util.HashMap
;
import
java.util.HashMap
;
...
@@ -19,16 +21,16 @@ public class HookWrapper {
...
@@ -19,16 +21,16 @@ public class HookWrapper {
Class
targetHookClass
=
getTargetHookClass
(
clazz
);
Class
targetHookClass
=
getTargetHookClass
(
clazz
);
if
(
targetHookClass
==
null
)
if
(
targetHookClass
==
null
)
throw
new
HookErrorException
(
"error hook wrapper class :"
+
clazz
.
getName
());
throw
new
HookErrorException
(
"error hook wrapper class :"
+
clazz
.
getName
());
Map
<
Me
thod
,
HookEntity
>
hookEntityMap
=
getHookMethods
(
targetHookClass
,
clazz
);
Map
<
Me
mber
,
HookEntity
>
hookEntityMap
=
getHookMethods
(
targetHookClass
,
clazz
);
for
(
HookEntity
entity:
hookEntityMap
.
values
())
{
for
(
HookEntity
entity:
hookEntityMap
.
values
())
{
if
(
entity
.
target
!=
null
&&
entity
.
hook
!=
null
)
{
if
(
entity
.
target
!=
null
&&
entity
.
hook
!=
null
)
{
SandHook
.
hook
(
entity
.
target
,
entity
.
hook
,
entity
.
backup
);
}
}
}
}
}
}
private
static
Map
<
Me
thod
,
HookEntity
>
getHookMethods
(
Class
targetHookClass
,
Class
<?>
hookWrapperClass
)
throws
HookErrorException
{
private
static
Map
<
Me
mber
,
HookEntity
>
getHookMethods
(
Class
targetHookClass
,
Class
<?>
hookWrapperClass
)
throws
HookErrorException
{
Map
<
Me
thod
,
HookEntity
>
hookEntityMap
=
new
HashMap
<>();
Map
<
Me
mber
,
HookEntity
>
hookEntityMap
=
new
HashMap
<>();
Method
[]
methods
=
hookWrapperClass
.
getDeclaredMethods
();
Method
[]
methods
=
hookWrapperClass
.
getDeclaredMethods
();
if
(
methods
==
null
&&
methods
.
length
==
0
)
if
(
methods
==
null
&&
methods
.
length
==
0
)
throw
new
HookErrorException
(
"error hook wrapper class :"
+
targetHookClass
.
getName
());
throw
new
HookErrorException
(
"error hook wrapper class :"
+
targetHookClass
.
getName
());
...
@@ -36,13 +38,17 @@ public class HookWrapper {
...
@@ -36,13 +38,17 @@ public class HookWrapper {
HookMethod
hookMethodAnno
=
method
.
getAnnotation
(
HookMethod
.
class
);
HookMethod
hookMethodAnno
=
method
.
getAnnotation
(
HookMethod
.
class
);
HookMethodBackup
hookMethodBackupAnno
=
method
.
getAnnotation
(
HookMethodBackup
.
class
);
HookMethodBackup
hookMethodBackupAnno
=
method
.
getAnnotation
(
HookMethodBackup
.
class
);
String
methodName
;
String
methodName
;
Me
thod
foundMethod
;
Me
mber
foundMethod
;
Class
[]
pars
;
Class
[]
pars
;
if
(
hookMethodAnno
!=
null
)
{
if
(
hookMethodAnno
!=
null
)
{
methodName
=
hookMethodAnno
.
value
();
methodName
=
hookMethodAnno
.
value
();
pars
=
parseMethodPars
(
method
);
pars
=
parseMethodPars
(
method
);
try
{
try
{
foundMethod
=
targetHookClass
.
getDeclaredMethod
(
methodName
,
pars
);
if
(
methodName
.
equals
(
"<init>"
))
{
foundMethod
=
targetHookClass
.
getConstructor
(
pars
);
}
else
{
foundMethod
=
targetHookClass
.
getDeclaredMethod
(
methodName
,
pars
);
}
}
catch
(
NoSuchMethodException
e
)
{
}
catch
(
NoSuchMethodException
e
)
{
throw
new
HookErrorException
(
"can not find target method: "
+
methodName
,
e
);
throw
new
HookErrorException
(
"can not find target method: "
+
methodName
,
e
);
}
}
...
@@ -57,7 +63,11 @@ public class HookWrapper {
...
@@ -57,7 +63,11 @@ public class HookWrapper {
methodName
=
hookMethodBackupAnno
.
value
();
methodName
=
hookMethodBackupAnno
.
value
();
pars
=
parseMethodPars
(
method
);
pars
=
parseMethodPars
(
method
);
try
{
try
{
foundMethod
=
targetHookClass
.
getDeclaredMethod
(
methodName
,
pars
);
if
(
methodName
.
equals
(
"<init>"
))
{
foundMethod
=
targetHookClass
.
getConstructor
(
pars
);
}
else
{
foundMethod
=
targetHookClass
.
getDeclaredMethod
(
methodName
,
pars
);
}
}
catch
(
NoSuchMethodException
e
)
{
}
catch
(
NoSuchMethodException
e
)
{
throw
new
HookErrorException
(
"can not find target method: "
+
methodName
,
e
);
throw
new
HookErrorException
(
"can not find target method: "
+
methodName
,
e
);
}
}
...
@@ -116,11 +126,17 @@ public class HookWrapper {
...
@@ -116,11 +126,17 @@ public class HookWrapper {
}
}
}
}
public
static
void
checkSignature
(
Me
thod
origin
,
Method
fake
,
Class
[]
originPars
)
throws
HookErrorException
{
public
static
void
checkSignature
(
Me
mber
origin
,
Method
fake
,
Class
[]
originPars
)
throws
HookErrorException
{
if
(!
Modifier
.
isStatic
(
fake
.
getModifiers
()))
if
(!
Modifier
.
isStatic
(
fake
.
getModifiers
()))
throw
new
HookErrorException
(
"hook method must static! - "
+
fake
.
getName
());
throw
new
HookErrorException
(
"hook method must static! - "
+
fake
.
getName
());
if
(
origin
.
getReturnType
()
!=
fake
.
getReturnType
()
&&
!
origin
.
getReturnType
().
isAssignableFrom
(
origin
.
getReturnType
()))
if
(
origin
instanceof
Constructor
)
{
throw
new
HookErrorException
(
"error return type! - "
+
fake
.
getName
());
if
(!
fake
.
getReturnType
().
equals
(
Void
.
TYPE
))
throw
new
HookErrorException
(
"error return type! - "
+
fake
.
getName
());
}
else
if
(
origin
instanceof
Method
)
{
Class
originRet
=
((
Method
)
origin
).
getReturnType
();
if
(
originRet
!=
fake
.
getReturnType
()
&&
!
originRet
.
isAssignableFrom
(
originRet
))
throw
new
HookErrorException
(
"error return type! - "
+
fake
.
getName
());
}
Class
[]
fakePars
=
fake
.
getParameterTypes
();
Class
[]
fakePars
=
fake
.
getParameterTypes
();
if
(
fakePars
==
null
)
if
(
fakePars
==
null
)
fakePars
=
new
Class
[
0
];
fakePars
=
new
Class
[
0
];
...
@@ -149,13 +165,18 @@ public class HookWrapper {
...
@@ -149,13 +165,18 @@ public class HookWrapper {
public
static
class
HookEntity
{
public
static
class
HookEntity
{
public
Me
thod
target
;
public
Me
mber
target
;
public
Method
hook
;
public
Method
hook
;
public
Method
backup
;
public
Method
backup
;
public
HookEntity
(
Me
thod
target
)
{
public
HookEntity
(
Me
mber
target
)
{
this
.
target
=
target
;
this
.
target
=
target
;
}
}
public
boolean
isCtor
()
{
return
target
instanceof
Constructor
;
}
}
}
}
}
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