Hi Amit,
Try that one
def bindObject[T <: AnyRef](className: Class[T]): Option[Object] = {
val sClassName =
className.getPackage.getName.concat(".Wrap".concat(className.getSimpleName))
try {
Some(Class.forName(sClassName.replaceFirst("com.vtech",
"com.vtech.appxtension")).newInstance.asInstanceOf[T])
} catch {
case e: Exception =>
try {
Some(Class.forName(className.getName).newInstance.asInstanceOf[T])
} catch {
case e1: Exception =>
println(e1.printStackTrace)
None
}
}
}
Regards,
Sergey
On Mon, Apr 20, 2009 at 3:41 PM, Amit Kumar Verma <[email protected]>wrote:
>
> Hi All,
>
> This is a sample function for making an object from string at run
> time. Here we are not casting the object but creating one. I wanted
> the same thing for casting the object.
>
> public static Object bindObject(Class className) {
> Object objOutput = null;
> try {
> String sClassName = className.getPackage().getName().concat
> (".Wrap".concat(className.getSimpleName()));
> objOutput = Class.forName(sClassName.replaceFirst
> ("com.vtech", "com.vtech.appxtension")).newInstance();
> } catch (Exception e) {
> try {
> objOutput = Class.forName(className.getName
> ()).newInstance();
> } catch (Exception e1) {
> e1.printStackTrace();
> }
> }
>
> return objOutput;
> }
>
>
> Thanks to all for kind support..
> Amit Kumar Verma
>
> On Apr 18, 8:51 pm, Timothy Perrett <[email protected]> wrote:
> > So your talking about reflection right? Take a look at scala Manifests
> > (which aide getting round type erasure) - other than that scala supports
> all
> > the normal reflection tooling that Java does.
> >
> > Tim
> >
> > On 18/04/2009 06:56, "Amit Kumar Verma" <[email protected]> wrote:
> >
> > > "Scala is a static language, so the class for casting must be known at
> > > compile time. It's not possible to construct a String at runtime and
> > > cast
> > > an object into a class represented by that String. "
> >
> > > But we use this feature in Java for casting the objects.
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Lift" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---