Java Code Examples for org.codehaus.groovy.runtime.InvokerHelper#getMetaClass()

The following examples show how to use org.codehaus.groovy.runtime.InvokerHelper#getMetaClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MetaClassTest.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void testSetPropertyWithArray() {
    DymmyClass dymmyClass = new DymmyClass();
    MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass);

    // test int[]
    int[] ints = new int[]{
            0, 1, 2, 3
    };
    metaClass.setProperty(dymmyClass, "ints", ints);
    assertEquals(ints, metaClass.getProperty(dymmyClass, "ints"));

    // test Integer[]
    Integer[] integers = new Integer[]{
            Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)
    };
    metaClass.setProperty(dymmyClass, "integers", integers);
    assertEquals(integers, metaClass.getProperty(dymmyClass, "integers"));
}
 
Example 2
Source File: CallSiteArray.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static CallSite createCallStaticSite(CallSite callSite, final Class receiver, Object[] args) {
    AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
        try {
            Class.forName(receiver.getName(), true, receiver.getClassLoader());
        } catch (Exception e) {
            // force <clinit>
        }
        return null;
    });
    MetaClass metaClass = InvokerHelper.getMetaClass(receiver);
    CallSite site =
            metaClass instanceof MetaClassImpl
                    ? ((MetaClassImpl) metaClass).createStaticSite(callSite, args)
                    : new StaticMetaClassSite(callSite, metaClass);

    replaceCallSite(callSite, site);
    return site;
}
 
Example 3
Source File: AbstractCallSite.java    From groovy with Apache License 2.0 6 votes vote down vote up
private CallSite createPojoMetaClassGetPropertySite(final Object receiver) {
    final MetaClass metaClass = InvokerHelper.getMetaClass(receiver);

    CallSite site;
    if (metaClass.getClass() != MetaClassImpl.class || GroovyCategorySupport.hasCategoryInCurrentThread()) {
        site = new PojoMetaClassGetPropertySite(this);
    } else {
        final MetaProperty effective = ((MetaClassImpl) metaClass).getEffectiveGetMetaProperty(receiver.getClass(), receiver, name, false);
        if (effective != null) {
            if (effective instanceof CachedField)
                site = new GetEffectivePojoFieldSite(this, (MetaClassImpl) metaClass, (CachedField) effective);
            else
                site = new GetEffectivePojoPropertySite(this, (MetaClassImpl) metaClass, effective);
        } else {
            site = new PojoMetaClassGetPropertySite(this);
        }
    }

    array.array[index] = site;
    return site;
}
 
Example 4
Source File: JO.java    From groovy with Apache License 2.0 5 votes vote down vote up
MetaClass getStaticMetaClass (Object obj) {
    MetaClass mc;
    if (staticMetaClass == null || (mc = (MetaClass) staticMetaClass.get()) == null ) {
        mc = InvokerHelper.getMetaClass(obj);
        staticMetaClass = new SoftReference(mc);
    }
    return mc;
}
 
Example 5
Source File: MetaClassTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void testPublicField() {
    DymmyClass dymmyClass = new DymmyClass();

    MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass);

    assertEquals(metaClass.getProperty(dymmyClass, "x"), Integer.valueOf(0));
    assertEquals(metaClass.getProperty(dymmyClass, "y"), "none");

    metaClass.setProperty(dymmyClass, "x", Integer.valueOf(25));
    assertEquals(dymmyClass.x, 25);

    metaClass.setProperty(dymmyClass, "y", "newvalue");
    assertEquals(dymmyClass.y, "newvalue");
}
 
Example 6
Source File: Inspector.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Get info about instance and class Methods that are dynamically added through Groovy.
 *
 * @return Array of StringArrays that can be indexed with the MEMBER_xxx_IDX constants
 */
public Object[] getMetaMethods() {
    MetaClass metaClass = InvokerHelper.getMetaClass(objectUnderInspection);
    List metaMethods = metaClass.getMetaMethods();
    Object[] result = new Object[metaMethods.size()];
    int i = 0;
    for (Iterator iter = metaMethods.iterator(); iter.hasNext(); i++) {
        MetaMethod metaMethod = (MetaMethod) iter.next();
        result[i] = methodInfo(metaMethod);
    }
    return result;
}
 
Example 7
Source File: ClosureMetaClass.java    From groovy with Apache License 2.0 5 votes vote down vote up
private MetaClass lookupObjectMetaClass(Object object) {
    if (object instanceof GroovyObject) {
        GroovyObject go = (GroovyObject) object;
        return go.getMetaClass();
    }
    Class ownerClass = object.getClass();
    if (ownerClass == Class.class) {
        ownerClass = (Class) object;
        return registry.getMetaClass(ownerClass);
    }
    MetaClass metaClass = InvokerHelper.getMetaClass(object);
    return metaClass;
}
 
Example 8
Source File: DefaultInvoker.java    From groovy-cps with Apache License 2.0 5 votes vote down vote up
public Object superCall(Class methodType, Object receiver, String method, Object[] args) throws Throwable {
    try {
        MetaClass mc = InvokerHelper.getMetaClass(receiver.getClass());
        return mc.invokeMethod(methodType.getSuperclass(), receiver, method, args, true, true);
    } catch (GroovyRuntimeException gre) {
        throw ScriptBytecodeAdapter.unwrap(gre);
    }
}
 
Example 9
Source File: MetaClassTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void testSetPropertyWithList() {
    DymmyClass dymmyClass = new DymmyClass();
    MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass);

    // test list
    ArrayList list = new ArrayList();
    list.add(Integer.valueOf(120));
    list.add(Integer.valueOf(150));

    // test int[]
    metaClass.setProperty(dymmyClass, "ints", list);

    // test Integer[]
    metaClass.setProperty(dymmyClass, "integers", list);
}
 
Example 10
Source File: ManagedConcurrentValueMapStressTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static int size(ManagedConcurrentValueMap<String, Object> map) {
    MetaClass metaClass = InvokerHelper.getMetaClass(map);
    ConcurrentHashMap<String, Object> internalMap = (ConcurrentHashMap<String, Object>)metaClass.getProperty(map, "internalMap");
    return internalMap.size();
}
 
Example 11
Source File: GroovyBeanDefinitionReader.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public GroovyRuntimeBeanReference(String beanName, GroovyBeanDefinitionWrapper beanDefinition, boolean toParent) {
	super(beanName, toParent);
	this.beanDefinition = beanDefinition;
	this.metaClass = InvokerHelper.getMetaClass(this);
}
 
Example 12
Source File: MixedInMetaClass.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected MetaClass getOwnerMetaClass(Object owner) {
    return InvokerHelper.getMetaClass(owner);
}
 
Example 13
Source File: GroovyBeanDefinitionReader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public GroovyRuntimeBeanReference(String beanName, GroovyBeanDefinitionWrapper beanDefinition, boolean toParent) {
	super(beanName, toParent);
	this.beanDefinition = beanDefinition;
	this.metaClass = InvokerHelper.getMetaClass(this);
}
 
Example 14
Source File: MetaClassTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void testSetPropertyWithInt() {
    DymmyClass dymmyClass = new DymmyClass();
    MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass);
    metaClass.setProperty(dymmyClass, "anInt", Integer.valueOf(10));
}
 
Example 15
Source File: GroovySessionFile.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * GroovyObject support method
 */
@Override
public void setMetaClass(MetaClass metaClass) {
    this.metaClass = metaClass == null ? InvokerHelper.getMetaClass(this.getClass()) : metaClass;
}
 
Example 16
Source File: DelegatingScript.java    From groovy with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the delegation target.
 */
public void setDelegate(Object delegate) {
    this.delegate = delegate;
    this.metaClass = InvokerHelper.getMetaClass(delegate.getClass());
}
 
Example 17
Source File: GroovyBeanDefinitionReader.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public GroovyRuntimeBeanReference(String beanName, GroovyBeanDefinitionWrapper beanDefinition, boolean toParent) {
	super(beanName, toParent);
	this.beanDefinition = beanDefinition;
	this.metaClass = InvokerHelper.getMetaClass(this);
}
 
Example 18
Source File: MetaClassTest.java    From groovy with Apache License 2.0 3 votes vote down vote up
public void testObject() {
    Object value = new Object();

    MetaClass metaClass = InvokerHelper.getMetaClass(value);

    assertTrue("got metaclass", metaClass != null);

    metaClass.invokeMethod(value, "toString", new Object[0]);
}
 
Example 19
Source File: MetaClassTest.java    From groovy with Apache License 2.0 3 votes vote down vote up
public void testString() {
    String value = "hello";

    MetaClass metaClass = InvokerHelper.getMetaClass(value);

    assertTrue("got metaclass", metaClass != null);

    Object answer = metaClass.invokeMethod(value, "toString", new Object[0]);

    assertEquals("hello", answer);
}
 
Example 20
Source File: MetaClassTest.java    From groovy with Apache License 2.0 3 votes vote down vote up
public void testMetaClass() {
    Class foo = String[].class;
    System.out.println(foo + " name: " + foo.getName());

    MetaClass metaClass = InvokerHelper.getMetaClass(this);

    assertTrue("got metaclass", metaClass != null);

    metaClass.invokeMethod(this, "doSomething", new Object[0]);
}