0

I have implemented an application that simulates a shop in java with all information placed in a database made in SQL. My issue is with a reflection technique which I have to implement in order to show the list of elements from a table. I have tried this method(the first answer, the one with metaData) Most simple code to populate JTable from ResultSet However I have been told that this is not a reflection method. I made this, guided by the teacher. But, I have no idea how to actually call it in my function and where the "object " is supposed to come from. The first function returns the headers and the second extracts the info and creates the table.

public class ReflectionExample {

    protected static final Logger LOGGER = Logger.getLogger(ClientDao.class.getName());
    public static ArrayList retrieveProperties(Object object) {
        ArrayList<String> ob = new ArrayList<String>();
        Class obiect = object.getClass();

        int i = 0;
        for (Field field : object.getClass().getDeclaredFields()) {
            field.setAccessible(true); // set modifier to public
            ob.add(field.getName());

            i++;
        }
        return ob;

    }

    public static JTable retrievePropertiesM(List<?> object,    Object[]lit) {
        Class obiect = object.getClass();
        Object [][] matrice = new Object[object.size()][20];
        //Object[] lit=null;

        int row = 0;
        for (Object o : object) {
            int col = 0;
            for (Field field : o.getClass().getDeclaredFields()) {
                field.setAccessible(true); // set modifier to public

                Object value;
                try {

                    value = field.get(object);
                    matrice[row][col]=value;
                    //lit[col]=field.getName();

                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                col++;
            }
            row++;
        }
    //  Object[] lit=retrieveProperties(object);
        JTable t = new JTable(matrice,lit);
        return t;


    }
}

However, I am in doubt as to why the first way I made was not a reflection? As per all, I did not understood what reflection means beside that a method with reflection can be called by any type of object from the database. Some explanations or maybe somewhere I can understand better reflection?

Diana G
  • 261
  • 3
  • 15
  • Reflection is the ability to reflect the types at runtime. This means that you can, at runtime, inspect an object. You can, for example, ask, whether a type has a constructor with a specific parameter list or whether a method with a specific name and return type does exists. And, of course, you can get those methods, parse arguments to them and call them. This also goes for attributes. In this sense, your approach uses reflection. – Turing85 May 03 '18 at 16:17
  • It is unclear to me what you are asking about. Is the code in your question the "first way" you tried, or the second attempt? The code you show uses reflection. Although the larger question is: should you use reflection? Probably not. – Mark Rotteveel May 03 '18 at 17:27
  • In any case, the code as shown has nothing to do with JDBC, so I have removed the jdbc tag. If your code does involve jdbc, then please post a [mcve] and clarify exactly what you're asking. – Mark Rotteveel May 03 '18 at 17:27
  • Why not use reflection? – Diana G May 03 '18 at 19:12
  • I am just trying to understand better what I did wrong so that I can improve. To me, the metadata example looked like some sort of reflection. – Diana G May 04 '18 at 11:17

0 Answers0