Check if the method is invoked - java

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.

Related

Can I declare advice for surrounding an advice itself in aop java?

I have made an annotation(#MethodLogger) and written an aspect over it which basically logs the timing of the method and keep storing it. So on any method on which i put that annotation it works fine
But in a special use case I need to monitor an advice itself:
eg:
#MethodLogger
#Around("some function specified")
public Object method(ProceedingJoinPoint pjp) throws Throwable{
// code here
}
But this thing does not works. It never invokes my annotations aspect.
This does not work in Spring AOP, as is documented here:
Advising aspects with other aspects?
In Spring AOP, it is not possible to have aspects themselves be the target of advice from other aspects. The #Aspect annotation on a class marks it as an aspect, and hence excludes it from auto-proxying.
If you want to do this, you need to activate full AspectJ via LTW in Spring. Then you can target advices directly if you know their method name. There even is a special pointcut designator adviceexecution() if you generally want to limit pointcut matching to advice execution or exclude the latter from matching via !adviceexecution(). For more details please check the AspectJ documentation.

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 that disregards method call if a condition is not satisfied

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.

How should I write my own custom Annotation in Java

Let say I want to check condition[let say boundary values] on some of the method arguments.Instead of writing "if" condition to check [boundary condition] on every method, I want to annotate argument only. Let me know the Steps to understand it. Working code will be awesome.
You need to look into method interception. What you are wanting is an interceptor that can validate method arguments on invocation. I like the AOP Alliance interfaces for this, they work pretty well. It also integrates with Guice natively and I think Spring has support for it as well.
Steps:
Define an annotation
Create an interceptor to process the annotation
Bind the interceptor (manually or using some framework)

Categories

Resources