about AspectJ

A

applex

I created two files:
PreConditionExample.java
public class PreConditionExample {
public double sqrt(double p) {
return Math.sqrt(p);
}

public static void main(String[] args) {
PreConditionExample t = new PreConditionExample();
System.out.println("sqrt -4 : "+t.sqrt(-4));
System.out.println("sqrt 9 : "+t.sqrt(9));
}
}

PreconditionAspect.aj
public aspect PreconditionAspect
{
pointcut sqrtPc(double param) : call ( * Math.sqrt(double)) &&
args(param) ;

before(double param ) : sqrtPc(param)
{

if (param < 0)
{
System.out.println("illegal parameter");
//throw new RuntimeException();
}
}

}


but it doesn't work in this way. "illegal parameter " was not printed.
but I put them in the same file, it works.
Why?
help/
 
M

Mark Thomas

applex said:
I created two files:
PreConditionExample.java
public class PreConditionExample {
public double sqrt(double p) {
return Math.sqrt(p);
}

public static void main(String[] args) {
PreConditionExample t = new PreConditionExample();
System.out.println("sqrt -4 : "+t.sqrt(-4));
System.out.println("sqrt 9 : "+t.sqrt(9));
}
}

PreconditionAspect.aj
public aspect PreconditionAspect
{
pointcut sqrtPc(double param) : call ( * Math.sqrt(double)) &&
args(param) ;

before(double param ) : sqrtPc(param)
{

if (param < 0)
{
System.out.println("illegal parameter");
//throw new RuntimeException();
}
}

}


but it doesn't work in this way. "illegal parameter " was not printed.
but I put them in the same file, it works.
Why?
help/
There's nothing wrong with the code. When I run it, I get:

illegal parameter
sqrt -4 : NaN
sqrt 9 : 3.0

as expected. How are you compiling them?

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top