1

This is how I have written my enum.

public enum JobClass {

    START_STOP(SchedularEvent<ExceptionNotificationsJob>.class),
    EMAIL_NOTIFICATION(SchedularEvent<GenericNotificationsJob>.class);

    private Class clazz; 

    JobClass(Class<?> clazz) {
        this.clazz = clazz;
    }

    public Class getClazz() {
        return this.clazz;
    }

}

However this does not compile! It throws following errors.

Multiple markers at this line

  • The constructor JobClass() is undefined
  • Syntax error on token ">", byte expected after this token
  • The constructor JobClass() is undefined

I am not sure how I should write this.

My aim is to get SchedularEvent<ExceptionNotificationsJob>.class when I call JobClass.START_STOP.getClazz(). How do I achieve this?

Thanks in advance!

2
  • 1
    Currently you can't do this with enums. There also is no runtime Class representation of a parametrized type, the best you can do is use a class literal of the raw type: SchedularEvent.class. The solution really depends on the use, do you just need the class to create instances? Commented Sep 7, 2017 at 10:04
  • Thanks @JornVernee that helped! To elaborate further on my use case, I am using quartz to schedule some jobs. I want to publish some custom events from execute method for those jobs. I am using spring event listeners to listen to these events. In listener class, I wanted to identify what was exact type of Scheduler that fired the event. Was it ExceptionNotificationsJob or GenericNotificationsJob? Depending on that my rest of logic will flow. Commented Sep 7, 2017 at 11:22

1 Answer 1

1

You cannot do that like this. SchedularEvent<GenericNotificationsJob>.class is not valid, because generics are erased at runtime, it only provides compile-time safety.

How you would have to rewrite the code depends on what exactly it is you want to achieve. One thing you can do is just SchedularEvent.class. You could also use Reflection to get the class of the containing type of your SchedularEvent, but that is generally not a good idea.


Also, you shouldn't use raw types. Replace Class clazz by Class<?> clazz.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.