groovy.lang.GroovyObject Java Examples
The following examples show how to use
groovy.lang.GroovyObject.
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: Selector.java From groovy with Apache License 2.0 | 6 votes |
/** * Gives the meta class to an Object. */ public MetaClass getMetaClass() { Object receiver = args[0]; if (receiver == null) { mc = NullObject.getNullObject().getMetaClass(); } else if (receiver instanceof GroovyObject) { mc = ((GroovyObject) receiver).getMetaClass(); } else if (receiver instanceof Class) { Class<?> c = (Class<?>) receiver; mc = GroovySystem.getMetaClassRegistry().getMetaClass(c); this.cache &= !ClassInfo.getClassInfo(c).hasPerInstanceMetaClasses(); } else { mc = ((MetaClassRegistryImpl) GroovySystem.getMetaClassRegistry()).getMetaClass(receiver); this.cache &= !ClassInfo.getClassInfo(receiver.getClass()).hasPerInstanceMetaClasses(); } mc.initialize(); return mc; }
Example #2
Source File: ProxyGeneratorAdapter.java From groovy with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") public GroovyObject delegatingProxy(Object delegate, Map<Object, Object> map, Object... constructorArgs) { if (constructorArgs == null && cachedNoArgConstructor != null) { // if there isn't any argument, we can make invocation faster using the cached constructor try { return (GroovyObject) cachedNoArgConstructor.newInstance(map, delegate); } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { throw new GroovyRuntimeException(e); } } if (constructorArgs == null) constructorArgs = EMPTY_ARGS; Object[] values = new Object[constructorArgs.length + 2]; System.arraycopy(constructorArgs, 0, values, 0, constructorArgs.length); values[values.length - 2] = map; values[values.length - 1] = delegate; return DefaultGroovyMethods.<GroovyObject>newInstance(cachedClass, values); }
Example #3
Source File: GroovyExecUtil.java From nh-micro with Apache License 2.0 | 6 votes |
public static Object execGroovyRetObj(GroovyObject groovyObject, String methodName, Object... paramArray) { try { //add 201806 ninghao if(groovyObject==null){ throw new RuntimeException("groovyObject is null methodName="+methodName); } //GroovyObject groovyObject = (GroovyObject) getGroovyObj(groovyName); //GroovyAopInter groovyAop=(GroovyAopInter) MicroContextHolder.getContextMap().get("groovyAop"); GroovyAopInter firstAop=GroovyAopChain.getFirstAop(); Object retObj=null; if(firstAop==null){ retObj=groovyObject.invokeMethod(methodName, paramArray); }else{ retObj=firstAop.invokeMethod(groovyObject,null, methodName, paramArray); } return retObj; } catch (Throwable t) { logger.error(t.toString(), t); if(throwFlag){ throw new RuntimeException(t); } return null; } }
Example #4
Source File: GroovyRuleEngine.java From Zebra with Apache License 2.0 | 6 votes |
@SuppressWarnings("resource") private static final GroovyObject getGroovyObject(String rule) throws IllegalAccessException, InstantiationException { if (!RULE_CLASS_CACHE.containsKey(rule)) { synchronized (GroovyRuleEngine.class) { if (!RULE_CLASS_CACHE.containsKey(rule)) { Matcher matcher = DimensionRule.RULE_COLUMN_PATTERN.matcher(rule); StringBuilder engineClazzImpl = new StringBuilder(200) .append("class RuleEngineBaseImpl extends " + RuleEngineBase.class.getName() + "{") .append("Object execute(Map context) {").append(matcher.replaceAll("context.get(\"$1\")")) .append("}").append("}"); GroovyClassLoader loader = new GroovyClassLoader(AbstractDimensionRule.class.getClassLoader()); Class<?> engineClazz = loader.parseClass(engineClazzImpl.toString()); RULE_CLASS_CACHE.put(rule, engineClazz); } } } return (GroovyObject) RULE_CLASS_CACHE.get(rule).newInstance(); }
Example #5
Source File: PogoMetaClassSite.java From groovy with Apache License 2.0 | 6 votes |
public final Object call(final Object receiver, final Object[] args) throws Throwable { if (checkCall(receiver)) { try { try { return metaClass.invokeMethod(receiver, name, args); } catch (MissingMethodException e) { if (e instanceof MissingMethodExecutionFailed) { throw (MissingMethodException)e.getCause(); } else if (receiver.getClass() == e.getType() && e.getMethod().equals(name)) { // in case there's nothing else, invoke the object's own invokeMethod() return ((GroovyObject)receiver).invokeMethod(name, args); } else { throw e; } } } catch (GroovyRuntimeException gre) { throw ScriptBytecodeAdapter.unwrap(gre); } } else { return CallSiteArray.defaultCall(this, receiver, args); } }
Example #6
Source File: GroovyLoadUtil.java From nh-micro with Apache License 2.0 | 6 votes |
public static void loadGroovy(String name, String content) throws Exception { logger.info("begin load groovy name=" + name); logger.debug("groovy content=" + content); if(name.toLowerCase().contains("abstract")){ logger.info("skip load groovy name=" + name); return; } ClassLoader parent = GroovyLoadUtil.class.getClassLoader(); GroovyClassLoader loader = new GroovyClassLoader(parent); Class<?> groovyClass = loader.parseClass(content,name+".groovy"); GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance(); GroovyObject proxyObject=groovyObject; if(pluginList!=null){ int size=pluginList.size(); for(int i=0;i<size;i++){ IGroovyLoadPlugin plugin=(IGroovyLoadPlugin) pluginList.get(i); proxyObject=plugin.execPlugIn(name, groovyObject, proxyObject); } } GroovyExecUtil.getGroovyMap().put(name, proxyObject); //GroovyExecUtil.getGroovyMap().put(name, groovyObject); logger.info("finish load groovy name=" + name); }
Example #7
Source File: GPathResult.java From groovy with Apache License 2.0 | 6 votes |
/** * Creates a Closure representing the body of this GPathResult. * * @return the body of this GPathResult, converted to a <code>Closure</code> */ public Closure getBody() { return new Closure(this.parent,this) { public void doCall(Object[] args) { final GroovyObject delegate = (GroovyObject)getDelegate(); final GPathResult thisObject = (GPathResult)getThisObject(); Node node = (Node)thisObject.getAt(0); List children = node.children(); for (Object child : children) { delegate.getProperty("mkp"); if (child instanceof Node) { delegate.invokeMethod("yield", new Object[]{new NodeChild((Node) child, thisObject, "*", null)}); } else { delegate.invokeMethod("yield", new Object[]{child}); } } } }; }
Example #8
Source File: ProxyGeneratorAdapter.java From groovy with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") public GroovyObject proxy(Map<Object, Object> map, Object... constructorArgs) { if (constructorArgs == null && cachedNoArgConstructor != null) { // if there isn't any argument, we can make invocation faster using the cached constructor try { return (GroovyObject) cachedNoArgConstructor.newInstance(map); } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { throw new GroovyRuntimeException(e); } } if (constructorArgs == null) constructorArgs = EMPTY_ARGS; Object[] values = new Object[constructorArgs.length + 1]; System.arraycopy(constructorArgs, 0, values, 0, constructorArgs.length); values[values.length - 1] = map; return DefaultGroovyMethods.<GroovyObject>newInstance(cachedClass, values); }
Example #9
Source File: GroovyExecUtil.java From nh-micro with Apache License 2.0 | 6 votes |
public static boolean execGroovy(String groovyName, String methodName, Object... paramArray) { try { GroovyObject groovyObject = (GroovyObject) getGroovyObj(groovyName); //groovyObject.invokeMethod(methodName, paramArray); //GroovyAopInter groovyAop=(GroovyAopInter) MicroContextHolder.getContextMap().get("groovyAop"); GroovyAopInter firstAop=GroovyAopChain.getFirstAop(); Object retObj=null; if(firstAop==null){ retObj=groovyObject.invokeMethod(methodName, paramArray); }else{ retObj=firstAop.invokeMethod(groovyObject,groovyName, methodName, paramArray); } return true; } catch (Throwable t) { logger.error(t.toString(), t); if(throwFlag){ throw new RuntimeException(t); } return false; } }
Example #10
Source File: GroovyCompiler.java From zuul-netty with Apache License 2.0 | 6 votes |
@Test public void testLoadGroovyFromString() { GroovyCompiler compiler = spy(new GroovyCompiler()); try { String code = "class test { public String hello(){return \"hello\" } } "; Class clazz = compiler.compile(code, "test"); assertNotNull(clazz); assertEquals(clazz.getName(), "test"); GroovyObject groovyObject = (GroovyObject) clazz.newInstance(); Object[] args = {}; String s = (String) groovyObject.invokeMethod("hello", args); assertEquals(s, "hello"); } catch (Exception e) { assertFalse(true); } }
Example #11
Source File: Node.java From groovy with Apache License 2.0 | 5 votes |
protected void appendNode(final Object newValue, final GPathResult result) { if (newValue instanceof Closure) { this.children.add(new ReplacementNode() { public void build(final GroovyObject builder, final Map namespaceMap, final Map<String, String> namespaceTagHints) { final Closure c = (Closure) ((Closure) newValue).clone(); c.setDelegate(builder); c.call(new Object[]{result}); } }); } else { this.children.add(newValue); } }
Example #12
Source File: CallSiteArray.java From groovy with Apache License 2.0 | 5 votes |
private static CallSite createPogoSite(CallSite callSite, Object receiver, Object[] args) { if (receiver instanceof GroovyInterceptable) { return new PogoInterceptableSite(callSite); } MetaClass metaClass = ((GroovyObject)receiver).getMetaClass(); if (metaClass instanceof MetaClassImpl) { return ((MetaClassImpl)metaClass).createPogoCallSite(callSite, args); } return new PogoMetaClassSite(callSite, metaClass); }
Example #13
Source File: Node.java From groovy with Apache License 2.0 | 5 votes |
private void buildChildren(final GroovyObject builder, final Map namespaceMap, final Map<String, String> namespaceTagHints) { for (Object child : this.children) { if (child instanceof Node) { ((Node) child).build(builder, namespaceMap, namespaceTagHints); } else if (child instanceof Buildable) { ((Buildable) child).build(builder); } else { builder.getProperty("mkp"); builder.invokeMethod("yield", new Object[]{child}); } } }
Example #14
Source File: ObjectBuilderTest.java From everrest with Eclipse Public License 2.0 | 5 votes |
@Test public void createsSimpleGroovyBean() throws Exception { Class<?> aClass = parseGroovyClass("SimpleBean.groovy"); JsonValue jsonObject = createJsonObject("value", new StringValue("test restore groovy bean")); GroovyObject simpleBean = (GroovyObject)ObjectBuilder.createObject(aClass, jsonObject); assertEquals("test restore groovy bean", simpleBean.invokeMethod("getValue", new Object[0])); }
Example #15
Source File: PogoMetaClassSite.java From groovy with Apache License 2.0 | 5 votes |
public final Object callCurrent(final GroovyObject receiver, final Object[] args) throws Throwable { if (checkCall(receiver)) { try { try { return metaClass.invokeMethod(array.owner, receiver, name, args, false, true); } catch (MissingMethodException e) { if (e instanceof MissingMethodExecutionFailed) { throw (MissingMethodException) e.getCause(); } else if (receiver.getClass() == e.getType() && e.getMethod().equals(name)) { // in case there's nothing else, invoke the receiver's own invokeMethod() try { return receiver.invokeMethod(name, args); } catch (MissingMethodException mme) { if (mme instanceof MissingMethodExecutionFailed) throw (MissingMethodException) mme.getCause(); // GROOVY-9387: in rare cases, this form still works return metaClass.invokeMethod(receiver, name, args); } } else { throw e; } } } catch (GroovyRuntimeException gre) { throw ScriptBytecodeAdapter.unwrap(gre); } } else { return CallSiteArray.defaultCallCurrent(this, receiver, args); } }
Example #16
Source File: PogoGetPropertySite.java From groovy with Apache License 2.0 | 5 votes |
public Object getProperty(Object receiver) throws Throwable { try{ return ((GroovyObject)receiver).getProperty(name); } catch (GroovyRuntimeException gre) { throw ScriptBytecodeAdapter.unwrap(gre); } }
Example #17
Source File: AbstractCallSite.java From groovy with Apache License 2.0 | 5 votes |
@Override public Object callCurrent(final GroovyObject receiver, final Object arg1, final Object arg2, final Object arg3) throws Throwable { CallSite stored = array.array[index]; if (stored!=this) { return stored.callCurrent(receiver, arg1, arg2, arg3); } return callCurrent(receiver, ArrayUtil.createArray(arg1, arg2, arg3)); }
Example #18
Source File: ScriptEngine.java From MirServer-Netty with GNU General Public License v3.0 | 5 votes |
public static synchronized void loadScript(String scriptName, boolean flush) throws Exception { if (!flush && scriptInstance.containsKey(scriptName)) return; logger.debug("加载脚本: {}", scriptName); Class groovyClass = engine.loadScriptByName(scriptName + DEFUALT_SCRIPT_SUFFIX); GroovyObject instance = (GroovyObject) groovyClass.newInstance(); scriptInstance.put(scriptName, instance); }
Example #19
Source File: GrailsHibernateUtil.java From gorm-hibernate5 with Apache License 2.0 | 5 votes |
/** * Ensures the meta class is correct for a given class * * @param target The GroovyObject * @param persistentClass The persistent class */ @Deprecated public static void ensureCorrectGroovyMetaClass(Object target, Class<?> persistentClass) { if (target instanceof GroovyObject) { GroovyObject go = ((GroovyObject)target); if (!go.getMetaClass().getTheClass().equals(persistentClass)) { go.setMetaClass(GroovySystem.getMetaClassRegistry().getMetaClass(persistentClass)); } } }
Example #20
Source File: ScriptBytecodeAdapter.java From groovy with Apache License 2.0 | 5 votes |
public static void setGroovyObjectFieldSafe(Object messageArgument, Class senderClass, GroovyObject receiver, String messageName) throws Throwable { if (receiver == null) return; try { receiver.getMetaClass().setAttribute(receiver, messageName, messageArgument); } catch (GroovyRuntimeException gre) { throw unwrap(gre); } }
Example #21
Source File: GPathResult.java From groovy with Apache License 2.0 | 5 votes |
/** * Lazily adds the specified Object to this GPathResult. * * @param newValue the Object to add * @return <code>this</code> */ public Object plus(final Object newValue) { this.replaceNode(new Closure(this) { public void doCall(Object[] args) { final GroovyObject delegate = (GroovyObject) getDelegate(); delegate.getProperty("mkp"); delegate.invokeMethod("yield", args); delegate.getProperty("mkp"); delegate.invokeMethod("yield", new Object[]{newValue}); } }); return this; }
Example #22
Source File: GetEffectivePogoPropertySite.java From groovy with Apache License 2.0 | 5 votes |
public final CallSite acceptGroovyObjectGetProperty(Object receiver) { if (GroovyCategorySupport.hasCategoryInCurrentThread() || !(receiver instanceof GroovyObject) || ((GroovyObject)receiver).getMetaClass() != metaClass) { return createGroovyObjectGetPropertySite(receiver); } else { return this; } }
Example #23
Source File: GetEffectivePogoFieldSite.java From groovy with Apache License 2.0 | 5 votes |
public final CallSite acceptGroovyObjectGetProperty(Object receiver) { if (GroovyCategorySupport.hasCategoryInCurrentThread() || !(receiver instanceof GroovyObject) || ((GroovyObject)receiver).getMetaClass() != metaClass) { return createGroovyObjectGetPropertySite(receiver); } else { return this; } }
Example #24
Source File: ScriptBytecodeAdapter.java From groovy with Apache License 2.0 | 5 votes |
public static Object getFieldOnSuper(Class senderClass, Object receiver, String messageName) throws Throwable { try { if (receiver instanceof Class) { return InvokerHelper.getAttribute(receiver, messageName); } else { MetaClass mc = ((GroovyObject) receiver).getMetaClass(); return mc.getAttribute(senderClass, receiver, messageName, true); } } catch (GroovyRuntimeException gre) { throw unwrap(gre); } }
Example #25
Source File: MicroInjectGroovyPlugin.java From nh-micro with Apache License 2.0 | 5 votes |
@Override public GroovyObject execPlugIn(String name, GroovyObject groovyObject, GroovyObject proxyObject) throws Exception { Field[] fields=groovyObject.getClass().getDeclaredFields(); int size=fields.length; for(int i=0;i<size;i++){ Field field=fields[i]; InjectGroovy anno=field.getAnnotation(InjectGroovy.class); if(anno==null){ continue; } String groovyName=null; groovyName=anno.name(); if(groovyName==null || "".equals(groovyName)){ groovyName=field.getName(); } Class cls=field.getType(); InjectGroovyProxy injectGroovyProxy=new InjectGroovyProxy(); injectGroovyProxy.setGroovyName(groovyName); //injectGroovyProxy.setGroovyObject(proxyObject); Object proxy = Proxy.newProxyInstance(cls.getClassLoader(), new Class[]{cls}, (InvocationHandler) injectGroovyProxy); field.set(groovyObject, proxy); } return proxyObject; }
Example #26
Source File: MicroControllerPlugin.java From nh-micro with Apache License 2.0 | 5 votes |
@Override public GroovyObject execPlugIn(String name, GroovyObject groovyObject, GroovyObject proxyObject) { Annotation[] annos=groovyObject.getClass().getAnnotations(); MicroUrlMapping anno=groovyObject.getClass().getAnnotation(MicroUrlMapping.class); String root=""; if(anno!=null){ root=anno.name(); //MicroControllerMap.setUrl(root, name); }else{ return proxyObject; } Method[] methods=groovyObject.getClass().getMethods(); int size=methods.length; for(int i=0;i<size;i++){ Method method=methods[i]; MicroUrlMapping subAnno=method.getAnnotation(MicroUrlMapping.class); if(subAnno==null){ continue; } String subPath=subAnno.name(); String version=subAnno.version(); String methodName=method.getName(); String fullPath=root+subPath; MicroControllerMap.setUrl(fullPath, name, methodName,version); } return proxyObject; }
Example #27
Source File: MicroInjectSpringPlugin.java From nh-micro with Apache License 2.0 | 5 votes |
public GroovyObject execPlugIn(String name, GroovyObject groovyObject, GroovyObject proxyObject) throws Exception { ApplicationContext context=MicroContextHolder.getContext(); AutowireCapableBeanFactory autowireCapableBeanFactory=context.getAutowireCapableBeanFactory(); autowireCapableBeanFactory.autowireBeanProperties(groovyObject, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false); return proxyObject; }
Example #28
Source File: ScriptBytecodeAdapter.java From groovy with Apache License 2.0 | 5 votes |
public static void setPropertyOnSuper(Object messageArgument, Class senderClass, GroovyObject receiver, String messageName) throws Throwable { try { InvokerHelper.setAttribute(receiver, messageName, messageArgument); } catch (GroovyRuntimeException gre) { throw unwrap(gre); } }
Example #29
Source File: ScriptBytecodeAdapter.java From groovy with Apache License 2.0 | 5 votes |
public static Object invokeMethodOnCurrentN(Class senderClass, GroovyObject receiver, String messageName, Object[] messageArguments) throws Throwable { Object result = null; boolean intercepting = receiver instanceof GroovyInterceptable; try { try { // if it's a pure interceptable object (even intercepting toString(), clone(), ...) if (intercepting) { result = receiver.invokeMethod(messageName, messageArguments); } //else if there's a statically typed method or a GDK method else { result = receiver.getMetaClass().invokeMethod(senderClass, receiver, messageName, messageArguments, false, true); } } catch (MissingMethodException e) { if (e instanceof MissingMethodExecutionFailed) { throw (MissingMethodException)e.getCause(); } else if (!intercepting && receiver.getClass() == e.getType() && e.getMethod().equals(messageName)) { // in case there's nothing else, invoke the object's own invokeMethod() result = receiver.invokeMethod(messageName, messageArguments); } else { throw e; } } } catch (GroovyRuntimeException gre) { throw unwrap(gre); } return result; }
Example #30
Source File: AbstractCallSite.java From groovy with Apache License 2.0 | 5 votes |
@Override public Object callCurrent(final GroovyObject receiver, final Object arg1) throws Throwable { CallSite stored = array.array[index]; if (stored!=this) { return stored.callCurrent(receiver, arg1); } return callCurrent(receiver, ArrayUtil.createArray(arg1)); }