FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OnJava8-Examples/reflection/SelectingMethods.java at master · CSapprenticeWT/OnJava8-Examples · GitHub
CSapprenticeWT
OnJava8-Examples
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OnJava8-Examples
/
reflection
/
SelectingMethods.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
64 lines (60 loc) · 1.61 KB
Breadcrumbs
OnJava8-Examples
/
reflection
/
SelectingMethods.java
Copy path
File metadata and controls
64 lines (60 loc) · 1.61 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// reflection/SelectingMethods.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Looking for particular methods in a dynamic proxy
import
java
.
lang
.
reflect
.*;
class
MethodSelector
implements
InvocationHandler
{
private
Object
proxied
;
MethodSelector
(
Object
proxied
) {
this
.
proxied
=
proxied
;
}
@
Override
public
Object
invoke
(
Object
proxy
,
Method
method
,
Object
[]
args
)
throws
Throwable
{
if
(
method
.
getName
().
equals
(
"interesting"
))
System
.
out
.
println
(
"Proxy detected the interesting method"
);
return
method
.
invoke
(
proxied
,
args
);
}
}
interface
SomeMethods
{
void
boring1
();
void
boring2
();
void
interesting
(
String
arg
);
void
boring3
();
}
class
Implementation
implements
SomeMethods
{
@
Override
public
void
boring1
() {
System
.
out
.
println
(
"boring1"
);
}
@
Override
public
void
boring2
() {
System
.
out
.
println
(
"boring2"
);
}
@
Override
public
void
interesting
(
String
arg
) {
System
.
out
.
println
(
"interesting "
+
arg
);
}
@
Override
public
void
boring3
() {
System
.
out
.
println
(
"boring3"
);
}
}
class
SelectingMethods
{
public
static
void
main
(
String
[]
args
) {
SomeMethods
proxy
=
(
SomeMethods
)
Proxy
.
newProxyInstance
(
SomeMethods
.
class
.
getClassLoader
(),
new
Class
[]{
SomeMethods
.
class
},
new
MethodSelector
(
new
Implementation
()));
proxy
.
boring1
();
proxy
.
boring2
();
proxy
.
interesting
(
"bonobo"
);
proxy
.
boring3
();
}
}
/* Output:
boring1
boring2
Proxy detected the interesting method
interesting bonobo
boring3
*/
Back
|
FazBrowse Home
|
New Git URL