com.sun.source.tree.ArrayAccessTree Java Examples

The following examples show how to use com.sun.source.tree.ArrayAccessTree. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ArrayAccess.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public List<Fix> run(CompilationInfo info, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
    if (treePath.getLeaf().getKind() != Kind.ARRAY_ACCESS) {
        return Collections.emptyList();
    }
    ArrayAccessTree aa = (ArrayAccessTree) treePath.getLeaf();
    TypeMirror onType = info.getTrees().getTypeMirror(new TreePath(treePath, aa.getExpression()));
    boolean list = isSubType(info, onType, "java.util.List");
    boolean map = isSubType(info, onType, "java.util.Map");
    
    if (list || map) {
        Kind parentKind = treePath.getParentPath().getLeaf().getKind();
        if (CANNOT_HANDLE_PARENTS.contains(parentKind)) return null;
        return Collections.singletonList(new ConvertFromArrayAccess(info, treePath, map, parentKind == Kind.ASSIGNMENT).toEditorFix());
    }
    
    return Collections.emptyList();
}
 
Example #2
Source File: ExpectedTypeResolver.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Index in array access must be int
 */
@Override
public List<? extends TypeMirror> visitArrayAccess(ArrayAccessTree node, Object p) {
    if (theExpression == null) {
        return null;
    }
    // for now we do not guess array type, just the indexes.
    if (theExpression == node.getExpression()) {
        return null;
    }
    return Collections.singletonList(info.getTypes().getPrimitiveType(TypeKind.INT));
}
 
Example #3
Source File: TreeDiffer.java    From compile-testing with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitArrayAccess(ArrayAccessTree expected, Tree actual) {
  Optional<ArrayAccessTree> other = checkTypeAndCast(expected, actual);
  if (!other.isPresent()) {
    addTypeMismatch(expected, actual);
    return null;
  }

  scan(expected.getExpression(), other.get().getExpression());
  scan(expected.getIndex(), other.get().getIndex());
  return null;
}
 
Example #4
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
/**
 * Returns all array indices for the given expression, e.g. given {@code foo[0][0]} returns the
 * expressions for {@code [0][0]}.
 */
private Deque<ExpressionTree> getArrayIndices(ExpressionTree expression) {
  Deque<ExpressionTree> indices = new ArrayDeque<>();
  while (expression instanceof ArrayAccessTree) {
    ArrayAccessTree array = (ArrayAccessTree) expression;
    indices.addLast(array.getIndex());
    expression = array.getExpression();
  }
  return indices;
}
 
Example #5
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the base expression of an erray access, e.g. given {@code foo[0][0]} returns {@code
 * foo}.
 */
private ExpressionTree getArrayBase(ExpressionTree node) {
  while (node instanceof ArrayAccessTree) {
    node = ((ArrayAccessTree) node).getExpression();
  }
  return node;
}
 
Example #6
Source File: JavaInputAstVisitor.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns all array indices for the given expression, e.g. given {@code foo[0][0]} returns the
 * expressions for {@code [0][0]}.
 */
private Deque<ExpressionTree> getArrayIndices(ExpressionTree expression) {
    Deque<ExpressionTree> indices = new ArrayDeque<>();
    while (expression instanceof ArrayAccessTree) {
        ArrayAccessTree array = (ArrayAccessTree) expression;
        indices.addLast(array.getIndex());
        expression = array.getExpression();
    }
    return indices;
}
 
Example #7
Source File: JavaInputAstVisitor.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the base expression of an erray access, e.g. given {@code foo[0][0]} returns {@code
 * foo}.
 */
private ExpressionTree getArrayBase(ExpressionTree node) {
    while (node instanceof ArrayAccessTree) {
        node = ((ArrayAccessTree) node).getExpression();
    }
    return node;
}
 
Example #8
Source File: NullAway.java    From NullAway with MIT License 5 votes vote down vote up
@Override
public Description matchArrayAccess(ArrayAccessTree tree, VisitorState state) {
  if (!matchWithinClass) {
    return Description.NO_MATCH;
  }
  Description description = matchDereference(tree.getExpression(), tree, state);
  if (!description.equals(Description.NO_MATCH)) {
    return description;
  }
  // also check for unboxing of array index expression
  return doUnboxingCheck(state, tree.getIndex());
}
 
Example #9
Source File: JDIWrappersTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Element getElement(Tree tree) {
    TreePath expPath = TreePath.getPath(cut, tree);
    Element e = trees.getElement(expPath);
    if (e == null) {
        if (tree instanceof ParenthesizedTree) {
            e = getElement(((ParenthesizedTree) tree).getExpression());
            //if (e == null) {
            //    System.err.println("Have null element for "+tree);
            //}
            //System.err.println("\nHAVE "+e.asType().toString()+" for ParenthesizedTree "+tree);
        }
        else if (tree instanceof TypeCastTree) {
            e = getElement(((TypeCastTree) tree).getType());
            //if (e == null) {
            //    System.err.println("Have null element for "+tree);
            //}
            //System.err.println("\nHAVE "+e.asType().toString()+" for TypeCastTree "+tree);
        }
        else if (tree instanceof AssignmentTree) {
            e = getElement(((AssignmentTree) tree).getVariable());
        }
        else if (tree instanceof ArrayAccessTree) {
            e = getElement(((ArrayAccessTree) tree).getExpression());
            if (e != null) {
                TypeMirror tm = e.asType();
                if (tm.getKind() == TypeKind.ARRAY) {
                    tm = ((ArrayType) tm).getComponentType();
                    e = types.asElement(tm);
                }
            }
            //System.err.println("ArrayAccessTree = "+((ArrayAccessTree) tree).getExpression()+", element = "+getElement(((ArrayAccessTree) tree).getExpression())+", type = "+getElement(((ArrayAccessTree) tree).getExpression()).asType());
        }
    }
    return e;
}
 
Example #10
Source File: ArrayAccess.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected void performRewrite(TransformationContext ctx) throws Exception {
    ArrayAccessTree aa = (ArrayAccessTree) ctx.getPath().getLeaf();
    TreeMaker make = ctx.getWorkingCopy().getTreeMaker();
    
    if (assignment) {
        AssignmentTree at = (AssignmentTree) ctx.getPath().getParentPath().getLeaf();
        ctx.getWorkingCopy().rewrite(at, make.MethodInvocation(Collections.<ExpressionTree>emptyList(), make.MemberSelect(aa.getExpression(), map ? " put" : "set"), Arrays.asList(aa.getIndex(), at.getExpression())));
    } else {
        ctx.getWorkingCopy().rewrite(aa, make.MethodInvocation(Collections.<ExpressionTree>emptyList(), make.MemberSelect(aa.getExpression(), "get"), Collections.singletonList(aa.getIndex())));
    }
}
 
Example #11
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the base expression of an erray access, e.g. given {@code foo[0][0]} returns {@code
 * foo}.
 */
private ExpressionTree getArrayBase(ExpressionTree node) {
    while (node instanceof ArrayAccessTree) {
        node = ((ArrayAccessTree) node).getExpression();
    }
    return node;
}
 
Example #12
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns all array indices for the given expression, e.g. given {@code foo[0][0]} returns the
 * expressions for {@code [0][0]}.
 */
private Deque<ExpressionTree> getArrayIndices(ExpressionTree expression) {
    Deque<ExpressionTree> indices = new ArrayDeque<>();
    while (expression instanceof ArrayAccessTree) {
        ArrayAccessTree array = (ArrayAccessTree) expression;
        indices.addLast(array.getIndex());
        expression = array.getExpression();
    }
    return indices;
}
 
Example #13
Source File: TreeDuplicator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Tree visitArrayAccess(ArrayAccessTree tree, Void p) {
    ArrayAccessTree n = make.ArrayAccess(tree.getExpression(), tree.getIndex());
    model.setType(n, model.getType(tree));
    comments.copyComments(tree, n);
    model.setPos(n, model.getPos(tree));
    return n;
}
 
Example #14
Source File: IteratorToFor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@TriggerPattern(value="for (int $index = 0; $index < $arr.length; $index++) $statement;", constraints=@ConstraintVariableType(variable="$arr", type="Object[]"))
public static ErrorDescription forIndexedArray(final HintContext ctx) {
    AccessAndVarVisitor v = new AccessAndVarVisitor(ctx) {
        @Override public Void visitArrayAccess(ArrayAccessTree node, Void p) {
        TreePath path = getCurrentPath();
            if (MatcherUtilities.matches(ctx, path, "$arr[$index]")) { // NOI18N
                if (path.getParentPath() != null) {
                    if (   path.getParentPath().getLeaf().getKind() == Kind.ASSIGNMENT
                        && ((AssignmentTree) path.getParentPath().getLeaf()).getVariable() == node) {
                        unsuitable();
                    }
                    if (CompoundAssignmentTree.class.isAssignableFrom(path.getParentPath().getLeaf().getKind().asInterface())
                        && ((CompoundAssignmentTree) path.getParentPath().getLeaf()).getVariable() == node) {
                        unsuitable();
                    }
                }
                toReplace.add(path);
                return null;
            }
            return super.visitArrayAccess(node, p);
        }
    };
    v.scan(ctx.getVariables().get("$statement"), null); // NOI18N
    if (v.unsuitable) return null;
    
    return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_IteratorToForArray(), 
            new ReplaceIndexedForEachLoop(ctx.getInfo(), ctx.getPath(), ctx.getVariables().get("$arr"), 
            v.toReplace, v.definedVariables).toEditorFix());
}
 
Example #15
Source File: TreeNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitArrayAccess(ArrayAccessTree tree, List<Node> d) {
    List<Node> below = new ArrayList<Node>();
    
    addCorrespondingType(below);
    addCorrespondingComments(below);
    super.visitArrayAccess(tree, below);
    
    d.add(new TreeNode(info, getCurrentPath(), below));
    return null;
}
 
Example #16
Source File: Flow.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public Boolean visitArrayAccess(ArrayAccessTree node, ConstructorData p) {
    boolean lv = lValueDereference;
    // even the array reference is just read from. There's no support to track array-item lvalues.
    this.lValueDereference = false;
    scan(node.getExpression(), p);
    scan(node.getIndex(), p);
    this.lValueDereference = lv;
    return null;
}
 
Example #17
Source File: CopyFinder.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public Boolean visitArrayAccess(ArrayAccessTree node, TreePath p) {
    if (p == null)
        return super.visitArrayAccess(node, p);

    ArrayAccessTree t = (ArrayAccessTree) p.getLeaf();

    if (!scan(node.getExpression(), t.getExpression(), p))
        return false;

    return scan(node.getIndex(), t.getIndex(), p);
}
 
Example #18
Source File: JavaInputAstVisitor.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Void visitArrayAccess(ArrayAccessTree node, Void unused) {
    sync(node);
    visitDot(node);
    return null;
}
 
Example #19
Source File: UArrayAccess.java    From Refaster with Apache License 2.0 4 votes vote down vote up
@Override
@Nullable
public Unifier visitArrayAccess(ArrayAccessTree arrayAccess, @Nullable Unifier unifier) {
  unifier = getExpression().unify(arrayAccess.getExpression(), unifier);
  return getIndex().unify(arrayAccess.getIndex(), unifier);
}
 
Example #20
Source File: UTemplater.java    From Refaster with Apache License 2.0 4 votes vote down vote up
@Override
public UArrayAccess visitArrayAccess(ArrayAccessTree tree, Void v) {
  return UArrayAccess.create(template(tree.getExpression()), template(tree.getIndex()));
}
 
Example #21
Source File: TreeConverter.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private TreeNode convertArrayAccess(ArrayAccessTree node, TreePath parent) {
  TreePath path = getTreePath(parent, node);
  return new ArrayAccess()
      .setArray((Expression) convert(node.getExpression(), path))
      .setIndex((Expression) convert(node.getIndex(), path));
}
 
Example #22
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
@Override
public Void visitArrayAccess(ArrayAccessTree node, Void unused) {
  sync(node);
  visitDot(node);
  return null;
}
 
Example #23
Source File: CreateElementUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static List<? extends TypeMirror> computeArrayAccess(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    ArrayAccessTree aat = (ArrayAccessTree) parent.getLeaf();
    
    if (aat.getExpression() == error) {
        TreePath parentParent = parent.getParentPath();
        List<? extends TypeMirror> upperTypes = resolveType(types, info, parentParent, aat, offset, null, null);
        
        if (upperTypes == null) {
            return null;
        }
        
        List<TypeMirror> arrayTypes = new ArrayList<TypeMirror>();
        
        for (TypeMirror tm : upperTypes) {
            if (tm == null)
                continue;
            switch (tm.getKind()) {
                case VOID:
                case EXECUTABLE:
                case WILDCARD:
                case PACKAGE:
                    continue;
            }
            
            arrayTypes.add(info.getTypes().getArrayType(tm));
        }
        
        if (arrayTypes.isEmpty())
            return null;
        
        return arrayTypes;
    }
    
    if (aat.getIndex() == error) {
        types.add(ElementKind.PARAMETER);
        types.add(ElementKind.LOCAL_VARIABLE);
        types.add(ElementKind.FIELD);
        
        return Collections.singletonList(info.getTypes().getPrimitiveType(TypeKind.INT));
    }
    
    return null;
}
 
Example #24
Source File: ExpressionScanner.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public List<Tree> visitArrayAccess(ArrayAccessTree node, ExpressionScanner.ExpressionsInfo p) {
    return scan(node.getExpression(), node.getIndex(), p);
}
 
Example #25
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static String varNameForTree(Tree et) {
    if (et == null)
        return null;
    switch (et.getKind()) {
        case IDENTIFIER:
            return ((IdentifierTree) et).getName().toString();
        case MEMBER_SELECT:
            return ((MemberSelectTree) et).getIdentifier().toString();
        case METHOD_INVOCATION:
            return varNameForTree(((MethodInvocationTree) et).getMethodSelect());
        case NEW_CLASS:
            return firstToLower(varNameForTree(((NewClassTree) et).getIdentifier()));
        case PARAMETERIZED_TYPE:
            return firstToLower(varNameForTree(((ParameterizedTypeTree) et).getType()));
        case STRING_LITERAL:
            String name = guessLiteralName((String) ((LiteralTree) et).getValue());
            if (name == null) {
                return firstToLower(String.class.getSimpleName());
            } else {
                return firstToLower(name);
            }
        case VARIABLE:
            return ((VariableTree) et).getName().toString();
        case ARRAY_ACCESS:
            name = varNameForTree(((ArrayAccessTree)et).getExpression());
            if (name != null) {
                String singular = getSingular(name);
                if (singular != null) {
                    return singular;
                }
            }
            return null;
        case ASSIGNMENT:
            if (((AssignmentTree)et).getExpression() != null) {
                return varNameForTree(((AssignmentTree)et).getExpression());
            }
            return null;
        default:
            return null;
    }
}
 
Example #26
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
@Override
public Void visitArrayAccess(ArrayAccessTree node, Void unused) {
    sync(node);
    visitDot(node);
    return null;
}
 
Example #27
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private String simpleName(Tree t) {
    if (t == null) {
        return Bundle.DisplayName_Unknown();
    }
    if (t.getKind() == Kind.IDENTIFIER) {
        return ((IdentifierTree) t).getName().toString();
    }

    if (t.getKind() == Kind.MEMBER_SELECT) {
        return ((MemberSelectTree) t).getIdentifier().toString();
    }

    if (t.getKind() == Kind.METHOD_INVOCATION) {
        return scan(t, null);
    }

    if (t.getKind() == Kind.PARAMETERIZED_TYPE) {
        return simpleName(((ParameterizedTypeTree) t).getType()) + "<...>"; // NOI18N
    }

    if (t.getKind() == Kind.ARRAY_ACCESS) {
        return simpleName(((ArrayAccessTree) t).getExpression()) + "[]"; //NOI18N
    }

    if (t.getKind() == Kind.PARENTHESIZED) {
        return "(" + simpleName(((ParenthesizedTree)t).getExpression()) + ")"; //NOI18N
    }

    if (t.getKind() == Kind.TYPE_CAST) {
        return simpleName(((TypeCastTree)t).getType());
    }

    if (t.getKind() == Kind.ARRAY_TYPE) {
        return simpleName(((ArrayTypeTree)t).getType());
    }

    if (t.getKind() == Kind.PRIMITIVE_TYPE) {
        return ((PrimitiveTypeTree) t).getPrimitiveTypeKind().name().toLowerCase();
    }
    
    throw new IllegalStateException("Currently unsupported kind of tree: " + t.getKind()); // NOI18N
}
 
Example #28
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public @Override String visitArrayAccess(ArrayAccessTree node, Void p) {
    return "..." + simpleName(node.getExpression()) + "[]"; // NOI18N
}
 
Example #29
Source File: CreateElementUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static List<? extends TypeMirror> computeArrayAccess(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    ArrayAccessTree aat = (ArrayAccessTree) parent.getLeaf();
    
    if (aat.getExpression() == error) {
        TreePath parentParent = parent.getParentPath();
        List<? extends TypeMirror> upperTypes = resolveType(types, info, parentParent, aat, offset, null, null);
        
        if (upperTypes == null) {
            return null;
        }
        
        List<TypeMirror> arrayTypes = new ArrayList<TypeMirror>();
        
        for (TypeMirror tm : upperTypes) {
            if (tm == null)
                continue;
            switch (tm.getKind()) {
                case VOID:
                case EXECUTABLE:
                case WILDCARD:
                case PACKAGE:
                    continue;
            }
            
            arrayTypes.add(info.getTypes().getArrayType(tm));
        }
        
        if (arrayTypes.isEmpty())
            return null;
        
        return arrayTypes;
    }
    
    if (aat.getIndex() == error) {
        types.add(ElementKind.PARAMETER);
        types.add(ElementKind.LOCAL_VARIABLE);
        types.add(ElementKind.FIELD);
        
        return Collections.singletonList(info.getTypes().getPrimitiveType(TypeKind.INT));
    }
    
    return null;
}