I would like to write a pointcut for the new FileInputstream(File file)
constructor. For example, a common way to create a new file in java is:
File file = new File(myDirectory, "myFileName.txt");
new FileOutputStream(file);
What I've tried so far is this:
Inside FileCreation.aj
:
import java.io.File;
import java.io.FileOutputStream;
aspect FileCreation {
pointcut FileOutputStream1(File file): call(FileOutputStream FileOutputStream(File)) && args(file);
FileOutputStream around(File file): FileOutputStream1(file) {
System.out.println("I was called!!");
return proceed(file);
}
}
In order to test whether this hook is working, I added a print statement.
However, it seems like this isn't getting invoked.
Not sure what the mistake is in this case.
The problem is invalid constructor call syntax. You need to use .new
and no return type specifier, because the return type implicitly is always the class of the intercepted constructor.
BTW, be careful not to name methods like classes. Better use fileOutputStream1
with a lower-case "f" as a pointcut name. Otherwise, your code is difficult to read.
package de.scrum_master.aspect;
import java.io.File;
import java.io.FileOutputStream;
aspect FileCreation {
pointcut fileOutputStream1(File file) :
call(FileOutputStream.new(File)) &&
args(file);
FileOutputStream around(File file) : fileOutputStream1(file) {
System.out.println("I was called!!");
return proceed(file);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.