Example #1
0
  public static ASTNode getASTNode(String codeFragment) {
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(codeFragment.toCharArray());
    parser.setResolveBindings(false);
    ASTNode node = null;

    parser.setKind(ASTParser.K_STATEMENTS);
    try {
      node = (Block) parser.createAST(null);
    } catch (Exception e) {
      return null;
    }
    if (node.toString().trim().equalsIgnoreCase("{\n}")) {
      parser.setKind(ASTParser.K_COMPILATION_UNIT);
      parser.setSource(codeFragment.toCharArray());
      parser.setResolveBindings(false);
      node = (CompilationUnit) parser.createAST(null);
      if (node.toString().trim().equalsIgnoreCase("{\n}")
          || node.toString().trim().equalsIgnoreCase("")) {
        codeFragment = "public class A { \n" + codeFragment + "\n }";

        parser.setKind(ASTParser.K_COMPILATION_UNIT);
        parser.setSource(codeFragment.toCharArray());
        parser.setSource(codeFragment.toCharArray());
        parser.setResolveBindings(false);
        node = (CompilationUnit) parser.createAST(null);
      }
    }
    return node;
  }
  private void assertRoundTripConversion(
      final String code, final ParseType parseType, final boolean useComments) {
    final JavaASTExtractor ex = new JavaASTExtractor(false, useComments);
    final ASTNode cu = ex.getAST(code, parseType);
    final TempletizedJavaTreeExtractor converter = new TempletizedJavaTreeExtractor();
    final TreeNode<Integer> treeCu = converter.getTree(cu, useComments);

    final ASTNode reconvertedCu = converter.getASTFromTree(treeCu);

    assertEquals(cu.toString(), reconvertedCu.toString());
  }
  protected ISourceLocation getSourceLocation(ASTNode node) {
    try {
      int nodeLength = compilUnit.getExtendedLength(node);

      if (nodeLength > 0) {
        int start = compilUnit.getExtendedStartPosition(node);
        int end = start + nodeLength - 1;

        if (end < start && ((node.getFlags() & 9) > 0)) {
          insert(
              messages,
              values.constructor(
                  DATATYPE_RASCAL_MESSAGE_ERROR_NODE_TYPE,
                  values.string("Recovered/Malformed node, guessing the length"),
                  values.sourceLocation(loc, 0, 0)));

          nodeLength = node.toString().length();
          end = start + nodeLength - 1;
        }

        return values.sourceLocation(
            loc,
            start,
            nodeLength,
            compilUnit.getLineNumber(start),
            compilUnit.getLineNumber(end),
            // TODO: only adding 1 at the end seems to work, need to test.
            compilUnit.getColumnNumber(start),
            compilUnit.getColumnNumber(end) + 1);
      }
    } catch (IllegalArgumentException e) {
      insert(
          messages,
          values.constructor(
              DATATYPE_RASCAL_MESSAGE_ERROR_NODE_TYPE,
              values.string("Most probably missing dependency"),
              values.sourceLocation(loc, 0, 0)));
    }
    return values.sourceLocation(loc, 0, 0, 0, 0, 0, 0);
  }
 protected boolean visitNode(ASTNode node) {
   Assert.isTrue(false, "No implementation to flatten node: " + node.toString()); // $NON-NLS-1$
   return false;
 }
  private Example makeExample(
      MethodDeclaration node,
      Set<? extends Expression> envolvedInvocations,
      List<ApiMethod> envolvedApiMethods) {

    //  Visitor responsável por realizar o slicing de programas
    SlicingStatementVisitor visitor =
        new SlicingStatementVisitor(node, new HashSet<ASTNode>(envolvedInvocations));
    node.accept(visitor);

    Collection<Statement> relatedStatements = visitor.getSlicedStatements();

    ASTNode newAST =
        ASTUtil.copyStatements(node.getBody(), relatedStatements, AST.newAST(AST.JLS3));
    if (!relatedStatements.isEmpty()) {
      LOGGER.error("Some statements were not included!");
    }

    if (newAST == null) {
      LOGGER.error("Slicing process failed for node ");
      // TODO Se AST retornada for nula é porque faltou incluir statement(s)
      return null;
    } else if (((Block) newAST).statements().isEmpty()) {
      LOGGER.error("Slicing process failed for node ");
      // TODO Se o Block retornado for vazio é porque faltou incluir statement(s)
      return null;
    }

    ASTUtil.removeEmptyBlocks((Block) newAST);

    // Adiciona declarações de variáveis que não foram encontradas no escopo do método
    // Para facilitar, tipos iguais são declarados no mesmo Statement
    Set<String> additionalDeclarationLines = new HashSet<String>();
    Map<ITypeBinding, List<IVariableBinding>> typesMap =
        new HashMap<ITypeBinding, List<IVariableBinding>>();
    for (IVariableBinding ivb : visitor.getUndiscoveredDeclarations()) {
      if (!typesMap.containsKey(ivb.getType())) {
        typesMap.put(ivb.getType(), new ArrayList<IVariableBinding>(2));
      }
      typesMap.get(ivb.getType()).add(ivb);
    }

    for (ITypeBinding typeBinding : typesMap.keySet()) {
      List<IVariableBinding> variableBindings = typesMap.get(typeBinding);

      Stack<VariableDeclarationFragment> fragments = new Stack<VariableDeclarationFragment>();
      for (IVariableBinding ivb : variableBindings) {
        VariableDeclarationFragment declarationFragment =
            newAST.getAST().newVariableDeclarationFragment();
        declarationFragment.setName(newAST.getAST().newSimpleName(ivb.getName()));
        fragments.add(declarationFragment);
      }

      VariableDeclarationStatement statement =
          newAST.getAST().newVariableDeclarationStatement(fragments.pop());
      while (!fragments.isEmpty()) {
        statement.fragments().add(fragments.pop());
      }

      statement.setType(this.getType(typeBinding, newAST.getAST()));

      additionalDeclarationLines.add(statement.toString());
      ((Block) newAST).statements().add(0, statement);
    }

    Example example = new Example();
    example.setAttachment(this.attachmentMap.get(node.getRoot()));
    example.setApiMethods(new HashSet<ApiElement>(envolvedApiMethods));
    example.setImports(visitor.getImports());

    for (Expression seed : envolvedInvocations) {
      example.getSeeds().add(seed.toString());
    }

    example.setSourceMethod(node.toString());
    example.setAddedAt(new Date(System.currentTimeMillis()));

    try {
      IMethodBinding nodeBinding = node.resolveBinding();
      if (!this.methodMap.containsKey(nodeBinding)) {
        ApiClass newApiClass = new ApiClass(nodeBinding.getDeclaringClass().getQualifiedName());
        methodDeclarationHandler(node, newApiClass);
      }
      example.setSourceMethodCall(this.methodMap.get(nodeBinding).getFullName());
    } catch (Exception e) {
      LOGGER.error(e);
      if (example.getSourceMethodCall() == null) {
        example.setSourceMethodCall("?");
      }
    }

    String codeExample = newAST.toString();
    for (String line : additionalDeclarationLines) {
      codeExample =
          codeExample.replace(
              line,
              line.replace("\n", "").concat("  ").concat("//initialized previously").concat("\n"));
    }

    // FIXME
    codeExample = codeExample.replaceAll("(\\{\n)(\\s+)(\\})", "$1 //do something \n$3");

    try {
      example.setCodeExample(codeExample);
      example.setFormattedCodeExample(ASTUtil.codeFormatter(codeExample));
    } catch (Exception e) {
      LOGGER.error(e);
      if (example.getFormattedCodeExample() == null) {
        example.setFormattedCodeExample(codeExample);
      }
    }

    // TODO Obter métricas do exemplo
    example
        .getMetrics()
        .put(ExampleMetric.LOC.name(), example.getFormattedCodeExample().split("\n").length - 1);
    example.getMetrics().put(ExampleMetric.ARGUMENTS.name(), visitor.getNumberOfArguments());
    example
        .getMetrics()
        .put(ExampleMetric.DECISION_STATEMENTS.name(), visitor.getNumberOfDecisionStatements());
    example.getMetrics().put(ExampleMetric.INVOCATIONS.name(), visitor.getNumberOfInvocations());
    example
        .getMetrics()
        .put(ExampleMetric.NULL_ARGUMENTS.name(), visitor.getNumberOfNullArguments());
    example
        .getMetrics()
        .put(ExampleMetric.PRIMITIVE_ARGUMENTS.name(), visitor.getNumberOfPrimitiveArguments());
    example
        .getMetrics()
        .put(ExampleMetric.FIELD_ARGUMENTS.name(), visitor.getNumberOfFieldArguments());
    example
        .getMetrics()
        .put(
            ExampleMetric.UNDISCOVERED_DECLARATIONS.name(),
            visitor.getNumberOfUndiscoveredDeclarations());
    example
        .getMetrics()
        .put(ExampleMetric.UNHANDLED_EXCEPTIONS.name(), visitor.getNumberOfUnhandledExceptions());

    return example;
  }
  @Test
  public void testOneMethod() {
    System.out.println("filetypes in given file");
    System.out.println("inheritance " + resolver.getSuperType());
    System.out.println("interfaces " + resolver.getInterfaces());
    Map<String, String> types = resolver.getClassesInFile();
    for (MethodInvocationResolver.TypeDecl typeDeclaration : resolver.getTypeDeclarations()) {
      System.out.println(
          types.get(typeDeclaration.getClassName())
              + "   "
              + unit.getLineNumber(typeDeclaration.getLoc())
              + "  "
              + unit.getColumnNumber(typeDeclaration.getLoc()));
    }

    Map<ASTNode, String> typesAtPos = resolver.getVariableTypesAtPosition();
    System.out.println("~~~~~~variable Typeaatposition  ");
    for (Entry<ASTNode, String> e : typesAtPos.entrySet()) {
      Integer line = unit.getLineNumber(e.getKey().getStartPosition());
      Integer col = unit.getColumnNumber(e.getKey().getStartPosition());
      System.out.println(
          line + " , " + col + " , " + e.getKey().getLength() + " : " + e.getValue());
    }

    System.out.println("########  Import name position");
    for (Entry<ASTNode, String> e : resolver.getImportsDeclarationNode().entrySet()) {
      Integer line = unit.getLineNumber(e.getKey().getStartPosition());
      Integer col = unit.getColumnNumber(e.getKey().getStartPosition());
      System.out.println(
          line + " , " + col + " , " + e.getKey().getLength() + " : " + e.getValue());
    }

    System.out.println("--------------Type at positions ");
    for (Entry<ASTNode, String> e : resolver.getTypesAtPosition().entrySet()) {
      Integer line = unit.getLineNumber(e.getKey().getStartPosition());
      Integer col = unit.getColumnNumber(e.getKey().getStartPosition());
      System.out.println(
          line + " , " + col + " , " + e.getKey().getLength() + " : " + e.getValue());
    }

    for (Entry<ASTNode, ASTNode> e : resolver.getVariableDependencies().entrySet()) {
      ASTNode child = e.getKey();
      Integer chline = unit.getLineNumber(child.getStartPosition());
      Integer chcol = unit.getColumnNumber(child.getStartPosition());
      Integer chLength = child.getLength();
      ASTNode parent = e.getValue();
      Integer pline = unit.getLineNumber(parent.getStartPosition());
      Integer pcol = unit.getColumnNumber(parent.getStartPosition());
      Integer plength = parent.getLength();
      System.out.println(
          "**** "
              + child
              + "["
              + chline
              + ", "
              + chcol
              + ", "
              + chLength
              + "] ==> "
              + parent.toString()
              + "["
              + pline
              + ", "
              + pcol
              + ", "
              + plength
              + "]");
    }

    for (Entry<String, List<MethodInvokRef>> entry : resolver.getMethodInvoks().entrySet()) {
      System.out.println(" ~~~~~~~~~~~ For method " + entry.getKey() + " ~~~~~~~~~~~");
      for (MethodInvokRef m : entry.getValue()) {
        Integer loc = m.getLocation();
        Integer line = unit.getLineNumber(loc);
        Integer col = unit.getColumnNumber(loc);
        System.out.println("[" + line + ", " + col + ", " + m.getLength() + " ] ==> " + m);
      }
    }

    for (MethodDecl m : resolver.getDeclaredMethods()) {
      System.out.println("~~~~~~~~~~~~~~~~~ Declared Methods ~~~~~~~~~~~~~~~~~");
      System.out.println(m);
    }
    System.out.println(resolver.getVariableTypes());
  }