1

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?

asked Sep 17, 2017 at 18:54

2 Answers 2

4

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.

answered Sep 17, 2017 at 19:36
2
  • access thru alternative means? These types implement AnnotatedElement, which allows access to annotations assigned to these types. Commented 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. Commented Sep 18, 2017 at 19:00
2

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.

answered Sep 17, 2017 at 19:16
2
  • 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? Commented 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. Commented Sep 18, 2017 at 19:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.