Example #1
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;
 }
 @Override
 protected void setUp() throws Exception {
   super.setUp();
   ExpandoMetaClass.enableGlobally();
   MockGrailsPluginManager pluginManager = new MockGrailsPluginManager();
   PluginManagerHolder.setPluginManager(pluginManager);
 }
Example #3
0
  public MixinInMetaClass(ExpandoMetaClass emc, CachedClass mixinClass) {
    super(softBundle);
    this.emc = emc;
    this.mixinClass = mixinClass;

    constructor = findDefaultConstructor(mixinClass);
    emc.addMixinClass(this);
  }
 public static <T extends GroovyObject> T expandoMetaClass(T obj) {
   ExpandoMetaClass emc = new ExpandoMetaClass(obj.getClass(), false);
   emc.initialize();
   obj.setMetaClass(emc);
   return obj;
 }
  /**
   * Evaluate the arguments to get the name of the script to execute, which environment to run it
   * in, and the arguments to pass to the script. This also evaluates arguments of the form
   * "-Dprop=value" and creates system properties from each one.
   *
   * @param args Command line arguments
   */
  public static void main(String[] args) {
    System.setProperty("net.sf.ehcache.skipUpdateCheck", "true");
    ExpandoMetaClass.enableGlobally();
    originalIn = System.in;
    originalOut = System.out;

    CommandLineParser parser = getCommandLineParser();

    GrailsConsole console = GrailsConsole.getInstance();

    CommandLine commandLine;
    try {
      if (args.length == 0) {
        commandLine = new DefaultCommandLine();
      } else {
        commandLine = parser.parseString(args[0]);
        if (commandLine.hasOption(CommandLine.NOANSI_ARGUMENT)) {
          console.setAnsiEnabled(false);
        }
      }
    } catch (ParseException e) {
      console.error("Error processing command line arguments: " + e.getMessage());
      System.exit(1);
      return;
    }

    ScriptAndArgs script = processArgumentsAndReturnScriptName(commandLine);

    // Get hold of the GRAILS_HOME environment variable if it is available.
    String grailsHome = System.getProperty("grails.home");

    // Now we can pick up the Grails version from the Ant project properties.
    BuildSettings build = null;
    try {
      build = new BuildSettings(new File(grailsHome));
      build.setModified(commandLine.hasOption(CommandLine.REFRESH_DEPENDENCIES_ARGUMENT));
      build.setOffline(commandLine.hasOption(CommandLine.OFFLINE_ARGUMENT));
      if (commandLine.hasOption(CommandLine.DEBUG_FORK)) {
        System.setProperty(ForkedGrailsProcess.DEBUG_FORK, "true");
      }
      if (build.getRootLoader() == null) {
        build.setRootLoader((URLClassLoader) GrailsScriptRunner.class.getClassLoader());
      }
    } catch (Exception e) {
      exitWithError(
          "An error occurred loading the grails-app/conf/BuildConfig.groovy file: "
              + e.getMessage(),
          null);
    }

    // Check that Grails' home actually exists.
    final File grailsHomeInSettings = build.getGrailsHome();
    if (grailsHomeInSettings == null || !grailsHomeInSettings.exists()) {
      exitWithError("Grails' installation directory not found: " + build.getGrailsHome(), null);
    }

    if (commandLine.hasOption(CommandLine.VERSION_ARGUMENT)) {
      console.log("Grails version: " + build.getGrailsVersion());
      System.exit(0);
    }

    if (commandLine.hasOption(CommandLine.HELP_ARGUMENT)) {
      if (commandLine.getCommandName() != null) {
        console.log("The '-help' option is deprecated; use 'grails help [target]'");
      } else {
        console.log("The '-help' option is deprecated; use 'grails help'");
      }
      System.exit(0);
    }

    boolean resolveDeps = commandLine.hasOption(CommandLine.REFRESH_DEPENDENCIES_ARGUMENT);
    if (resolveDeps) {
      if (commandLine.hasOption("include-source")) {
        build.setIncludeSource(true);
      }
      if (commandLine.hasOption("include-javadoc")) {
        build.setIncludeJavadoc(true);
      }
    }
    GrailsScriptRunner scriptRunner = new GrailsScriptRunner(build);
    scriptRunner.setInteractive(!commandLine.hasOption(CommandLine.NON_INTERACTIVE_ARGUMENT));
    if ("Interactive".equals(script.name)) {
      console.error(
          "The 'interactive' script is deprecated; to run in interactive mode just omit the script name");
      script.name = null;
    }

    if (script.name == null) {
      String version = System.getProperty("grails.version");
      console.updateStatus(
          "Loading Grails " + (version == null ? build.getGrailsVersion() : version));

      loadConfigEnvironment(commandLine, build);
      if (resolveDeps) {
        ClasspathConfigurer.cleanResolveCache(build);
      }
      scriptRunner.initializeState();
      try {
        new InteractiveMode(build, scriptRunner).run();
      } catch (Throwable e) {
        console.error("Interactive mode exited with error: " + e.getMessage(), e);
      }
    } else {
      console.getCategory().push(script.inputName);
      console.verbose("Base Directory: " + build.getBaseDir().getPath());

      try {
        int exitCode = scriptRunner.executeCommand(commandLine, script.name, script.env);
        GrailsConsole.getInstance().flush();
        System.exit(exitCode);
      } catch (ScriptNotFoundException ex) {
        console.error("Script not found: " + ex.getScriptName());
      } catch (Throwable t) {
        String msg = "Error executing script " + script.name + ": " + t.getMessage();
        exitWithError(msg, t);
      }
    }
  }
Example #6
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 #7
0
 public CachedClass getInstanceClass() {
   return emc.getTheCachedClass();
 }