Groovy Documentation

## org.codehaus.groovy.runtime.m12n
[Java] Class SimpleExtensionModule

    java.lang.Object
      org.codehaus.groovy.runtime.m12n.ExtensionModule
          org.codehaus.groovy.runtime.m12n.SimpleExtensionModule

    public abstract class SimpleExtensionModule
    extends ExtensionModule

An extension module which provides extension methods using a DefaultGroovyMethods-like implementation, that is to say using static methods defined in an "extension class".

For commodity, multiple extension classes may be defined in a single module, including classes used to define new static methods.

Modules are required to implement the getInstanceMethodsExtensionClasses for classes defining new instance methods, and getStaticMethodsExtensionClasses() for classes defining static methods.

For example, to define a module adding methods to the java.lang.String class, you can write a helper class:

    class StringExtension {
         public static int count(String self, char c) {
             int result = 0;
             for (int i=0;i<self.length(); i++) {
                 if (self.charAt(i)==c) result++;
             }
             return result;
         }
     }

This class defines a single static method taking the string instance as first argument, allowing to define a new instance method on the String class:

    String#count(char c)

.

To define a new static method on a class, as the static modifier is already used for instance methods, you must use another helper class, for example:

    class StaticStringExtension {
         public static void foo(String self) { System.out.println("foo"); }
     }

The first argument of the method is only used to tell the class for which we add a static method. You can now define an extension module:

    class MyStringModule extends SimpleExtensionModule {
         // ...

         public List<Class> getInstanceMethodsExtensionClasses() {
             return Collections.singletonList(StringExtension.class);
         }

         public List<Class> getStaticMethodsExtensionClasses() {
             return Collections.singletonList(StaticStringExtension.class);
         }
     }

Authors:
  Cedric Champeau

Since:
  2.0.0

Constructor Summary
`SimpleExtensionModule(java.lang.String moduleName, java.lang.String moduleVersion)`

Method Summary
`java.util.List` | `getInstanceMethodsExtensionClasses()` @return the list of classes defining new instance methods.
`java.util.List` | `getMetaMethods()`
`java.util.List` | `getStaticMethodsExtensionClasses()` @return the list of classes defining new static methods.

Methods inherited from class ExtensionModule
getMetaMethods, getName, getVersion

Methods inherited from class java.lang.Object
java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()

Constructor Detail

### SimpleExtensionModule

    public SimpleExtensionModule(java.lang.String moduleName, java.lang.String moduleVersion)

Method Detail

### getInstanceMethodsExtensionClasses

    public java.util.List getInstanceMethodsExtensionClasses()

  Returns:
    the list of classes defining new instance methods.

### getMetaMethods

    @Override
    public java.util.List getMetaMethods()

### getStaticMethodsExtensionClasses

    public java.util.List getStaticMethodsExtensionClasses()

  Returns:
    the list of classes defining new static methods.

Groovy Documentation