Exemplo n.º 1
0
 public static Object getPropertyOnSuperSpreadSafe(
     Class senderClass, GroovyObject receiver, String messageName) throws Throwable {
   List answer = new ArrayList();
   for (Iterator it = InvokerHelper.asIterator(receiver); it.hasNext(); ) {
     answer.add(getPropertySafe(senderClass, it.next(), messageName));
   }
   return answer;
 }
Exemplo n.º 2
0
 public static Object getFieldSpreadSafe(Class senderClass, Object receiver, String messageName)
     throws Throwable {
   if (receiver == null) return null;
   List answer = new ArrayList();
   for (Iterator it = InvokerHelper.asIterator(receiver); it.hasNext(); ) {
     answer.add(getFieldSafe(senderClass, it.next(), messageName));
   }
   return answer;
 }
Exemplo n.º 3
0
 public static Object invokeMethodOnSuperNSpreadSafe(
     Class senderClass, GroovyObject receiver, String messageName, Object[] messageArguments)
     throws Throwable {
   List answer = new ArrayList();
   for (Iterator it = InvokerHelper.asIterator(receiver); it.hasNext(); ) {
     answer.add(invokeMethodNSafe(senderClass, it.next(), messageName, messageArguments));
   }
   return answer;
 }
 private void createMetaMethodFromClass(Map<CachedClass, List<MetaMethod>> map, Class aClass) {
   try {
     MetaMethod method = (MetaMethod) aClass.newInstance();
     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 (InstantiationException e) {
     /* ignore */
   } catch (IllegalAccessException e) {
     /* ignore */
   }
 }
Exemplo n.º 5
0
 // spread expressions
 public static Object[] despreadList(Object[] args, Object[] spreads, int[] positions) {
   List ret = new ArrayList();
   int argsPos = 0;
   int spreadPos = 0;
   for (int pos = 0; pos < positions.length; pos++) {
     for (; argsPos < positions[pos]; argsPos++) {
       ret.add(args[argsPos]);
     }
     Object value = spreads[spreadPos];
     if (value == null) {
       ret.add(null);
     } else if (value instanceof List) {
       ret.addAll((List) value);
     } else if (value.getClass().isArray()) {
       ret.addAll(DefaultTypeTransformation.primitiveArrayToList(value));
     } else {
       throw new IllegalArgumentException(
           "cannot spread the type " + value.getClass().getName() + " with value " + value);
     }
     spreadPos++;
   }
   for (; argsPos < args.length; argsPos++) {
     ret.add(args[argsPos]);
   }
   return ret.toArray();
 }
 public void addGrailsBuildListener(GrailsBuildListener listener) {
   buildListeners.add(listener);
 }
  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);
            }
          }
        }
      }
    }
  }