Write An Annotation to Constrain Class/Method Naming in Java? - java

I'm trying to find a way to use annotation to constrain namings in Java. Might be something looks like this:
#Getter
public String getXxx();
And when the method name does not meet the convention, for example not started with "get", the compiler will throw some error. So that during the coding progress in IDE, such like eclipse, the developer will be able to notice.
I'm getting some ideas from this article, but it's too long and complex for me.
Any simpler examples or directions would be very nice.

Check out the custom annotations guide HERE
It also shows you how to use reflection to get info from the annotated elements, you could easily use this to fetch the method name and verify whether it meets your convention and take appropriate actions etc...

Didn't get to do it myself but it appears to be possible. You can do some background reading or go directly to the 3rd party tool they link.

Related

Java: Making own annotation that highlights methods and adds custom messaging

I have some importand methods in code that are used in a wrong way, people don't get the whole context of the process and invokes wrong methods, for example setters. If I had something like #Deprecated it could highlight / strike/ underline methods and show som info when somebody uses it. For example someone set some variables that are even not persisted as he thought that it would persist. Another person changed one method and spoiled dozen of usecases becaouse he didnt know about them..
I use Java7 and IntelliJ Idea 14
Instead of using an annotation, program defensively, check if the parameters you get make sense. Write tests to verify what happens when invalid input is provided.
I think Automated Tests, Good Method Names and such will do more good than some fancy IDE plugin to stop other developers from invoking wrong methods.

How to build call graph using JDT

In eclipse, there is "Call Hierarchy" to find the call graph(or method invoke relationship) easily.
I want to find some APIs to extract this relationship. However I cannot find any existing tutorial to help me. Can anybody give me some clues? Mainly I want to know which class in JDT should be used and what should be the input.
By the way, the goal is to extract this invoke relationship, so some other way may also help. I tried SOOT, but I think JDT is better for it has both caller and callee.
I figured it out looking the JDT code for those features, it's a nice place to find about this things.
Basically you can start with the following code snippet:
CallHierarchy callHierarchy = CallHierarchy.getDefault();
IMember[] members = { method };
MethodWrapper[] callers = callHierarchy.getCallerRoots(members);
Where method is the IMethod type from JDT.
With the resulting MethodWrappers you can get all information about it.
It's important to notice that the class CallHierarchy is internal, so it can break in a Eclipse release, but I don't know a public API for that.

How does BeanProperty.create work?

I'm trying to get beans binding to work, because it seems like the closest thing to functional reactive programming I can get NetBeans GUI builder to give me. I'm trying to understand how the code in http://javakenai-dev.cognisync.net/pub/a/2008/03/20/synchronizing-properties-with-beans-binding.html works. In particular, I'm trying to figure out how the TintedPanel example works. It seems like BeanProperty.create either has to do voodoo magic, access private fields, or take the string it gets, capitalize the first letter, prepend "get" or "set" to that, and do runtime introspection to change the string into a method it can call. Can someone point me at complete source code for the TintedPanel example, or explain to me how BeanProperty.create works (preferably with a self-contained working example)?
It looks like JSR 295 the beans binding spec is withdrawn:
https://jcp.org/en/jsr/detail?id=295
There are a couple other SO posts with suggestions for other libraries, or pointers to the old beans binding source:
What will replace Beans Binding in Java 7?
What is the state of Java Beans Binding?
Good luck!

Dependency Injection With Annotations

I would like to build my own custom DI framework based on Java annotations and I need a little direction to get started. I know it would be much easier to use one of the many wonderful frameworks out there such as guice or spring, but for the sake of my own curiosity, i'd like to build my own.
I'm not very familiar with annotations, so i'm having a bit of trouble finding resources and would really appreciate someone just sort of spelling out a few of the steps i'll need to take to get started.
As fore mentioned, id like to take a factory approach and somehow label my getters with an #Resource or #Injectable type annotation, and then in my business classes be able to set my variable dependencies with an #Inject annotation and have the resource automatically available.
Does anyone have any sort of resource they can pass along to help me understand the process of tagging methods based on annotations and then retrieving values from a separate class based on an annotation. A little direction is all I need, something to get me started. And of course i'll be happy to post a little code sample here once I get going, for the sake of others future reading of course.
EDIT
The resources I am using to put this together:
Java Reflection: Annotations
How to find annotations in a given package: Stack Overflow ?
Scanning Annotations at Runtime
I have not actually finished writing this yet, but the basic task list is going to be as follows (for anyone who might be interested in doing something similar in the future)
At class runtime scan for all #Inject fields and get object type.
Scan all classes (or just a specific package of classes (I haven't
decided yet)) for annotated methods #InjectableResource.
Loop all annotated methods and find the method that returns the
object type I am looking for.
Run the method and get the dependency.
It will also be helpful to note that when scanning all the classes I will be using a library called Javassist. Basically what this does is allows me to read the bytecode information of each class without actually loading the class. So I can read the annotation strings without creating serious memory problems.
Interesting that you want to build your own. I love Google Guice - it makes code so elegant and simple.
I've used this guide before which I found pretty useful for learning about annotations and how you can pull them out of classes and methods.
You will have to define your own Annotations which is done using #interface. Then you will have to define some kind of class for doing bindings e.g. where you see an interface bind in this concrete class. Finally, you will need some logic to pull it altogether e.g. go through each class, find each annotation, and then find a suitable binding.
Give consideration to things like lazy instantiation through Reflections and singletons. Guice, for example, allows you to use a singleton so your only using one instance of the concrete class, or you can bind a new version each time.
Good luck!
Have a look at the following methods:
java/lang/Class.html#getAnnotation(java.lang.Class)
java/lang/Class.html#getAnnotations()
java/lang/Class.html#getDeclaredAnnotations()
Methods of the same name also exist for the java/lang/reflect/Method, java/lang/reflect/Field and java/lang/reflect/Constructor classes.
So in order to use these sorts of methods, you need to know a bit about Java reflection.

Doxygen with custom annotations

I couldn't really find anythingon this anywhere: i'd like to use custom annotations with doxygen. To be more exact, I have an #android before every method thats supported on android, and would like doxygen to recognize, support and link his, similar to what it does with xrefitem. Is that possible?
The ultimate awesomeness would be to be able to do some light inference on this. For example, i'd like to infer he supportion level of a class from the percentage of it's method that are annotated wih android. I knowhat i can do somthing like that with java and inspectors, i'm just not sure if i could do this with doxygen.
Thanks a lot!
If you start with reading the documentation for \xrefitem here:
http://www.doxygen.org/commands.html#cmdxrefitem
you'll find that you can define custom commands via ALIASES, which are documented in more detail here
http://www.doxygen.org/custcmd.html
The computation of a 'coverage level' is not directly supported. You could check if you can use doxygen's XML output for that (see config option GENERATE_XML).

Categories