A IntelliJ plugin to provide check on 'value type' which is limited to numerical constant values

Overview

ValueType

A IntelliJ plugin to provide check on 'value type' which is limited to numerical constant values.

This is similar with the Android annotations like @IntDef and @StringDef but more.

Sample

package com.bennyhuo.kotlin.valuetype

/**
 * Created by benny.
 */
const val Red = "red"
const val Green = "green"
const val Yellow = "yellow"

@Target(AnnotationTarget.CLASS)
annotation class ValueType(vararg val value: String)

@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.EXPRESSION)
annotation class UnsafeValueType

@Target(AnnotationTarget.TYPE)
@ValueType(Red, Yellow, Green)
annotation class Color

@Target(AnnotationTarget.TYPE)
@ValueType(Red, Yellow, Green, "black")
annotation class Color2

fun setTrafficLightColor(color: @Color String) {
    println("current light: $color")
}

fun getColor(): @Color String = "red1" // Error, Wrong value. Only one of [red, yellow, green] is allowed. 

fun getColor2(): @Color2 String {
    return getColor() // OK, @Color is a subset of @Color2
}

fun main() {
    setTrafficLightColor("red") // OK
    setTrafficLightColor("black") // Error, "black" is not allowed
    setTrafficLightColor(Red) // OK
    setTrafficLightColor(Yellow) // OK
    setTrafficLightColor("r" + "ed") // OK, "red" is one of the Colors
    
    val color: @Color String = Red
    setTrafficLightColor(color) // OK, color is type of @Color

    val unsafeColor: String = getColor()
    setTrafficLightColor(@UnsafeValueType unsafeColor) // OK, unsafe but on your own
    setTrafficLightColor(unsafeColor) // Error, unknown String value is not allowed
    setTrafficLightColor(getColor()) // OK, getColor returns '@Color String' 

    val color2: @Color @Color2 String = Red + 1 // Error, More than one value types are declared: [@Color, @Color2]. 
    val color3: @Color Int = 1 // Error, Value type 'kotlin.String' is not compatible with declared type 'kotlin.Int'.
}

License

MIT License

Copyright (c) 2021 Bennyhuo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
You might also like...
Android Studio & IntelliJ Plugin for sort xml by name=
Android Studio & IntelliJ Plugin for sort xml by name="xxx".

AndroidXmlSorter Android Studio & IntelliJ Plugin for sort xml by name="xxx". Options Insert space between difference prefix ('Snake Case', 'Camel Cas

IntelliJ plugin that provides some useful utilities to support the daily work with Gradle.
IntelliJ plugin that provides some useful utilities to support the daily work with Gradle.

IntelliJ Gradle Utilities Plugin This IntelliJ plugin provides some useful utilities to support the daily work with Gradle. It's available on the offi

IntelliJ-based IDEs Protobuf Language Plugin that provides Protobuf language support.
IntelliJ-based IDEs Protobuf Language Plugin that provides Protobuf language support.

IntelliJ Protobuf Language Plugin Reference Inspired by protobuf-jetbrains-plugin and intellij-protobuf-editor. Descriptor IntelliJ-based IDEs Protobu

Plugin for IntelliJ-based IDEs folding root files in the ProjectView
Plugin for IntelliJ-based IDEs folding root files in the ProjectView

Foldable ProjectView The Foldable ProjectView is a plugin for the IntelliJ-based IDEs that lets you fold files located in the root of your project. Av

IntelliJ plugin that provides a modern and powerful byte code analyzer tool window.
IntelliJ plugin that provides a modern and powerful byte code analyzer tool window.

IntelliJ Byte Code Analyzer Plugin This IntelliJ plugin provides a modern and powerful byte code analyzer tool window. Its supports Java, Kotlin, Groo

A Mirai console plugin that can provide osu!std players some appropriate beatmap (WIP)
A Mirai console plugin that can provide osu!std players some appropriate beatmap (WIP)

OsuMapSuggester A mirai-console plugin that can provide osu!std players some appropriate beatmap. (WIP) Features It can analyze player's aim, speed an

A plugin that adds support for rs2asm files to IntelliJ.

Rs2Asm This plugin adds some simple features when interacting with rs2asm files. Features Syntax highlighting Autocompletion for label names and instr

 IntelliJ Platform Plugin Template
IntelliJ Platform Plugin Template

IntelliJ Platform Plugin Template is a repository that provides a pure template to make it easier to create a new plugin project (check the Creating a repository from a template article).

IntelliJ IDEA / PhpStorm InertiaJS Plugin

PhpStorm and IntelliJ IDEA Inertia.js Plugin Provides support in PhpStorm and IntelliJ IDEA Ultimate for Inertia.js. 💡 GitHub Issues: feature request

Owner
Bennyhuo
Google Developer Expert @Kotlin ////////// 微信公众号 Kotlin //////////////////////////// Bilibili:bennyhuo 不是算命的
Bennyhuo
K6-intellij-plugin - IntelliJ-based Plugin to run k6 tests locally or in the k6 Cloud from your IntelliJ IDE

IntelliJ-based Plugin to run k6 tests locally or in the k6 Cloud from your Intel

Mikhail Bolotov 8 Jan 2, 2023
IntelliJ Idea Astor Plugin is a plugin that integrates Astor in Intellij Idea

IntelliJ Idea Astor Plugin IntelliJ Idea Astor Plugin is a plugin that integrates Astor in Intellij Idea. It communicates with a local/remote program

null 4 Aug 28, 2021
Intellij-platform-plugin-template - IntelliJ Platform Plugin Template

IntelliJ Platform Plugin Template TL;DR: Click the Use this template button and

null 0 Jan 1, 2022
A small plugin which loads an additional properties file for secret values.

Gradle Secrets Plugin A small plugin which loads an additional properties file for secret values. Why Using this plugin, you can have an additional fi

null 7 Dec 25, 2022
Kirill Rakhman 4 Sep 15, 2022
eventbus-intellij-plugin 3.8 0.0 L1 Java Plugin to navigate between events posted by EventBus.

eventbus-intellij-plugin Plugin to navigate between events posted by EventBus. Post to onEvent and onEvent to Post Install There are two ways. Prefere

Shinnosuke Kugimiya 315 Aug 8, 2022
✈️ IDE plugin for the IntelliJ platform which adds GitHub Copilot support. (VERY WIP)

JetBrains Copilot GitHub Copilot support for the IntellIJ Platform. Installation Download the latest release. Select the Install Plugin from Disk opti

Koding 155 Dec 10, 2022
IntelliJ Plugin for Android Parcelable boilerplate code generation.

IntelliJ/Android Studio Plugin for Android Parcelable boilerplate code generation This tool generates an Android Parcelable implementation based on fi

Michał Charmas 2.1k Dec 27, 2022
A plugin for Android Studio and Intellij IDEA that speeds up your day to day android development.

ADB Idea A plugin for Android Studio and Intellij IDEA that speeds up your day to day android development. The following commands are provided: Uninst

Philippe Breault 2k Dec 28, 2022
IntelliJ / Android Studio plugin for Android Holo Colors

This project is not maintained anymore. Holo Colors doesn't make sense since the introduction of Material Design and the ability to set the primary co

Jérôme Van Der Linden 644 Nov 10, 2022