I am trying to instanciate an object of a non public class in AspectJ.
I have this class:
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() {
(...)
}
}
In the aspects class I wanted to be able to do:
Bill b = new Bill(currInvoice, idOperator, idClient);
The problem is that I get an error:
The constructor Bill(int, String, String) is not visible
So I investigated and tried to use reflection like it's explained in this post.
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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But I get an error in the lines:
invoice.id = 1;
invoice.idOperator = "foo";
invoice.idClient = "bar";
The error is:
The field Bill.X is not visible.
Does anyone know if there is any workaround?
Aucun commentaire:
Enregistrer un commentaire