Annotations { static List<String> fieldNameCandidatesFor(final String methodName) { if(methodName == null) { return Collections.emptyList(); } int beginIndex; if (methodName.startsWith("get")) { beginIndex = 3; } else if (methodName.startsWith("is")) { beginIndex = 2; } else { beginIndex = -1; } if(beginIndex == -1) { return Collections.emptyList(); } final String suffix = methodName.substring(beginIndex); if(suffix.length() == 0) { return Collections.emptyList(); } final char c = suffix.charAt(0); final char lower = Character.toLowerCase(c); final String candidate = "" + lower + suffix.substring(1); return Arrays.asList(candidate, "_" + candidate); } private  Annotations(); static boolean isString(final Class<?> cls); @SuppressWarnings("unchecked") static T getDeclaredAnnotation(Class<?> cls, Class<T> annotationClass); static T getAnnotation(final Class<?> cls, final Class<T> annotationClass); static List<T> getAnnotations(
            final Class<?> cls,
            final Class<T> annotationClass); static List<Evaluator<T>> getEvaluators(
            final Class<?> cls,
            final Class<T> annotationClass); static List<Evaluator<T>> firstEvaluatorsInHierarchyHaving(
            final Class<?> cls,
            final Class<T> annotationClass,
            final Predicate<Evaluator<T>> filter); static boolean isAnnotationPresent(
            final Method method, 
            final Class<? extends Annotation> annotationClass); static List<T> getAnnotations(
            final Method method,
            final int paramNum,
            final Class<T> annotationClass);  }
@Test public void when_null() throws Exception { final List<String> fieldNameCandidates = Annotations.fieldNameCandidatesFor(null); assertThat(fieldNameCandidates, is(not(nullValue()))); assertThat(fieldNameCandidates.size(), is(0)); }
@Test public void when_starts_with_getter() throws Exception { final List<String> fieldNameCandidates = Annotations.fieldNameCandidatesFor("getFirstName"); assertThat(fieldNameCandidates, is(not(nullValue()))); assertThat(fieldNameCandidates.size(), is(2)); assertThat(fieldNameCandidates.get(0), is("firstName")); assertThat(fieldNameCandidates.get(1), is("_firstName")); }
@Test public void tolerant_to_not_exactly_following_javaBean_convention() throws Exception { final List<String> fieldNameCandidates = Annotations.fieldNameCandidatesFor("getfirstName"); assertThat(fieldNameCandidates, is(not(nullValue()))); assertThat(fieldNameCandidates.size(), is(2)); assertThat(fieldNameCandidates.get(0), is("firstName")); assertThat(fieldNameCandidates.get(1), is("_firstName")); }
@Test public void when_is_exactly_just_the_get() throws Exception { final List<String> fieldNameCandidates = Annotations.fieldNameCandidatesFor("get"); assertThat(fieldNameCandidates, is(not(nullValue()))); assertThat(fieldNameCandidates.size(), is(0)); }
@Test public void when_starts_with_something_else_getter() throws Exception { final List<String> fieldNameCandidates = Annotations.fieldNameCandidatesFor("XetFirstName"); assertThat(fieldNameCandidates, is(not(nullValue()))); assertThat(fieldNameCandidates.size(), is(0)); }