|
| 1 | +/** |
| 2 | + * Copyright 2013 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package rx.lang.groovy; |
| 17 | + |
| 18 | +import groovy.lang.Closure; |
| 19 | +import groovy.lang.GroovySystem; |
| 20 | +import groovy.lang.MetaMethod; |
| 21 | + |
| 22 | +import java.lang.reflect.InvocationTargetException; |
| 23 | +import java.lang.reflect.Method; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Properties; |
| 29 | + |
| 30 | +import org.codehaus.groovy.reflection.CachedClass; |
| 31 | +import org.codehaus.groovy.reflection.ReflectionCache; |
| 32 | +import org.codehaus.groovy.runtime.m12n.ExtensionModule; |
| 33 | +import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl; |
| 34 | + |
| 35 | +import rx.Observable; |
| 36 | +import rx.observables.BlockingObservable; |
| 37 | +import rx.util.functions.Action; |
| 38 | +import rx.util.functions.Function; |
| 39 | + |
| 40 | +/** |
| 41 | + * ExtensionModule that adds extension methods to support groovy.lang.Closure |
| 42 | + * anywhere rx.util.functions.Function/Action is used in classes defined in CLASS_TO_EXTEND. |
| 43 | + * |
| 44 | + * It is specifically intended for providing extension methods on Observable. |
| 45 | + */ |
| 46 | +public class RxGroovyExtensionModule extends ExtensionModule { |
| 47 | + |
| 48 | + @SuppressWarnings("rawtypes") |
| 49 | + private final static Class[] CLASS_TO_EXTEND = new Class[] { Observable.class, BlockingObservable.class }; |
| 50 | + |
| 51 | + public RxGroovyExtensionModule() { |
| 52 | + super("RxGroovyExtensionModule", "1.0"); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Keeping this code around a little while as it was hard to figure out ... and I'm still messing with it while debugging. |
| 57 | + * |
| 58 | + * Once the rest of this ExtensionModule stuff is working I'll delete this method. |
| 59 | + * |
| 60 | + * This is used for manually initializing rather than going via the org.codehaus.groovy.runtime.ExtensionModule properties file. |
| 61 | + */ |
| 62 | + public static void initializeManuallyForTesting() { |
| 63 | + System.out.println("initialize"); |
| 64 | + MetaClassRegistryImpl mcRegistry = ((MetaClassRegistryImpl) GroovySystem.getMetaClassRegistry()); |
| 65 | + // RxGroovyExtensionModule em = new RxGroovyExtensionModule(); |
| 66 | + |
| 67 | + Properties p = new Properties(); |
| 68 | + p.setProperty("moduleFactory", "rx.lang.groovy.RxGroovyPropertiesModuleFactory"); |
| 69 | + Map<CachedClass, List<MetaMethod>> metaMethods = new HashMap<CachedClass, List<MetaMethod>>(); |
| 70 | + mcRegistry.registerExtensionModuleFromProperties(p, RxGroovyExtensionModule.class.getClassLoader(), metaMethods); |
| 71 | + |
| 72 | + for (ExtensionModule m : mcRegistry.getModuleRegistry().getModules()) { |
| 73 | + System.out.println("Module: " + m.getName()); |
| 74 | + } |
| 75 | + |
| 76 | + for (CachedClass cc : metaMethods.keySet()) { |
| 77 | + System.out.println("Adding MetaMethods to CachedClass: " + cc); |
| 78 | + cc.addNewMopMethods(metaMethods.get(cc)); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @SuppressWarnings("rawtypes") |
| 83 | + @Override |
| 84 | + public List<MetaMethod> getMetaMethods() { |
| 85 | + // System.out.println("**** RxGroovyExtensionModule => Initializing and returning MetaMethods."); |
| 86 | + List<MetaMethod> methods = new ArrayList<MetaMethod>(); |
| 87 | + |
| 88 | + for (Class classToExtend : CLASS_TO_EXTEND) { |
| 89 | + for (final Method m : classToExtend.getMethods()) { |
| 90 | + for (Class c : m.getParameterTypes()) { |
| 91 | + if (Function.class.isAssignableFrom(c)) { |
| 92 | + methods.add(createMetaMethod(m)); |
| 93 | + // break out of parameter-type loop |
| 94 | + break; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return methods; |
| 101 | + } |
| 102 | + |
| 103 | + private MetaMethod createMetaMethod(final Method m) { |
| 104 | + return new MetaMethod() { |
| 105 | + |
| 106 | + @Override |
| 107 | + public int getModifiers() { |
| 108 | + return m.getModifiers(); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public String getName() { |
| 113 | + return m.getName(); |
| 114 | + } |
| 115 | + |
| 116 | + @SuppressWarnings("rawtypes") |
| 117 | + @Override |
| 118 | + public Class getReturnType() { |
| 119 | + return m.getReturnType(); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public CachedClass getDeclaringClass() { |
| 124 | + return ReflectionCache.getCachedClass(m.getDeclaringClass()); |
| 125 | + } |
| 126 | + |
| 127 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 128 | + @Override |
| 129 | + public Object invoke(Object object, Object[] arguments) { |
| 130 | + // System.out.println("***** RxGroovyExtensionModule => invoked [" + getName() + "]: " + object + " args: " + arguments[0]); |
| 131 | + try { |
| 132 | + Object[] newArgs = new Object[arguments.length]; |
| 133 | + for (int i = 0; i < arguments.length; i++) { |
| 134 | + final Object o = arguments[i]; |
| 135 | + if (o instanceof Closure) { |
| 136 | + if (Action.class.isAssignableFrom(m.getParameterTypes()[i])) { |
| 137 | + newArgs[i] = new GroovyActionWrapper((Closure) o); |
| 138 | + } else { |
| 139 | + newArgs[i] = new GroovyFunctionWrapper((Closure) o); |
| 140 | + } |
| 141 | + |
| 142 | + } else { |
| 143 | + newArgs[i] = o; |
| 144 | + } |
| 145 | + } |
| 146 | + return m.invoke(object, newArgs); |
| 147 | + } catch (IllegalAccessException e) { |
| 148 | + throw new RuntimeException(e); |
| 149 | + } catch (IllegalArgumentException e) { |
| 150 | + throw new RuntimeException(e); |
| 151 | + } catch (InvocationTargetException e) { |
| 152 | + if (e.getCause() instanceof RuntimeException) { |
| 153 | + // re-throw whatever was thrown to us |
| 154 | + throw (RuntimeException) e.getCause(); |
| 155 | + } else { |
| 156 | + throw new RuntimeException(e); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + @SuppressWarnings("rawtypes") |
| 162 | + @Override |
| 163 | + public CachedClass[] getParameterTypes() { |
| 164 | + Class[] pts = m.getParameterTypes(); |
| 165 | + CachedClass[] cc = new CachedClass[pts.length]; |
| 166 | + for (int i = 0; i < pts.length; i++) { |
| 167 | + if (Function.class.isAssignableFrom(pts[i])) { |
| 168 | + // function type to be replaced by closure |
| 169 | + cc[i] = ReflectionCache.getCachedClass(Closure.class); |
| 170 | + } else { |
| 171 | + // non-function type |
| 172 | + cc[i] = ReflectionCache.getCachedClass(pts[i]); |
| 173 | + } |
| 174 | + } |
| 175 | + return cc; |
| 176 | + } |
| 177 | + }; |
| 178 | + } |
| 179 | +} |
0 commit comments