0

I am getting an error in the following line - Utils.read(new File("asd"),Map<String,Commit>.class);
The error is cannot select from parameterized type
The readObject function is declared as public static <T extends Serializable> T readObject(File file, Class<T> expectedClass)
How do I pass a Map of type <String, Commit> in this case

The complete definition of readObject function: <br>
 public static <T extends Serializable> T readObject(File file,
                                                        Class<T> expectedClass) {
        try {
            ObjectInputStream in =
                    new ObjectInputStream(new FileInputStream(file));
            T result = expectedClass.cast(in.readObject());
            in.close();
            return result;
        } catch (IOException | ClassCastException
                | ClassNotFoundException excp) {
            throw new IllegalArgumentException(excp.getMessage());
        }
    }<br>

Is instead of the above code to read the object, I use the following code :
Object readObject(File f) {
        byte[] arr = Utils.readContents(f);
        ByteArrayInputStream i = new ByteArrayInputStream(arr);
        try {
            ObjectInputStream s = new ObjectInputStream(i);
            Object o = s.readObject();
            s.close();
            i.close();
            return o;
        } catch (IOException | ClassNotFoundException c) {
            System.out.println("IOException");
            return null;
        }
    }<br>

and call (HashMap<String, Commit>) readObject(Utils.join(dir, "commits")); Then it works perfectly. The Commit is serializable and internally has a HashMap. This is coming null if I use the earlier code, but it's coming alright with this code. The earlier code I am calling like. ``

yemen
  • 1
  • 1
  • Given the information provided, you could consider just using `Map.class`? – Ben Anderson Dec 05 '21 at 20:27
  • @BenAnderson That too shows a compilation error `no instance of type variable exists so that map conforms to serializable` – yemen Dec 05 '21 at 20:34
  • Using `HashMap.class` works but I am not sure If it will cause some issue as the actual type I am expecting is Map – yemen Dec 05 '21 at 20:56
  • It does cause a problem as Branch is a custom class that internally contains a hashmap. It seems it cannot deserialize this properly – yemen Dec 05 '21 at 21:02
  • `Branch` is serializable right? – Ben Anderson Dec 05 '21 at 21:04
  • And if you can't get your current method to work I'd recommend creating a new map and then deserializing all the objects and adding them to it manually. – Ben Anderson Dec 05 '21 at 21:09
  • @BenAnderson, yes, the Branch is serializable. I have changed it to `Commit,` which is the same as Branch, and added another code that works well. But not sure why this is not working – yemen Dec 05 '21 at 21:18
  • 1
    See [this question](https://stackoverflow.com/questions/1079279/class-object-of-generic-class-java) for some possible work-arounds. – Ben Anderson Dec 05 '21 at 21:24

0 Answers0