I want to write a point cut for class instantiation in various packages,like classes inside the subpackages inside com.kepler.xenon (eg.com.kepler.xenon.modules.ticklers.pojo.Tickler, com.kepler.xenon.modules.product.pojo.Product etc).

//This is my advice
@Aspect
@Component
public class OxAspect {
@After("execution(* com.oxane.xenon..*new(..)) && @within(java.lang.Deprecated)")
public void myAdvice(final JoinPoint jp){
    System.out.println(jp.getSignature().getName()+""+jp.getTarget().getClass());
 }
}
//This is my class
package com.kepler.xenon.modules.ticklers.pojo;
@Deprecated
public Class Ticklers{
@Id
@TableGenerator(name = "TICKLERS_ID", table = "ID_GENERATOR", pkColumnName = "GEN_KEY", valueColumnName = "GEN_VALUE", pkColumnValue = "TICKLERS_ID", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.TABLE, generator = "TICKLERS_ID")
@Column(name = "TICKLERS_ID", unique = true, nullable = false)
private int ticklersId;

@Column(name = "TASK", nullable = false, length = 256)
private String taskName;

public int getTicklersId() {
    return ticklersId;
}

public void setTicklersId(int ticklersId) {
    this.ticklersId = ticklersId;
}

public String getTaskName() {
    return taskName;
}

public void setTaskName(String taskName) {
    this.taskName = taskName;
}

}

What i want is that if anyone tries to access the class which is deprecated,then pointcut filters that call and triggers advice. I have done it for methods but i am failing to do it for classes.

I am adding aspect which works for methods,controller and Dao

@Aspect
@Component
public class OxAspect {

private final OxAspectService oxAspectService;

public OxAspect(OxAspectService oxAspectService) {
    this.oxAspectService=oxAspectService;
}

@Pointcut("execution(@java.lang.Deprecated * com.oxane.xenon..*(..))"
        + " || execution(* com.oxane.xenon..*.*(..)) && @within(java.lang.Deprecated)")
public void deprecated() {
}

@Before("deprecated()")
public void log(final JoinPoint jp) {
    oxAspectService.logDeprecatedMethod(jp);
}

}

Edit: I have done some research on spring io and found that it can't be done using spring aop. I have to use load time weaving or compile time weaving to achieve what i want. For that i have to use pure aspect j implementation. Correct me if i am wrong.

1

There are 1 best solutions below

2
On

If I were you I will devide @Pointcut to signle condition like below:

@Pointcut("execution(* com.oxane.xenon..*(..))")
public void anyClassInSubpackage() {
}

@Pointcut("@annotation(java.lang.Deprecated)")
public void deprecatedClass() {
}

@Pointcut("execution(* com.oxane.xenon..*new(..))")
public void anyMethodInSubpackege() {
}

@Pointcut("@within(java.lang.Deprecated)")
public void deprecatedMethod() {
}

@Before("(anyClassInSubpackage() && deprecatedClass()) || (anyMethodInSubpackege() && deprecatedMethod())")
public void myAdvice(final JoinPoint jp){
    //TODO
}