Exemplo n.º 1
0
    public static List<String> createCompilationProblemsList(
        List<CompilationResult> compilationResults) {
      List<String> res = new ArrayList<String>();

      for (CompilationResult r : compilationResults) {
        if (r.getErrors() != null) {
          for (CategorizedProblem p : r.getErrors()) {
            res.add(
                new String(r.getCompilationUnit().getFileName())
                    + " ("
                    + p.getSourceLineNumber()
                    + "): "
                    + p.getMessage());
          }
        }
      }

      return res;
    }
Exemplo n.º 2
0
  protected void report(Environment environment, CategorizedProblem problem) {
    if (problem == null) {
      throw new IllegalArgumentException("problem cannot be null");
    }

    File file = new File(new String(problem.getOriginatingFileName()));
    String filename = file.getAbsolutePath();

    String message = problem.getMessage() + " at " + filename + ":" + problem.getSourceLineNumber();

    if (problem.isError()) {
      if (!environment.getNoClasspath()) {
        // by default, compilation errors are notified as exception
        throw new ModelBuildingException(message);
      } else {
        // in noclasspath mode, errors are only reported
        environment.report(null, problem.isError() ? Level.ERROR : Level.WARN, message);
      }
    }
  }
Exemplo n.º 3
0
    @Override
    public void onCompilationResult(CompilationResult cr) {
      Set<String> classesWithErrors = new HashSet<String>();
      if (cr.getErrors() != null) {
        myTracer.push("handling errors", false);
        for (final CategorizedProblem cp : cr.getErrors()) {
          String fileName = new String(cp.getOriginatingFileName());
          final String fqName =
              NameUtil.namespaceFromPath(
                  fileName.substring(0, fileName.length() - MPSExtentions.DOT_JAVAFILE.length()));
          classesWithErrors.add(fqName);

          SModule containingModule = myContainingModules.get(fqName);
          assert containingModule != null;
          JavaFile javaFile = myModuleSources.get(containingModule).getJavaFile(fqName);

          String messageString = new String(cp.getOriginatingFileName()) + " : " + cp.getMessage();

          // final SNode nodeToShow = getNodeByLine(cp, fqName);

          Object hintObject = new FileWithPosition(javaFile.getFile(), cp.getSourceStart());

          String errMsg = messageString + " (line: " + cp.getSourceLineNumber() + ")";
          if (cp.isWarning()) {
            myMessages.add(createMessage(MessageKind.WARNING, errMsg, hintObject));
          } else {
            if (myOutputtedErrors == 0) {
              myMessages.add(createMessage(MessageKind.ERROR, "Compilation problems", null));
              myMessages.add(
                  createMessage(
                      MessageKind.INFORMATION,
                      "Modules: "
                          + myModules.toString()
                          + "\nClasspath: "
                          + myClassPathItems
                          + "\n",
                      null));
            }
            if (myOutputtedErrors < MAX_ERRORS) {
              myOutputtedErrors++;
              myMessages.add(createMessage(MessageKind.ERROR, errMsg, hintObject));
            }
          }
        }
        myTracer.pop();

        myErrorCount += cr.getErrors().length;
      }

      myTracer.push("storing files", false);
      for (ClassFile cf : cr.getClassFiles()) {
        String fqName = convertCompoundToFqName(cf.getCompoundName());
        String containerClassName = fqName;
        if (containerClassName.contains("$")) {
          int index = containerClassName.indexOf('$');
          containerClassName = containerClassName.substring(0, index);
        }
        if (myContainingModules.containsKey(containerClassName)) {
          SModule m = myContainingModules.get(containerClassName);
          myChangedModules.add(m);
          File classesGen = new File(getJavaFacet(m).getClassesGen().getPath());
          String packageName = NameUtil.namespaceFromLongName(fqName);
          File outputDir =
              new File(classesGen + File.separator + NameUtil.pathFromNamespace(packageName));
          if (!outputDir.exists()) {
            if (!outputDir.mkdirs()) {
              throw new RuntimeException("Can't create " + outputDir.getPath() + " directory");
            }
          }
          String className = NameUtil.shortNameFromLongName(fqName);
          File output = new File(outputDir, className + ".class");
          if (!classesWithErrors.contains(containerClassName)) {
            FileOutputStream os = null;
            try {
              os = new FileOutputStream(output);
              os.write(cf.getBytes());
            } catch (IOException e) {
              String errMsg = "Can't write to " + output.getAbsolutePath();
              myMessages.add(createMessage(MessageKind.ERROR, errMsg, null));
            } finally {
              if (os != null) {
                try {
                  os.close();
                } catch (IOException e) {
                  myHandler.handle(createMessage(MessageKind.ERROR, e.toString(), e));
                }
              }
            }
          } else {
            if (output.exists() && !(output.delete())) {
              String errMsg = "Can't delete " + output.getPath();
              myMessages.add(createMessage(MessageKind.ERROR, errMsg, null));
            }
          }
        } else {
          String errMsg =
              "I don't know in which module's output path I should place class file for " + fqName;
          myMessages.add(createMessage(MessageKind.ERROR, errMsg, null));
        }
      }
      myTracer.pop();
    }