Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [Xtend] How to do equivalent of java Class.forName(String className)(Reflection Java)
icon5.gif  [Xtend] How to do equivalent of java Class.forName(String className) [message #795289] Fri, 10 February 2012 09:43 Go to next message
Evermind Mising name is currently offline Evermind Mising nameFriend
Messages: 7
Registered: July 2009
Junior Member
Xtend version: 2.3M5

I found I cannot do Class.forName(String className) of java in Xtend. I think this *shall* be able to be done since it seems to me is commonly used. Any guidance is appreciated:

// ClassForName.xtend -------------------------------------------
package test

class ClassForName {
def foo() {
// cannot create java.lang.Class object from string of class name
var Class c1 = typeof(Object) // OK
var Class c2 = Class.forName("java.lang.Object") // Error
var Class c3 = typeof("java.lang.Object") // Error by now; Syntax suggested
}
}

Re: [Xtend] How to do equivalent of java Class.forName(String className) [message #795292 is a reply to message #795289] Fri, 10 February 2012 09:48 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
Class::forName("foo")

does the trick: forName is a static method of java.lang.Class so needs a static accessor ('::') instead of a member accessor ('.') operator.

Btw: using Class#forName is quite error-prone in any JVM context, especially in an OSGi context. Use
this.^class.classLoader.loadClass("foo")

instead as it plays nice with the various class loaders.


Re: [Xtend] How to do equivalent of java Class.forName(String className) [message #795328 is a reply to message #795292] Fri, 10 February 2012 10:32 Go to previous messageGo to next message
Evermind Mising name is currently offline Evermind Mising nameFriend
Messages: 7
Registered: July 2009
Junior Member
Thank for your help & advice! Razz I didn't found these info(:: for static method invocation, this.^class) in document www.eclipse.org/xtend/documentation/index.html(sorry cannot use url links yet), which I thought was official and complete. Would you please recommend any other comprehensive learning material about Xtend?
Re: [Xtend] How to do equivalent of java Class.forName(String className) [message #813070 is a reply to message #795289] Sun, 04 March 2012 20:11 Go to previous messageGo to next message
Jonathan Blakes is currently offline Jonathan BlakesFriend
Messages: 1
Registered: March 2012
Junior Member
typeof(this)

or
typeof(ClassName)
Re: [Xtend] How to do equivalent of java Class.forName(String className) [message #813387 is a reply to message #813070] Mon, 05 March 2012 07:44 Go to previous message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
Jonathan Blakes wrote on Sun, 04 March 2012 21:11
typeof(this)

or
typeof(ClassName)

Neither of these work, I'm afraid.


Previous Topic:Opening Xbase-using Xtext models programmatically
Next Topic:Formatting my Xtext Language
Goto Forum:
  


Current Time: Sun May 31 17:05:05 GMT 2020

Powered by FUDForum. Page generated in 0.04938 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top