FasterXML / java-classmate

Library for introspecting generic type information of types, member/static methods, fields. Especially useful for POJO/Bean introspection.
http://fasterxml.com
Apache License 2.0
258 stars 42 forks source link

MemberResolver resolves String to Ljava/lang/String<Ljava/lang/String;>; #35

Closed TeemuStenhammar closed 7 years ago

TeemuStenhammar commented 7 years ago

Hi,

I ran into a mysterious behavior, for me at least. Consider the following two test cases:

private static TypeResolver typeResolver;
private static MemberResolver memberResolver;
private static AnnotationConfiguration annoConfig;

@BeforeClass
public static void setUpBefore() {
    typeResolver = new TypeResolver();
    memberResolver = new MemberResolver(typeResolver);
    annoConfig = new AnnotationConfiguration.StdConfiguration(AnnotationInclusion.INCLUDE_AND_INHERIT);
}

static class Container<T> {
    String str;
}

@Test
public void shouldFindFieldOfTypeString() {
    Container<?> container = new Container<String>();

    ResolvedType strType = typeResolver.resolve(String.class);
    ResolvedType fieldType = getFirstFieldType(container.getClass());

    assertEquals(strType.getSignature(), fieldType.getSignature());
}

@Test
public void shouldFindFieldOfTypeStringAnonymous() {
    Container<?> container = new Container<String>() {};

    ResolvedType strType = typeResolver.resolve(String.class);
    ResolvedType fieldType = getFirstFieldType(container.getClass());

    assertEquals(strType.getSignature(), fieldType.getSignature());
}

private ResolvedType getFirstFieldType(Class<?> clazz) {
    ResolvedType type = typeResolver.resolve(clazz);
    ResolvedTypeWithMembers members = memberResolver.resolve(type, annoConfig, null);
    ResolvedField field = members.getMemberFields()[0];

    return field.getType();
}

The anonymous version states that the type of the field is

String<String>

while non-anonymous gives correct plain String. Is this a known thing that happens with anonymous classes or what is going on?

TeemuStenhammar commented 7 years ago

It seems it does not have to be anonymous class at all as the following test case shows the same bahavior:

static class Actual extends Container<String> {
}

@Test
public void shouldFindFieldOfTypeStringFromActual() {
    Actual actual = new Actual();

    ResolvedType strType = typeResolver.resolve(String.class);
    ResolvedType fieldType = getFirstFieldType(actual.getClass());

    assertEquals(strType.getSignature(), fieldType.getSignature());
}
cowtowncoder commented 7 years ago

Could be related to #33, #28? Does this occur with 1.3.3 too (which has fixes for both earlier issues)

TeemuStenhammar commented 7 years ago

Those two were manifestations of the same issue. Sorry, I totally missed that you had released a new version and looked only for open issues to find similar one. Now my tests are all green again. Thanks!

cowtowncoder commented 7 years ago

@TeemuStenhammar np. Just glad it wasn't a new thing. It's bit of a concern still, but at least your case did not expose new unhandled case.