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 8267565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:34:44+00:00 2026-06-08T05:34:44+00:00

I have a class with a field that is specialized and is using a

  • 0

I have a class with a field that is specialized and is using a raw datatype. For instance a Tuple2[Int, String]:

scala> class TupleReflection(val tuple: Tuple2[Int, String])
defined class TupleReflection

scala> val refl = new TupleReflection((5, "hello"))
refl: TupleReflection = TupleReflection@1a597ec8   

I want to use reflection now to find out the type parameters of the Tuple2 inside my ‘refl’ instance. (I cheat a little using ‘head’ to get the field because I know it’s the only one.)

scala> val field = refl.getClass.getDeclaredFields.head
field: java.lang.reflect.Field = private final scala.Tuple2 TupleReflection.tuple

Now that I have the field I can query the generic types.

scala> field.getGenericType
res41: java.lang.reflect.Type = scala.Tuple2<java.lang.Object, java.lang.String>

The problem now is that the first type is Object. Is there a way to know, via reflection alone, the real type (Int) of that parameter?

Update:

I’m using this in a context of automatic serialization within my own API. Given a class marked with @Serializable I can serialize it. To do so I must build a tree of the fields and types of the class recursively using reflection so I can do a deep serialization.

If I’m working directly with a @Specialized class it works because the types are explicit and known at compile time at the invocation site. If a field in the hierarchy is @specialized I have no way to tell via reflection. Querying the fields or methods declared in a class doesn’t yield the correct value. The type is present in runtime but only on the instance held in the field, not on the declaration of the field itself. Thus if the instance is null and can’t do a “getClass” I can’t know the correct types by reflection alone.

  • 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-06-08T05:34:47+00:00Added an answer on June 8, 2026 at 5:34 am

    The Problem is you are using a Java reflection API which doesn’t workaround the JVM’s "Type Erasure" issue, because of that there’s no way to find out the actual generic types with it.

    Fortunately the coming 2.10 version of Scala implements new reflection API, which solves the type erasure issue. But since 2.10 hasn’t been released yet, the API isn’t standardized nor documented yet. Your best bet is to dig into it with some tool like debugger and ask more specific questions that come up here.

    In Scala 2.10-M5 you can access the API like follows:

    scala> reflect.runtime.universe.typeOf[(Int, String)]
    res0: reflect.runtime.universe.Type = (Int, String)
    
    scala> reflect.runtime.universe.typeOf[(Int, String)].typeArguments
    res1: List[reflect.runtime.universe.Type] = List(Int, String)
    
    scala> reflect.runtime.universe.typeOf[(Int, String)].typeArguments.head
    res2: reflect.runtime.universe.Type = Int
    

    Update #1

    The following function shows how you can get a type of an instance:

    scala> import reflect.runtime.universe._
    import reflect.runtime.universe._
    
    scala> def typeOf[T : TypeTag](x : T) = reflect.runtime.universe.typeOf[T]
    typeOf: [T](x: T)(implicit evidence$1: reflect.runtime.universe.TypeTag[T])reflect.runtime.universe.Type
    
    scala> typeOf(Seq((1,"sldf"),(20,"sldkfjew")))
    res0: reflect.runtime.universe.Type = Seq[(Int, String)]
    

    In fact it’s all based around the [T : TypeTag] part which tells the compiler to magically create an implicit instance of the reflection to the type passed.

    Update #2

    scala> class TupleReflection(val tuple: Tuple2[Int, String])
    defined class TupleReflection
    
    scala> import reflect.runtime.universe._
    import reflect.runtime.universe._
    
    scala> typeOf[TupleReflection].member(newTermName("tuple")).typeSignature
    res6: reflect.runtime.universe.Type = => (scala.Int, String)
    
    scala> typeOf[TupleReflection].members
    res7: Iterable[reflect.runtime.universe.Symbol] = List(constructor TupleReflection, value tuple, value tuple, method $asInstanceOf, method $isInstanceOf, method synchronized, method ##, method !=, method ==, method ne, method eq, constructor Object, method notifyAll, method notify, method clone, method getClass, method hashCode, method toString, method equals, method wait, method wait, method wait, method finalize, method asInstanceOf, method isInstanceOf, method !=, method ==)
    
    scala> typeOf[TupleReflection].members.view.filter(_.isValue).filter(!_.isMethod).toList
    res16: List[reflect.runtime.universe.Symbol] = List(value tuple)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that contains a boolean field like this one: public class
I have an Item class. There's an itemType field inside of that class which
I have a Grails command object that contains an emailAddresses field, e.g. public class
I have a class A that implements IEquatable<>, using its fields (say, A.b and
I have an entity class that has 10 fields. I am using MS Validation
I have a class that I serialize/deserialize using XmlSerializer . This class contains a
So I have a class that I was planning on using for simple JSON
Say, I have a class X which has a field value, that is, class
I have a class that needs to be deserialized from JSON using Jackson. The
I have a class Field of which there are two sub-classes AccountField and DecimalField

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.