-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
201 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/scala/ScalaBasic/DynamicProxy1/MatchService.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package ScalaBasic.DynamicProxy1 | ||
|
||
// https://www.bilibili.com/video/BV12N411R726?p=270&spm_id_from=pageDriver | ||
|
||
import java.lang.reflect.Proxy | ||
|
||
class MatchService { | ||
|
||
// create a person | ||
val tom = getPersonInfo("tom", "male", "cook") | ||
|
||
// get our own proxy (represents the tom instance) | ||
val OwnerProxy = getOwnerProxy(tom) | ||
|
||
println("Name = " + OwnerProxy.gatName()) // tom | ||
//println("Interests = " + OwnerProxy.gatInterests()) // cook | ||
|
||
OwnerProxy.setInterests("hiking") | ||
//println("Interests = " + OwnerProxy.gatInterests()) // hiking | ||
|
||
OwnerProxy.setScore(100) | ||
//println("Score = " + OwnerProxy.gatScore()) // still 0 | ||
|
||
// method | ||
|
||
def getPersonInfo(name:String, gender:String, interests:String):PersonBean = { | ||
val person = new PersonBeanImplement() | ||
person.setName(name) | ||
person.setGender(gender) | ||
person.setInterests(interests) | ||
|
||
person | ||
} | ||
|
||
def getOwnerProxy(person: PersonBean):PersonBean = { | ||
Proxy.newProxyInstance( | ||
person.getClass().getClassLoader, | ||
person.getClass().getInterfaces(), | ||
new OwnerInvocationHandler(person) | ||
).asInstanceOf[PersonBean] | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/scala/ScalaBasic/DynamicProxy1/NonOwnerInvocationHandler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ScalaBasic.DynamicProxy1 | ||
|
||
// https://www.bilibili.com/video/BV12N411R726?p=270&spm_id_from=pageDriver | ||
|
||
import java.lang.reflect.{InvocationHandler, Method} | ||
|
||
// proxy used by OTHER USERS | ||
class NonOwnerInvocationHandler extends InvocationHandler{ | ||
|
||
// attr | ||
var person:PersonBean = _ | ||
|
||
// constructor | ||
def this(person:PersonBean) { | ||
this | ||
this.person = person | ||
} | ||
|
||
// method | ||
@throws(classOf[Throwable]) | ||
override def invoke(proxy: Any, method: Method, args: Array[AnyRef]): AnyRef = { | ||
|
||
// if `get` method, then we invoke directly | ||
if (method.getName().startsWith("get")){ | ||
return method.invoke(person) | ||
|
||
// *** OTHER USERS can use below methods (setScore) | ||
} else if (method.getName().equals("setScore")){ | ||
return method.invoke(person, Integer.valueOf(args(0).toString)) | ||
|
||
// OTHER USERS CAN'T use set method | ||
} else if (method.getName().equals("set")){ | ||
return new IllegalAccessException() | ||
} | ||
null | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/scala/ScalaBasic/DynamicProxy1/OwnerInvocationHandler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package ScalaBasic.DynamicProxy1 | ||
|
||
// https://www.bilibili.com/video/BV12N411R726?p=270&spm_id_from=pageDriver | ||
|
||
import java.lang.reflect.{InvocationHandler, Method} | ||
|
||
// proxy used by ourselves | ||
class OwnerInvocationHandler extends InvocationHandler{ | ||
|
||
// attr | ||
// the instance is going to be called | ||
var person:PersonBean = _ | ||
|
||
// constructor | ||
def this(person:PersonBean){ | ||
this | ||
this.person = person | ||
} | ||
|
||
// method | ||
/** | ||
* 1) the proxy here will work with OwnerInvocationHandler together | ||
*/ | ||
@throws(classOf[Throwable]) | ||
override def invoke(proxy: scala.Any, method: Method, args: Array[AnyRef]): AnyRef = { | ||
|
||
// if `get` method, then we invoke directly | ||
if (method.getName().startsWith("get")){ | ||
return method.invoke(person) | ||
|
||
// we CAN NOT score ourselves (setScore) | ||
} else if (method.getName().equals("setScore")){ | ||
return new IllegalAccessException() | ||
|
||
// if `set` method, then we invoke directly | ||
} else if (method.getName().equals("set")){ | ||
return method.invoke(person, args(0).toString) | ||
} | ||
null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package ScalaBasic.DynamicProxy1 | ||
|
||
// https://www.bilibili.com/video/BV12N411R726?p=270&spm_id_from=pageDriver | ||
|
||
// a scala trait, similar as interface in java | ||
trait PersonBean { | ||
|
||
// getter, setter | ||
def gatName():String | ||
|
||
def gatGender():String | ||
|
||
def gatInterests:String | ||
|
||
def gatScore():Int | ||
|
||
def setName(name:String) | ||
|
||
def setGender(gender:String) | ||
|
||
def setInterests(interests:String) | ||
|
||
def setScore(score:Int) | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/scala/ScalaBasic/DynamicProxy1/PersonBeanImplement.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package ScalaBasic.DynamicProxy1 | ||
|
||
// https://www.bilibili.com/video/BV12N411R726?p=270&spm_id_from=pageDriver | ||
|
||
// the class is going to be used | ||
class PersonBeanImplement extends PersonBean { | ||
|
||
// attr | ||
var name = "" | ||
var gender = "" | ||
var interests = "" | ||
var score:Int = _ | ||
|
||
// method | ||
override def gatName(): String = { | ||
name | ||
} | ||
|
||
override def gatGender(): String = { | ||
gender | ||
} | ||
|
||
// can be used by ourselves, but CAN'T be used by OTHER users | ||
override def gatInterests: String = { | ||
interests | ||
} | ||
|
||
override def gatScore(): Int = { | ||
score | ||
} | ||
|
||
override def setName(name: String): Unit = { | ||
this.name = name | ||
} | ||
|
||
override def setGender(gender: String): Unit = { | ||
this.gender = gender | ||
} | ||
|
||
override def setInterests(interests: String): Unit = { | ||
this.interests = interests | ||
} | ||
|
||
// can't be used by ourselves, but can be used by OTHER users | ||
override def setScore(score: Int): Unit = { | ||
this.score = score | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ScalaBasic.DynamicProxy1 | ||
|
||
// https://www.bilibili.com/video/BV12N411R726?p=270&spm_id_from=pageDriver | ||
|
||
object app extends App { | ||
var mMatchService: MatchService = new MatchService() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,6 @@ object MyRemoteClient { | |
// for (i <- 1 to 5){ | ||
// new MyRemoteClient().go() | ||
// } | ||
|
||
} | ||
} |