|
AOP
1, IntroductionShowcase available at: Gwtent_Showcase And a local runnable showcase included in download page. Now Aspect-Oriented Programming is available in GWT. The following advice type supported by GWTENT(Compatible with AspectJ annotation).
And for pointcut, we support both Google Guice matcher classes and AspectJ expression(a subset of AspectJ expression) Please see here for all interceptors in showcase project. Please download the sample projecthere 2, Pointcut2.1, OverviewPointcut: A predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP: we support AspectJ pointcut language and Google Guice matcher classes. 2.1.1, Client-side code?Please note, All pointcut match code is runing at compile time, it's mean you can write truely java code in your matcher class and without any limit in GWT client-side code. In compile time and host-mode, GWTENT require AspectJ jars to dealwith AspectJ expression, but after your compile it, all are javascript. 2.2, AspectJ pointcut expression styleto be continue 2.2, Google Guice matcher styleWe support "matchclass(the class name which implement com.gwtent.aop.matcher.ClassMethodMatcher)", for example:
the matcher class will looks like this: public class TestMatcher implements ClassMethodMatcher {
public Matcher<? super Class<?>> getClassMatcher() {
return Matchers.subclassesOf(Phone.class);
}
public Matcher<? super Method> getMethodMatcher() {
return Matchers.returns(Matchers.only(Phone.Receiver.class));
//return Matchers.any();
}
}3, Advice3.1, Arguments Binding
@AfterReturning( pointcut="**", returning="retVal" ) public void afterReturning(Object retVal) @AfterThrowing( throwing="e" } public void afterThrowing(Throwable e) 3.2, @AroundAdvice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception. 3.2.1, Example @Around("execution(* com.gwtent.sample.client.Phone.call(java.lang.Number))")
public Object invoke(MethodInvocation invocation) throws Throwable {
invocation.proceed();
System.out.println("Do something in PhoneRedirectInterceptor...");
return new Receiver("Alberto's Pizza Place");
}3.3, @BeforeAdvice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception). 3.3.1, Example @Before("execution(* com.gwtent.sample.client.Phone.call(java.lang.Number))")
public void beforeCall(MethodInvocation invocation) {
for (Object arg : invocation.getArguments()){
System.out.println("Do something in PhoneLoggerInterceptor, Before...");
if (arg instanceof Number)
System.out.println("Call to: " + arg);
}
}3.4, @AfterAfter (finally) Advice to be executed regardless of the means by which a join point exits (normal or exceptional return). 3.4.1, Example @After("execution(* com.gwtent.sample.client.Phone.call(java.lang.Number))")
public void afterCall(MethodInvocation invocation) {
for (Object arg : invocation.getArguments()){
System.out.println("Do something in PhoneLoggerInterceptor, After...");
if (arg instanceof Number)
System.out.println("After Call: " + arg);
}
}3.5, @AfterReturningAdvice to be executed after a join point completes normally: for example, if a method returns without throwing an exception. 3.5.1, Example @AfterReturning(pointcut = "execution(* com.gwtent.sample.client.Phone.call(java.lang.Number))",
returning = "returnValue")
public void afterReturningCall(MethodInvocation invocation, Object returnValue) {
System.out.println("Do something in PhoneLoggerInterceptor, AfterReturning...");
if ((returnValue != null)){
if (returnValue instanceof Number)
System.out.println("Returning Number: " + returnValue.toString());
else
System.out.println("Returning Object: " + returnValue.toString());
}else{
System.out.println("Returning Object is NULL?");
}
}3.6, @AfterThrowingAdvice to be executed if a method exits by throwing an exception. 3.6.1, Example @AfterThrowing(pointcut="execution(* com.gwtent.sample.client.Phone.call(java.lang.Number))",
throwing="e")
public void phoneCallErrorLog(MethodInvocation invocation, Throwable e){
System.out.println("PhoneCallErrorLog: " + e.getMessage());
}4, The whole exampleClassThe normal gwt class, for now you need implement "Aspectable" mark interface
public class Phone implements Aspectable {
private static final Map<Number, Receiver> RECEIVERS = new HashMap<Number, Receiver>();
static {
RECEIVERS.put(123456789, new Receiver("Aunt Jane"));
RECEIVERS.put(111111111, new Receiver("Santa"));
}
/**
* the phone number, like your home number
*/
private Number number;
public Receiver call(Number number) {
System.out.println("The call here...");
Receiver result = RECEIVERS.get(number);
if (result != null)
return result;
else
throw new NumberNotFoundException("Can't found receiver, number: " + number);
}
public Receiver call(String number){
System.out.println("The call here...");
return RECEIVERS.get(111111111);
}
public String toString(){
return super.toString();
}
public void setNumber(Number number) {
this.number = number;
}
public Number getNumber() {
return number;
}
public static class NumberNotFoundException extends RuntimeException{
/**
*
*/
private static final long serialVersionUID = 1L;
public NumberNotFoundException(String msg){
super(msg);
}
}
public static class Receiver {
private final String name;
public Receiver(String name) {
this.name = name;
}
@Override
public String toString() {
return getClass().getName() + "[name=" + name + "]";
}
}Aspect Classes
@Aspect
public static class PhoneLoggerInterceptor {
//execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
@Before("execution(* com.gwtent.client.test.aop.Phone.call(java.lang.Number))")
public void beforeCall(MethodInvocation invocation) {
for (Object arg : invocation.getArguments()){
System.out.println("Do something in PhoneLoggerInterceptor, Before...");
if (arg instanceof Number)
System.out.println("Call to: " + arg);
}
}
@AfterReturning(pointcut = "execution(* com.gwtent.client.test.aop.Phone.call(java.lang.Number))",
returning = "returnValue")
public void afterReturningCall(MethodInvocation invocation, Object returnValue) {
System.out.println("Do something in PhoneLoggerInterceptor, AfterReturning...");
if ((returnValue != null)){
if (returnValue instanceof Number)
System.out.println("Returning Number: " + returnValue.toString());
else
System.out.println("Returning Object: " + returnValue.toString());
}else{
System.out.println("Returning Object is NULL?");
}
}
@After("execution(* com.gwtent.sample.client.Phone.call(java.lang.Number))")
public void afterCall(MethodInvocation invocation) {
for (Object arg : invocation.getArguments()){
System.out.println("Do something in PhoneLoggerInterceptor, After...");
if (arg instanceof Number)
System.out.println("After Call: " + arg);
}
}
}
@Aspect
public static class PhoneRedirectInterceptor {
@Around("execution(* com.gwtent.client.test.aop.Phone.call(java.lang.Number))")
public Object invoke(MethodInvocation invocation) throws Throwable {
invocation.proceed();
System.out.println("Do something in PhoneRedirectInterceptor...");
return new Receiver("Alberto's Pizza Place");
}
}Using it public void testAOP(){
Phone phone = (Phone) GWT.create(Phone.class);
Receiver auntJane = phone.call(123456789);
System.out.println(auntJane);
}The Result - System OutputDo something in PhoneLoggerInterceptor, Before... Call to: 123456789 Validate, you cann't dail to 0, current Number(most time it's null): null The call here... Do something in PhoneRedirectInterceptor... Do something in PhoneLoggerInterceptor, After... After Call: 123456789 Do something in PhoneLoggerInterceptor, AfterReturning... Returning Object: com.gwtent.sample.client.Phone$Receiver[name=Alberto's Pizza Place] com.gwtent.sample.client.Phone$Receiver[name=Alberto's Pizza Place] | |
► Sign in to add a comment
Hi excellent tutorial thanks.
I noticed that
@After("execution( com.gwtent.sample.client.Phone.call(java.lang.Number))") public void afterCall(MethodInvocation? invocation) {
is only called the first time call() is executed. Is there anyway for afterCall() to be executed every time call() is executed?
Thanks
The showcase download is corrupted.
Can you please include a how to install. I've added: "<inherits name="com.gwtent.GwtEntAll"/>" to the xml, thrown in all the jars I could come up with, "implements Aspectable", etc. The code compiles but the interceptors aren't being executed.
I don't know if I'm missing, jars, configs, etc.
Btw, what jars do I need if I'm only using gwt-ent for aspects? I don't have spring, etc. installed as part of this system.
I tried writing interceptors for class that extends gwt RemoteServiceServlet?? but those are not getting executed. I made this servlet class implement Aspectabe interface, not sure will servlets be intercepted by Aspect poincut ? Please help !!!
Hi, Guys, Please have a look showcase, there is a full example there.
The showcase doesn't work!!!
Can you please fix the links: http://code.google.com/p/gwt-ent/source/browse/trunk/gwtent/src/com/gwtent/reflection/client/Method.java and http://code.google.com/p/gwt-ent/source/browse/trunk/gwtent/src/com/gwtent/reflection/client/Reflectable.java Thank you
Hi, lama, I just tested showcase, it's working, have you got any error message?
I had updated links, please see http://code.google.com/p/gwt-ent/wiki/UseReflection?ts=1281308626&updated=UseReflection
Hi! I use ext GWT and can`t intercept ui component methods. Is It a possible to use AOP library without GWT.create(...class) ?
Thank you
I am getting the following errors when I do GWT compile. [ERROR] Errors in 'file:/home/sreeni/bocWkspc/TestGwtEnt/src/test/client/PageValidationMatcher.java' [ERROR] Line 7: No source code is available for type com.gwtent.aop.matcher.ClassMethodMatcher; did you forget to inherit a required module? [ERROR] Line 9: No source code is available for type com.gwtent.aop.matcher.Matcher<T>; did you forget to inherit a required module? [ERROR] Line 10: No source code is available for type com.gwtent.aop.matcher.Matchers; did you forget to inherit a required module? When I try to add it to my project. I also have <inherits name='com.gwtent.GwtEnt'/> in my module file. In your document , you mentioned about AspectJ.. I tried using AspectJ 1.6.11 still not compiling. I dont see Showcase example in download list. What are all the dependencies I need to have for this to work. ThanksPerhaps someone can give me detailed instructions how to get a project working using AOP. I tried to check out the trunk to look at the sample code, but there are classes missing, HTMLWidget, HTMLEvent and some others. I see those in http://gwt-ent.googlecode.com/svn/branches/0.8/gwtent/src/com/gwtent/htmltemplate/client
I tried exporting the .08 branch, but got all sorts of errors in that too, including JavaScriptObject? not found. It also objected to java.lang.String and Object not being on the class path.
I tried to check out just the showcase project, but it needs the parent pom
I have exhausted my knowledge, so any help would be appreciated.
Thanks!
so...I checked out just the showcase, disabled maven and tried the old-fashioned way. Eventually I got to the same error I had when I first tried creating a project with the wiki example code...which is...
This is in the validation-api.1.0.0.GA.jar and ConstraintValidatorContextImpl? does not implement the method ... can anyone tell me what I am missing please because whatever I try there are errors and seemingly invalid code that will not compileIs this project even still active ?
I solved all the issues I was having by only checking out the gwtent project, disabling maven and removing a gwt.xml file.
There is a gwt.xml file in src/main/java/com/gwtent/pagebus/client (PageBus?.gwt.xml) and there is also one with the same name in the parent folder, gwt.xml file in src/main/java/com/gwtent/pagebus
I removed the one from the client directory.
Then, I created a new GWT project and copied only those classes from the gwtent_showcase sub-directories that I needed to test aop:
Interceptors.java, Phone.java Copied and modified AOPBasicPage so it used standard GWT UI components (did not use the HTMLtemplating stuff)
...more to come, I guess, as I work through this.
Hopefully this will help anyone else who wanders across this thread.
Hi,
I'm trying to intecept the onClickMethod on a button, but when i implement the Aspectable interface or annotate the method, my proyect just wont compile. i keep getting errors since in my interceptor im trying to validate session attributes and control session settings. The error i get is:
java'
but when ever i remove the annotations and the aspectable interface my project compiles correctly. does anyone know what could be wrong?