michelesalvador / FamilyGem

Android app for genealogical trees
https://www.familygem.app
GNU General Public License v3.0
125 stars 34 forks source link

Code clarification #79

Closed Sternbach-Software closed 7 months ago

Sternbach-Software commented 1 year ago

https://github.com/michelesalvador/FamilyGem/blob/master/app/src/main/java/app/familygem/DetailActivity.java#L481

    public Object cast(Class aClass) {
        Object casted = null;
        try {
            // If it goes wrong will return a new instance of the class, just to not crash DetailActivity
            if (aClass.equals(GedcomTag.class))
                casted = new GedcomTag(null, null, null);
            else
                casted = aClass.newInstance();
            casted = aClass.cast(object);
        } catch (Exception e) {
            onBackPressed();
        }
        return casted;
    }

Were you trying to accomplish this?

    public Object cast(Class aClass) {
        Object casted = null;
            // If it goes wrong will return a new instance of the class, just to not crash DetailActivity
            if (aClass.equals(GedcomTag.class))
                casted = new GedcomTag(null, null, null);
            else
                casted = aClass.newInstance();

        try {
            casted = aClass.cast(object);
        } catch (Exception e) {
            onBackPressed();
        }
        return casted;
    }
michelesalvador commented 10 months ago

No I didn't. newInstance() needs to handle exceptions, must be surrounded by try.