Annotations on Java8 default are not available in Generator

157 views
Skip to first unread message

Freddy Boucher

unread,
Oct 3, 2019, 12:34:14 AM10/3/19
to GWT Users
Hi,

I'm a user of the gwt-jackson library and I've just found the following bug:

To fix it, I need to get access of the annotations on a default Java8 method interface:

    public static interface InterfaceBean {
       
@JsonIgnore
       
default String getName(){
           
return "defaultName";
       
}
   
}


But tell me if I'm wrong, GWT doesn't provide this information!
I did setup a minimal demo project to explain the issue:
https://github.com/freddyboucher/gwt-java8-default-method-interface

As you see the default `getGender` method is not listed in neither HasGender or Person JClassType

public interface HasGender {
 
@Nonnull
 
default String getGender() {
   
return "male";
 
}
}


public class Person implements HasGender {
 
@Nonnull
 
public String getName() {
   
return "Paul";
 
}
}

public class CustomGenerator extends Generator {


 
@Override
 
public final String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {
   
JClassType genderType = context.getTypeOracle().findType(HasGender.class.getName());
   
assert genderType.getMethods().length == 0;
   
assert genderType.getInheritableMethods().length == 0;
   
assert genderType.getOverridableMethods().length == 0;


   
JClassType personType = context.getTypeOracle().findType(Person.class.getName());
   
assert Arrays.equals(new String[] { "getName" }, Arrays.stream(personType.getMethods()).map(JAbstractMethod::getName).toArray());
   
assert personType.getMethods()[0].isAnnotationPresent(Nonnull.class);


   
String[] common = { "equals", "finalize", "getClass", "getName", "hashCode", "toString" };
   
assert Arrays.equals(common, Arrays.stream(personType.getInheritableMethods()).map(JAbstractMethod::getName).toArray());
   
assert Arrays.equals(common, Arrays.stream(personType.getOverridableMethods()).map(JAbstractMethod::getName).toArray());
   
return null;
 
}


}

Thomas Broyer

unread,
Oct 3, 2019, 6:28:57 AM10/3/19
to GWT Users
This is a known (and on-purpose) limitation; see https://github.com/gwtproject/gwt/issues/9371 (tl;dr: exposing the method like any other would have been a breaking change for all existing generators, which would have wanted to implement the method, unless they are updated to recognize them as default methods; and for static methods it would have really broken many generators which would have tried to override the static method!).
Because we want to encourage migration of generators to annotation processors, there was no pressing need to support it in generators; so generators (and users of generators!) cannot take advantage of static or default methods on interfaces.

Freddy Boucher

unread,
Oct 3, 2019, 9:13:05 PM10/3/19
to GWT Users
@Thomas

As always, your answer is really helpful.
Unfortunately that's not the answer I was expected. I guess the effort to migrate gwt-jackson to annotation processors is not something trivial and it won't happen soon.
Anyway, thanks again.

Frank

unread,
Oct 4, 2019, 4:24:06 AM10/4/19
to GWT Users
https://github.com/DominoKit/domino-jackson


I still use gwt-jacskon myself, so I don't know how good/advanced this domino-jacskon version is.
Reply all
Reply to author
Forward
0 new messages