Below are the relevant types(in java) for annotation processing,
enter image description here
In addition to Field
& Method
types, Package
type is also used in context of annotations with reflection, because these types implement AnnotatedElement
interface.
This contract allows to access(in runtime) the instances of those types to get their meta data.
Why Package
does not inherit AccessibleObject
?
2 Answers 2
Because packages aren't accessible. What the word "Accessible" in the name AccessibleObject
means is effectively has an access specification and associated scope. For example, an object might have public
access (at which point its scope is irrelevant), or private
access from within class Foobar
, or package-local (default) access in package com.example.foo
. AccessibleObject
also provides access to annotations that are declared on a Java source element, but this isn't its primary purpose.
Packages don't have this. All packages in Java are public, and are globally accessible. (I believe Java 9 introduces a system that is going to change this in some ways, but I believe it just introduces a new orthogonal system of classification that isn't going to affect AccessibleObject and packages).
Packages can have annotations, but access to these is provide through alternative means, so have no need to extend AccessibleObject
to provide the relevant API for this.
This means that the methods in AccessibleObject
(mostly isAccessible()
and setAccessible(boolean)
) are irrelevant for packages, because packages are always accessible and this is not something that can be set. Therefore, extending this type would not be appropriate for them.
-
access thru alternative means? These types implement
AnnotatedElement
, which allows access to annotations assigned to these types.user1787812– user17878122017年09月17日 19:44:43 +00:00Commented Sep 17, 2017 at 19:44 -
@user1787812 You are confounding the meaning of the word "access". The term "Accessible" has a very specific meaning, and you are trying to use it in its more general, natural language way. The given answer is correct.BobDalgleish– BobDalgleish2017年09月18日 19:00:56 +00:00Commented Sep 18, 2017 at 19:00
You could just as well ask "why don't List
collections inherit from AccessibleObject
," or any other class in Java, for that matter? The short answer is "because they don't have to."
The java.lang.reflect
namespace is meant to provide Reflection capabilities over any object in the Java ecosystem, without requiring that said object inherits from a Reflection base class, or any other class. That's kind of the whole point of reflection: to do things in Java that would normally require inheritance or composition to achieve.
You said it yourself: "Package type is used in context of annotations with reflection." It is those annotations that Reflection is interested in, not your class inheritance hierarchy.
-
When you say: that would normally require inheritance or composition to achieve, you mean, for every class loaded by class loader, there will be a separate copy of
Class
object?user1787812– user17878122017年09月18日 18:40:16 +00:00Commented Sep 18, 2017 at 18:40 -
No, I mean things that you would normally use inheritance or composition to accomplish. Reflection doesn't require inheritance or composition (of your own classes) to function properly.Robert Harvey– Robert Harvey2017年09月18日 19:12:57 +00:00Commented Sep 18, 2017 at 19:12
Explore related questions
See similar questions with these tags.