AspectJ - 使用反射来实现非公共类

时间:2017-03-15 19:46:58

标签: java reflection aspectj

我正试图在AspectJ中实现非公共类的对象。

我有这堂课:

package ca1.business;

public class Bill {

    int id;
    String idOperator;
    String idClient;

    Bill(int id, String idOperator, String idClient) { /* (...) */ }
    public String toString() { /* (...) */ }
    public boolean equals(Object o) { /* (...) */ }
    public int getId() { /* (...) */ }
    public String getOperator() { /* (...) */ }
    public String getClient() { /* (...) */ }
}

在我希望能够做到的方面课程中:

Bill b = new Bill(currInvoice, idOperator, idClient);

问题是我收到错误:

The constructor Bill(int, String, String) is not visible

所以我调查并尝试使用像this帖子中解释的那样的反射。

try {
    Constructor<Bill> cons = Bill.class.getDeclaredConstructor(null);
    cons.setAccessible(true);
    Bill invoice = cons.newInstance(null);
    invoice.id = 1;
    invoice.idOperator = "foo";
    invoice.idClient = "bar";
    // etc...
} catch (SecurityException e) {
    e.printStackTrace();
} catch (NoSuchMethodException e) {
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (InstantiationException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (InvocationTargetException e) {
    e.printStackTrace();
}

但我在行中出错:

invoice.id = 1;
invoice.idOperator = "foo";
invoice.idClient = "bar";

错误是:

The field Bill.X is not visible.

有没有人知道是否有任何解决方法?

1 个答案:

答案 0 :(得分:1)

为什么要使用反射?

让我们假设Bill类看起来像这样:

package ca1.business;

public class Bill {
  int id;
  String idOperator;
  String idClient;

  Bill(int id, String idOperator, String idClient) {
    this.id = id;
    this.idOperator = idOperator;
    this.idClient = idClient;
  }

  @Override
  public String toString() {
    return "Bill [id=" + id + ", idOperator=" + idOperator + ", idClient=" + idClient + "]";
  }

  public static Bill instance;

  public static void main(String[] args) {
    System.out.println(instance);
  }
}

我们希望我们的方面在执行main之前填充静态成员(愚蠢的例子,仅用于演示):

选项A:特权方面

package de.scrum_master.aspect;

import ca1.business.Bill;

public privileged aspect MyAspect {
  before() : execution(public static void main(String[])) {
    Bill.instance = new Bill(11, "operator", "client");
  }
}

选项B:将方面放入包ca1.business

构造函数是package-private,即同一个包中的其他类可以访问它。

package ca1.business;

public aspect MyAspect {
  before() : execution(public static void main(String[])) {
    Bill.instance = new Bill(11, "operator", "client");
  }
}

选项C:将工厂类作为帮助程序放入受保护的包中

package ca1.business;

public class BillFactory {
  public static Bill create(int id, String idOperator, String idClient) {
    return new Bill(id, idOperator, idClient);
  }
}
package de.scrum_master.aspect;

import ca1.business.Bill;
import ca1.business.BillFactory;;

public aspect MyAspect {
  before() : execution(public static void main(String[])) {
    Bill.instance = BillFactory.create(11, "operator", "client");
  }
}

每个选项A,B,C的控制台日志

Bill [id=11, idOperator=operator, idClient=client]