ReflectiveAttribute extends SimpleAttribute<O, A> { public ReflectiveAttribute(Class<O> objectType, Class<A> fieldType, String fieldName) { super(objectType, fieldType, fieldName); Field field; try { field = getField(objectType, fieldName); if (!field.isAccessible()) { field.setAccessible(true); } } catch (Exception e) { throw new IllegalStateException("Invalid attribute definition: No such field '" + fieldName + "' in object '" + objectType.getName() + "'"); } if (!fieldType.isAssignableFrom(field.getType())) { throw new IllegalStateException("Invalid attribute definition: The type of field '" + fieldName + "', type '" + field.getType() + "', in object '" + objectType.getName() + "', is not assignable to the type indicated: " + fieldType.getName()); } this.field = field; } ReflectiveAttribute(Class<O> objectType, Class<A> fieldType, String fieldName); @Override A getValue(O object, QueryOptions queryOptions); @Override boolean equals(Object o); @Override boolean canEqual(Object other); @Override int hashCode(); static ReflectiveAttribute<O, A> forField(Class<O> objectType, Class<A> fieldType, String fieldName);  }
@Test public void testReflectiveAttribute() { IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>(); Attribute<Car, String> NAME = ReflectiveAttribute.forField(Car.class, String.class, "name"); cars.addIndex(HashIndex.onAttribute(NAME)); cars.add(new Car(1, "ford focus", "great condition, low mileage", Arrays.asList("spare tyre", "sunroof"))); cars.add(new Car(2, "ford taurus", "dirty and unreliable, flat tyre", Arrays.asList("spare tyre", "radio"))); cars.add(new Car(3, "honda civic", "has a flat tyre and high mileage", Arrays.asList("radio"))); Assert.assertEquals(cars.retrieve(equal(NAME, "honda civic")).size(), 1); }