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

Allow TYPE_USE on MagicConstant, Pattern, RegExp and Subst #98

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bric3
Copy link

@bric3 bric3 commented Feb 29, 2024

This should fix #43 (and complements #77 as it enable TYPE_USE on more annotations).

The main goal of this PR is to enable documenting the code on the generic type parameters. Later this could also be integrated in IDE inspection.

After this PR and this is released a developer can document it's API this way :

Map<@Pattern("[a-z]+(-[a-z]+)*") String, @Language("RegExp") String> regexes

Since TYPE_USE is not available prior Java 8, the Java 5 project stripped this enum value from source. However it was missing some cases. This PR is more lenient in detecting and stripping this value.

  1. @Target({LOCAL_VARIABLE, TYPE_USE, ANNOTATION_TYPE})
    => @Target({LOCAL_VARIABLE, ANNOTATION_TYPE})
  2. @Target({ElementType.LOCAL_VARIABLE, ElementType.TYPE_USE, ElementType.PARAMETER})
    => @Target({ElementType.LOCAL_VARIABLE, ElementType.PARAMETER})
  3. @Target({ElementType.FIELD, ElementType.TYPE_USE})
    => @Target({ElementType.FIELD, })
  4. @Target({ElementType.TYPE_USE, ElementType.FIELD})
    => @Target({ ElementType.FIELD})

The previous regex only supported the typed enum usage, and didn't work when the enum was at the end.

The new regex should be much more lenient on the placement and handles use of enum values when imported.
This allows to place this annotation on the type declaration of generic types, e.g.

Map<@pattern("p") String, @language("RegExp") String>
@bric3 bric3 changed the title Add for support type use Allow TYPE_USE on MagicConstant, Pattern, RegExp and Subst Feb 29, 2024
@amaembo
Copy link
Collaborator

amaembo commented Feb 29, 2024

Hello, Brice! As you may guess, these requests were not satisfied so far, not because we are too lazy to modify four lines of the code, but because there's no support from the tooling. Without tooling support, the change in annotation targets is useless and even confusing, as people would expect the tooling support and would complain about it.

In general, stringly-typed code is not the best thing. Especially now, when we have records. Declare your domain types, and you'll need no type annotations:

record Id(@Pattern("[a-z]+(-[a-z]+)*") String id) {}

Map<Id, java.util.regex.Pattern> regexes 
// now the code is safe and strongly typed. E.g. you won't accidentally mix key and value

@bric3
Copy link
Author

bric3 commented Feb 29, 2024

I agree with you on the DSL and "stringly-typed", however some set of specific APIs it's not practical to have intermediate types.

but because there's no support from the tooling. Without tooling support, the change in annotation targets is useless and even confusing, as people would expect the tooling support and would complain about it.

I totally get that tolling is not there. However, I don't believe having those without tooling support is confusing, instead they allow to document expectations on these values.

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

Successfully merging this pull request may close these issues.

@Language should be applicable as a type-annotation.
2 participants