예제 #1
0
  //  --------------------------------------------------------
  //                   exception handling
  //  --------------------------------------------------------
  public static Throwable unwrap(GroovyRuntimeException gre) {
    if (gre instanceof MissingPropertyExceptionNoStack) {
      MissingPropertyExceptionNoStack noStack = (MissingPropertyExceptionNoStack) gre;
      return new MissingPropertyException(noStack.getProperty(), noStack.getType());
    }

    if (gre instanceof MissingMethodExceptionNoStack) {
      MissingMethodExceptionNoStack noStack = (MissingMethodExceptionNoStack) gre;
      return new MissingMethodException(
          noStack.getMethod(), noStack.getType(), noStack.getArguments(), noStack.isStatic());
    }

    Throwable th = gre;
    if (th.getCause() != null && th.getCause() != gre) th = th.getCause();
    if (th != gre && (th instanceof GroovyRuntimeException))
      return unwrap((GroovyRuntimeException) th);
    return th;
  }
  public void loadEventsScript(File eventScript) {
    if (eventScript == null) {
      return;
    }

    GrailsConsole console = GrailsConsole.getInstance();
    try {
      Class<?> scriptClass = classLoader.parseClass(eventScript);
      if (scriptClass == null) {
        console.error("Could not load event script (script may be empty): " + eventScript);
        return;
      }

      Script script = (Script) scriptClass.newInstance();
      script.setBinding(
          new Binding(binding.getVariables()) {
            @SuppressWarnings("rawtypes")
            @Override
            public void setVariable(String var, Object o) {
              final Matcher matcher = EVENT_NAME_PATTERN.matcher(var);
              if (matcher.matches() && (o instanceof Closure)) {
                String eventName = matcher.group(1);
                List<Closure> hooks = globalEventHooks.get(eventName);
                if (hooks == null) {
                  hooks = new ArrayList<Closure>();
                  globalEventHooks.put(eventName, hooks);
                }
                hooks.add((Closure<?>) o);
              }
              super.setVariable(var, o);
            }
          });
      script.run();
    } catch (Throwable e) {
      StackTraceUtils.deepSanitize(e);
      console.error(
          "Error loading event script from file [" + eventScript + "] " + e.getMessage(), e);
    }
  }
  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);
            }
          }
        }
      }
    }
  }