private Module loadModuleFromSource(String pkgName, LinkedList<JCCompilationUnit> moduleTrees) {
   if (pkgName.isEmpty()) return null;
   String moduleClassName = pkgName + ".module";
   JavaFileObject fileObject;
   try {
     if (options.get(OptionName.VERBOSE) != null) {
       Log.printLines(log.noticeWriter, "[Trying to load module " + moduleClassName + "]");
     }
     fileObject =
         fileManager.getJavaFileForInput(
             StandardLocation.SOURCE_PATH, moduleClassName, Kind.SOURCE);
     if (options.get(OptionName.VERBOSE) != null) {
       Log.printLines(log.noticeWriter, "[Got file object: " + fileObject + "]");
     }
   } catch (IOException e) {
     e.printStackTrace();
     return loadModuleFromSource(getParentPackage(pkgName), moduleTrees);
   }
   if (fileObject != null) {
     CeylonCompilationUnit ceylonCompilationUnit = (CeylonCompilationUnit) parse(fileObject);
     moduleTrees.add(ceylonCompilationUnit);
     // parse the module info from there
     Module module = ceylonCompilationUnit.phasedUnit.visitSrcModulePhase();
     ceylonCompilationUnit.phasedUnit.visitRemainingModulePhase();
     // now try to obtain the parsed module
     if (module != null) {
       ceylonCompilationUnit.phasedUnit.getPackage().setModule(module);
       return module;
     }
   }
   return loadModuleFromSource(getParentPackage(pkgName), moduleTrees);
 }
Example #2
0
 /** Print a string that explains usage for X options. */
 void xhelp() {
   for (int i = 0; i < recognizedOptions.length; i++) {
     recognizedOptions[i].xhelp(out);
   }
   out.println();
   Log.printLines(out, getLocalizedString("msg.usage.nonstandard.footer"));
 }
Example #3
0
 /** Print a string that explains usage. */
 void help() {
   Log.printLines(out, getLocalizedString("msg.usage.header", ownName));
   for (int i = 0; i < recognizedOptions.length; i++) {
     recognizedOptions[i].help(out);
   }
   out.println();
 }
Example #4
0
 /** Report a usage error. */
 void error(String key, Object... args) {
   if (fatalErrors) {
     String msg = getLocalizedString(key, args);
     throw new PropagatedException(new IllegalStateException(msg));
   }
   warning(key, args);
   Log.printLines(out, getLocalizedString("msg.usage", ownName));
 }
 private JavaFileObject genCodeUnlessError(Env<AttrContext> env, JCClassDecl cdef)
     throws IOException {
   CeylonFileObject sourcefile = (CeylonFileObject) env.toplevel.sourcefile;
   try {
     // do not look at the global number of errors but only those for this file
     if (super.gen.genClass(env, cdef) && !sourcefile.hasError(cdef.pos)) {
       String packageName = cdef.sym.packge().getQualifiedName().toString();
       Package pkg = modelLoader.findPackage(packageName);
       if (pkg == null) throw new RuntimeException("Failed to find package: " + packageName);
       Module module = pkg.getModule();
       if (!module.isDefault()) {
         String moduleName = module.getNameAsString();
         CeylonFileObject moduleFileObject = moduleNamesToFileObjects.get(moduleName);
         // if there's no module source file object it means the module descriptor had parse errors
         if (moduleFileObject == null || moduleFileObject.hasError()) {
           // we do not produce any class files for modules with errors
           if (options.get(OptionName.VERBOSE) != null) {
             Log.printLines(
                 log.noticeWriter,
                 "[Not writing class "
                     + cdef.sym.className()
                     + " because its module has errors: "
                     + moduleName
                     + "]");
           }
           return null;
         }
       }
       return writer.writeClass(cdef.sym);
     }
   } catch (ClassWriter.PoolOverflow ex) {
     log.error(cdef.pos(), "limit.pool");
   } catch (ClassWriter.StringOverflow ex) {
     log.error(cdef.pos(), "limit.string.overflow", ex.value.substring(0, 20));
   } catch (CompletionFailure ex) {
     chk.completionError(cdef.pos(), ex);
   } catch (AssertionError e) {
     throw new RuntimeException("Error generating bytecode for " + sourcefile.getName(), e);
   }
   return null;
 }
Example #6
0
 public void printFullVersion() {
   Log.printLines(
       out, getLocalizedString("fullVersion", ownName, JavaCompiler.fullVersion()));
 }
Example #7
0
 /** Print a message reporting an uncaught exception from an annotation processor. */
 void apMessage(AnnotationProcessingError ex) {
   Log.printLines(out, getLocalizedString("msg.proc.annotation.uncaught.exception"));
   ex.getCause().printStackTrace();
 }
Example #8
0
 /** Print a message reporting an out-of-resources error. */
 void resourceMessage(Throwable ex) {
   Log.printLines(out, getLocalizedString("msg.resource"));
   //      System.out.println("(name buffer len = " + Name.names.length + " " + Name.nc);//DEBUG
   ex.printStackTrace(out);
 }
Example #9
0
 /** Print a message reporting an input/output error. */
 void ioMessage(Throwable ex) {
   Log.printLines(out, getLocalizedString("msg.io"));
   ex.printStackTrace(out);
 }
Example #10
0
 /** Print a message reporting an fatal error. */
 void feMessage(Throwable ex) {
   Log.printLines(out, ex.getMessage());
 }
Example #11
0
 /** Print a message reporting an internal error. */
 void bugMessage(Throwable ex) {
   Log.printLines(out, getLocalizedString("msg.bug", JavaCompiler.version()));
   ex.printStackTrace(out);
 }
Example #12
0
  /**
   * Programmatic interface for main function.
   *
   * @param args The command line parameters.
   */
  public int compile(
      String[] args,
      Context context,
      List<JavaFileObject> fileObjects,
      Iterable<? extends Processor> processors) {
    if (options == null) options = Options.instance(context); // creates a new one

    filenames = new ListBuffer<File>();
    classnames = new ListBuffer<String>();
    JavaCompiler comp = null;
    /*
     * TODO: Logic below about what is an acceptable command line
     * should be updated to take annotation processing semantics
     * into account.
     */
    try {
      if (args.length == 0 && fileObjects.isEmpty()) {
        help();
        return EXIT_CMDERR;
      }

      List<File> files;
      try {
        files = processArgs(CommandLine.parse(args));
        if (files == null) {
          // null signals an error in options, abort
          return EXIT_CMDERR;
        } else if (files.isEmpty() && fileObjects.isEmpty() && classnames.isEmpty()) {
          // it is allowed to compile nothing if just asking for help or version info
          if (options.get("-help") != null
              || options.get("-X") != null
              || options.get("-version") != null
              || options.get("-fullversion") != null) return EXIT_OK;
          error("err.no.source.files");
          return EXIT_CMDERR;
        }
      } catch (java.io.FileNotFoundException e) {
        Log.printLines(
            out, ownName + ": " + getLocalizedString("err.file.not.found", e.getMessage()));
        return EXIT_SYSERR;
      }

      boolean forceStdOut = options.get("stdout") != null;
      if (forceStdOut) {
        out.flush();
        out = new PrintWriter(System.out, true);
      }

      context.put(Log.outKey, out);

      // allow System property in following line as a Mustang legacy
      boolean batchMode =
          (options.get("nonBatchMode") == null && System.getProperty("nonBatchMode") == null);
      if (batchMode) CacheFSInfo.preRegister(context);

      fileManager = context.get(JavaFileManager.class);

      comp = JavaCompiler.instance(context);
      if (comp == null) return EXIT_SYSERR;

      if (!files.isEmpty()) {
        // add filenames to fileObjects
        comp = JavaCompiler.instance(context);
        List<JavaFileObject> otherFiles = List.nil();
        JavacFileManager dfm = (JavacFileManager) fileManager;
        for (JavaFileObject fo : dfm.getJavaFileObjectsFromFiles(files))
          otherFiles = otherFiles.prepend(fo);
        for (JavaFileObject fo : otherFiles) fileObjects = fileObjects.prepend(fo);
      }
      comp.compile(fileObjects, classnames.toList(), processors);

      if (comp.errorCount() != 0) return EXIT_ERROR;
    } catch (IOException ex) {
      ioMessage(ex);
      return EXIT_SYSERR;
    } catch (OutOfMemoryError ex) {
      resourceMessage(ex);
      return EXIT_SYSERR;
    } catch (StackOverflowError ex) {
      resourceMessage(ex);
      return EXIT_SYSERR;
    } catch (FatalError ex) {
      feMessage(ex);
      return EXIT_SYSERR;
    } catch (AnnotationProcessingError ex) {
      apMessage(ex);
      return EXIT_SYSERR;
    } catch (ClientCodeException ex) {
      // as specified by javax.tools.JavaCompiler#getTask
      // and javax.tools.JavaCompiler.CompilationTask#call
      throw new RuntimeException(ex.getCause());
    } catch (PropagatedException ex) {
      throw ex.getCause();
    } catch (Throwable ex) {
      // Nasty.  If we've already reported an error, compensate
      // for buggy compiler error recovery by swallowing thrown
      // exceptions.
      if (comp == null || comp.errorCount() == 0 || options == null || options.get("dev") != null)
        bugMessage(ex);
      return EXIT_ABNORMAL;
    } finally {
      if (comp != null) comp.close();
      filenames = null;
      options = null;
    }
    return EXIT_OK;
  }
Example #13
0
 /** Report a warning. */
 void warning(String key, Object... args) {
   Log.printLines(out, ownName + ": " + getLocalizedString(key, args));
 }
Example #14
0
 void xhelp() {
   String s = "  " + helpSynopsis();
   out.print(s);
   for (int j = s.length(); j < 29; j++) out.print(" ");
   Log.printLines(out, getLocalizedString(descrKey));
 }
 private Module loadModuleFromSource(
     String pkgName,
     LinkedList<JCCompilationUnit> moduleTrees,
     List<JCCompilationUnit> parsedTrees) {
   if (pkgName.isEmpty()) return null;
   String moduleClassName = pkgName + ".module";
   JavaFileObject fileObject;
   try {
     if (options.get(OptionName.VERBOSE) != null) {
       Log.printLines(log.noticeWriter, "[Trying to load module " + moduleClassName + "]");
     }
     fileObject =
         fileManager.getJavaFileForInput(
             StandardLocation.SOURCE_PATH, moduleClassName, Kind.SOURCE);
     if (options.get(OptionName.VERBOSE) != null) {
       Log.printLines(log.noticeWriter, "[Got file object: " + fileObject + "]");
     }
   } catch (IOException e) {
     e.printStackTrace();
     return loadModuleFromSource(getParentPackage(pkgName), moduleTrees, parsedTrees);
   }
   if (fileObject != null) {
     // first make sure we're not already compiling it: this can happen if we have several versions
     // of the
     // same module already loaded: we will get one which isn't the one we compile, but that's not
     // the one
     // we really want to compile.
     for (JCCompilationUnit parsedTree : parsedTrees) {
       if (parsedTree.sourcefile.equals(fileObject)
           && parsedTree instanceof CeylonCompilationUnit) {
         // same file! we already parsed it, let's return this one's module
         PhasedUnit phasedUnit = ((CeylonCompilationUnit) parsedTree).phasedUnit;
         // the module visitor does load the module but does not set the unit's package module
         if (phasedUnit.getPackage().getModule() == null) {
           // so find the module it created
           for (Module mod : ceylonContext.getModules().getListOfModules()) {
             // we recognise it with the unit
             if (mod.getUnit() == phasedUnit.getUnit()) {
               // set the package's module
               Package pkg = phasedUnit.getPackage();
               pkg.setModule(mod);
               mod.getPackages().add(pkg);
               modulesLoadedFromSource.add(mod);
               break;
             }
           }
         }
         // now return it
         return phasedUnit.getPackage().getModule();
       }
     }
     JCCompilationUnit javaCompilationUnit = parse(fileObject);
     Module module;
     if (javaCompilationUnit instanceof CeylonCompilationUnit) {
       CeylonCompilationUnit ceylonCompilationUnit = (CeylonCompilationUnit) javaCompilationUnit;
       moduleTrees.add(ceylonCompilationUnit);
       // parse the module info from there
       module = ceylonCompilationUnit.phasedUnit.visitSrcModulePhase();
       ceylonCompilationUnit.phasedUnit.visitRemainingModulePhase();
       // now set the module
       if (module != null) {
         ceylonCompilationUnit.phasedUnit.getPackage().setModule(module);
       }
     } else {
       // there was a syntax error in the module descriptor, make a pretend module so that we can
       // correctly mark all declarations as part of that module, but we won't generate any code
       // for it
       ModuleManager moduleManager = phasedUnits.getModuleManager();
       module = moduleManager.getOrCreateModule(Arrays.asList(pkgName.split("\\.")), "bogus");
     }
     // now remember it
     if (module != null) {
       modulesLoadedFromSource.add(module);
       return module;
     }
   }
   return loadModuleFromSource(getParentPackage(pkgName), moduleTrees, parsedTrees);
 }