0

What I have known are:

  • annotation was added in java 5
  • annotation can be using in method, class, and property
  • annotation can work in RUNTIME, CLASS, SOURCE( I don't know how to work with CLASS and SOURCE, and their's features)
  • annotation with retention which is RUNTIME can be implement when java program is running.

And I want to implement a annotation to have follows features:

  • ensure class only being allowed to create a instance
  • ensure methods only being allowed to access method in the class
  • it is like as friend in c++
  • it is same as public and private , but more dynamicall, like

    @MyAnnotation(allowMethods={xxx.doSomething})
    public void getValue(){}

    the getValues method only can be accessed in the instance self and xxx.doSomething() method

What should I do and learn in next?

And Where can I learn about these?

freeeze king
  • 489
  • 2
  • 6
  • 13
  • I do not completely understand the two features you want to implement, but it sounds a lot like that could be done with access modifiers (`private` vs `public`). – Thilo Dec 05 '16 at 10:20
  • @Thilo If I understand the OP correctly, it sounds like the opposite of access modifiers. "ensure methods only being allowed to access method in the class" sounds like the method shall only be allowed to call methods of the same class ( == no "outside class" dependencies ) ... could it be that? freeeze king please correct me if I understood wrongly. – Fildor Dec 05 '16 at 10:27
  • @Fildor I have update question description, can you understand it ? if not ,please @ me – freeeze king Dec 05 '16 at 10:38

1 Answers1

1

I think you might be misunderstanding something there. Annotations are descriptive elements, not parts of your program. You can write as many annotations as you want, and people who use your code will still be able to ignore them.

That said, an annotation that enforces a policy (as yours does) can actually be implemented, either at compile or at runtime, but you need an external mechanism to help you. I can think of 3:

  1. Annotation processing lets you interact with the compiler and process annotations by generating code or by omitting compiler errors. Unfortunately, I don't think it will work for your case, as you want to protect your annotated type from instantiation, and that means the call site doesn't actually have an annotation. Annotation processing only gives you access to the actual code pieces that have annotations, not to those that refer to them.
  2. AspectJ allows you to write policy enforcement aspects and omit compiler errors, based on static pointcuts. The problem here is that static pointcuts have very limited semantics, so while you could forbid the instantiation of your class altogether, or from certain packages, you could not limit the your class instantiations to 1.
  3. The third way, and probably the only sane way is that you use a container like Spring or Guice and configure your class as singleton. As long as you only retrieve your class from the container, it will never create a second instance.

Finally: If you want to limit the number of instantiations of your class, you can always use a classic Singleton pattern approach.

Community
  • 1
  • 1
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588