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

Is it intended not to index anon classes? #39

Open
twwwt opened this issue Jul 27, 2017 · 4 comments
Open

Is it intended not to index anon classes? #39

twwwt opened this issue Jul 27, 2017 · 4 comments

Comments

@twwwt
Copy link

twwwt commented Jul 27, 2017

Consider the following example

@IndexSubclasses
public interface I { /* ... */ }

public interface J extends I { /* ... */ }

public class SomeClass
{
   public static final J j = new J() { /* ... */ }; // Anon implementation of J.
}

I would expect that the anon class (which compiles to a class file name SomeClass$1, disregarding the package name) would be included in the index of the interface I. However, a test showed that this is not the case, which brings me to the question whether this is intended?

@sentinelt
Copy link
Member

Actually this is intended. I think in most of the cases its not what the user of the library would expect. Such a feature could be added with a (disabled by default) configuration option though.

@twwwt
Copy link
Author

twwwt commented Mar 2, 2018

Adding a configuration option would be great, perhaps something like @IndexSubclasses(includeAnonClasses = true).

@sentinelt
Copy link
Member

@twwwt I'm trying to understand the purpose of such an index. I don't see how it can be useful to have an index of anonymous classes, because they have exactly one instance and it is hidden inside some class or method. What is your use case?

@twwwt
Copy link
Author

twwwt commented Mar 19, 2018

@sentinelt Consider the following class hierarchy

@IndexSubclasses
public interface Indexed { /* ... */ }

public class SomeClass {
   public static final Indexed SOME_CONSTANT = new Indexed() {
      // Anon class with some logic inside.
   };
}

public interface SubIndexed extends Indexed {
   public static final SubIndexed ANOTHER_CONSTANT = new SubIndexed() {
      // Anon class with some logic inside.
   };
}

public enum EnumIndexed implements Indexed {
   one, two, three;
}

We want to find the constants implementing Indexed. This is possible for ANOTHER_CONSTANT and the enum values in EnumIndexed because SubIndexed and EnumIndexed are contained in the index built for Indexed. However, since the anon class defined for SOME_CONSTANT is not part of the index, we have no chance to find that constant (at least not by means of the index created by Classindex). If that anon class would be part of the index, we could call getEnclosinglass() on it thereby getting SomeClass.class, on which we could then call getDeclaredFields() and filter those that are assignable from Indexed (and whose modifier is public and static).

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

2 participants