获取父类的属性

  public static Field[] getAllFields(Object object){
        Class clazz = object.getClass();
        List fieldList = new ArrayList<>();
        while (clazz != null){
            fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
            clazz = clazz.getSuperclass();
        }
        Field[] fields = new Field[fieldList.size()];
        fieldList.toArray(fields);
        return fields;
    }

	public static Object getValByField(Class entityClazz , String field , Object o){
		Object result = null;
		Method[] mss = entityClazz.getMethods();
    	for(Method m : mss){
    		if(m.getName().equalsIgnoreCase("get"+field)){
    			try {
					result = m.invoke(o, new Object[]{});
					break;
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
    		}
    	}
    	return result;
	}

 

你可能感兴趣的:(java)