Ejemplo n.º 1
0
  private NLPResult testCases() {
    NLPResult res = new NLPResult();

    res.addComment(
        AnalysisType.SRA,
        new RangeAnalysisComment(-1, 5, 14, PositiveNegativeLattice.POS, "x", "testPos"));
    res.addComment(
        AnalysisType.SRA,
        new RangeAnalysisComment(-1, 5, 14, PositiveNegativeLattice.POS, "y", "testPos"));

    res.addComment(
        AnalysisType.SRA,
        new RangeAnalysisComment(-1, 16, 25, PositiveNegativeLattice.NEG, "x", "testNeg"));
    res.addComment(
        AnalysisType.SRA,
        new RangeAnalysisComment(-1, 16, 25, PositiveNegativeLattice.NEG, "y", "testNeg"));

    res.addComment(
        AnalysisType.SRA,
        new RangeAnalysisComment(-1, 27, 36, PositiveNegativeLattice.ZERO, "x", "testZero"));
    res.addComment(
        AnalysisType.SRA,
        new RangeAnalysisComment(-1, 27, 36, PositiveNegativeLattice.ZERO, "y", "testZero"));

    res.addComment(
        AnalysisType.SRA,
        new RangeAnalysisComment(-1, 38, 50, PositiveNegativeLattice.POS, "x", "testEvaluation"));
    res.addComment(
        AnalysisType.SRA,
        new RangeAnalysisComment(-1, 38, 50, PositiveNegativeLattice.POS, "y", "testEvaluation"));

    return res;
  }
Ejemplo n.º 2
0
  @Override
  public void beforeAllMethods(ITypeRoot root, CompilationUnit cUnit) {
    JMLElement rootXML = null;
    try {
      rootXML = XMLFromSource.createXML(cUnit, root.getSource(), true, true);
    } catch (JavaModelException e) {
      e.printStackTrace();
    }

    // for use of NLP
    XMLOutputter output = new XMLOutputter(rootXML);

    // could make this pretty later - at the moment building XML just to parse it...
    try {
      InputStream istream = new ByteArrayInputStream(output.getString().getBytes());
      Document jdoc = new SAXBuilder().build(istream);
      istream.close();

      NLPResult result = new NLPResult();

      for (Element classElement : jdoc.getRootElement().getChildren()) {
        for (Element methodElement : classElement.getChildren()) {

          // get the start and end lines of this method
          Integer startLine = new Integer(methodElement.getAttributeValue("line"));
          Integer endLine = new Integer(methodElement.getAttributeValue("endLine"));

          // build set of variable names in scope
          Set<String> variableNames = new HashSet<String>();

          Element paramsElement = methodElement.getChild("params");
          if (paramsElement != null)
            for (Element paramElement : paramsElement.getChildren())
              variableNames.add(paramElement.getAttributeValue("name"));

          for (Element declarationElement : methodElement.getChildren("declaration"))
            variableNames.add(declarationElement.getAttributeValue("name"));

          Machine intRangeMachine = new IntRangeMachine(variableNames);

          // analyse each of the comments
          for (Element commentElement : methodElement.getChildren("comment")) {

            Map<String, String> frame =
                intRangeMachine.recognise(commentElement.getTextNormalize());
            if (frame != null) {

              String op = frame.get("op");
              String a1 = frame.containsKey("a1") ? frame.get("a1") : "0";

              PositiveNegativeLattice expected = null;
              if (op.equals("eq") && a1.equals("0")) {
                expected = PositiveNegativeLattice.ZERO;
              } else if ((op.equals("gt") && a1.equals("0"))
                  || (op.equals("ge") && a1.equals("1"))) {
                expected = PositiveNegativeLattice.POS;
              } else if ((op.equals("lt") && a1.equals("0"))
                  || (op.equals("le") && a1.equals("1"))) {
                expected = PositiveNegativeLattice.NEG;
              }

              if (expected != null) {
                int commentLineNo = -1;
                int lineFrom = new Integer(methodElement.getAttributeValue("line"));
                int lineTo = new Integer(methodElement.getAttributeValue("endLine"));
                String varName = frame.get("a0");
                String methodName = methodElement.getAttributeValue("name");
                result.addComment(
                    AnalysisType.SRA,
                    new RangeAnalysisComment(
                        commentLineNo, lineFrom, lineTo, expected, varName, methodName));
              }
            }
          }
        }
      }

      for (AnalysisType c : result.getTypes()) {
        try {

          c.classFile
              .getConstructor(CommentCollection.class)
              .newInstance(result.getAnnotations(c))
              .runAnalysis(getReporter(), getInput(), root, cUnit);

        } catch (InstantiationException
            | IllegalAccessException
            | IllegalArgumentException
            | InvocationTargetException
            | NoSuchMethodException
            | SecurityException e) {
          e.printStackTrace();
        }
      }

    } catch (JDOMException | IOException e) {
      e.printStackTrace();
    }
  }