Java Code Examples for com.sun.source.tree.ArrayAccessTree#getExpression()

The following examples show how to use com.sun.source.tree.ArrayAccessTree#getExpression() . 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: 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 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: 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 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: 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 6
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;
}