Scopes.getReadableSource() should output annotation values.
See original GitHub issueIn Square Register, we use a single scope annotation for most scopes and rely on annotation equality that Dagger implements correctly. Our annotation is @SingleIn
:
/**
* A class annotated with SingleIn(foo) is a singleton scoped to (ie, held by) the
* {@link dagger.Component} that is also annotated with SingleIn(foo).
*/
@Scope public @interface SingleIn {
Class<?> value();
}
Usage example: @SingleIn(LoggedInScope.class)
This has two advantages:
- No need to create new annotations for every scope (we have a lot of scope)
- We can command + click the class, which is usually an outer class of where the component is defined, so that provides easy navigation.
This works great. Unfortunately, Dagger error messages do not output the annotation values:
/src/main/java/com/squareup/FooComponent.java:18: error: com.squareup.FooComponent scoped with @com.squareup.dagger.SingleIn may not reference bindings with different scopes:
@Component(
^
@com.squareup.dagger.SingleIn class com.squareup.bar.Bar
1 error
Ideally this would be:
/src/main/java/com/squareup/FooComponent.java:18: error: com.squareup.FooComponent scoped with @com.squareup.dagger.SingleIn(com.squareup.AppScope.class) may not reference bindings with different scopes:
@Component(
^
@com.squareup.dagger.SingleIn(com.squareup.LoggedInScope.class) class com.squareup.bar.Bar
1 error
Scopes.getReadableSource() is currently implemented like so:
/**
* Returns the readable source representation (name with @ prefix) of the scope's annotation type.
*
* <p>It's readable source because it has had common package prefixes removed, e.g.
* {@code @javax.inject.Singleton} is returned as {@code @Singleton}.
*
* <p>Does not return any annotation values, since {@link javax.inject.Scope @Scope} annotations
* are not supposed to have any.
*/
static String getReadableSource(Scope scope) {
return stripCommonTypePrefixes("@" + scope.scopeAnnotationElement().getQualifiedName());
}
While the Javax scope annotation javadoc states it should not have attributes, Dagger doesn’t enforce that and it gives us much more flexibility.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6
Top Results From Across the Web
Is it possible to read the value of a annotation in java?
Yes, if your Column annotation has the runtime retention @Retention(RetentionPolicy.RUNTIME) @interface Column { .... } you can do something ...
Read more >Java Spring - Using @Scope Annotation to Set a POJO's Scope
Scope means the lifecycle of an instance. Generally, when a bean is requested by the getBean() method from other beans, the Spring framework ......
Read more >An Introduction to Annotations and Annotation Processing in ...
Annotations provide information to a program at compile time or at runtime based on which the program can take further action.
Read more >Scope (Java EE 6 ) - Oracle Help Center
Identifies scope annotations. A scope annotation applies to a class containing an injectable constructor and governs how the injector reuses instances of the ......
Read more >Spring @Value Annotation - DigitalOcean
Spring @Value annotation is used to assign default values to variables and method arguments. We can read spring environment variables as ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
s/at Square/in Square Register/
On Mon, Apr 30, 2018, 8:24 PM Pierre-Yves Ricau notifications@github.com wrote:
💃