@H_ 403_ 0 @ this series's < a href=“ http://www.ibm.com/developerworks/cn/java/j-dyn0916/ "& gt; Part 4 and < a href =" "" http://www.ibm.com/developerworks/cn/java/j-dyn0203/ "& gt; Part 5 discusses how to make local changes to binary classes with javassist. This time, you will learn to use the framework in a more powerful way to take full advantage of javassist's support for finding all specific methods or fields in bytecode. For javassist functionality, this functionality is at least as good as its ability to specify bytecode in a source like manner Equally important. Support for alternative operations also helps make javasssist a great tool for adding aspect oriented programming capabilities to standard Java code.
With the method information, you can configure a transformation with codeconverterinsertbeforemethod() to add a call to the report method before each call to the set method. Then all you have to do is apply the converter to one or more classes. In the code in Listing 1, I do this in the onwrite () method of the convertertranslator internal class by calling the instrument () method of the class object. This will automatically apply this transformation to each class loaded from the classpool instance.
= 3) { try { // set up class loader with translator
public Bean() {} public Bean(String a,String b) { m_a = a; m_b = b; } public String getA() { return m_a; } public String getB() { return m_b; } public void setA(String string) { m_a = string; } public void setB(String string) { m_b = string; } } public class BeanTest { private Bean m_bean; private BeanTest() {
Transformations handled by codeconverter are useful, but have limitations. For example, if you want to call a monitoring method before or after calling the target method, then the monitoring method must be defined as static void and must first accept the parameters of a class of a target method, and then the same number and type of parameters required by the target method. This strict structure means that the monitoring method needs to exactly match the target class and method. As an example, suppose I change the definition of the reportset () method in to accept a generic Java Lang. object parameter, which you want to use for different target classes: @ h_ 403_ 0 @ compilation is OK, but it will break when I run it: @ h_ 403_ 0@ there are ways to bypass this restriction. One solution is to actually generate a custom monitoring method that matches the target method at run time. However, this requires a lot of work, and I do not intend to experiment with this method in this article. Fortunately, javassist also provides another way to handle system bytecode conversion. This method uses javassist Expreditor is more flexible and powerful than codeconverter. Bytecode conversion with codeconverter and javassist Expreditor works the same way. However, the expreditor approach may be more difficult to understand, so I'll first show the fundamentals and then add the actual transformation. Listing 3 shows how to use expreditor to report the basic project of possible targets of aspect oriented transformation. Here, I derived the expreditor subclass from my verboseeditor and overridden three basic class methods -- their names are edit (), but they have different parameter types. As shown in the code in, I actually use this subclass in the onwrite () method of the internal class of the sectiontranslator to pass an instance in the call to the instrument () method of the class object for each class loaded from the classpool instance= 1) { try { // set up class loader with translator