Tim Nelson | 16 Aug 2012 22:33
Picon
Gravatar

How do you get reference to an object from it's class name

Hi All,


Say I have the following object:

package code

object MyObj {
  ...
}

Then, elsewhere, I have:

val myobj = MyObj.getClass.getClassName

Is there a way to get a reference to the actual MyObj object using the myobj String?

The reason I need to do this is I have to pass the myobj String in a url param.

Thanks,
Tim
Brian Maso | 17 Aug 2012 00:30
Gravatar

Re: How do you get reference to an object from it's class name

Through reflection you can find the field "MODULE$" in class "MyObj$" (the value of MyObj.getClass.getName).


What you'll get is a reference to the singleton object, with "java.lang.Object" type annotation. I don't think there's a way in the Scala language to cast this reference to the MyObj singleton type -- though you can cast it to type MyObj.type, which might do.

Brian Maso

On Thu, Aug 16, 2012 at 1:33 PM, Tim Nelson <tnelly27-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hi All,

Say I have the following object:

package code

object MyObj {
  ...
}

Then, elsewhere, I have:

val myobj = MyObj.getClass.getClassName

Is there a way to get a reference to the actual MyObj object using the myobj String?

The reason I need to do this is I have to pass the myobj String in a url param.

Thanks,
Tim



--
Best regards,
Brian Maso
(949) 395-8551
Follow me: <at> bmaso
brian-zVB9M1GYnxxPzT/ju5Bd1kEOCMrvLtNR@public.gmane.org

Naftoli Gugenheim | 17 Aug 2012 09:04
Picon
Gravatar

Re: How do you get reference to an object from it's class name

Try something like Class.forName(theName).getField("MODULE$").get(null).asInstanceOf[MyObj.type]



On Thu, Aug 16, 2012 at 4:33 PM, Tim Nelson <tnelly27-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hi All,

Say I have the following object:

package code

object MyObj {
  ...
}

Then, elsewhere, I have:

val myobj = MyObj.getClass.getClassName

Is there a way to get a reference to the actual MyObj object using the myobj String?

The reason I need to do this is I have to pass the myobj String in a url param.

Thanks,
Tim

Tim Nelson | 17 Aug 2012 14:39
Picon
Gravatar

Re: How do you get reference to an object from it's class name

Hi,


Thanks for the response, but I don't have the last bit to cast it to the type I need. If I had that I wouldn't need to do the rest of it, I could just reference my object directly. Without the cast it returns Object, but I need the real type. I guess this just isn't possible.

Tim

On Friday, August 17, 2012 2:04:47 AM UTC-5, nafg wrote:
Try something like Class.forName(theName).getField("MODULE$").get(null).asInstanceOf[MyObj.type]



On Thu, Aug 16, 2012 at 4:33 PM, Tim Nelson <tnel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hi All,

Say I have the following object:

package code

object MyObj {
  ...
}

Then, elsewhere, I have:

val myobj = MyObj.getClass.getClassName

Is there a way to get a reference to the actual MyObj object using the myobj String?

The reason I need to do this is I have to pass the myobj String in a url param.

Thanks,
Tim

Dennis Haupt | 17 Aug 2012 15:17
Picon
Picon

Re: How do you get reference to an object from it's class name

i don't understand what you want to do.
if you know the type at runtime, you obviously cannot use it at compile time.

-------- Original-Nachricht --------
> Datum: Fri, 17 Aug 2012 05:39:42 -0700 (PDT)
> Von: Tim Nelson <tnelly27@...>
> An: scala-user@...
> CC: Tim Nelson <tnelly27@...>
> Betreff: Re: [scala-user] How do you get reference to an object from it\'s class name

> Hi,
> 
> Thanks for the response, but I don't have the last bit to cast it to the 
> type I need. If I had that I wouldn't need to do the rest of it, I could 
> just reference my object directly. Without the cast it returns Object, but
> I need the real type. I guess this just isn't possible.
> 
> Tim
> 
> On Friday, August 17, 2012 2:04:47 AM UTC-5, nafg wrote:
> >
> > Try something like 
> >
> Class.forName(theName).getField("MODULE$").get(null).asInstanceOf[MyObj.type]
> >
> >
> >
> > On Thu, Aug 16, 2012 at 4:33 PM, Tim Nelson
> <tnel...@...<javascript:>
> > > wrote:
> >
> >> Hi All,
> >>
> >> Say I have the following object:
> >>
> >> package code
> >>
> >> object MyObj {
> >>   ...
> >> }
> >>
> >> Then, elsewhere, I have:
> >>
> >> val myobj = MyObj.getClass.getClassName
> >>
> >> Is there a way to get a reference to the actual MyObj object using the 
> >> myobj String?
> >>
> >> The reason I need to do this is I have to pass the myobj String in a
> url 
> >> param.
> >>
> >> Thanks,
> >> Tim
> >>
> >
> >

Naftoli Gugenheim | 17 Aug 2012 15:37
Picon
Gravatar

Re: How do you get reference to an object from it's class name

I didn't understand what you meant by "I don't have the last bit." What's wrong with asInstanceOf[MyObj.type]?


On Fri, Aug 17, 2012 at 8:39 AM, Tim Nelson <tnelly27-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hi,

Thanks for the response, but I don't have the last bit to cast it to the type I need. If I had that I wouldn't need to do the rest of it, I could just reference my object directly. Without the cast it returns Object, but I need the real type. I guess this just isn't possible.

Tim


On Friday, August 17, 2012 2:04:47 AM UTC-5, nafg wrote:
Try something like Class.forName(theName).getField("MODULE$").get(null).asInstanceOf[MyObj.type]



On Thu, Aug 16, 2012 at 4:33 PM, Tim Nelson <tnel... <at> gmail.com> wrote:
Hi All,

Say I have the following object:

package code

object MyObj {
  ...
}

Then, elsewhere, I have:

val myobj = MyObj.getClass.getClassName

Is there a way to get a reference to the actual MyObj object using the myobj String?

The reason I need to do this is I have to pass the myobj String in a url param.

Thanks,
Tim


Tim Nelson | 17 Aug 2012 16:21
Picon
Gravatar

Re: How do you get reference to an object from it's class name

Hi,

On Friday, August 17, 2012 8:37:37 AM UTC-5, nafg wrote:
I didn't understand what you meant by "I don't have the last bit." What's wrong with asInstanceOf[MyObj.type]?
 
At that point I don't know what the type is. All I have is the class
name String. That is the whole point of my question.

Let me try to explain the whole situation. I have solved this in a
different way, but I'd still like to know if this is possible.

I have some case objects like this:

package code
sealed trait Concepts
case object Address extends Concepts
case object City extends Concepts

From this I need to create an html select list:

<select name="concept">
  <option value="code.Address$">Address</option>
  <option value="code.City$">City</option>
</select>

Then, on the backend, I need to take the value submitted and get a
reference to the case object, so I can create a class that takes a
Concept as a parameter.

Does that make more sense?

Thanks,
Tim

On Friday, August 17, 2012 8:37:37 AM UTC-5, nafg wrote:
I didn't understand what you meant by "I don't have the last bit." What's wrong with asInstanceOf[MyObj.type]?


On Fri, Aug 17, 2012 at 8:39 AM, Tim Nelson <tnel... <at> gmail.com> wrote:
Hi,

Thanks for the response, but I don't have the last bit to cast it to the type I need. If I had that I wouldn't need to do the rest of it, I could just reference my object directly. Without the cast it returns Object, but I need the real type. I guess this just isn't possible.

Tim


On Friday, August 17, 2012 2:04:47 AM UTC-5, nafg wrote:
Try something like Class.forName(theName).getField("MODULE$").get(null).asInstanceOf[MyObj.type]



On Thu, Aug 16, 2012 at 4:33 PM, Tim Nelson <tnel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hi All,

Say I have the following object:

package code

object MyObj {
  ...
}

Then, elsewhere, I have:

val myobj = MyObj.getClass.getClassName

Is there a way to get a reference to the actual MyObj object using the myobj String?

The reason I need to do this is I have to pass the myobj String in a url param.

Thanks,
Tim


Naftoli Gugenheim | 17 Aug 2012 16:21
Picon
Gravatar

Re: How do you get reference to an object from it's class name

Well, do you know that it's a Concepts?
If the code can't know the type, then it can't do anything specific to that type, right?

On Fri, Aug 17, 2012 at 10:21 AM, Tim Nelson <tnelly27-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hi,

On Friday, August 17, 2012 8:37:37 AM UTC-5, nafg wrote:
I didn't understand what you meant by "I don't have the last bit." What's wrong with asInstanceOf[MyObj.type]?
 
At that point I don't know what the type is. All I have is the class
name String. That is the whole point of my question.

Let me try to explain the whole situation. I have solved this in a
different way, but I'd still like to know if this is possible.

I have some case objects like this:

package code
sealed trait Concepts
case object Address extends Concepts
case object City extends Concepts

From this I need to create an html select list:

<select name="concept">
  <option value="code.Address$">Address</option>
  <option value="code.City$">City</option>
</select>

Then, on the backend, I need to take the value submitted and get a
reference to the case object, so I can create a class that takes a
Concept as a parameter.

Does that make more sense?

Thanks,
Tim


On Friday, August 17, 2012 8:37:37 AM UTC-5, nafg wrote:
I didn't understand what you meant by "I don't have the last bit." What's wrong with asInstanceOf[MyObj.type]?


On Fri, Aug 17, 2012 at 8:39 AM, Tim Nelson <tnel... <at> gmail.com> wrote:
Hi,

Thanks for the response, but I don't have the last bit to cast it to the type I need. If I had that I wouldn't need to do the rest of it, I could just reference my object directly. Without the cast it returns Object, but I need the real type. I guess this just isn't possible.

Tim


On Friday, August 17, 2012 2:04:47 AM UTC-5, nafg wrote:
Try something like Class.forName(theName).getField("MODULE$").get(null).asInstanceOf[MyObj.type]



On Thu, Aug 16, 2012 at 4:33 PM, Tim Nelson <tnel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hi All,

Say I have the following object:

package code

object MyObj {
  ...
}

Then, elsewhere, I have:

val myobj = MyObj.getClass.getClassName

Is there a way to get a reference to the actual MyObj object using the myobj String?

The reason I need to do this is I have to pass the myobj String in a url param.

Thanks,
Tim



Tim Nelson | 17 Aug 2012 16:26
Picon
Gravatar

Re: How do you get reference to an object from it's class name

Yes, I do know that it's a Concept so I can cast it to that. I just tried it and it works.


Thanks.

On Friday, August 17, 2012 9:21:56 AM UTC-5, nafg wrote:
Well, do you know that it's a Concepts?
If the code can't know the type, then it can't do anything specific to that type, right?

On Fri, Aug 17, 2012 at 10:21 AM, Tim Nelson <tnel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hi,

On Friday, August 17, 2012 8:37:37 AM UTC-5, nafg wrote:
I didn't understand what you meant by "I don't have the last bit." What's wrong with asInstanceOf[MyObj.type]?
 
At that point I don't know what the type is. All I have is the class
name String. That is the whole point of my question.

Let me try to explain the whole situation. I have solved this in a
different way, but I'd still like to know if this is possible.

I have some case objects like this:

package code
sealed trait Concepts
case object Address extends Concepts
case object City extends Concepts

From this I need to create an html select list:

<select name="concept">
  <option value="code.Address$">Address</option>
  <option value="code.City$">City</option>
</select>

Then, on the backend, I need to take the value submitted and get a
reference to the case object, so I can create a class that takes a
Concept as a parameter.

Does that make more sense?

Thanks,
Tim


On Friday, August 17, 2012 8:37:37 AM UTC-5, nafg wrote:
I didn't understand what you meant by "I don't have the last bit." What's wrong with asInstanceOf[MyObj.type]?


On Fri, Aug 17, 2012 at 8:39 AM, Tim Nelson <tnel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hi,

Thanks for the response, but I don't have the last bit to cast it to the type I need. If I had that I wouldn't need to do the rest of it, I could just reference my object directly. Without the cast it returns Object, but I need the real type. I guess this just isn't possible.

Tim


On Friday, August 17, 2012 2:04:47 AM UTC-5, nafg wrote:
Try something like Class.forName(theName).getField("MODULE$").get(null).asInstanceOf[MyObj.type]



On Thu, Aug 16, 2012 at 4:33 PM, Tim Nelson <tnel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hi All,

Say I have the following object:

package code

object MyObj {
  ...
}

Then, elsewhere, I have:

val myobj = MyObj.getClass.getClassName

Is there a way to get a reference to the actual MyObj object using the myobj String?

The reason I need to do this is I have to pass the myobj String in a url param.

Thanks,
Tim




Gmane