question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scopes.getReadableSource() should output annotation values.

See original GitHub issue

In 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:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

3reactions
JakeWhartoncommented, May 1, 2018

s/at Square/in Square Register/

On Mon, Apr 30, 2018, 8:24 PM Pierre-Yves Ricau notifications@github.com wrote:

At Square, 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() https://github.com/google/dagger/blob/master/java/dagger/internal/codegen/Scopes.java#L75 is currently implemented like so:

/**

  • Returns the readable source representation (name with @ prefix) of the scope’s annotation type.
  • It's readable source because it has had common package prefixes removed, e.g.

  • {@code @javax.inject.Singleton} is returned as {@code @Singleton}.
  • 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.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/google/dagger/issues/1160, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEUILtEiLIRyrVbhQxZ6jkHmNVj9Mks5tt6tHgaJpZM4Ttac2 .

0reactions
pyricaucommented, May 30, 2018

💃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found