Example #1
0
 /** @return the super class of this type or null */
 public Type getSuperclass() {
   try {
     return superClass == null ? null : ReflectionCache.forName(superClass.getName());
   } catch (ClassNotFoundException e) {
     return null;
   }
 }
Example #2
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);
      }
    }
  }
Example #3
0
 /**
  * Sets the element i in the array object to value.
  *
  * @param obj an array object of this type.
  * @param i the index of the element to set.
  * @param value the element value.
  */
 public void setArrayElement(Object obj, int i, Object value) {
   ReflectionCache.setArrayElement(this, obj, i, value);
 }
Example #4
0
 /**
  * @param obj an array object of this type.
  * @param i the index of the element to retrieve.
  * @return the element at position i in the array.
  */
 public Object getArrayElement(Object obj, int i) {
   return ReflectionCache.getArrayElement(this, obj, i);
 }
Example #5
0
 /**
  * @param obj an array object of this type.
  * @return the length of the given array object.
  */
 public int getArrayLength(Object obj) {
   return ReflectionCache.getArrayLength(this, obj);
 }
  private void registerMethods(
      final Class theClass,
      final boolean useMethodWrapper,
      final boolean useInstanceMethods,
      Map<CachedClass, List<MetaMethod>> map) {
    if (useMethodWrapper) {
      // Here we instantiate objects representing MetaMethods for DGM methods.
      // Calls for such meta methods done without reflection, so more effectively.

      try {
        List<GeneratedMetaMethod.DgmMethodRecord> records =
            GeneratedMetaMethod.DgmMethodRecord.loadDgmInfo();

        for (GeneratedMetaMethod.DgmMethodRecord record : records) {
          Class[] newParams = new Class[record.parameters.length - 1];
          System.arraycopy(record.parameters, 1, newParams, 0, record.parameters.length - 1);

          MetaMethod method =
              new GeneratedMetaMethod.Proxy(
                  record.className,
                  record.methodName,
                  ReflectionCache.getCachedClass(record.parameters[0]),
                  record.returnType,
                  newParams);
          final CachedClass declClass = method.getDeclaringClass();
          List<MetaMethod> arr = map.get(declClass);
          if (arr == null) {
            arr = new ArrayList<MetaMethod>(4);
            map.put(declClass, arr);
          }
          arr.add(method);
          instanceMethods.add(method);
        }
      } catch (Throwable e) {
        e.printStackTrace();
        // we print the error, but we don't stop with an exception here
        // since it is more comfortable this way for development
      }
    } else {
      CachedMethod[] methods = ReflectionCache.getCachedClass(theClass).getMethods();

      for (CachedMethod method : methods) {
        final int mod = method.getModifiers();
        if (Modifier.isStatic(mod)
            && Modifier.isPublic(mod)
            && method.getCachedMethod().getAnnotation(Deprecated.class) == null) {
          CachedClass[] paramTypes = method.getParameterTypes();
          if (paramTypes.length > 0) {
            List<MetaMethod> arr = map.get(paramTypes[0]);
            if (arr == null) {
              arr = new ArrayList<MetaMethod>(4);
              map.put(paramTypes[0], arr);
            }
            if (useInstanceMethods) {
              final NewInstanceMetaMethod metaMethod = new NewInstanceMetaMethod(method);
              arr.add(metaMethod);
              instanceMethods.add(metaMethod);
            } else {
              final NewStaticMetaMethod metaMethod = new NewStaticMetaMethod(method);
              arr.add(metaMethod);
              staticMethods.add(metaMethod);
            }
          }
        }
      }
    }
  }