Skip to content

Commit

Permalink
fix: deprecated annotation won't work on helper options
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyashsaitwal committed Sep 28, 2023
1 parent b07aecf commit db1e1e7
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import javax.annotation.processing.*
import javax.lang.model.SourceVersion
import javax.lang.model.element.Element
import javax.lang.model.element.ExecutableElement
import javax.lang.model.element.Modifier
import javax.lang.model.element.TypeElement
import javax.lang.model.util.Elements
import javax.tools.Diagnostic.Kind
Expand Down Expand Up @@ -51,7 +50,7 @@ class ExtensionProcessor : AbstractProcessor() {
isFirstRound = false

val elements = roundEnv.getElementsAnnotatedWith(Extension::class.java)
val extensions = elements.map { processExtensionElement(it, elementUtils) }
val extensions = elements.map { processExtensionElement(it) }

val generator = InfoFilesGenerator(extensions)
try {
Expand All @@ -64,7 +63,7 @@ class ExtensionProcessor : AbstractProcessor() {
return false
}

private fun processExtensionElement(element: Element, elementUtils: Elements): Ext {
private fun processExtensionElement(element: Element): Ext {
val events = element.enclosedElements
.filter { it.getAnnotation(SimpleEvent::class.java) != null }
.map { Event(it as ExecutableElement, messager, elementUtils) }
Expand All @@ -79,7 +78,7 @@ class ExtensionProcessor : AbstractProcessor() {

val designerProperties = element.enclosedElements
.filter { it.getAnnotation(DesignerPropertyAnnotation::class.java) != null }
.map { DesignerProperty(it as ExecutableElement, messager, properties) }
.map { DesignerProperty(it as ExecutableElement, messager, elementUtils, properties) }

val packageName = elementUtils.getPackageOf(element).qualifiedName.toString()
val fqcn = "$packageName.${element.simpleName}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package io.github.shreyashsaitwal.rush.block

import io.github.shreyashsaitwal.rush.Utils
import shaded.org.json.JSONObject
import java.lang.Deprecated
import javax.annotation.processing.Messager
import javax.lang.model.element.ExecutableElement
import javax.lang.model.element.Modifier
import javax.lang.model.element.VariableElement
import javax.lang.model.util.Elements
import javax.tools.Diagnostic
import kotlin.String

/**
* Represents AI2 block types:
Expand All @@ -17,7 +16,7 @@ import kotlin.String
* - `SimpleProperty`
* - `DesignerProperty`
*/
abstract class Block(val element: ExecutableElement, val messager: Messager) {
abstract class Block(val element: ExecutableElement, val messager: Messager, val elementUtils: Elements) {

/**
* Name of this block.
Expand All @@ -37,12 +36,12 @@ abstract class Block(val element: ExecutableElement, val messager: Messager) {
/**
* Helper (dropdown blocks) definition of this block.
*/
open val helper = Helper.tryFrom(element)
open val helper = Helper.tryFrom(element, elementUtils)

/**
* Whether this block is deprecated.
*/
val deprecated = element.getAnnotation(Deprecated::class.java) != null
val deprecated = elementUtils.isDeprecated(element)

/**
* Checks that are supposed to be performed on this block.
Expand All @@ -51,7 +50,11 @@ abstract class Block(val element: ExecutableElement, val messager: Messager) {
val type = this::class.java.simpleName.toString()

if (!Utils.isPascalCase(name)) {
messager.printMessage(Diagnostic.Kind.WARNING, "$type should follow `PascalCase` naming convention.", element)
messager.printMessage(
Diagnostic.Kind.WARNING,
"$type should follow `PascalCase` naming convention.",
element
)
}

if (!element.modifiers.contains(Modifier.PUBLIC)) {
Expand All @@ -70,7 +73,8 @@ abstract class Block(val element: ExecutableElement, val messager: Messager) {
* - `SimpleFunction`
* - `SimpleEvent`
*/
abstract class ParameterizedBlock(element: ExecutableElement, messager: Messager) : Block(element, messager) {
abstract class ParameterizedBlock(element: ExecutableElement, messager: Messager, elementUtil: Elements) :
Block(element, messager, elementUtil) {

data class Parameter(
val element: VariableElement,
Expand Down Expand Up @@ -103,7 +107,7 @@ abstract class ParameterizedBlock(element: ExecutableElement, messager: Messager
* @return The parameters of this parameterized block.
*/
val params: List<Parameter> = element.parameters.map {
val helper = Helper.tryFrom(it)
val helper = Helper.tryFrom(it, elementUtil)
Parameter(
it,
it.simpleName.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import com.google.appinventor.components.annotations.DesignerProperty
import shaded.org.json.JSONObject
import javax.annotation.processing.Messager
import javax.lang.model.element.ExecutableElement
import javax.lang.model.util.Elements
import javax.tools.Diagnostic.Kind

class DesignerProperty(
element: ExecutableElement,
messager: Messager,
elementUtils: Elements,
private val properties: List<Property>,
) : Block(element, messager) {
) : Block(element, messager, elementUtils) {

init {
runChecks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import javax.tools.Diagnostic.Kind
class Event(
element: ExecutableElement,
messager: Messager,
private val elementUtils: Elements,
) : ParameterizedBlock(element, messager) {
elementUtils: Elements,
) : ParameterizedBlock(element, messager, elementUtils) {

init {
runChecks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import javax.tools.Diagnostic.Kind
class Function(
element: ExecutableElement,
messager: Messager,
private val elementUtils: Elements,
) : ParameterizedBlock(element, messager) {
elementUtils: Elements,
) : ParameterizedBlock(element, messager, elementUtils) {

init {
runChecks()
Expand All @@ -34,7 +34,7 @@ class Function(
override val returnType: String?
get() {
val continuationType = continuationUnderlyingType()
val helper = Helper.tryFrom(element)
val helper = Helper.tryFrom(element, elementUtils)

return if (continuationType != null) {
if (element.returnType.kind != TypeKind.VOID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import javax.lang.model.element.ExecutableElement
import javax.lang.model.element.TypeElement
import javax.lang.model.type.DeclaredType
import javax.lang.model.type.MirroredTypeException
import javax.lang.model.util.Elements

/**
* Possible types of helper blocks.
Expand Down Expand Up @@ -74,7 +75,7 @@ data class Helper(
/**
* @return Generates [Helper] from [element] if it is of valid [HelperType], otherwise null.
*/
fun tryFrom(element: Element): Helper? {
fun tryFrom(element: Element, elementUtils: Elements): Helper? {
val helperType = HelperType.tryFrom(element)
return when (helperType) {
HelperType.ASSET -> {
Expand Down Expand Up @@ -111,7 +112,7 @@ data class Helper(
val data = if (isCached) {
HelperSingleton.optionListCache[optionListEnumName]!!
} else {
OptionListData(helperElement).apply {
OptionListData(helperElement, elementUtils).apply {
HelperSingleton.optionListCache.putIfAbsent(optionListEnumName, this)
}
}
Expand Down Expand Up @@ -168,7 +169,7 @@ private object HelperSingleton {
}
}

private class OptionListData(private val element: Element) : HelperData() {
private class OptionListData(element: Element, private val elementUtils: Elements) : HelperData() {

data class Option(
val deprecated: Boolean,
Expand Down Expand Up @@ -217,7 +218,8 @@ private class OptionListData(private val element: Element) : HelperData() {
val enclosedElements = (elementType as DeclaredType).asElement().enclosedElements
val enumElements = enclosedElements.filter { enumConsts.containsKey(it.simpleName.toString()) }

defaultOption = enumElements.singleOrNull { it.getAnnotation(Default::class.java) != null }
defaultOption = enumElements
.singleOrNull { it.getAnnotation(Default::class.java) != null }
?.simpleName?.toString() ?: enumConsts.keys.first()

// The typeName property returns name like this: com.google.appinventor.components.common.OptionList<java.lang.String>
Expand All @@ -226,7 +228,9 @@ private class OptionListData(private val element: Element) : HelperData() {

return enumConsts.map {
Option(
deprecated = element.getAnnotation(Deprecated::class.java) != null,
deprecated = elementUtils.isDeprecated(enumElements.singleOrNull { el ->
el.simpleName.toString() == it.key
}),
name = it.key,
value = it.value,
).asJsonObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ private val processedProperties = mutableListOf<Property>()
class Property(
element: ExecutableElement,
messager: Messager,
private val elementUtils: Elements,
) : Block(element, messager) {
elementUtils: Elements,
) : Block(element, messager, elementUtils) {

private val accessType: PropertyAccessType

Expand Down Expand Up @@ -73,11 +73,11 @@ class Property(

override val helper = when (element.returnType.kind) {
// Setters
TypeKind.VOID -> Helper.tryFrom(element.parameters[0])
TypeKind.VOID -> Helper.tryFrom(element.parameters[0], elementUtils)
// Getters with [DeclaredType] return type
TypeKind.DECLARED -> Helper.tryFrom((element.returnType as DeclaredType).asElement() as TypeElement)
TypeKind.DECLARED -> Helper.tryFrom((element.returnType as DeclaredType).asElement() as TypeElement, elementUtils)
// Getters with primitive return type
else -> Helper.tryFrom(element)
else -> Helper.tryFrom(element, elementUtils)
}

/**
Expand Down

0 comments on commit db1e1e7

Please sign in to comment.