public Object invokeMethod(String name, Object args) { return this.metaClass.invokeMethod(this, name, args); }
@Override public Object getProperty(String property) { try { return metaClass.getProperty(delegate,property); } catch (MissingPropertyException e) { return super.getProperty(property); } }
public void setProperty(String property, Object newValue) { this.metaClass.setProperty(this, property, newValue); }
protected static MetaMethod findOwnMethod(Class instanceKlazz, String methodName, Class[] arguments, MetaClass metaClass, MetaMethod method) { // we trick ourselves here if (instanceKlazz == metaClass.getTheClass()) return method; MetaMethod ownMethod = metaClass.pickMethod(methodName, arguments); if (ownMethod != null) { if (method == null) method = ownMethod; else method = mostSpecific(method, ownMethod, instanceKlazz); } return method; }
public Object callConstructor(Object receiver, Object[] args) throws Throwable { if (receiver == metaClass.getTheClass()) { try { return metaClass.invokeConstructor(args); } catch (GroovyRuntimeException gre) { throw ScriptBytecodeAdapter.unwrap(gre); } } else { return CallSiteArray.defaultCallConstructor(this, (Class)receiver, args); } } }
public Object getProperty(String property) { if(ExpandoMetaClass.isValidExpandoProperty(property)) { if(property.equals(ExpandoMetaClass.STATIC_QUALIFIER) || property.equals(ExpandoMetaClass.CONSTRUCTOR) || Holder.META_CLASS.hasProperty(this, property) == null) { return replaceDelegate().getProperty(property); } } return Holder.META_CLASS.getProperty(this, property); }
public void testPublicField() { DymmyClass dymmyClass = new DymmyClass(); MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass); assertEquals(metaClass.getProperty(dymmyClass, "x"), new Integer(0)); assertEquals(metaClass.getProperty(dymmyClass, "y"), "none"); metaClass.setProperty(dymmyClass, "x", new Integer(25)); assertEquals(dymmyClass.x, 25); metaClass.setProperty(dymmyClass, "y", "newvalue"); assertEquals(dymmyClass.y, "newvalue"); }
/** * This method is invoked by Groovy when a method that's not defined in Java is invoked. * We use that as a syntax for bean definition. */ public Object methodMissing(String name, Object arg) { Object[] args = (Object[])arg; if(args.length == 0) throw new MissingMethodException(name,getClass(),args); if(args[0] instanceof Closure) { // abstract bean definition return invokeBeanDefiningMethod(name, args); } else if(args[0] instanceof Class || args[0] instanceof RuntimeBeanReference || args[0] instanceof Map) { return invokeBeanDefiningMethod(name, args); } else if (args.length > 1 && args[args.length -1] instanceof Closure) { return invokeBeanDefiningMethod(name, args); } WebApplicationContext ctx = springConfig.getUnrefreshedApplicationContext(); MetaClass mc = DefaultGroovyMethods.getMetaClass(ctx); if(!mc.respondsTo(ctx, name, args).isEmpty()){ return mc.invokeMethod(ctx,name, args); } return this; }
private Tuple2<Object, MetaMethod> invokeMethod(MetaMethod method, Object delegate, Closure closure, String methodName, Class[] argClasses, Object[] originalArguments, Object owner) { if (method == null && delegate != closure && delegate != null) { MetaClass delegateMetaClass = lookupObjectMetaClass(delegate); method = delegateMetaClass.pickMethod(methodName, argClasses); if (method != null) return new Tuple2<Object,MetaMethod>(delegateMetaClass.invokeMethod(delegate, methodName, originalArguments), method); } if (method == null && owner != closure) { MetaClass ownerMetaClass = lookupObjectMetaClass(owner); method = ownerMetaClass.pickMethod(methodName, argClasses); if (method != null) return new Tuple2<Object,MetaMethod>(ownerMetaClass.invokeMethod(owner, methodName, originalArguments), method); } return new Tuple2<Object,MetaMethod>(InvokeMethodResult.NONE, method); }
@Override public Object respond(IMockInvocation invocation) { Object instance = invocation.getMockObject().getInstance(); Object[] arguments = invocation.getArguments().toArray(); if (invocation.getMethod().isStatic()) { if ("<init>".equals(invocation.getMethod().getName())) { return metaClass.invokeConstructor(arguments); } return metaClass.invokeStaticMethod(instance, invocation.getMethod().getName(), arguments); } return metaClass.invokeMethod(instance, invocation.getMethod().getName(), arguments); } }
public List<MetaMethod> respondsTo(Object obj, String name, Object[] argTypes) { return delegate.respondsTo(obj, name, argTypes); }
public static void mixinClassesToMetaClass(MetaClass self, List<Class> categoryClasses) { final Class selfClass = self.getTheClass(); final List<MetaProperty> propList = metaClass.getProperties(); for (MetaProperty prop : propList) if (self.getMetaProperty(prop.getName()) == null) { mc.registerBeanProperty(prop.getName(), new MixinInstanceMetaProperty(prop, mixin)); if (self.getMetaProperty(prop.getName()) == null) { mc.registerBeanProperty(prop.getName(), new MixinInstanceMetaProperty(prop, mixin)); for (MetaMethod method : metaClass.getMethods()) { final int mod = method.getModifiers();
public MetaMethod getMetaMethod(String name, Object[] args) { return this.delegate.getMetaMethod(name, args); }
public PojoMetaClassSite(CallSite site, MetaClass metaClass) { super(site, metaClass); classInfo = ClassInfo.getClassInfo(metaClass.getTheClass()); version = classInfo.getVersion(); }
public List<MetaProperty> getProperties() { return delegate.getProperties(); }
/** * @deprecated */ @Deprecated public MetaMethod pickMethod(String methodName, Class[] arguments) { return delegate.pickMethod(methodName, arguments); }
public MetaProperty getMetaProperty(String name) { return this.delegate.getMetaProperty(name); }
static public boolean isJavaBeanPropertyWritable(Object instance, String propertyName) { MetaClass mc = InvokerHelper.getMetaClass(instance); MetaProperty metaProperty = mc.getMetaProperty(propertyName); if(metaProperty != null) { String setterName = MetaProperty.getSetterName(propertyName); return !mc.respondsTo(instance, setterName, new Class[]{metaProperty.getType()}).isEmpty(); } else if(instance instanceof Script) { return ((Script)instance).getProperty(propertyName) != null; } return false; }
MetaMethod method = mc.getStaticMetaMethod(methodName, argClasses); if (method != null) return method; } else { MetaClass delegateMetaClass = lookupObjectMetaClass(delegate); MetaMethod method = delegateMetaClass.pickMethod(methodName, argClasses); if (method != null) { return method;
private static Object getStaticPropertyValue(MetaClass theMetaClass, String name) { MetaProperty metaProperty = theMetaClass.getMetaProperty(name); if(metaProperty != null && Modifier.isStatic(metaProperty.getModifiers())) { return metaProperty.getProperty(theMetaClass.getTheClass()); } return null; }