Java annotation that disregards method call if a condition is not satisfied - java

Is there a way in Java to make an annotation you put before a method to check if a boolean or any variable is defined/a-certain-value and if it's not it disregards the call to the method (no code from the method will be executed)?

Annotations just provide metadata about your code. They cannot directly influence your code without the use of tools such as JUnit. As stated here:
Annotations, a form of metadata, provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.

Related

Design a custom annotation to invoke a method in a jar

I'm a bit new to annotations in Java. Is it possible to design a custom annotation which when used in a class automatically invokes a method present inside another Class. TIA.
The answer is "sort of": annotations by themselves do nothing (except "compile time" annotations that affect the compile process).
Meaning: just attaching an annotation to something doesn't magically cause a method to be called at some arbitrary point in time.
This works differently: you have some sort of framework - and at some point you ask the framework to process an object, class, ... And then the framework might check for the presence of certain annotations to "do" something based on that check.
Thus: it is possible to implement custom annotations, and it is also possible to make some "framework" react to the presence of that annotation.
In case you find this answer too generic - well, it can't be more precise/specific than the question ...

How to understand annotation in java And How to implement my annotation in java?

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?
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:
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.
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.
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.

Java annotation and code execution at runtime

Annotations supposedly are markers/provide metadata, so for example how does the #RolesAllowed annotation in JAX-RS work? This annotation "executes" code since it checks that the user is authenticated and has the required roles, at runtime (when an annotated method is called).
No. There is code that checks if the annotation exists and performs operations based on it. The annotation itself just lies there. The code that performs the checking and operations is executed around the method call.

Allow annotation only for specific methods

I've implemented some custom runtime method annotation. Now I want to check (preferably in compile-time) that all methods which were marked with my newly implemented annotation are static and have only one serializable argument. How can I archive it in Java 7?
You can use annotation processing to do that. Run javac with proc, processor and processorpath option and implement a Processor that does all the checking you want.
Note that you can't enforce others to use these options, so you should still include appropriate runtime checking.

Check if the method is invoked

I am trying to create a logging OSGi bundle, which would be able to log input, output, exception (if any) parameters of all the methods which are marked with specific annotation.
I have tried to set up the Spring AOP logging in OSGi but I was unsuccessful.
All I need to know is how to determine when the method is being called? For example I have scanned all methods in a class which are annotated with my annotation and I have those methods in array or list.
For example there are three methods: getStatus, getDetails, getSomething. And the user invokes getDetails method, so how should I know when this method is invoked?
Detecting method calls requires you to instrument your code. If you want to do this at runtime this requires byte-code manipulation (which is what Spring AOP does, as far as I know). Alternatively you could instrument your code at compile-time using a custom preprocessing step that generates the instrumented Java-code, but I don't know if that is much easier.
I guess your best bet is to try and Spring AOP working.

Categories

Resources