grandstaish/paperparcel

Anonymous classes in enum

Closed this issue · 3 comments

According to https://kotlinlang.org/docs/reference/enum-classes.html#anonymous-classes, I have the following enum class:

enum class Permission {
    NONE {
        override fun canViewContent(): Boolean {
            return false
        }

    },
    INVITED {
        override fun canViewContent(): Boolean {
            return false
        }
    },
    NORMAL {
        override fun canViewContent(): Boolean {
            return true
        }

    },
    ADMIN {
        override fun canViewContent(): Boolean {
            return true
        }
    };

    abstract fun canViewContent(): Boolean
}

This enum is used in the following parceled data class in a map:

@PaperParcel
data class Group(
        var title: String? = null,
        var description: String? = null,
        val users: Map<String, Permission> = emptyMap(),
        var id: String? = null
) : PaperParcelable {
    companion object {
        @JvmField val CREATOR = PaperParcelGroup.CREATOR
    }
}

Building the project now prints out the following error:

Error:modifier abstract not allowed here

//This works fine
enum class Permission {
    NONE, INVITED, NORMAL, ADMIN
}

Having the enum define only the values themselves and nothing else compiles and works just fine, and the enum values are basically all I need to parcel anyway.

Is there a way how to setup the ProcessorConfig so that it excludes this anonymous class part, or is it a bug in PaperParcel? I'm new to Kotlin, so I don't really understand yet what's going on behind the scenes when creating this kind of enum.

Thank you.

That's weird. I'm fairly certain that isn't a PaperParcel error. Here's all of the possible PaperParcel errors: https://github.com/grandstaish/paperparcel/blob/master/paperparcel-compiler/src/main/java/paperparcel/ErrorMessages.java

It might be a kapt issue. Let me investigate after work and get back to you.

All right, sounds good, thanks.

This is part of the error too, if it's worth anything:

Error:Execution failed for task ':app:kaptDebugKotlin'.

Annotation processing error. See log for more details

For the record, I implemented what I wanted using Kotlin's extensions methods instead.

fun Permission.canViewContent(): Boolean {
    return this.ordinal > Permission.INVITED.ordinal
}

FYI I just ran your code snippet on the latest kotlin EAP and it has been fixed, so expect this to work in the coming future!