Java tutorial
//package com.java2s; //License from project: Open Source License import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main { @SuppressWarnings({ "unchecked", "rawtypes" }) public static List parents(Object o) { try { Method getParent = o.getClass().getMethod("getParent"); getParent.setAccessible(true); List result = new ArrayList(8); try { while (true) { Object parent = getParent.invoke(o); if (parent == null) { return result; } else { result.add(parent); } o = parent; } } catch (IllegalArgumentException e) { return result; } catch (IllegalAccessException e) { return result; } catch (InvocationTargetException e) { return result; } } catch (NoSuchMethodException e) { return Collections.EMPTY_LIST; } } }