Search in sources :

Example 6 with NewInstanceMetaMethod

use of org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod in project groovy-core by groovy.

the class NewStaticMetaMethodTest method testInvokeDefaultGroovyMethod.

public void testInvokeDefaultGroovyMethod() throws Exception {
    Method method = DefaultGroovyMethods.class.getMethod("plus", new Class[] { String.class, Object.class });
    assertTrue("Should have found a method", method != null);
    NewInstanceMetaMethod metaMethod = createNewMetaMethod(method);
    Object answer = metaMethod.invoke("abc", new Object[] { "123" });
    assertEquals("abc123", answer);
    System.out.println("Found: " + answer);
}
Also used : NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) Method(java.lang.reflect.Method) CachedMethod(org.codehaus.groovy.reflection.CachedMethod)

Example 7 with NewInstanceMetaMethod

use of org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod in project groovy-core by groovy.

the class MixinInMetaClass method staticMethod.

private static void staticMethod(final MetaClass self, List<MetaMethod> arr, final CachedMethod method) {
    CachedClass[] paramTypes = method.getParameterTypes();
    if (paramTypes.length == 0)
        return;
    NewInstanceMetaMethod metaMethod;
    if (paramTypes[0].isAssignableFrom(self.getTheClass())) {
        if (paramTypes[0].getTheClass() == self.getTheClass())
            metaMethod = new NewInstanceMetaMethod(method);
        else
            metaMethod = new NewInstanceMetaMethod(method) {

                public CachedClass getDeclaringClass() {
                    return ReflectionCache.getCachedClass(self.getTheClass());
                }
            };
        arr.add(metaMethod);
    } else {
        if (self.getTheClass().isAssignableFrom(paramTypes[0].getTheClass())) {
            metaMethod = new NewInstanceMetaMethod(method);
            arr.add(metaMethod);
        }
    }
}
Also used : NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)

Example 8 with NewInstanceMetaMethod

use of org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod in project groovy by apache.

the class NewStaticMetaMethodTest method testInvokeDefaultGroovyMethod.

public void testInvokeDefaultGroovyMethod() throws Exception {
    Method method = DefaultGroovyMethods.class.getMethod("plus", new Class[] { String.class, Object.class });
    assertTrue("Should have found a method", method != null);
    NewInstanceMetaMethod metaMethod = createNewMetaMethod(method);
    Object answer = metaMethod.invoke("abc", new Object[] { "123" });
    assertEquals("abc123", answer);
}
Also used : NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) Method(java.lang.reflect.Method) CachedMethod(org.codehaus.groovy.reflection.CachedMethod)

Example 9 with NewInstanceMetaMethod

use of org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod in project groovy by apache.

the class NewStaticMetaMethodTest method testInvokeMetaMethod.

public void testInvokeMetaMethod() throws Exception {
    Method method = getClass().getMethod("dummyMethod", new Class[] { String.class, String.class });
    assertTrue("Should have found a method", method != null);
    NewInstanceMetaMethod metaMethod = createNewMetaMethod(method);
    Object answer = metaMethod.invoke("abc", new Object[] { "xyz" });
    assertEquals("def", answer);
    assertTrue("Should not appear as static method", !metaMethod.isStatic());
}
Also used : NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) Method(java.lang.reflect.Method) CachedMethod(org.codehaus.groovy.reflection.CachedMethod)

Example 10 with NewInstanceMetaMethod

use of org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod in project groovy by apache.

the class SimpleExtensionModule method createMetaMethods.

private static void createMetaMethods(final Class extensionClass, final List<MetaMethod> metaMethods, final boolean isStatic) {
    CachedClass cachedClass = ReflectionCache.getCachedClass(extensionClass);
    CachedMethod[] methods = cachedClass.getMethods();
    for (CachedMethod method : methods) {
        if (method.isStatic() && method.isPublic() && method.getParamsCount() > 0) {
            // an extension method is found
            metaMethods.add(isStatic ? new NewStaticMetaMethod(method) : new NewInstanceMetaMethod(method));
        }
    }
}
Also used : NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) NewStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod) CachedMethod(org.codehaus.groovy.reflection.CachedMethod) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Aggregations

NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)12 CachedMethod (org.codehaus.groovy.reflection.CachedMethod)8 Method (java.lang.reflect.Method)4 CachedClass (org.codehaus.groovy.reflection.CachedClass)4 CachedField (org.codehaus.groovy.reflection.CachedField)2 GeneratedMetaMethod (org.codehaus.groovy.reflection.GeneratedMetaMethod)2 GetBeanMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty)2 GetMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty)2 NewStaticMetaMethod (org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod)2 ComplexKeyHashMap (org.codehaus.groovy.util.ComplexKeyHashMap)2 SingleKeyHashMap (org.codehaus.groovy.util.SingleKeyHashMap)2