Boxfit icon indicating copy to clipboard operation
Boxfit copied to clipboard

Generic and array type

Open benjaminledet opened this issue 7 years ago • 5 comments

Hi, I've followed your method in the part "No generic classes" but I'm having trouble with the results part. Indeed, my api doesn't always return an array but a single object (when I do a GET for one object for example) so I can't write this otherwise the single data will not be retrieved: List<T> results = new ArrayList<>();

So my thought was to write this :

open class Response<T>(

        @BoxfitField
        var status: String = "",

        @BoxfitField
        open var data: T? = null,

        @BoxfitField
        var message: String = ""
) {
        override fun toString(): String = "status : $status  message : $message  \n data : $data"
}

@BoxfitClass class GroupOneResponse: Response<Group>()

@BoxfitClass class GroupListResponse: Response<ArrayList<Group>>()

The GroupOneResponse works well, but the one with the arrayList return this error :

[kapt] An exception occurred: java.lang.NullPointerException at . com.manuege.boxfit_processor.info.FieldInfo.newInstance(FieldInfo.java:136) at com.manuege.boxfit_processor.info.ClassInfo.getFields(ClassInfo.java:117) at com.manuege.boxfit_processor.info.ClassInfo.getFields(ClassInfo.java:108) at com.manuege.boxfit_processor.info.ClassInfo.newInstance(ClassInfo.java:80) at . com.manuege.boxfit_processor.processor.BoxfitProcessor.process(BoxfitProcessor.java:43) at org.jetbrains.kotlin.kapt3.base.ProcessorWrapper.process(annotationProcessing.kt:99) ... 

Do you have a solution? Thanks.

benjaminledet avatar Jul 17 '18 14:07 benjaminledet

I think that the issue is actually only in GroupListResponse, could you remove the BoxfitClass annotation for it and build it again? If it builds, It may be a problem with the ArrayList in the generic parameter, so I think you can try this thing:

@BoxfitClass class GroupOneResponse: Response<Group>() // As you already have
class ListResponse<T> : Response<ArrayList<T>>()
class GroupListResponse : ArrayListResponse <Group>()

I know this is not perfect, but handling with generic parameters is quite tricky and I'll need time to fix the issue.

ManueGE avatar Jul 18 '18 06:07 ManueGE

It does build without the BoxfitClass annotation. And yes, it works with : @BoxfitField open var data: List<T> = arrayListOf()

But it makes me have two generic classes so it's indeed not perfect. I understand it can be tricky, I hope you'll be able to fix it. Thanks

benjaminledet avatar Jul 18 '18 07:07 benjaminledet

Good!

Did you try my solution? I didn't say to add a completely different generic class for lists. Subclassing it in the way I told you may be enough and it will be less repetitive:

// Your original Response class
open class Response<T>( // Whatever it does ) {}

// Your Concrete subclass for one single object
@BoxfitClass class GroupOneResponse: Response<Group>() // As you already have

// Concrete subclass (but still generic) with an ArrayList as parameter
open class ListResponse<T> : Response<ArrayList<T>>()

// Concrete subclass of ListResponse
class GroupListResponse : ListResponse <Group>()

I don't expect to update the library because of this in the short future, so I hope that one of these solutions is enough.

Thanks :)

ManueGE avatar Jul 18 '18 07:07 ManueGE

Hum I don't really understand what you trying to do, the class "GroupListResponse" should be @BoxfitClass class GroupListResponse: ListResponse<Group>() and not class GroupListResponse : ArrayListResponse <KtParent>()

and it does not work :

Type mismatch: inferred type is kotlin.collections.ArrayList? /* = java.util.ArrayList? */ but Group? 
was expected

Type mismatch: inferred type is Group? but kotlin.collections.ArrayList? /* = java.util.ArrayList? */ was 
expected 

And if I don't set the @BoxfitClass annotation the app crash because the class need it for the BoxfitFields

benjaminledet avatar Jul 18 '18 07:07 benjaminledet

Yes, you are right, this is not working. You will need to go with your first solution. Sorry.

ManueGE avatar Jul 18 '18 07:07 ManueGE