Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this:</p> <pre><code>def cl = Class.forName("org.package.Foo") cl.get(1) </code></pre> <p>A little bit longer but should work.</p> <p>If you want to create "switch"-like code for static methods, I suggest to instantiate the classes (even if they have only static methods) and save the instances in a map. You can then use</p> <pre><code>map[name].get(1) </code></pre> <p>to select one of them.</p> <p><strong>[EDIT]</strong> <code>"$name"</code> is a <code>GString</code> and as such a valid statement. <code>"$name".foo()</code> means "call the method <code>foo()</code> of the class <code>GString</code>.</p> <p><strong>[EDIT2]</strong> When using a web container (like Grails), you have to specify the classloader. There are two options:</p> <pre><code>Class.forName("com.acme.MyClass", true, Thread.currentThread().contextClassLoader) </code></pre> <p>or </p> <pre><code>Class.forName("com.acme.MyClass", true, getClass().classLoader) </code></pre> <p>The first option will work only in a web context, the second approach also works for unit tests. It depends on the fact that you can usually use the same classloader as the class which invokes <code>forName()</code>.</p> <p>If you have problems, then use the first option and set the <code>contextClassLoader</code> in your unit test:</p> <pre><code>def orig = Thread.currentThread().contextClassLoader try { Thread.currentThread().contextClassLoader = getClass().classLoader ... test ... } finally { Thread.currentThread().contextClassLoader = orig } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload