コード例 #1
0
  private MethodDecl getMethodDecl(MethodDeclaration node) {
    String qualifiedTypeName = currentPackage + "." + Joiner.on(".").skipNulls().join(typesInFile);
    SimpleName nameNode = node.getName();
    String methodName = nameNode.toString();
    String returnType = "";
    if (node.getReturnType2() != null) {
      returnType = getNameOfType(node.getReturnType2());
    }

    Map<String, String> params = new HashMap<>();
    for (Object p : node.parameters()) {
      String typeName = OBJECT_TYPE;
      if (p instanceof SingleVariableDeclaration) {

        SingleVariableDeclaration svd = (SingleVariableDeclaration) p;
        String varName = svd.getName().toString();
        Type type = svd.getType();
        typeName = getNameOfType(type);

        params.put(varName, typeName);
      } else {
        System.err.println("Unxepected AST node type for param - " + p);
      }
    }
    return new MethodDecl(
        methodName, qualifiedTypeName, returnType, nameNode.getStartPosition(), params);
  }
コード例 #2
0
  @SuppressWarnings("rawtypes")
  @Override
  public boolean visit(MethodInvocation node) {
    SimpleName methodName = node.getName();

    List args = node.arguments();
    Expression expression = node.getExpression();
    Map<String, Integer> scopeBindings = getNodeScopes().get(node);
    String target = getTarget(expression);

    String targetType = translateTargetToType(target, scopeBindings);
    // Add only if you could guess the type of target, else ignore.
    // TODO: In case of a method in super type, this will still infer it as in "this".
    if (!targetType.isEmpty()) {
      List<String> argTypes = translateArgsToTypes(args, scopeBindings);
      if (!methodStack.empty()) {
        MethodDeclaration currentMethod = methodStack.peek();
        MethodDecl methodDecl = getMethodDecl(currentMethod);
        List<MethodInvokRef> invoks = methodInvoks.get(methodDecl);
        if (invoks == null) {
          invoks = new ArrayList<>();
          methodInvoks.put(methodDecl, invoks);
        }
        MethodInvokRef methodInvokRef =
            new MethodInvokRef(
                methodName.toString(),
                targetType,
                target,
                args.size(),
                node.getName().getStartPosition(),
                argTypes,
                methodName.getLength(),
                false,
                getReturnType(node));
        invoks.add(methodInvokRef);
      }
    }
    return true;
  }