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

Annotation implementation builder #54

Closed
gissuebot opened this issue Jul 7, 2014 · 6 comments
Closed

Annotation implementation builder #54

gissuebot opened this issue Jul 7, 2014 · 6 comments

Comments

@gissuebot
Copy link

From limpbizkit on March 07, 2007 03:46:26

In the user's guide, there's a lengthy section on how to implement an annotation for use with
annotatedWith(). See the java listing, "NamedAnnotation"

Why not provide a builder that creates objects with the appropriate equals, hashCode etc:
  Named bob = new AnnotationBuilder<Named>(Named.class)
     .set("value", "Bob")
     .create();

It would be also possible to do an alternate form of the set() method that doesn't use Strings for
method names but it requires a ThreadLocal:
  Named bob = new AnnotationBuilder<Named>(Named.class)
        .set(fields(Named.class).value(), "Bob")
        .create();

If you like the idea, I'd love to code it!

Original issue: http://code.google.com/p/google-guice/issues/detail?id=54

@gissuebot
Copy link
Author

From crazyboblee on March 07, 2007 08:33:34

We could write an APT processor to generate implementations of all annotations
annotated with @BindingAnnotation at compile time.

@gissuebot
Copy link
Author

From crazyboblee on March 07, 2007 18:48:16

Here's another generic method we could consider. It works if you know the attribute
values at compile time.

Example usage:

  @Named("Bob")
  static Named bob = bindingAnnotationOn("bob");

The library method:

  public static <A extends Annotation> A bindingAnnotationOn(String fieldName) {
    try {
      StackTraceElement element = new Throwable().getStackTrace()[1];
      Class<?> declaringClass =
          Class.forName(element.getClassName());
      Field field = declaringClass.getDeclaredField(fieldName);
      for (Annotation annotation : field.getAnnotations()) {
        if (annotation.annotationType().isAnnotationPresent(
            BindingAnnotation.class)) {
          return (A) field.getType().cast(annotation);
        }
      }
      throw new RuntimeException("No binding annotation is present.");
    }
    catch (ClassNotFoundException e) {
      throw new RuntimeException(e);
    }
    catch (NoSuchFieldException e) {
      throw new RuntimeException(e);
    }
  }

I wish Java provided some language level meta programming support. It would be cool
if you could just write:

  @Named("Bob")
  static Named bob = &bob.getAnnotation(&bob.getType());

@gissuebot
Copy link
Author

From limpbizkit on March 07, 2007 21:57:15

Yeah, it's kinda sad that we can do this:
   Class<Foo> fooClass = Foo.class
...but not these:
   Method<Bar> getBarMethod = Foo#bar();
   Field<Bar> barField = Foo#bar;

@gissuebot
Copy link
Author

From kevinb9n on March 19, 2007 11:23:22

we should also simplify what's in the user's guide right away; they should not have
to implement their Annotation interface themselves.

Owner: kevinb9n
Labels: 1.1

@gissuebot
Copy link
Author

From kevinb9n on April 29, 2007 11:41:45

this is less important since we're supporting @Provides tag on methods ( issue 83 ).
So I'm untagging for 1.1.

Labels: -1.1

@gissuebot
Copy link
Author

From kevinb9n on June 03, 2007 11:30:49

(No comment was entered for this change.)

Status: WontFix

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

No branches or pull requests

1 participant