2

Certain specifications of my project require me to create POJO classes from information provided via an excel sheet or JSON; then creating objects of that class with relevant information at hand that will be used later in the code.

Extracting relevant data from excel sheets and JSON is not an issue. I was even able to create POJO classes dynamically thanks to this answer. But I'm unsure if it is possible to create objects of this class. As this guy mentioned in his above answer that -

But the problem is: you have no way of coding against these methods, as they don't exist at compile-time, so I don't know what good this will do you.

Is it possible to instantiate the class created in the above answer? If so, how? If not, what are other alternatives to this problem? Or should I change my approach regarding this specification and think of some other option?

Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
  • 1
    First, to answer your question: is this what you were looking for? [Invoking Methods](https://docs.oracle.com/javase/tutorial/reflect/member/methodInvocation.html). Second, this sound like a spectacularly bad idea. I hope you have a good reason for doing this. – Assaf Mar 15 '16 at 11:52
  • 1
    Have you considered List? That is a collection of excel rows, where each item is a map, where key is the column name and value simple it's value? – Kojotak Mar 15 '16 at 11:56
  • @Assaf I think this will work. Thanks. BTW, why is it a bad idea? – Bilesh Ganguly Mar 15 '16 at 11:58
  • @Kojotak Actually, my initial solution was that only. I used Jackson for parsing the JSON string and Apache POI for extracting data from Excel to be stored in a List>. But one of my seniors insists that we make a POJO class out of it. The reason for which isn't known to me. – Bilesh Ganguly Mar 15 '16 at 12:01
  • Probably you should go for Map – Kartic Mar 15 '16 at 12:03
  • 1
    @BileshGanguly, not knowing the specifics of the problem I don't know why you're going with this solution, like I said I hope you have a good reason for doing this. Speaking in general dynamic class loading is unwieldy and should be avoided if there are other solutions. – Assaf Mar 15 '16 at 12:09
  • 1
    Let the senior developer join this discussion! :) – Kojotak Mar 15 '16 at 12:15
  • 2
    Creating the POJOs on the fly, then having to access them through reflection solely, is an utterly pointless pain in the a*** . Stick to the map and send the "senior" here for a well-deserved roasting. – Erich Kitzmueller Mar 15 '16 at 12:20
  • @Assaf Your solution has worked for me (for the time being). You can add it as a answer for others to see. – Bilesh Ganguly Mar 15 '16 at 12:29
  • 1
    @Kojotak I'll ask him to get involved and put forth his reasons. :) – Bilesh Ganguly Mar 15 '16 at 12:32
  • 1
    @ammoQ I'll ask him to get involved and put forth his reasons. :) – Bilesh Ganguly Mar 15 '16 at 12:32
  • A bit late to respond but I had kept forth the issues you people raised, and we reverted back to the original solution (that I had thought of earlier and many of you had mentioned), that is, using `List>`. Thanks, for all the input. :) – Bilesh Ganguly May 12 '16 at 04:51

2 Answers2

1

You can use reflection to instantiate the generated classses and access the provided methods.

leftbit
  • 848
  • 1
  • 7
  • 18
1

Probably in your situation I would go for something like below. This could not be post as a comment. So posting here.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GenericDTO {

    private Map<String, List<Object>> resultSetMap = new HashMap<String, List<Object>>() ;

    public void addAttribute(String attributeName, Object attributeValue) {
        if(resultSetMap.containsKey(attributeName)) {
            resultSetMap.get(attributeName).add(attributeValue);
        } else {
            List<Object> list = new ArrayList<Object>();
            list.add(attributeValue);
            resultSetMap.put(attributeName, list);
        }
    }

    public Object getAttributeValue(String key) {
        return (resultSetMap.get(key) == null) ? null : resultSetMap.get(key).get(0); 
    }

    public List<Object> getAttributeValues(String key) {
        return resultSetMap.get(key); 
    }

}

You can use it like:

GenericDTO dto = new GenericDTO();
dto.addAttribute("aa", 1);
dto.addAttribute("aa", "aa");
dto.addAttribute("bb", 5);

System.out.println(dto.getAttributeValue("bb"));
System.out.println(dto.getAttributeValues("aa"));
Kartic
  • 2,935
  • 5
  • 22
  • 43