Exemplo n.º 1
0
  private static CachedConstructor findDefaultConstructor(CachedClass mixinClass) {
    for (CachedConstructor constr : mixinClass.getConstructors()) {
      if (!Modifier.isPublic(constr.getModifiers())) continue;

      CachedClass[] classes = constr.getParameterTypes();
      if (classes.length == 0) return constr;
    }

    throw new GroovyRuntimeException(
        "No default constructor for class " + mixinClass.getName() + "! Can't be mixed in.");
  }
  public MetaClassRegistryImpl(final int loadDefault, final boolean useAccessible) {
    this.useAccessible = useAccessible;

    if (loadDefault == LOAD_DEFAULT) {
      Map<CachedClass, List<MetaMethod>> map = new HashMap<CachedClass, List<MetaMethod>>();

      // let's register the default methods
      registerMethods(null, true, true, map);
      final Class[] additionals = DefaultGroovyMethods.additionals;
      for (int i = 0; i != additionals.length; ++i) {
        createMetaMethodFromClass(map, additionals[i]);
      }

      Class[] pluginDGMs = VMPluginFactory.getPlugin().getPluginDefaultGroovyMethods();
      for (Class plugin : pluginDGMs) {
        registerMethods(plugin, false, true, map);
      }
      registerMethods(DefaultGroovyStaticMethods.class, false, false, map);
      Class[] staticPluginDGMs = VMPluginFactory.getPlugin().getPluginStaticGroovyMethods();
      for (Class plugin : staticPluginDGMs) {
        registerMethods(plugin, false, false, map);
      }

      for (Map.Entry<CachedClass, List<MetaMethod>> e : map.entrySet()) {
        CachedClass cls = e.getKey();
        cls.setNewMopMethods(e.getValue());
      }
    }

    installMetaClassCreationHandle();

    final MetaClass emcMetaClass = metaClassCreationHandle.create(ExpandoMetaClass.class, this);
    emcMetaClass.initialize();
    ClassInfo.getClassInfo(ExpandoMetaClass.class).setStrongMetaClass(emcMetaClass);

    addMetaClassRegistryChangeEventListener(
        new MetaClassRegistryChangeEventListener() {
          public void updateConstantMetaClass(MetaClassRegistryChangeEvent cmcu) {
            synchronized (metaClassInfo) {
              metaClassInfo.add(cmcu.getNewMetaClass());
              DefaultMetaClassInfo.getNewConstantMetaClassVersioning();
              Class c = cmcu.getClassToUpdate();
              DefaultMetaClassInfo.setPrimitiveMeta(c, cmcu.getNewMetaClass() == null);
              Field sdyn;
              try {
                sdyn = c.getDeclaredField(Verifier.STATIC_METACLASS_BOOL);
                sdyn.setBoolean(null, cmcu.getNewMetaClass() != null);
              } catch (Throwable e) {
                // DO NOTHING
              }
            }
          }
        });
  }
Exemplo n.º 3
0
 public int hashCode() {
   int result = super.hashCode();
   result = 31 * result + (emc != null ? emc.hashCode() : 0);
   result = 31 * result + (mixinClass != null ? mixinClass.hashCode() : 0);
   result = 31 * result + (constructor != null ? constructor.hashCode() : 0);
   return result;
 }
Exemplo n.º 4
0
  public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof MixinInMetaClass)) return false;
    if (!super.equals(o)) return false;

    MixinInMetaClass that = (MixinInMetaClass) o;

    if (mixinClass != null ? !mixinClass.equals(that.mixinClass) : that.mixinClass != null)
      return false;

    return true;
  }
Exemplo n.º 5
0
  public static void mixinClassesToMetaClass(MetaClass self, List<Class> categoryClasses) {
    final Class selfClass = self.getTheClass();

    if (self instanceof HandleMetaClass) {
      self = (MetaClass) ((HandleMetaClass) self).replaceDelegate();
    }

    if (!(self instanceof ExpandoMetaClass)) {
      if (self instanceof DelegatingMetaClass
          && ((DelegatingMetaClass) self).getAdaptee() instanceof ExpandoMetaClass) {
        self = ((DelegatingMetaClass) self).getAdaptee();
      } else {
        throw new GroovyRuntimeException("Can't mixin methods to meta class: " + self);
      }
    }

    ExpandoMetaClass mc = (ExpandoMetaClass) self;

    List<MetaMethod> arr = new ArrayList<MetaMethod>();
    for (Class categoryClass : categoryClasses) {

      final CachedClass cachedCategoryClass = ReflectionCache.getCachedClass(categoryClass);
      final MixinInMetaClass mixin = new MixinInMetaClass(mc, cachedCategoryClass);

      final MetaClass metaClass = GroovySystem.getMetaClassRegistry().getMetaClass(categoryClass);
      final List<MetaProperty> propList = metaClass.getProperties();
      for (MetaProperty prop : propList)
        if (self.getMetaProperty(prop.getName()) == null) {
          mc.registerBeanProperty(prop.getName(), new MixinInstanceMetaProperty(prop, mixin));
        }

      for (MetaProperty prop : cachedCategoryClass.getFields())
        if (self.getMetaProperty(prop.getName()) == null) {
          mc.registerBeanProperty(prop.getName(), new MixinInstanceMetaProperty(prop, mixin));
        }

      for (MetaMethod method : metaClass.getMethods()) {
        final int mod = method.getModifiers();

        if (!Modifier.isPublic(mod)) continue;

        if (method instanceof CachedMethod
            && ((CachedMethod) method).getCachedMethod().isSynthetic()) continue;

        if (Modifier.isStatic(mod)) {
          if (method instanceof CachedMethod) staticMethod(self, arr, (CachedMethod) method);
        } else if (method.getDeclaringClass().getTheClass() != Object.class
            || method.getName().equals("toString")) {
          //                    if (self.pickMethod(method.getName(),
          // method.getNativeParameterTypes()) == null) {
          final MixinInstanceMetaMethod metaMethod = new MixinInstanceMetaMethod(method, mixin);
          arr.add(metaMethod);
          //                    }
        }
      }
    }

    for (Object res : arr) {
      final MetaMethod metaMethod = (MetaMethod) res;
      if (metaMethod.getDeclaringClass().isAssignableFrom(selfClass))
        mc.registerInstanceMethod(metaMethod);
      else {
        mc.registerSubclassInstanceMethod(metaMethod);
      }
    }
  }