-
Notifications
You must be signed in to change notification settings - Fork 44
Support bindInterceptor[T1, T2] #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,13 @@ | |
*/ | ||
package net.codingwell.scalaguice | ||
|
||
import com.google.inject.{PrivateModule, PrivateBinder, Binder, Scope, AbstractModule} | ||
import binder._ | ||
import com.google.inject.matcher.Matchers | ||
import com.google.inject.{PrivateModule, PrivateBinder, Binder, Scope, AbstractModule} | ||
import java.lang.annotation.Annotation | ||
import javax.inject.Provider | ||
import org.aopalliance.intercept.MethodInterceptor | ||
|
||
|
||
/** | ||
* Allows binding via type parameters. Mix into <code>AbstractModule</code> | ||
|
@@ -33,6 +36,8 @@ import javax.inject.Provider | |
* bind(classOf[CreditCardPaymentService]) | ||
* bind(new TypeLiteral[Bar[Foo]]{}).to(classOf[FooBarImpl]) | ||
* bind(classOf[PaymentService]).to(classOf[CreditCardPaymentService]) | ||
* | ||
* bindInterceptor(Matchers.any(), Matchers.annotatedWith(classOf[Logging]), new LoggingInterceptor()) | ||
* } | ||
* } | ||
* }}} | ||
|
@@ -44,6 +49,8 @@ import javax.inject.Provider | |
* bind[CreditCardPaymentService] | ||
* bind[Bar[Foo]].to[FooBarImpl] | ||
* bind[PaymentService].to[CreditCardPaymentService] | ||
* | ||
* bindInterceptor[Logging, LoggingInterceptor] | ||
* } | ||
* } | ||
* }}} | ||
|
@@ -63,6 +70,12 @@ trait InternalModule[B <: Binder] { | |
val self = myBinder.bind(typeLiteral[T]) | ||
} | ||
|
||
protected[this] def bindInterceptor[T <: Annotation : Manifest, T2: Manifest] { | ||
val myBinder = binderAccess | ||
val interceptor = manifest[T2].runtimeClass.newInstance.asInstanceOf[MethodInterceptor] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can drop the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is mean? [error] found : Any There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh weird. You're right: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
myBinder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(cls[T]), interceptor) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to see Matchers as a parameter to bindInterceptor. Matching all objects is not always the right answer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also add a call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's "requestInjection(interceptor)" this mean? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Plesae help me one thing, I tried bindInterceptor[m: Matcher[_ >: Class[_]], A <: Annotation : Manifest, I <: MethodInterceptor : Manifest] It show compile error "com.google.inject.matcher.Matcher[_ >: Class[_]] does not take type parameters" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If your interceptor has any @Inject setters then it will invoke them when the injector is created. About the matcher as a parameter: What I had meant was to do something like this: def bindInterceptor[A ..., I ...] {
bindInterceptor[A, I](Matchers.any())
}
def bindInterceptor[A ..., I ...](m: Matcher[_ >: Class[_]]) {
...
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow really thank you. |
||
} | ||
|
||
protected[this] def bindScope[T <: Annotation : Manifest](scope: Scope) = binderAccess.bindScope(cls[T], scope) | ||
protected[this] def requestStaticInjection[T: Manifest](): Unit = binderAccess.requestStaticInjection(cls[T]) | ||
protected[this] def getProvider[T: Manifest] = binderAccess.getProvider(cls[T]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, you've got to stick to the copyright already in the project. Please omit this block. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
* Copyright 2010-2014 Peerapat Asoktummarungsri | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package net.codingwell.scalaguice; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
|
||
/** | ||
* There is no support for runtime annotation in Scala, so far | ||
* Java interfaces need to be used. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface AOP { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2010-2014 Peerapat Asoktummarungsri | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package net.codingwell.scalaguice; | ||
|
||
import org.aopalliance.intercept.MethodInterceptor; | ||
import org.aopalliance.intercept.MethodInvocation; | ||
|
||
public class AOPI implements MethodInterceptor { | ||
|
||
@Override | ||
public Object invoke(MethodInvocation invocation) throws Throwable { | ||
return null; | ||
} | ||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ import org.scalatest.{Matchers, WordSpec} | |
|
||
import com.google.inject._ | ||
|
||
class ScalaModuleSpec extends WordSpec with Matchers { | ||
class ScalaModuleSpec extends WordSpec with Matchers { | ||
|
||
"A Scala Guice module" should { | ||
|
||
|
@@ -123,6 +123,15 @@ class ScalaModuleSpec extends WordSpec with Matchers { | |
twoStrings.first should be ("first") | ||
twoStrings.second should be ("second") | ||
} | ||
|
||
"allow binding annotation interceptro" in { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: interceptor |
||
val module = new AbstractModule with ScalaModule { | ||
def configure() = { | ||
bindInterceptor[AOP, AOPI] | ||
} | ||
} | ||
Guice.createInjector(module).getInstance(classOf[AOPI]) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to see a test that checks that the interceptor intercepts an annotated method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For bonus points, you're welcome to add a test with a class annotated with AOP. But by no means is that a requirement to merge this! |
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rename the type parameters to something more like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok