1

I have a String Person, which is the name of a class Person. Can I get the class Person from that string?. I need the class to call static methods of if.

In other words: is there an inverse to Class.getName()?

If that does not work at all, let me know. It is an answer too.

Opal
  • 81,889
  • 28
  • 189
  • 210
user3647093
  • 363
  • 1
  • 6
  • 17

4 Answers4

5

Class.forName will work for a full name including the package.

If all you have is the bare name without a package, then there's no generic way but in the specific case of a Grails domain class (or other Grails artefact) you can look it up via the grailsApplication.

Community
  • 1
  • 1
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • I'm working in agrails environment, buthow can I get grailsApplication ? – user3647093 Nov 19 '14 at 08:30
  • I made it working now as generic as I want by getting grailsApplication by:'def grailsApplication = new Historie().domainClass.grailsApplication' – user3647093 Nov 19 '14 at 12:30
  • @user3647093 controllers have it automatically, services and domains can use autowiring (a class-level `def grailsApplication`), anywhere else you can retrieve it from `grails.util.Holders` – Ian Roberts Nov 19 '14 at 12:45
4
class Person {
    static someMethod() {
        println 'Yep'
    }
}

If you know the name of the method that you want to invoke you can do something like this...

def className = 'Person'
def clz = Class.forName(className)
clz.someMethod()

If the method name is dynamic, you can do something like this...

def className = 'Person'
def methodName = 'someMethod'
def clz = Class.forName(className)
clz."$methodName"()
Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • Calling the method directly on the clz object requires that method to be static as you're effectively calling Person.someMethod(). – beechovsky Dec 01 '20 at 16:02
  • 1
    `"Calling the method directly on the clz object requires that method to be static as you're effectively calling Person.someMethod()"` @beechovsky That is correct. The JVM doesn't allow you to invoke instance methods on a Class reference, because that wouldn't make sense. The original question includes `"I need the class to call static methods of if."` so I demonstrated how to do that. – Jeff Scott Brown Dec 01 '20 at 17:33
  • Ah - yup! I see now. – beechovsky Dec 01 '20 at 22:53
0

Yes, there is a method: Class.forName(<String>) that should do the job You need. Mind the fact that You have to pass fully qualified class name (with package). See the docs.

Opal
  • 81,889
  • 28
  • 189
  • 210
-1

You can get class using Class.forName() . Below code may help you

String type ="com.yourPackage.Person";
Class<Person> m = (Class<Person>) Class.forName(type);
sai
  • 75
  • 4
  • Why are you suggesting that he invoke `newInstance`? – Jeff Scott Brown Nov 18 '14 at 13:46
  • Also, the `(Object)` cast on the return value from `newInstance()` makes no sense. Your `destClass` reference is declared with `Object`. You can assign anything to an `Object` reference. The cast doesn't do anything in this case. The fact that you named the variable `destClass` makes me wonder if you thought that `newInstance()` returns the `java.lang.Class` object, as opposed to an instance of that class. – Jeff Scott Brown Nov 18 '14 at 16:03