Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5846741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:37:11+00:00 2026-05-22T12:37:11+00:00

I need to get a Java enum value from a string given Enum’s Class

  • 0

I need to get a Java enum value from a string given Enum’s Class instance. I tried code like below, but I’m getting “unbound wildcard type” compilation error. Seems, I need to do something with existential types, forSome {} or something, but I can’t get how to do it right.

val paramClass = method.getParameterTypes()(0)
val value = paramClass match {
  case _ if classOf[Enum[_]].isAssignableFrom(paramClass) => Enum.valueOf[_ <: Enum[_]](paramClass.asInstanceOf[Class[_ <: Enum[_]]], "MYENUM")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:37:11+00:00Added an answer on May 22, 2026 at 12:37 pm

    Hmm, tough one. I have a working solution, but I find it ugly. I’ll be interested in any more elegant approach!

    def enumValueOf[T <: Enum[T]](cls: Class[_], stringValue: String): Enum[_] =
      Enum.valueOf(cls.asInstanceOf[Class[T]], stringValue).asInstanceOf[Enum[_]]
    
    val value = paramClass match {
      case _ if classOf[Enum[_]].isAssignableFrom(paramClass) => enumValueOf(paramClass, "MYENUM")
      case _ => // other cases
    }
    

    The reason why I think we need this complexity…

    We need the compiler to believe that the Class[_] we have is actually a Class[T <: Enum[T]] (so of course, a preliminary test that this is indeed a Java enum — as done in your code — is needed). So we cast cls to Class[T], where T was inferred by the compiler to be <: Enum[T]. But the compiler still has to find a suitable T, and defaults to Nothing here. So, as far as the compiler is concerned, cls.asInstanceOf[Class[T]] is a Class[Nothing]. This is temporarily OK since it can be used to call Enum.valueOf — the problem is that the inferred return type of valueOf is then, naturally, Nothing as well. And here we have a problem, because the compiler will insert an exception when we try to actually use an instance of type Nothing. So, we finally cast the return value of valueOf to an Enum[_].

    The trick is then to always let the compiler infer the type argument to enumValueOf and never try to specify it ourselves (since we’re not supposed to know it anyway) — and thus to extract the call to Enum.valueOf in another method, giving the compiler a chance to bind a T <: Enum[T].

    As I said, I’m not very happy with this solution, which looks way more complicated than it should be…

    Update: I’ve simplified the code slightly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to calculate Math.exp() from java very frequently, is it possible to get
I need to do a few very simple URL manipulations in Java. Like get
I have this string (Java 1.5): :alpha;beta:gamma;delta I need to get an array: {:alpha,
I need to get UTF-8 working in my Java webapp (servlets + JSP, no
I need to get the value of the 'test' attribute in the xsl:when tag,
I am creating a java application and I need to get the user PINs
I'm writing an app in Java in Eclipse where I need to get the
I need to get two functions. I want to transfer data from my website
I have a problem with transferring the image (RGB data) I get from Java
I'm working with C#.net developing applications for windows mobile 6, and i need get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.