Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] fault injection in byte code:

vegiraju kanthirekha wrote:
> Hi,
> I was wondering whether I can inject a fault using AspectJ into byte
> code ? I want to inject an exception into a class file. Is that possible
> with AspectJ.

Yes.

Exceptions can be thrown when code runs. If you can specify the target code as a join point using AspectJ pointcuts, then you can throw an exception before or after the join point runs. For example, you can compile your classes with this aspect:

----
aspect A {
  before() : target(MyClass) && call(void doSomething()) {
      throw new Error("Error before MyClass.doSomething runs");
  }
}
----

and when run, any call to an instance of MyClass method
doSomething() will throw an Error before the method executes.

The exception thrown must comply with the exceptions throwable by the join point. In this case, because Error is unchecked, it can be thrown from advice on any join point.

Wes

(btw, It's most helpful if you read the AspectJ documentation
and frame questions in similar terms.)




Back to the top