jeudi 24 janvier 2019

How change value of fields marked with the annotation by the annotation processing

How to set the value of the variable and then after the calculation read it using annotation processing? I know how to do this with reflection, but I need to use annotation processing mechanism. Is it possible? Do I have to change something in the annotation?

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Bind {

}

public class Model1 {
@Bind private int LL;
@Bind private  double[] twKI;
@Bind private  double[] twKS;
@Bind private  double[] twINW;
@Bind private  double[] twEKS;
@Bind private  double[] twIMP;
@Bind private double[] KI;
@Bind private double[] KS;
@Bind private double[] INW;
@Bind private double[] EKS;
@Bind private double[] IMP;
@Bind private double[] PKB;

 public Model1() {}

 public void run() {
 PKB = new double[LL];
 PKB[0] = KI[0] + KS[0] + INW[0] + EKS[0] - IMP[0];
 for (int t=1; t < LL; t++) {
  KI[t] = twKI[t]* KI[t-1];
  KS[t] = twKS[t]* KS[t-1];
  INW[t] = twINW[t]* INW[t-1];
  EKS[t] = twEKS[t]* EKS[t-1];
  IMP[t] = twIMP[t]* IMP[t-1];
  PKB[t] = KI[t] + KS[t] + INW[t] + EKS[t] - IMP[t];
  }
 }
}


 /* Part of the code in which I set the values */
 public class Controller{
 ...
 private Class class1;
 private Object modelName;

 class1 = Class.forName("zad1.models."+model);
 modelName = class1.newInstance();
 Field fieldtkWI  = class1.getDeclaredField("twKI");
 fieldtkWI.setAccessible(true);
 fieldtkWI.set(modelName, twKI);
 }





Aucun commentaire:

Enregistrer un commentaire