/**
   * Replace an AST node with another one by substituting the corresponding child of its parent.
   *
   * @param node The node to be replace.
   * @param newNode The new node.
   */
  public static void replaceNode(ASTNode node, ASTNode newNode) {
    ASTNode parent = node.getParent();
    StructuralPropertyDescriptor location = node.getLocationInParent();

    if (location.isChildProperty()) {
      parent.setStructuralProperty(location, newNode);
    } else {
      List<ASTNode> properties = (List<ASTNode>) parent.getStructuralProperty(location);
      int position = properties.indexOf(node);
      properties.set(position, newNode);
    }
  }
Ejemplo n.º 2
0
  private static SimpleType createRawTypeReferenceOperations(
      CompilationUnit compilationUnit, IProblemLocation[] locations, List operations) {
    if (hasFatalError(compilationUnit)) return null;

    List /*<SimpleType>*/ result = new ArrayList();
    for (int i = 0; i < locations.length; i++) {
      IProblemLocation problem = locations[i];

      if (isRawTypeReferenceProblem(problem.getProblemId())) {
        ASTNode node = problem.getCoveredNode(compilationUnit);
        if (node instanceof ClassInstanceCreation) {
          ASTNode rawReference =
              (ASTNode) node.getStructuralProperty(ClassInstanceCreation.TYPE_PROPERTY);
          if (isRawTypeReference(rawReference)) {
            result.add(rawReference);
          }
        } else if (node instanceof SimpleName) {
          ASTNode rawReference = node.getParent();
          if (isRawTypeReference(rawReference)) {
            ASTNode parent = rawReference.getParent();
            if (!(parent instanceof ArrayType || parent instanceof ParameterizedType))
              result.add(rawReference);
          }
        } else if (node instanceof MethodInvocation) {
          MethodInvocation invocation = (MethodInvocation) node;

          ASTNode rawReference = getRawReference(invocation, compilationUnit);
          if (rawReference != null) {
            result.add(rawReference);
          }
        }
      }
    }

    if (result.size() == 0) return null;

    SimpleType[] types = (SimpleType[]) result.toArray(new SimpleType[result.size()]);
    operations.add(new AddTypeParametersOperation(types));
    return types[0];
  }