Skip to content

Latest commit

 

History

History
111 lines (83 loc) · 3 KB

File metadata and controls

111 lines (83 loc) · 3 KB

Enchanted Java

Usage

// settings.gradle.kts

pluginManagement {
  repositories {
    maven("https://maven.yqloss.net") // The plugin
  }
}
// build.gradle.kts

plugins {
  id("net.yqloss.enchanted-java-plugin") version PLUGIN_VERSION
}

repositories {
  maven("https://maven.yqloss.net") // The library
}

dependencies {
  compileOnly("net.yqloss:enchanted-java-library:$LIBRARY_VERSION")
}

enchantedJava {
  properties.put("apiKey", "sk_test_123")
  properties.put("endpointUrl", "https://api.example.com")
  properties.put("retryCount", 3)
  properties.put("enabled", true)
}
// *.java

import static yqloss.E.*;

If you want to use hot reload, add the following JVM arguments:

-javaagent:"path/to/your/enchanted-java-plugin.jar"
-Dyqloss.enchant.prefixes=com.example.package1.,com/example/package2
-Dyqloss.enchant.classes=/path/to/your/build/classes/java/sourceSet

Only classes with the specified prefixes will be transformed.

The latest version numbers can be checked here:

Goals

  • No runtime libraries.
  • Can compile with vanilla Javac by introducing only one compileOnly library.
  • Transformations are all inside one class. No reading other classes. No generating extra classes.

Implemented Features

  • True (_all) and false (_any, _switch) constants for prettier chained logical expressions
  • Empty body placeholder: _pass()
  • Never (unreachable) as expression: _never() _never_()
  • Throw as expression: _throw(exception) _throw_(exception) $throw(exception) $$throw(exception)
  • Return as expression with finally and synchronized support: _return() _return(value) _return_() _return_(value) $return(value) $$return(value)
  • Elvis operator with short circuit: $elvis(value1, value2, ...) or $
  • Safe member access: $safe(getUser().getName()) or $
  • The two above combined: $$(a, b, c)
  • Scope functions: _void, _run, _also, _with, $also, $with, $$also, $$with
  • Unchecked cast without warning: _cast(value)
  • Compile-time random UUID for identifiers: _id
  • Compile-time computations: _const(() -> expr)
  • Class-local cached computation: _cached(() -> expr)
  • Compile-time properties: _property("name")
  • Default and named arguments: _defaultArgs(), _defaultArgsStrict(), _arg(), throw _defaultArgs_(), throw _defaultArgsStrict(), throw _arg_() _default(param, value), @Default, @Name
  • Discarded value receiver: _receiver(r), $ = sent, __ = notSent

For the bottom type, there are currently two implementations:

  • _func: returns <T> T to use in expressions
  • _func_: returns RuntimeException so as to throw _func_()

$ is used to note "null-safety related".

And you can also combine them as you want!

var input = $($(getOptionalInput().getFieldA()), _return(null));

TODO

  • Not coming soon...

License

MIT LICENSE