Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,13 @@ public static Set<Method> getterMethodsOf(TypeToken<?> type, Set<Class<?>> inclu
return filterGetterMethods(clazz.getMethods(), includeAnnotations, isClassAnnotated);
}

private static Set<Method> filterGetterMethods(Method[] methods, Set<Class<?>> includeAnnotations,
static Set<Method> filterGetterMethods(Method[] methods, Set<Class<?>> includeAnnotations,
boolean isClassAnnotated) {
Set<Method> getters = new TreeSet<>(GETTER_COMPARATOR);
for (Method method : methods) {
if (isPublic(method.getModifiers())
&& isNotDefinedInObjectClass(method)
&& !method.isBridge()
&& isGetter(method, includeAnnotations, isClassAnnotated)) {
getters.add(method);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;

import static java.util.Arrays.asList;
import static org.assertj.assertions.generator.util.ClassUtil.collectClasses;
Expand All @@ -77,6 +79,7 @@
import static org.assertj.assertions.generator.util.ClassUtil.propertyNameOf;
import static org.assertj.assertions.generator.util.ClassUtil.resolveTypeNameInPackage;
import static org.assertj.assertions.generator.util.ClassUtil.visibilityOf;
import static org.assertj.assertions.generator.util.ClassUtil.filterGetterMethods;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

Expand Down Expand Up @@ -276,6 +279,22 @@ void should_return_property_name_from_getter_method_name() throws Exception {
assertThat(getterProperty("get")).isEqualTo("get");
}

@Test
void should_not_return_getter_bridge_methods() throws Exception {
Method[] methods = ConcreteClass.class.getDeclaredMethods();
Method actualGetter = Arrays.stream(methods).filter(Predicate.not(Method::isBridge)).findFirst().orElseThrow();
Method bridgeGetter = Arrays.stream(methods).filter(Method::isBridge).findFirst().orElseThrow();

Method[] methodsActualFirst = new Method[] { actualGetter, bridgeGetter };
Method[] methodsBridgeFirst = new Method[] { bridgeGetter, actualGetter };

Set<Method> getterMethods1 = filterGetterMethods(methodsActualFirst, Collections.emptySet(), false);
assertThat(getterMethods1).singleElement().extracting(Method::getReturnType).isEqualTo(String.class);

Set<Method> getterMethods2 = filterGetterMethods(methodsBridgeFirst, Collections.emptySet(), false);
assertThat(getterMethods2).singleElement().extracting(Method::getReturnType).isEqualTo(String.class);
}

@ParameterizedTest
@FieldSource("NESTED_CLASSES")
void should_return_inner_class_name_with_outer_class_name(NestedClass nestedClass) {
Expand Down Expand Up @@ -476,4 +495,14 @@ public <T extends Number> T getNumber() {
return null;
}
}

private static abstract class AbstractClassWithGeneric<T extends Object> {
public abstract T getValue();
}
private static class ConcreteClass extends AbstractClassWithGeneric<String> {
@Override
public String getValue() {
return null;
}
}
}