samedi 9 avril 2022

Java cannot get values with method call using reflection in a loop

Here is the code: fyi this is someones else's code that I am working with:

Here itemsList has size 3 and the selectedColumns has size 10, This code is used to generate 3 rows of a table where each row has 10 columns in pdf format itext library 5.5.10.

for (int i = 0; i < itemsList.size(); i++) {
            for (int j = 0; j < selectedColumns.size(); j++){
                try{
                    Method method = itemsList.get(i).getClass().getMethod("get"+selectedColumns.get(j).getCol());
                    Debug.e("NewPDF : method name"+ String.valueOf(itemsList.get(i).getClass().getMethod("get"+selectedColumns.get(j).getCol())));
                    String value;
                    if(method.invoke(new CloudHolder(), null) == null){
                        value = "";
                        Debug.e("NewPDF : value from loop null ");
                    }else{
                        value = String.valueOf(method.invoke(new CloudHolder(),null)) ;
                        Debug.e("NewPDF : value from loop " + value);
                    }

                    c1 = new PdfPCell(new Phrase(value, smallFont));
                    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
                    c1.setVerticalAlignment(Element.ALIGN_CENTER);
                    c1.setMinimumHeight(30f);
                    table.addCell(c1);
                    table.writeSelectedRows(i,  i+1, 70, currentpos, canvas);
                }catch (NoSuchMethodException e){
                    Debug.e("NewPDF : method not found");
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                    Debug.e("NewPDF : Illegal access | Invocation Exp" + e.getMessage());

                }

                if (currentpos <= 149) {
                    document.newPage();
                    document.setPageSize(pageSize);
                    document.add(pageSizes);
                    currentpos = 1200;

                }
            }


        }

The Method name is correct as the getter method as usual has "get" written before the variable name, together making the function name. but when I invoke the method the values are always null, what am I missing.

Also I am new to itext libary for some reason the table.writeSelectedRows prints nothing,and the if statement block if (currentpos <= 149) is also beyond me. Some help with those parts of the code is also appreciated.

update:

here is a proof of concept that I wrote which you can try and run:

public class Reflection {

    public static void main(String[] args) {
        ArrayList<PDFColumn> selectedColumns = new ArrayList<>();
        ArrayList<CloudHolder> itemsList = new ArrayList<>();
        selectedColumns.add(new PDFColumn("Carat",true));
        selectedColumns.add(new PDFColumn("Category",true));
        selectedColumns.add(new PDFColumn("Cert",true));
        selectedColumns.add(new PDFColumn("Date",true));
        selectedColumns.add(new PDFColumn("Design_code",true));
        selectedColumns.add(new PDFColumn("Dia_Wt",true));
        selectedColumns.add(new PDFColumn("Dia_color",true));
        selectedColumns.add(new PDFColumn("Dia_desc",true));
        selectedColumns.add(new PDFColumn("Dia_pcs",true));
        selectedColumns.add(new PDFColumn("Doc_no",true));
        CloudHolder holder = new CloudHolder();
        holder.setCarat(1f);
        holder.setCategory("Test cat");
        holder.setCert("Test cert");
        holder.setDate("Test date");
        
        itemsList.add(holder);
        itemsList.add(holder);
        itemsList.add(holder);
        for (int i = 0; i < itemsList.size(); i++) {
            for (int j = 0; j < selectedColumns.size(); j++){
                try{
                    Method method = itemsList.get(i).getClass().getMethod("get"+selectedColumns.get(j).getCol());
                    System.out.println("NewPDF : method name"+ String.valueOf(itemsList.get(i).getClass().getMethod("get"+selectedColumns.get(j).getCol())));
                    String value;
                    if(method.invoke(itemsList.get(i), null) == null){
                        value = "";
                        System.out.println("NewPDF : value from loop null ");
                    }else{
                        value = String.valueOf(method.invoke(new CloudHolder(),null)) ;
                        System.out.println("NewPDF : value from loop " + value);
                    }

                }catch (NoSuchMethodException e){
                    System.out.println("NewPDF : method not found");
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                    System.out.println("NewPDF : Illegal access | Invocation Exp" + e.getMessage());

                }


            }


        }


    }
}

PDFColumn

public class PDFColumn {
    private String cols;
    private boolean selected;

    public PDFColumn(String cols, boolean selected) {
        this.cols = cols;
        this.selected = selected;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public String getCol() {
        return cols;
    }

    public void setCols(String cols) {
        this.cols = cols;
    }
}

CloudHolder

public class CloudHolder implements Serializable, Comparator<CloudHolder> {

    private String Item_Code;
    private int Qty;
    private float Gross_Wt;
    private float Net_Wt;
    private float Dia_Wt;
    private float St_Wt;
    private float Gold_Oth_Wt;
    private float Tag_Price;
    private float Carat;
    private float Dia_pcs;
    private float Ot_st_Wt;
    private float Silver_Wt;
    private float Pearl_wt_pcs;
    private float Price_2;

    private String Category;
    private String ItemType_Name;
    private String Location_Name;
    private String Item_Description;
    private String Img_url;
    private String scan_date;
    ////////// new filed added//////////


    private String Design_code;
    private String Dia_color;
    private String Tray_no;
    private String Dia_desc;

    private String St_desc;
    private String Doc_No;
    private String Date;
    private String Season;
    private String Sieve;
    private String Purity;
    private String Time;
    private String Size;
    private String Shape;
    private String Cert;
    private int index;
    private String zone;
    private String Type;
    private String MainBrand;
    private String SubBrand;
    private String MakingCharge;

.... default getters setters

I have set the values for Carat,Category,Cert,Date but none are retrieved.

Logs:

NewPDF : method namepublic float leetcode.CloudHolder.getCarat()
NewPDF : value from loop 0.0

NewPDF : method namepublic java.lang.String leetcode.CloudHolder.getCategory()
NewPDF : value from loop null

NewPDF : method namepublic java.lang.String leetcode.CloudHolder.getCert()
NewPDF : value from loop null

NewPDF : method namepublic java.lang.String leetcode.CloudHolder.getDate()
NewPDF : value from loop null




Aucun commentaire:

Enregistrer un commentaire