JetBrains / JetBrains/java-annotations

Advanced @Contract capabilities

Open
#30 3 comments 3 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
463
Forks
72
PR merge metrics
No merged PRs in 30d

Description

I was thinking about possible extra possibilities for the `Contract` annotation.
I have this example already; I will add more as I stumble upon them:
Consider this method:
```java
@Contract("CacheReference -> param1; null -> fail; !null -> new")
protected abstract CacheReference advanceIntoCacheRef(Reference reference);
```
The method should do the following things; as described by the contract:
- Throw an NPE, if the parameter is `null`
- Wrap the parameter `Reference` (if not `instanceof CacheReference`, else see next) into a `CacheReference`
- Return first parameter if the first parameter is `instanceof CacheReference` (which is already imported due to return type; return types should be validated by `paramN`)

Now, obviously this contract won't work, because it doesn't know about more things than nulls; really. Given other capabilities of IntelliJ, it should be fairly easy coming up with a simple, standalone concept for advanced `Contract` syntax.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.