Checking if a method is an overridden method of Interface using reflection - java

I am trying to auto generate some EJB service code, which are wrappers around Java DAO classes. DAO classes implement DAO interfaces, but also have their own public methods. This DAO layer is implemented by another team, so I cannot play around with it.
I am using CodeModel API to generate the code. I get each DAO class and want now to create the EJB Service code. Using java reflection I am trying to check if the method declared in the DAO class is an overridden implementing method of the interface or not. Is there anyway in which I can check that?

1) If overridden methods are with #Overridden annotation, than you could iterate through these methods, and check their annotation using this API: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/reflect/Method.html#getDeclaredAnnotations%28%29
2) If there are no annotations, I think, the only way is to iterate through parent classes and interfaces and compare method signatures, declared there with signatures in your class.

Related

Override equals on a cglib proxy

I would like to use a CGLIB proxy to add my own reusable equals() method to existing objects.
The objects do not necessarily implement any interfaces and I need to be able to cast the proxied object to the original class (without getting the target of the proxy).
Unfortunately, it seems that CGLIB implements its own equals() method and makes sure that only that method is called: there is a private static class (EqualsInterceptor) whose method intercept() implements a reasonable logic to compare proxied objects.
The problem is that this method, at the end, delegates the comparison to the target objects: I need instead to reuse some logic that is not implemented by the target classes.
Using a standard proxy, I was able to intercept the call to the equals() method and execute my logic. The problem is that these kind of proxies cannot be cast to the original class.
It seems that the only way is to rewrite some classes in the CGLIB library. It does not seem a good idea.
No, this is not possible using cglib.
You can use another library such as Byte Buddy which allows you to intercept equals/hashCode just like any other method.
For disclosure: I am the author of Byte Buddy and a maintainer for cglib which are both Apache 2.0 licensed.

Spring DAO class method - protected vs public?

In my Java Spring application I have a DAO class with methods. I would like to know which access modifier use: protected or public?
In which situation we should use protected modifier? I don't know when I should use a protected modifier, so I always use public. Is it proper way?
DAO layer is mostly used for database transactions. For eg: saving, updating, fetching, etc.
Now they don't have any business logic in them, because we put the business logic in the Service layer. Usually this Service layer makes a call to the DAO layer whenever it needs to perform a database related work.
Therefore, public should be used in most cases (as they are getting called from a different layer/package) all together.
Protected is good when you are sure that you will make call only from the same package(or sub classes), which may not always be the case. So no, protected is not recommended.
Protected doesn't really make sense for DAOs, since you need the methods in other packages, in classes which don't implement the DAO. Therefore public is almost always the way to go.
For DAO classes, you should make an interface with Dao methods declared(Which will obviously be public). Your DAO classes should extend the interface. In this way your Dao methods will be accessed from other classes via the interface reference.
It's the better way, because it will be easy to test. You can provide a mock implementation of the DAO interface to test your code. You can do this before you write your actual DAO class. If you are calling DAO methods with interface reference variable, then you can change your DAO class and it will still work because you are changing the class not the interface whose reference you are using for calling the methods(renaming class name for an example).
It's the important design principle that you should always code for interface wherever possible. I recommend you to see this answer to read about why you should code for interfaces in DAO.
You should use protected modifier in case of inheritance. When you want outside the package only child classes should be able to access the methods and properties of your class. when you need to do somethings that should not exposed in public API but still needs to be overriden by subclasses for example template method pattern.

How to get Java custom annotations from Implementation class methods

For one of my requirment i have created a inteface with multiple methods and each method annotated with my own java custom annotation.
I have a spring aop aspect for my implementation class and i am not able to get my custom annotation in aop aspect.
after doing debug i understood my custom annotation is part of the interface and not in implementation class.
How can i get my custom annotation in my implementation methods which declared in interface ?
In Java, annotations are not inhereted from interfaces.With aspects you must annotate the implementation class (and/or methods within that class), not the interface (if any) that the class implements. String aop follows Java’s rule that annotations on interfaces are not inherited.
So, if you want to work with your annotation , create an abstract super class to be able to do this.
Inside aspect you work with proxy object and methods are wrapped in proxy calls. But if you know a real class/interface , you can get annotation by reflection api from source class/interface

Dynamically instantiate classes which implement an interface and invoke interface method

I have an interface, LogParser, consisting of a single method parse(String x). I also have an ArrayList containing the names of multiple classes which all implement LogParser. Is it possible to loop through this list of class names and dynamically instantiate their respective classes and invoke parse(String x)?
The issue I've had is that the classes do not have zero-argument constructors, so I have run into InstantiationException a lot attempting to invoke the parse method through reflection.
Thanks!
If the implementation types don't have constructors, it will be very painful. The easiest way to do it is probably to use a dependency injection framework like Spring or Guice.
In Spring, you could just inject a List<LogParser>, and you would get all known implementations:
#Autowired
private List<LogParser> parsers;
Now of course you would have to define each of the LogParsers separately as Spring bean.
There is similar functionality in other frameworks as well.
You have to invoke some constructor to instantiate an object usefully. You use reflection to grab the Method for the constructor, and you instantiate with that. See Instantiate a class object with constructor that accepts a string parameter?. I didn't close as duplicate, since you have the additional question: Do you know that all your classes have a constructor with the same signature?

Placing annotations on Implementation class methods and using proxy with reflection

I am working with proxy-reflection-annotations concept in java. I have created a proxy with Proxy.getInstance method.What I observed is this method takes only interface as parameters. So while using annotations, I can only place annotations on method names in Interface, but I would like to place annotations on methods in implementations of those interfaces.
So how can I achieve this.
Thanks
I have understood that JDKProxy is used when proxying interface via implementation and CBLIB proxy is used via inheritance(subclass extends superclass) proxying.

Categories