IdPropertyResolverUtils { public static String getPropertyNameFromAnnotatedMethod(Class<?> entityType, Class<?> idType, Method idAnnotatedMethod) { if (idAnnotatedMethod != null) { final String name = PropertyUtils.getPropertyName(idAnnotatedMethod); if (!PropertyUtils.getTypeOf(idType).isAssignableFrom( PropertyUtils.getTypeOf(idAnnotatedMethod.getReturnType()))) { throw new PropertyTypeMismatchException(entityType, name, idType, idAnnotatedMethod.getReturnType()); } else { return name; } } return null; } private  IdPropertyResolverUtils(); static String getPropertyNameFromAnnotatedMethod(Class<?> entityType, Class<?> idType,
                                                            Method idAnnotatedMethod); static boolean isAnnotated(AnnotatedElement element);  }
@Test(expectedExceptions = PropertyTypeMismatchException.class) public void testPropertyNameFromMethodWhenIdTypeIsInvalid() throws Exception { IdPropertyResolverUtils.getPropertyNameFromAnnotatedMethod(EntityWithAnnotatedIdGetterFromJPA.class, Long.class, EntityWithAnnotatedIdGetterFromJPA.class .getDeclaredMethod("getMyCustomId")); }
@Test public void testPropertyNameFromMethod() throws Exception { final String propertyName = IdPropertyResolverUtils.getPropertyNameFromAnnotatedMethod( EntityWithAnnotatedIdGetterFromJPA.class, Integer.class, EntityWithAnnotatedIdGetterFromJPA.class.getDeclaredMethod("getMyCustomId")); assertThat(propertyName, is(notNullValue())); assertThat(propertyName, is("myCustomId")); }
@Test public void testPropertyNameFromMethodWithEmbeddedId() throws Exception { final String propertyName = IdPropertyResolverUtils.getPropertyNameFromAnnotatedMethod(EntityWithAnnotatedEmbeddedIdGetterFromJPA.class, Integer.class, EntityWithAnnotatedIdGetterFromJPA.class.getDeclaredMethod("getMyCustomId")); assertThat(propertyName, is(notNullValue())); assertThat(propertyName, is("myCustomId")); }
@Test public void testDeclaringAnnotationsThatAreNotPresent() throws Exception { final Field idAnnotations = IdPropertyResolverUtils.class.getDeclaredField("ID_ANNOTATIONS"); idAnnotations.setAccessible(true); final List list = (List) idAnnotations.get(null); list.add("random class name"); final String propertyName = IdPropertyResolverUtils.getPropertyNameFromAnnotatedMethod( EntityWithAnnotatedIdGetterFromJPA.class, Integer.class, EntityWithAnnotatedIdGetterFromJPA.class.getDeclaredMethod("getMyCustomId")); assertThat(propertyName, is(notNullValue())); }