Java 有没有办法用AspectJ向方法添加代码

Java 有没有办法用AspectJ向方法添加代码,java,runtime,aspectj,Java,Runtime,Aspectj,问题是我想更改类中方法的字节码。 假设我们有这种方法: public class Test{ public Connection buildConnection(){ Connection connection = null; if(connection==null){ connection = Mysql.getConnection(); } if(connection==null){ throw new Run

问题是我想更改类中方法的字节码。 假设我们有这种方法:

public class Test{

  public Connection buildConnection(){
    Connection connection = null;
      if(connection==null){
        connection = Mysql.getConnection();
      }
      if(connection==null){
        throw new RunTimeException("Error");
      }
      return connection;
    }
}
是否有方法在运行时模式下更改此代码以匹配以下内容:

public class Test{

      public Connection buildConnection(){
        Connection connection = null;
          if(connection==null){
            connection = Mysql.getConnection();
          }else{
            connection = Oracle.getConnection();
          }
          if(connection==null){
            throw new RunTimeException("Error");
          }
          return connection;
        }
}

您确定要创建的代码确实是您想要的,因为连接总是空的,并且您总是只运行if子句的第一部分吗?您的意思是,如果调用Mysql.getConnection()后连接仍然为空,那么它会调用Oracle.getConnection()作为备份吗?