java - How can I build an interface hierarchy for my fluent API? -


i'm working on fluent api , trying take advantage of java's generic methods offer elegant api handles type conversions users. i'm running trouble getting working though, due type erasure.

here's simplified version of interfaces shows problem i'm running into:

interface query<t extends query<t>> {   t execute();   query<t> appendclause(); }  interface a<t extends a> extends query<t> { }  class aimpl implements a<a> {   execute() { ... }   query<a> appendclause() { ... } } 

i'm getting error on aimpl.appendclause(). compiler says a not within bound , should extend query. far can tell, declaration aimpl implements a<a> means a does extend query<a>.

following answer here tried break potential unresolvable recursion changing aimpl to:

class aimpl implements a<aimpl> {   aimpl execute() { ... }   query<aimpl> appendclause() { ... } } 

now i'm getting error compiling a says "type parameter t not within bound".

has got suggestions on how handle this? java's generics giving me headache.

edit

i've changed definition of to

interface a<t extends a<t>> extends query<t> { } 

and got second implementation of aimpl working. want extend api ability query subclasses of a:

interface b<t extends b> extends a<b> { }  class bimpl implements b<bimpl> {   bimpl execute() { ... }   query<bimpl> appendclause() { ... } } 

this definition gets me error in b's declaration: "type parameter b not within bound; should extend a".

i can clear error changing b to

interface b<t extends b<t>> extends a<b<t>> { } 

but interface definitions starting ridiculous , feeling i'm doing wrong. plus, i'm still getting error in bimpl: "appendclause() in bimpl cannot implement appendclause() in query; attempting use incompatible return type".

any suggestions on how can clean subclass definitions don't need specify entire inheritance hierarchy in extends or how can bimpl working?

edit 2

okay, ran issue. have factory class generates queries:

public class queryfactory {   public static <t extends query<t>> query<t> queryfortype(class<t> type) { ... } } 

and client code:

query<b> bquery = queryfactory.queryfortype(b.class); 

my client code giving me error in declaration bquery: "type parameter 'b' not within bound; should extend 'query'". thought @ point b did extend query...

this error goes away if change queryfortype() call to

query<? extends b> bquery = queryfactory.queryfortype(b.class); 

but i'm still getting unchecked warning compiler:

unchecked method invocation: <t>queryfortype(class<t>) in queryfactory applied class<b> unchecked conversion found: query required: query<b> 

it looks type erasure fighting me again, don't understand these warnings. more suggestions me on track? thanks!

edit 3

i can compiling without warnings if change client code to

query<bimpl> bquery = queryfactory.queryfortype(bimpl.class); 

but i'd hide implementation classes users of api. tried making b abstract class instead of interface in case problem had that, didn't help.

here, interface a declares type parameter t extends raw type a. should try

//                      v--- add interface a<t extends a<t>> extends query<t> { } 

this makes sure t extends genericized a t. way, t within bound specified in query interface.

this work second version of aimpl implements a<aimpl>.

edit

having say

interface b<t extends b<t>> extends a<b<t>> { } 

looks overly complicated. b interface, extend a extended a query:

//                                    v-- don't mention b here interface b<t extends b<t>> extends a<t> { } 

then bimpl class can similar aimpl class:

class bimpl implements b<bimpl> {     public bimpl execute() { ... }     public query<bimpl> appendclause() { ... } } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -