@Nullable private MetaMethod findPropertyMissingMethod(MetaClass metaClass) { if (metaClass instanceof MetaClassImpl) { // Reach into meta class to avoid lookup try { return (MetaMethod) MISSING_PROPERTY_GET_METHOD.get(metaClass); } catch (IllegalAccessException e) { throw UncheckedException.throwAsUncheckedException(e); } } // Query the declared methods of the meta class for (MetaMethod method : metaClass.getMethods()) { if (method.getName().equals("propertyMissing") && method.getParameterTypes().length == 1) { return method; } } return null; }
/** * Check whether the script declares a method with the given name that takes a * corpus parameter, and if so, call it passing the corpus from the given * controller. If the controller is not a CorpusController, do nothing. * * @throws ExecutionException * if the script method throws an ExecutionException we re-throw it */ protected void callControllerAwareMethod(String methodName, Controller c) throws ExecutionException { if(!(c instanceof CorpusController)) { return; } List<MetaMethod> metaMethods = groovyScript.getMetaClass().respondsTo(groovyScript, methodName, new Class[]{gate.Corpus.class}); if(!metaMethods.isEmpty()) { try { metaMethods.get(0).invoke(groovyScript, new Corpus[]{((CorpusController)c).getCorpus()}); } catch(InvokerInvocationException iie) { if(iie.getCause() instanceof ExecutionException) { throw (ExecutionException)iie.getCause(); } else if(iie.getCause() instanceof RuntimeException) { throw (RuntimeException)iie.getCause(); } else if(iie.getCause() instanceof Error) { throw (Error)iie.getCause(); } else { throw iie; } } } }
public void checkIfStdMethod(MetaMethod method) { if (method.getClass() != NewInstanceMetaMethod.class) { String name = method.getName(); if (method.getParameterTypes().length != 1) return; if (!method.getParameterTypes()[0].isNumber && method.getParameterTypes()[0].getTheClass() != Object.class) return; if (!NAMES.contains(name)) return; checkNumberOps(name, method.getDeclaringClass().getTheClass()); } }
@Nullable @Override protected MetaMethod lookupMethod(MetaClass metaClass, String name, Class[] arguments) { MetaMethod metaMethod = super.lookupMethod(metaClass, name, arguments); if (metaMethod != null) { return metaMethod; } metaMethod = classMetaData.getMetaMethod(name, arguments); if (metaMethod != null && Modifier.isStatic(metaMethod.getModifiers())) { return metaMethod; } return null; }
protected Object doInvokeFunction(String name, boolean optional, Object[] args) { Object result = null; boolean invoked = false; for (Script script : scripts) { MetaMethod method = script.getMetaClass().getMetaMethod(name, args != null ? args : new Object[0]); if (method != null) { if (invoked) { // Invoke only the last function of the same name. This is required for compatibility with other supported // scripting languages. break; } result = script.invokeMethod(name, args); invoked = true; } } if (!invoked) { if (optional) { return null; } else { throw new SpongeException("Missing function '" + name + "'"); } } return result; }
@Override public ExtensionModule newModule(Properties properties, ClassLoader classLoader) { LOG.info("Registering new extension module {} defined in class {}", properties.getProperty(MODULE_NAME_KEY), properties.getProperty(MetaInfExtensionModule.MODULE_INSTANCE_CLASSES_KEY)); ExtensionModule module = createExtensionModule(properties, classLoader); if (LOG.isDebugEnabled()) { for(MetaMethod method : module.getMetaMethods()) { LOG.debug("registered method: {}", method); } } return module; }
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)); } } }
protected void addFunctionClosureMethods(MethodClosure methodClosure, String functionName) { // calling registerInstanceMethod(String, Closure) would register all methods, but we only want public methods List<MetaMethod> closureMethods = ClosureMetaMethod.createMethodList(functionName, getClass(), methodClosure); for (MetaMethod metaMethod : closureMethods) { if (!(metaMethod instanceof ClosureMetaMethod)) { // should not happen log.warn("Got unexpected closure method " + metaMethod + " of type " + metaMethod.getClass().getName()); continue; } ClosureMetaMethod closureMethod = (ClosureMetaMethod) metaMethod; if (!closureMethod.getDoCall().isPublic()) { if (log.isDebugEnabled()) { log.debug("method " + closureMethod.getDoCall() + " is not public, not registering"); } continue; } if (log.isDebugEnabled()) { log.debug("creating closure method for " + closureMethod.getDoCall()); } functionMethods.add(closureMethod); } }
@SuppressWarnings("UnusedDeclaration") @Nullable public Object methodMissing(String name, Object args) { final Object[] newArgs = constructNewArgs((Object[])args); // Get other DSL methods from extensions for (GdslMembersProvider provider : PROVIDERS) { final List<MetaMethod> variants = DefaultGroovyMethods.getMetaClass(provider).respondsTo(provider, name, newArgs); if (variants.size() == 1) { return InvokerHelper.invokeMethod(provider, name, newArgs); } } return null; }
private static MetaMethod adjustParamTypesForStdMethods(MetaMethod metaMethod, String methodName) { Class[] nativeParamTypes = metaMethod.getNativeParameterTypes(); nativeParamTypes = (nativeParamTypes != null) ? nativeParamTypes : EMPTY_CLASS_ARRAY; // for methodMissing, first parameter should be String type - to allow overriding of this method without // type String explicitly specified for first parameter (missing method name) - GROOVY-2951 if("methodMissing".equals(methodName) && nativeParamTypes.length == 2 && nativeParamTypes[0] != String.class) { nativeParamTypes[0] = String.class; } return metaMethod; }
private void updateSetNewMopMethods(List<MetaMethod> arr) { if (arr != null) { final MetaMethod[] metaMethods = arr.toArray(new MetaMethod[arr.size()]); classInfo.dgmMetaMethods = metaMethods; classInfo.newMetaMethods = metaMethods; } else classInfo.newMetaMethods = classInfo.dgmMetaMethods; }
private static boolean isNonRealMethod(MetaMethod method) { return method instanceof NewInstanceMetaMethod || method instanceof NewStaticMetaMethod || method instanceof ClosureMetaMethod || method instanceof GeneratedMetaMethod || method instanceof ClosureStaticMetaMethod || method instanceof MixinInstanceMetaMethod || method instanceof ClosureMetaMethod.AnonymousMetaMethod; }
private void updateAddNewMopMethods(List<MetaMethod> arr) { List<MetaMethod> res = new ArrayList<MetaMethod>(); res.addAll(Arrays.asList(classInfo.newMetaMethods)); res.addAll(arr); classInfo.newMetaMethods = res.toArray(new MetaMethod[res.size()]); Class theClass = classInfo.getCachedClass().getTheClass(); if (theClass==Closure.class || theClass==Class.class) { ClosureMetaClass.resetCachedMetaClasses(); } }
private void createProxy() { try { Class<?> aClass = getClass().getClassLoader().loadClass(className.replace('/', '.')); Constructor<?> constructor = aClass.getConstructor(String.class, CachedClass.class, Class.class, Class[].class); proxy = (MetaMethod) constructor.newInstance(getName(), getDeclaringClass(), getReturnType(), getNativeParameterTypes()); } catch (Throwable t) { t.printStackTrace(); throw new GroovyRuntimeException("Failed to create DGM method proxy : " + t, t); } }
public Constructor defineClassAndGetConstructor(final String name, final byte[] bytes) { final Class cls = AccessController.doPrivileged( new PrivilegedAction<Class>(){ public Class run() { return define(name, bytes); } }); if (cls != null) { try { return cls.getConstructor(CallSite.class, MetaClassImpl.class, MetaMethod.class, Class[].class, Constructor.class); } catch (NoSuchMethodException e) { // } } return null; }
public static CallSite createPojoMetaMethodSite(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) { if (metaMethod instanceof CallSiteAwareMetaMethod) { return ((CallSiteAwareMetaMethod)metaMethod).createPojoCallSite(site, metaClass, metaMethod, params, receiver, args); } if (metaMethod.getClass() == CachedMethod.class) return createCachedMethodSite (site, metaClass, (CachedMethod) metaMethod, params, args); return createNonAwareCallSite(site, metaClass, metaMethod, params, args); }
private static CallSite createNonAwareCallSite(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object[] args) { if (metaMethod.correctArguments(args) == args) { if (noWrappers(args)) { if (noCoerce(metaMethod,args)) return new PogoMetaMethodSiteNoUnwrap(site, metaClass, metaMethod, params); else { return new PogoMetaMethodSiteNoUnwrapNoCoerce(site, metaClass, metaMethod, params); } } } return new PogoMetaMethodSite(site, metaClass, metaMethod, params); }
private Class[] makeParameterTypes(Object owner, MetaMethod m) { Class[] newParameterTypes; if (owner instanceof Class && !m.isStatic()) { Class[] nativeParameterTypes = m.getNativeParameterTypes(); newParameterTypes = new Class[nativeParameterTypes.length + 1]; System.arraycopy(nativeParameterTypes, 0, newParameterTypes, 1, nativeParameterTypes.length); newParameterTypes[0] = (Class) owner; } else { newParameterTypes = m.getNativeParameterTypes(); } return newParameterTypes; }
public static GroovyRuntimeException createExceptionText(String init, MetaMethod method, Object object, Object[] args, Throwable reason, boolean setReason) { return new GroovyRuntimeException( init + method + " on: " + object + " with arguments: " + InvokerHelper.toString(args) + " reason: " + reason, setReason ? reason : null); }
/** * 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; }
@Override public Object getProperty(Object object) { MetaMethod getter = getGetter(); if (getter == null) { if (field != null) return field.getProperty(object); throw new GroovyRuntimeException("Cannot read write-only property: " + name); } return getter.invoke(object, MetaClassHelper.EMPTY_ARRAY); }
@Override public MetaMethod pickMethod(String name, Class[] argTypes) { if (argTypes == null) argTypes = MetaClassHelper.EMPTY_CLASS_ARRAY; if (name.equals(CLOSURE_CALL_METHOD) || name.equals(CLOSURE_DO_CALL_METHOD)) { return pickClosureMethod(argTypes); } return CLOSURE_METACLASS.getMetaMethod(name, argTypes); }
private static MetaMethod createSetter(final MetaProperty property, final MixinInMetaClass mixinInMetaClass) { return new MetaMethod() { final String name = getSetterName(property.getName()); { setParametersTypes (new CachedClass [] {ReflectionCache.getCachedClass(property.getType())} ); } public int getModifiers() { return Modifier.PUBLIC; } public String getName() { return name; } public Class getReturnType() { return property.getType(); } public CachedClass getDeclaringClass() { return mixinInMetaClass.getInstanceClass(); } public Object invoke(Object object, Object[] arguments) { property.setProperty(mixinInMetaClass.getMixinInstance(object), arguments[0]); return null; } }; }
@Nullable protected MetaMethod lookupMethod(MetaClass metaClass, String name, Class[] arguments) { return metaClass.getMetaMethod(name, arguments); }
public MetaMethod getMethod(String name, Class[] parameters) { return impl.pickMethod(name, parameters); }
public MetaMethod getMethod(String name, Class[] parameters) { return implRef.getPayload().getMethod(name, parameters); }
public NumberNumberCallSite(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Number receiver, Number arg) { super(site, metaClass, metaMethod, params); math = NumberMath.getMath(receiver,arg); }
public FloatDouble(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) { super(site, metaClass, metaMethod, params, (Number) receiver, (Number) args[0]); }
public void setMetaMethod(MetaMethod metaMethod) { this.metaMethod = metaMethod; }
public MetaMethod getMetaMethod() { return metaMethod; }
public IntegerDouble(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params, Object receiver, Object[] args) { super(site, metaClass, metaMethod, params, (Number) receiver, (Number) args[0]); }
public ConstructorMetaMethodSite(CallSite site, MetaClassImpl metaClass, MetaMethod method, Class [] params) { super(site, metaClass, method, params); this.version = metaClass.getVersion(); }
public StaticMetaMethodSite(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class params[]) { super(site, metaClass, metaMethod, params); version = metaClass.getVersion (); }
private static void refreshMopMethods(final Map<CachedClass, List<MetaMethod>> map) { for (Map.Entry<CachedClass, List<MetaMethod>> e : map.entrySet()) { CachedClass cls = e.getKey(); cls.setNewMopMethods(e.getValue()); } }
@Override public List<MetaMethod> getMethods() { List<MetaMethod> answer = CLOSURE_METACLASS.getMetaMethods(); answer.addAll(closureMethods.toList()); return answer; }
public StaticMetaMethodSiteNoUnwrapNoCoerce(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class params[]) { super(site, metaClass, metaMethod, params); }
public List<MetaMethod> getMetaMethods() { final Object owner = getOwner(); final MetaClass ownerMetaClass = getOwnerMetaClass(owner); return ownerMetaClass.getMetaMethods(); }
public MethodMetaProperty(String name, MetaMethod method) { super(name, Object.class); this.method = method; }
public PojoCachedMethodSite(CallSite site, MetaClassImpl metaClass, MetaMethod metaMethod, Class[] params) { super(site, metaClass, metaMethod, params); reflect = ((CachedMethod)metaMethod).setAccessible(); }
public MetaMethod getMetaMethod(String name, Class[] argTypes) { final Object owner = getOwner(); final MetaClass ownerMetaClass = getOwnerMetaClass(owner); return ownerMetaClass.getMetaMethod(name, argTypes); }