Skip to content

Commit

Permalink
fix: check & preserve visibility
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
rushiiMachine committed Nov 10, 2023
1 parent c32b508 commit 821569f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.github.materiiapps.enumutil.ksp

import com.google.devtools.ksp.getDeclaredProperties
import com.google.devtools.ksp.getVisibility
import com.google.devtools.ksp.processing.CodeGenerator
import com.google.devtools.ksp.processing.Dependencies
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.symbol.ClassKind
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSValueParameter
import com.google.devtools.ksp.symbol.KSVisitorVoid
import com.google.devtools.ksp.symbol.*
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ksp.toClassName
import com.squareup.kotlinpoet.ksp.toKModifier
import com.squareup.kotlinpoet.ksp.toTypeName
import com.squareup.kotlinpoet.ksp.writeTo

Expand All @@ -23,6 +24,12 @@ class FromValueVisitor(
return
}

// Check that target class isn't private
if (classDeclaration.getVisibility() == Visibility.PRIVATE) {
logger.error("FromValue: class must not be private", classDeclaration)
return
}

val annotation = classDeclaration.annotations
.find { it.shortName.asString() == "FromValue" } // I can't find a way to use qualified name, hopefully nobody else uses @FromValue
?: throw IllegalStateException("annotation missing; lib broken")
Expand Down Expand Up @@ -57,6 +64,16 @@ class FromValueVisitor(
param
}

// Check that the target param is not private
if (classDeclaration.getDeclaredProperties()
.find { it.simpleName.asString() == targetFieldName }!!
.getVisibility() == Visibility.PRIVATE
) {
logger.error("FromValue: target field cannot be private!", classDeclaration)
return
}

// Get all enum entries
val enumFields = classDeclaration.declarations
.filter { (it as? KSClassDeclaration)?.classKind == ClassKind.ENUM_ENTRY }
.map { it as KSClassDeclaration }
Expand Down Expand Up @@ -106,6 +123,7 @@ class FromValueVisitor(
val parentClassName = parentClass.toClassName()

return FunSpec.builder("fromValue")
.addModifiers(parentClass.getVisibility().toKModifier() ?: KModifier.PUBLIC)
.receiver(companion.toClassName())
.returns(parentClassName.copy(nullable = true))
.addParameter("param", param.type.toTypeName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.github.materiiapps.enumutil.ksp.example
import com.github.materiiapps.enumutil.FromValue

@FromValue("code")
enum class OpCodes(val code: Int, val display: String) {
internal enum class OpCodes(val code: Int, val display: String) {
READY(1, "Ready"),
DELETE(2, "Delete"),
CREATE(3, "Create"),
Expand Down

0 comments on commit 821569f

Please sign in to comment.