Java 类groovy.lang.ExpandoMetaClass 实例源码

项目:groovy    文件:CachedClass.java   
public void setNewMopMethods(List<MetaMethod> arr) {
    final MetaClass metaClass = classInfo.getStrongMetaClass();
    if (metaClass != null) {
      if (metaClass.getClass() == MetaClassImpl.class) {
          classInfo.setStrongMetaClass(null);
          updateSetNewMopMethods(arr);
          classInfo.setStrongMetaClass(new MetaClassImpl(metaClass.getTheClass()));
          return;
      }

      if (metaClass.getClass() == ExpandoMetaClass.class) {
          classInfo.setStrongMetaClass(null);
          updateSetNewMopMethods(arr);
          ExpandoMetaClass newEmc = new ExpandoMetaClass(metaClass.getTheClass());
          newEmc.initialize();
          classInfo.setStrongMetaClass(newEmc);
          return;
      }

      throw new GroovyRuntimeException("Can't add methods to class " + getTheClass().getName() + ". Strong custom meta class already set.");
    }

    classInfo.setWeakMetaClass(null);
    updateSetNewMopMethods(arr);
}
项目:groovy    文件:ClassInfo.java   
public void setStrongMetaClass(MetaClass answer) {
    version.incrementAndGet();

    // safe value here to avoid multiple reads with possibly
    // differing values due to concurrency
    MetaClass strongRef = strongMetaClass;

    if (strongRef instanceof ExpandoMetaClass) {
        ((ExpandoMetaClass)strongRef).inRegistry = false;
        for (Iterator<ClassInfo> itr = modifiedExpandos.iterator(); itr.hasNext(); ) {
            ClassInfo info = itr.next();
            if(info == this) {
                itr.remove();
            }
        }
    }

    strongMetaClass = answer;

    if (answer instanceof ExpandoMetaClass) {
        ((ExpandoMetaClass)answer).inRegistry = true;
        modifiedExpandos.add(this);
    }

    replaceWeakMetaClassRef(null);
}
项目:jasperreports    文件:GroovyEvaluator.java   
protected void registerMethods(ExpandoMetaClass extendedMetaClass)
{
    for (ClosureMetaMethod closureMethod : functionMethods)
    {
        extendedMetaClass.registerInstanceMethod(closureMethod);
    }
}
项目:HeliosStreams    文件:MetaClassRegistryCleaner.java   
public void updateConstantMetaClass(final MetaClassRegistryChangeEvent cmcu) {
    log.info("!! updateConstantMetaClass: {}", cmcu);
    if (!cleaning) {
        MetaClass oldMetaClass = cmcu.getOldMetaClass();
        Class classToUpdate = cmcu.getClassToUpdate();
        Object instanceToUpdate = cmcu.getInstance();
        if (instanceToUpdate == null && (cmcu.getNewMetaClass() instanceof ExpandoMetaClass)) {
            updateMetaClassOfClass(oldMetaClass, classToUpdate);
        } else if (instanceToUpdate != null) {
            updateMetaClassOfInstance(oldMetaClass, instanceToUpdate);
        }
    }
}
项目:groovy    文件:MixinInMetaClass.java   
public MixinInMetaClass(ExpandoMetaClass emc, CachedClass mixinClass) {
    super(softBundle);
    this.emc = emc;
    this.mixinClass = mixinClass;

    constructor = findDefaultConstructor(mixinClass);
    emc.addMixinClass(this);
}
项目:groovy    文件:CachedClass.java   
public void addNewMopMethods(List<MetaMethod> arr) {
    final MetaClass metaClass = classInfo.getStrongMetaClass();
    if (metaClass != null) {
      if (metaClass.getClass() == MetaClassImpl.class) {
          classInfo.setStrongMetaClass(null);
          List<MetaMethod> res = new ArrayList<MetaMethod>();
          Collections.addAll(res, classInfo.newMetaMethods);
          res.addAll(arr);
          updateSetNewMopMethods(res);
          MetaClassImpl answer = new MetaClassImpl(((MetaClassImpl)metaClass).getRegistry(),metaClass.getTheClass());
          answer.initialize();
          classInfo.setStrongMetaClass(answer);
          return;
      }

      if (metaClass.getClass() == ExpandoMetaClass.class) {
          ExpandoMetaClass emc = (ExpandoMetaClass)metaClass;
          classInfo.setStrongMetaClass(null);
          updateAddNewMopMethods(arr);
          ExpandoMetaClass newEmc = new ExpandoMetaClass(metaClass.getTheClass());
          for (MetaMethod mm : emc.getExpandoMethods()) {
              newEmc.registerInstanceMethod(mm);
          }
          newEmc.initialize();
          classInfo.setStrongMetaClass(newEmc);
          return;
      }

      throw new GroovyRuntimeException("Can't add methods to class " + getTheClass().getName() + ". Strong custom meta class already set.");
    }

    classInfo.setWeakMetaClass(null);

    updateAddNewMopMethods(arr);
}
项目:groovy    文件:ClassInfo.java   
/**
 * if EMC.enableGlobally() is OFF, return whatever the cached answer is.
 * but if EMC.enableGlobally() is ON and the cached answer is not an EMC, come up with a fresh answer
 */
private static boolean isValidWeakMetaClass(MetaClass metaClass, MetaClassRegistry.MetaClassCreationHandle mccHandle) {
    if(metaClass==null) return false;
    boolean enableGloballyOn = (mccHandle instanceof ExpandoMetaClassCreationHandle);
    boolean cachedAnswerIsEMC = (metaClass instanceof ExpandoMetaClass);
    return (!enableGloballyOn || cachedAnswerIsEMC);
}
项目:groovy    文件:HandleMetaClass.java   
public HandleMetaClass(MetaClass mc, Object obj) {
    super(mc);
    if (obj != null) {
        if (InvokerHelper.getMetaClass(obj.getClass()) == mc || !(mc instanceof ExpandoMetaClass))
          object = obj; // object has default meta class, so we need to replace it on demand
        else
          object = NONE; // object already has per instance meta class
    }
}
项目:groovy    文件:HandleMetaClass.java   
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);
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 * Converts a given object to a type. This method is used through
 * the "as" operator and is overloadable as any other operator.
 *
 * @param obj  the object to convert
 * @param type the goal type
 * @return the resulting object
 * @since 1.0
 */
@SuppressWarnings("unchecked")
public static <T> T asType(Object obj, Class<T> type) {
    if (String.class == type) {
        return (T) InvokerHelper.toString(obj);
    }

    // fall back to cast
    try {
        return (T) DefaultTypeTransformation.castToType(obj, type);
    }
    catch (GroovyCastException e) {
        MetaClass mc = InvokerHelper.getMetaClass(obj);
        if (mc instanceof ExpandoMetaClass) {
            ExpandoMetaClass emc = (ExpandoMetaClass) mc;
            Object mixedIn = emc.castToMixedType(obj, type);
            if (mixedIn != null)
                return (T) mixedIn;
        }
        if (type.isInterface()) {
            try {
                List<Class> interfaces = new ArrayList<Class>();
                interfaces.add(type);
                return (T) ProxyGenerator.INSTANCE.instantiateDelegate(interfaces, obj);
            } catch (GroovyRuntimeException cause) {
                // ignore
            }
        }
        throw e;
    }
}
项目:groovy    文件:DefaultGroovyMethods.java   
/**
 * Adds a "metaClass" property to all class objects so you can use the syntax
 * <code>String.metaClass.myMethod = { println "foo" }</code>
 *
 * @param c The java.lang.Class instance
 * @return An MetaClass instance
 * @since 1.5.0
 */
public static MetaClass getMetaClass(Class c) {
    MetaClassRegistry metaClassRegistry = GroovySystem.getMetaClassRegistry();
    MetaClass mc = metaClassRegistry.getMetaClass(c);
    if (mc instanceof ExpandoMetaClass
            || mc instanceof DelegatingMetaClass && ((DelegatingMetaClass) mc).getAdaptee() instanceof ExpandoMetaClass)
        return mc;
    else {
        return new HandleMetaClass(mc);
    }
}
项目:groovy    文件:Selector.java   
/**
 * Returns the MetaClassImpl if the given MetaClass is one of
 * MetaClassImpl, AdaptingMetaClass or ClosureMetaClass. If
 * none of these cases matches, this method returns null.
 */
private static MetaClassImpl getMetaClassImpl(MetaClass mc, boolean includeEMC) {
    Class mcc = mc.getClass();
    boolean valid = mcc == MetaClassImpl.class ||
                     mcc == AdaptingMetaClass.class ||
                     mcc == ClosureMetaClass.class ||
                     (includeEMC && mcc == ExpandoMetaClass.class);
    if (!valid) {
        if (LOG_ENABLED) LOG.info("meta class is neither MetaClassImpl, nor AdoptingMetaClass, nor ClosureMetaClass, normal method selection path disabled.");
        return null;
    }
    if (LOG_ENABLED) LOG.info("meta class is a recognized MetaClassImpl");
    return (MetaClassImpl) mc;
}
项目:JRLib    文件:ScriptExecutor.java   
private void initScript(Script script) {
    Class clazz = script.getClass();

    emc = new ExpandoMetaClass(clazz, false);
    for(FunctionProvider provider : providers)
        provider.initFunctions(script, emc);
    emc.initialize();

    script.setMetaClass(emc);
}
项目:JRLib    文件:OdpBootstrapAdapter.java   
@Override
public void initFunctions(Script script, ExpandoMetaClass emc) {
    residuals.initFunctions(script, emc);
    scales.initFunctions(script, emc);
    scaledResiduals.initFunctions(script, emc);
    bootstrap.initFunctions(script, emc);
}
项目:groovy    文件:ClassInfo.java   
public ExpandoMetaClass getModifiedExpando() {
    // safe value here to avoid multiple reads with possibly
    // differing values due to concurrency
    MetaClass strongRef = strongMetaClass;
    return strongRef == null ? null : strongRef instanceof ExpandoMetaClass ? (ExpandoMetaClass)strongRef : null;
}
项目:JRLib    文件:FunctionProvider.java   
public void initFunctions(Script script, ExpandoMetaClass emc);