0

I am very new to Java and to annotations. So far, I have been using a .jar file to be imported for my java classes to work. For example, there is a source code for "Controller". I make java objects that inherits the source class Controller and its functions. What I essentially want to do is to remove the need for this source code by consolidating it in an annotation. So, instead of importing the entire source code for controller, I can simply annotate my java class with @Controller.

Let me describe what I have learnt for this so far. I already know how to create custom annotations in Java. I am learning how to do the processing for these annotations. I have also reviewed Java Reflection, which is what I believe the processing will involve majorly.

Please help me with some steps on how to start this task. If there is anything else needed from my end, I would be more than glad to provide. I do not have a test code ready, so I am really into the very start of this project. Would really appreciate your inputs on my query.

Thank you in advance!

  • That's technically too broad of a question. Consider this: annotations are metadata, and aren't supposed to hold any proper logic. There's nothing wrong with importing and extending a class. Also, you didn't say what a controller is and what it's supposed to do. Are you looking for code generation tools or what? – M. Prokhorov Aug 03 '21 at 10:38
  • Hi, M. Prokhorov My task is simply to replace the need of extending classes by annotations. Maybe some other person is going to use them later. I used "@Controller" simply as an example. Say, Controller is a class that gets extended for use. I want to remove the Controller class, and make an annotation of Controller. I want a class with the definition of Controller, so I can just annotate my class with @Controller. It is probably a stylistic preference or convenience, but essentially, that is my problem statement. I hope that clarifies a little. Sorry, if it is a broad question. – Dingodudesir Aug 03 '21 at 12:41
  • It is not generally possible to "replace the need of extending classes by annotations" in Java, because annotations are *metadata* of the class file and they can't hold any code like a superclass could. For one thing, adding an annotation to a class doesn't make it have a method. You will have to roll a thick framework around each of your annotations and either use proxies and interceptors to give stuff behavior, or generate code by plugging into the javac. – M. Prokhorov Aug 03 '21 at 17:42
  • 1
    Perhaps, you should learn the fundamentals of Java first. `import` does not “import source code” and classes do not “inherit source code”. A class inheriting another depends on the *compiled* form of the other and both compiled classes must be present at runtime. Extending a class means more than inheriting its members. It means, at any place where an instance of the superclass is required, an instance of the subclass would fit as well. Code injection would work for a limited set of problems, but why do you think, the first thing someone “very new to Java” should do, is changing the language? – Holger Aug 04 '21 at 17:16

0 Answers0