1

I am trying to obtain the generic type of a class using the JoinPoint in Spring AOP. But there doesnt seem to be a way to do that. This is the class whose methods I am intercepting.

public abstract class AbstractHibernateDaoBase<T, ID extends Serializable> implements BaseDao<T, ID>, PersistenceAware { //Some methods }

I am able to intercept these methods and execute advices. But what I need is the type T (using AOP) which gets its value at runtime. Is there a way to do this?

  • possible duplicate of [AspectJ pointcuts - get a reference to the joinpoint class and name](http://stackoverflow.com/questions/5069293/aspectj-pointcuts-get-a-reference-to-the-joinpoint-class-and-name) – mkobit Sep 30 '14 at 04:00
  • Hey Mike, I am able to get the class name and method name, But I am looking to extract the generic type T from that class name. – Nitin Kumar Mangalam Sep 30 '14 at 04:26
  • Hi there. How about accepting and upvoting my answer? It is correct even if it does not tell you what you like to hear. Thank you very much. – kriegaex Oct 06 '14 at 09:14
  • I don't have enough reputation to upvote right now. When I have it, I will definitely upvote it. – Nitin Kumar Mangalam Oct 13 '14 at 09:09

1 Answers1

0

What you want would be possible in .NET languages like C#, but not in Java due to the way generics are implemented. Because at the time generics were introduced backward byte (and even source) code compatibility was one of the main design goals, generic type information is unavailable during runtime due to type erasure.

Bottom line: No, you cannot determine T during runtime by any other means than creating a T object as a workaround (or using an existing one you already have) and determining its type via myT.getClass() or myT instanceof Foo.

kriegaex
  • 63,017
  • 15
  • 111
  • 202