Android examples for java.lang.reflect:Field
check if two object equal by Fields via reflection
//package com.java2s; import java.lang.reflect.Field; public class Main { private static boolean equalFields(Object one, Object another) { Field[] onefields = one.getClass().getDeclaredFields(); Field[] anotherfields = another.getClass().getDeclaredFields(); if (onefields.length != anotherfields.length) { return false; }/*from w w w.j av a 2 s . c o m*/ try { for (int i = 0; i < onefields.length; i++) { Field oneField = onefields[i]; oneField.setAccessible(true); Field anotherfield = anotherfields[i]; anotherfield.setAccessible(true); Object oneFieldValue = oneField.get(one); Object anotherFieldValue = anotherfield.get(another); if (oneFieldValue == null && anotherFieldValue != null) { return false; } if (oneFieldValue != null && !oneFieldValue.equals(anotherFieldValue)) { return false; } } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return true; } }