⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 metaclassimpl.java

📁 大名鼎鼎的java动态脚本语言。已经通过了sun的认证
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*$Id: MetaClassImpl.java,v 1.18 2006/06/25 18:17:17 blackdrag Exp $Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.Redistribution and use of this software and associated documentation("Software"), with or without modification, are permitted providedthat the following conditions are met:1. Redistributions of source code must retain copyright   statements and notices.  Redistributions must also contain a   copy of this document.2. Redistributions in binary form must reproduce the   above copyright notice, this list of conditions and the   following disclaimer in the documentation and/or other   materials provided with the distribution.3. The name "groovy" must not be used to endorse or promote   products derived from this Software without prior written   permission of The Codehaus.  For written permission,   please contact info@codehaus.org.4. Products derived from this Software may not be called "groovy"   nor may "groovy" appear in their names without prior written   permission of The Codehaus. "groovy" is a registered   trademark of The Codehaus.5. Due credit should be given to The Codehaus -   http://groovy.codehaus.org/THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUTNOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY ANDFITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALLTHE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ORSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISEDOF THE POSSIBILITY OF SUCH DAMAGE.*/package groovy.lang;import java.beans.BeanInfo;import java.beans.EventSetDescriptor;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.Array;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.net.URL;import java.security.AccessController;import java.security.PrivilegedAction;import java.security.PrivilegedActionException;import java.security.PrivilegedExceptionAction;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.logging.Level;import org.codehaus.groovy.ast.ClassNode;import org.codehaus.groovy.classgen.ReflectorGenerator;import org.codehaus.groovy.control.CompilationUnit;import org.codehaus.groovy.control.CompilerConfiguration;import org.codehaus.groovy.control.Phases;import org.codehaus.groovy.runtime.CurriedClosure;import org.codehaus.groovy.runtime.DefaultGroovyMethods;import org.codehaus.groovy.runtime.GroovyCategorySupport;import org.codehaus.groovy.runtime.Invoker;import org.codehaus.groovy.runtime.InvokerHelper;import org.codehaus.groovy.runtime.MetaClassHelper;import org.codehaus.groovy.runtime.MethodClosure;import org.codehaus.groovy.runtime.MethodKey;import org.codehaus.groovy.runtime.NewInstanceMetaMethod;import org.codehaus.groovy.runtime.NewStaticMetaMethod;import org.codehaus.groovy.runtime.ReflectionMetaMethod;import org.codehaus.groovy.runtime.Reflector;import org.codehaus.groovy.runtime.TemporaryMethodKey;import org.codehaus.groovy.runtime.TransformMetaMethod;import org.codehaus.groovy.runtime.wrappers.Wrapper;import org.objectweb.asm.ClassVisitor;import org.objectweb.asm.ClassWriter;/*** Allows methods to be dynamically added to existing classes at runtime** @author <a href="mailto:james@coredevelopers.net">James Strachan</a>* @author Guillaume Laforge* @author Jochen Theodorou* @version $Revision: 1.18 $*/public class MetaClassImpl extends MetaClass {   protected MetaClassRegistry registry;   private ClassNode classNode;   private Map methodIndex = new HashMap();   private Map staticMethodIndex = new HashMap();   //private Map propertyDescriptors = Collections.synchronizedMap(new HashMap());   private Map propertyMap = Collections.synchronizedMap(new HashMap());   private Map listeners = new HashMap();   private Map methodCache = Collections.synchronizedMap(new HashMap());   private Map staticMethodCache = Collections.synchronizedMap(new HashMap());   private MetaMethod genericGetMethod;   private MetaMethod genericSetMethod;   private List constructors;   private List allMethods = new ArrayList();   private List interfaceMethods;   private Reflector reflector;   private boolean initialised;   // we only need one of these that can be reused over and over.   private MetaProperty arrayLengthProperty = new MetaArrayLengthProperty();   private final static MetaMethod AMBIGOUS_LISTENER_METHOD = new MetaMethod(null,null,new Class[]{},null,0);   private static final Object[] EMPTY_ARGUMENTS = {};      public MetaClassImpl(MetaClassRegistry registry, final Class theClass) throws IntrospectionException {       super(theClass);       this.registry = registry;       constructors = (List) AccessController.doPrivileged(new  PrivilegedAction() {               public Object run() {                   return Arrays.asList (theClass.getDeclaredConstructors());               }           });       addMethods(theClass,true);       // introspect       BeanInfo info = null;       try {           info =(BeanInfo) AccessController.doPrivileged(new PrivilegedExceptionAction() {               public Object run() throws IntrospectionException {                   return Introspector.getBeanInfo(theClass);               }           });       } catch (PrivilegedActionException pae) {           if (pae.getException() instanceof IntrospectionException) {               throw (IntrospectionException) pae.getException();           } else {               throw new RuntimeException(pae.getException());           }       }       PropertyDescriptor[] descriptors = info.getPropertyDescriptors();       // build up the metaproperties based on the public fields, property descriptors,       // and the getters and setters       setupProperties(descriptors);       /* old code       for (int i = 0; i < descriptors.length; i++) {           PropertyDescriptor descriptor = descriptors[i];           propertyDescriptors.put(descriptor.getName(), descriptor);       }       */       EventSetDescriptor[] eventDescriptors = info.getEventSetDescriptors();       for (int i = 0; i < eventDescriptors.length; i++) {           EventSetDescriptor descriptor = eventDescriptors[i];           Method[] listenerMethods = descriptor.getListenerMethods();           for (int j = 0; j < listenerMethods.length; j++) {               Method listenerMethod = listenerMethods[j];               MetaMethod metaMethod = createMetaMethod(descriptor.getAddListenerMethod());               String name = listenerMethod.getName();               if (listeners.containsKey(name)) {                   listeners.put(name, AMBIGOUS_LISTENER_METHOD);               } else{                   listeners.put(name, metaMethod);               }           }       }   }   private void addInheritedMethods() {       LinkedList superClasses = new LinkedList();       for (Class c = theClass.getSuperclass(); c!=Object.class && c!= null; c = c.getSuperclass()) {           superClasses.addFirst(c);       }       // lets add all the base class methods       for (Iterator iter = superClasses.iterator(); iter.hasNext();) {           Class c = (Class) iter.next();           addMethods(c,true);           addNewStaticMethodsFrom(c);       }       // now lets see if there are any methods on one of my interfaces       Class[] interfaces = theClass.getInterfaces();       for (int i = 0; i < interfaces.length; i++) {           addNewStaticMethodsFrom(interfaces[i]);       }       // lets add Object methods after interfaces, as all interfaces derive from Object.       // this ensures List and Collection methods come before Object etc       if (theClass != Object.class) {           addMethods(Object.class, false);           addNewStaticMethodsFrom(Object.class);       }       if (theClass.isArray() && !theClass.equals(Object[].class)) {           addNewStaticMethodsFrom(Object[].class);       }   }   /**    * @return all the normal instance methods avaiable on this class for the    *         given name    */   private List getMethods(String name) {       List answer = (List) methodIndex.get(name);       List used = GroovyCategorySupport.getCategoryMethods(theClass, name);       if (used != null) {           if (answer != null) {               used.addAll(answer);           }           answer = used;       }       if (answer == null) {           answer = Collections.EMPTY_LIST;       }       return answer;   }   /**    * @return all the normal static methods avaiable on this class for the    *         given name    */   private List getStaticMethods(String name) {       List answer = (List) staticMethodIndex.get(name);       if (answer == null) {           return Collections.EMPTY_LIST;       }       return answer;   }   /**    * Allows static method definitions to be added to a meta class as if it    * was an instance method    *    * @param method    */   public void addNewInstanceMethod(Method method) {       if (initialised) {           throw new RuntimeException("Already initialized, cannot add new method: " + method);       }       else {           NewInstanceMetaMethod newMethod = new NewInstanceMetaMethod(createMetaMethod(method));           if (! newGroovyMethodsList.contains(newMethod)){               newGroovyMethodsList.add(newMethod);               addMethod(newMethod,false);           }       }   }   public void addNewStaticMethod(Method method) {       if (initialised) {           throw new RuntimeException("Already initialized, cannot add new method: " + method);       }       else {           NewStaticMetaMethod newMethod = new NewStaticMetaMethod(createMetaMethod(method));           if (! newGroovyMethodsList.contains(newMethod)){               newGroovyMethodsList.add(newMethod);               addMethod(newMethod,false);           }       }   }   /**    * Invokes the given method on the object.    *    */   public Object invokeMethod(Object object, String methodName, Object[] arguments) {       if (object == null) {           throw new NullPointerException("Cannot invoke method: " + methodName + " on null object");       }                     if (log.isLoggable(Level.FINER)){           MetaClassHelper.logMethodCall(object, methodName, arguments);       }       if (arguments==null) arguments = EMPTY_ARGUMENTS;              //       // Temp code to ignore wrapped parameters       // The New MOP will deal with these properly       //       for (int i = 0; i != arguments.length; i++) {        if (arguments[i] instanceof Wrapper) {          arguments[i] = ((Wrapper)arguments[i]).unwrap();        }       }       MetaMethod method = retrieveMethod(object, methodName, arguments);       boolean isClosure = object instanceof Closure;       if (isClosure) {           Closure closure = (Closure) object;           Object delegate = closure.getDelegate();           Object owner = closure.getOwner();           if ("call".equals(methodName) || "doCall".equals(methodName)) {               if (object.getClass()==MethodClosure.class) {                   MethodClosure mc = (MethodClosure) object;                   methodName = mc.getMethod();                   MetaClass ownerMetaClass = registry.getMetaClass(owner.getClass());                   return ownerMetaClass.invokeMethod(owner,methodName,arguments);               } else if (object.getClass()==CurriedClosure.class) {                   CurriedClosure cc = (CurriedClosure) object;                   // change the arguments for an uncurried call                   arguments = cc.getUncurriedArguments(arguments);                   MetaClass ownerMetaClass = registry.getMetaClass(owner.getClass());                   return ownerMetaClass.invokeMethod(owner,methodName,arguments);               }           } else if ("curry".equals(methodName)) {               return closure.curry(arguments);           }           if (method==null && owner!=closure) {               MetaClass ownerMetaClass = registry.getMetaClass(owner.getClass());               method = ownerMetaClass.retrieveMethod(owner,methodName,arguments);               if (method!=null) return ownerMetaClass.invokeMethod(owner,methodName,arguments);           }           if (method==null && delegate!=closure && delegate!=null) {               MetaClass delegateMetaClass = registry.getMetaClass(delegate.getClass());               method = delegateMetaClass.retrieveMethod(delegate,methodName,arguments);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -