|
#1
|
|||
|
|||
|
Hi
If I have methods that I want to point cut, for example Code:
Entitled void checkUser(X User user) Entitled void checkSomethingElse(X SomethingElse se, Y MoreSomethingElse mse) Code:
Aspect(value="aroud("Entitled") && ???")
Liam PS CODE and HTML dont seem to work for At symbols |
|
#2
|
|||
|
|||
|
|
|
#3
|
|||
|
|||
|
Quote:
I.e. the parameters are passed in either using Arg names or specifying what exact Paramater level annotation you expect In my example I am saying you are applying the same Aspect but to variable signatures. Sure you can get the parameter from the ProceedingJointPoint but there is no access to their metadata from what I can see unless you start reflecting, which seems wroing |
|
#4
|
|||
|
|||
|
Hi Denis
I dont think this article answers my question exactly. The examples here seem to inidicate that the Aspect knows instrisic information on the context it is applied i.e. the Annoations used at the parameter level or parameters names. In my example I am assuming the declared Entitled annoation could be applied to any method and want to introspect the param data including their annotations. It is not obvious how this can be done. Sure from the pjp you can get the parameter values but you have no access to anything else. I would of thought the Parameter annotation meta data should be available as part of the StaticPart as it is really static data of the method after all ? Thanks Liam |
|
#5
|
|||
|
|||
|
Actually this seems maybe more fundamental. There doest seem anyway of getting pased the annotation of a method parameter.
By this I mean using the annotation at the parameter level (ElementType.PARAMETER), and not TYPE , where you could match with args() etc. Seems pretty big whole if you cant do this or get hold of the parameter annoatations from the JointPoint class ? |
|
#6
|
|||
|
|||
|
Method arguments annotations are part of the method meta-information, hence, the only thing you should do is to get a method meta-information and derive static information about parameters annotations. Here is an example for @AspectJ style:
TestAnnotation.java Code:
package com.spring.aop;
import java.lang.annotation.*;
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
}
Code:
package com.spring.aop;
import org.springframework.stereotype.Component;
@Component
public class AopService {
public void service(@TestAnnotation Object o) {
System.out.println("xxxxx: AopService.service()");
}
}
Code:
package com.spring.aop;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class TestAspect {
@Around("execution(* com.spring.aop.AopService.*(..))")
public Object advice(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("xxxxxxxxxxxx: TestAspect.advice(): aspect is called on " + joinPoint
+ ". This object: " + joinPoint.getThis());
if (joinPoint.getSignature() instanceof MethodSignature) {
MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
Method method = methodSignature.getMethod();
for (int i = 0; i < method.getParameterTypes().length; i++) {
for (Annotation[] annotations : method.getParameterAnnotations()) {
System.out.printf("Found the following static annotations at the parameter %d: %s%n",
i, Arrays.toString(annotations));
}
}
}
return joinPoint.proceed();
}
}
HTML Code:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"> <context:component-scan base-package="com.spring.aop"/> <aop:aspectj-autoproxy proxy-target-class="true"/> </beans> Code:
package com.spring;
import com.spring.aop.AopService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringStart {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
AopService bean = (AopService)context.getBean("aopService");
bean.service(new Object());
}
}
Quote:
It's possible to remove the cast by using pure spring advice style, i.e. perform the following change to the mentioned example:
|
|
#7
|
|||
|
|||
|
Thanks for your comprehensive reply. I actually found a similar article here.
Code:
http://www.nabble.com/Sring-AOP---ProceedingJoinPoint---Method-td20042019.html I would advise it should be mentioned as its seem as sensible thing to want todo. The down cast is ugly but bareable. I am assuming the main requirement for this is due to the age of these libs (pre Java 5), as in makes perfect sence to expose the metadata yet restrict the access to method. Perhaps in a later version we would see this encapsulation corrected. Regards Liam |
|
#8
|
|||
|
|||
|
Welcome.
Quote:
Quote:
I'm afraid it's not clear what do you mean here. Both static information about join point and actual parameters are available at runtime. What kind of improvement do you want to see? |
|
#9
|
|||
|
|||
|
if I have a method for example
Code:
getStaticPart() It already provides, name, type etc Obviously someone decided in the design of this API to favour not exposing the method. This sounds fair, if you just expose Code:
pjp.prooceed() However then they expose Code:
pjp.proceed(args) At which point you basically shoot youself in the foot really interms of why you didnt offer Code:
pjp.getMethod() |
|
#10
|
|||
|
|||
|
On your reply regarding the difficulty.
Do you downcast Map to HashMap to access fundamentals? Is this need or requirement documented in the Java API.... No, because you dont have to Are you basically saying it is obvious and GOOD to read an API and JavaDoc, see an interface, then consider what implementation could be behind it, so you can basically abuse the contract of this and make your code less portable I really am not sure what you are getting at but its very non OO |
![]() |
| Thread Tools | |
| Display Modes | |
|
|