Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advanced @Contract capabilities #30

Open
burdoto opened this issue Jul 26, 2020 · 2 comments
Open

Advanced @Contract capabilities #30

burdoto opened this issue Jul 26, 2020 · 2 comments

Comments

@burdoto
Copy link

burdoto commented Jul 26, 2020

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:

    @Contract("CacheReference -> param1; null -> fail; !null -> new")
    protected abstract CacheReference<K, V> advanceIntoCacheRef(Reference<V> 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.

@burdoto
Copy link
Author

burdoto commented Aug 26, 2023

Here's another potential use case for more advanced Contracts:
Simplifying the side effect of a method call, maybe like this:
grafik

That would remove the warning from the #pid() call here:
grafik

@burdoto
Copy link
Author

burdoto commented Jun 15, 2024

One more use case of simplifying side effects could be this;

    @Contract(sideEffect = "true -> mod.getPlayers().get(playerId) != null")
    public boolean isOnline(UUID playerId) {...}

where after a method calls isOnline() and checks whether it returns true (e.g. by throwing when its false), the analyzer would know that mod.getPlayers().get(playerId) will not return null when invoked with the same playerId parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant