Java源码示例:com.sun.source.tree.ArrayTypeTree

示例1
/**
 * Accumulates a flattened list of array dimensions specifiers with type annotations, and returns
 * the base type.
 * <p>
 * <p>Given {@code int @A @B [][] @C []}, adds {@code [[@A, @B], [@C]]} to dims and returns {@code
 * int}.
 */
private static Tree extractDims(Deque<List<AnnotationTree>> dims, Tree node) {
    switch (node.getKind()) {
        case ARRAY_TYPE:
            return extractDims(dims, ((ArrayTypeTree) node).getType());
        // TODO: 22-Jul-17 missing type
       /* case ANNOTATED_TYPE:
            AnnotatedTypeTree annotatedTypeTree = (AnnotatedTypeTree) node;
            if (annotatedTypeTree.getUnderlyingType().getKind() != Tree.Kind.ARRAY_TYPE) {
                return node;
            }
            node = extractDims(dims, annotatedTypeTree.getUnderlyingType());
            dims.addFirst(ImmutableList.<AnnotationTree>copyOf(annotatedTypeTree.getAnnotations()));
            return node;*/
        default:
            return node;
    }
}
 
示例2
public void moveToEnd(Tree t) {
    if (t == null) {
        return ;
    }

    long end = sourcePositions.getEndPosition(info.getCompilationUnit(), t);

    if (end == (-1))
        return ;

    if (t.getKind() == Kind.ARRAY_TYPE) {
        moveToEnd(((ArrayTypeTree) t).getType());
        return ;
    }
    moveToOffset(end);
}
 
示例3
/**
 * Accumulates a flattened list of array dimensions specifiers with type annotations, and returns
 * the base type.
 * <p>
 * <p>Given {@code int @A @B [][] @C []}, adds {@code [[@A, @B], [@C]]} to dims and returns {@code
 * int}.
 */
private static Tree extractDims(Deque<List<AnnotationTree>> dims, Tree node) {
    switch (node.getKind()) {
        case ARRAY_TYPE:
            return extractDims(dims, ((ArrayTypeTree) node).getType());
        // TODO: 22-Jul-17 missing type
       /* case ANNOTATED_TYPE:
            AnnotatedTypeTree annotatedTypeTree = (AnnotatedTypeTree) node;
            if (annotatedTypeTree.getUnderlyingType().getKind() != Tree.Kind.ARRAY_TYPE) {
                return node;
            }
            node = extractDims(dims, annotatedTypeTree.getUnderlyingType());
            dims.addFirst(ImmutableList.<AnnotationTree>copyOf(annotatedTypeTree.getAnnotations()));
            return node;*/
        default:
            return node;
    }
}
 
示例4
@Override
public Pair<ASTRecord, Integer> visitArrayType(ArrayTypeTree node, Insertion ins) {
  dbug.debug("TypePositionFinder.visitArrayType(%s)%n", node);
  JCArrayTypeTree att = (JCArrayTypeTree) node;
  dbug.debug("TypePositionFinder.visitArrayType(%s) preferred = %s%n",
      node, att.getPreferredPosition());
  // If the code has a type like "String[][][]", then this gets called
  // three times:  for String[][][], String[][], and String[]
  // respectively.  For each of the three, call String[][][] "largest".
  ArrayTypeTree largest = largestContainingArray(node);
  int largestLevels = arrayLevels(largest);
  int levels = arrayLevels(node);
  int start = arrayContentType(att).getPreferredPosition() + 1;
  int end = att.getEndPosition(tree.endPositions);
  int pos = arrayInsertPos(start, end);

  dbug.debug("  levels=%d largestLevels=%d%n", levels, largestLevels);
  for (int i=levels; i<largestLevels; i++) {
    pos = getFirstInstanceAfter('[', pos+1);
    dbug.debug("  pos %d at i=%d%n", pos, i);
  }
  return Pair.of(astRecord(node), pos);
}
 
示例5
@Override
public Void visitAnnotatedType(AnnotatedTypeTree node, Void unused) {
  sync(node);
  ExpressionTree base = node.getUnderlyingType();
  if (base instanceof MemberSelectTree) {
    MemberSelectTree selectTree = (MemberSelectTree) base;
    scan(selectTree.getExpression(), null);
    token(".");
    visitAnnotations(node.getAnnotations(), BreakOrNot.NO, BreakOrNot.NO);
    builder.breakToFill(" ");
    visit(selectTree.getIdentifier());
  } else if (base instanceof ArrayTypeTree) {
    visitAnnotatedArrayType(node);
  } else {
    visitAnnotations(node.getAnnotations(), BreakOrNot.NO, BreakOrNot.NO);
    builder.breakToFill(" ");
    scan(base, null);
  }
  return null;
}
 
示例6
/**
 * Accumulates a flattened list of array dimensions specifiers with type annotations, and returns
 * the base type.
 *
 * <p>Given {@code int @A @B [][] @C []}, adds {@code [[@A, @B], [@C]]} to dims and returns {@code
 * int}.
 */
private static Tree extractDims(Deque<List<AnnotationTree>> dims, Tree node) {
  switch (node.getKind()) {
    case ARRAY_TYPE:
      return extractDims(dims, ((ArrayTypeTree) node).getType());
    case ANNOTATED_TYPE:
      AnnotatedTypeTree annotatedTypeTree = (AnnotatedTypeTree) node;
      if (annotatedTypeTree.getUnderlyingType().getKind() != Tree.Kind.ARRAY_TYPE) {
        return node;
      }
      node = extractDims(dims, annotatedTypeTree.getUnderlyingType());
      dims.addFirst(ImmutableList.copyOf(annotatedTypeTree.getAnnotations()));
      return node;
    default:
      return node;
  }
}
 
示例7
@Override
public Tree visitArrayType(ArrayTypeTree tree, Void p) {
    ArrayTypeTree n = make.ArrayType(tree.getType());
    model.setType(n, model.getType(tree));
    comments.copyComments(tree, n);
    model.setPos(n, model.getPos(tree));
    return n;
}
 
示例8
public Boolean visitArrayType(ArrayTypeTree node, TreePath p) {
    if (p == null) {
        super.visitArrayType(node, p);
        return false;
    }

    ArrayTypeTree at = (ArrayTypeTree) p.getLeaf();

    return scan(node.getType(), at.getType(), p);
}
 
示例9
private static Tree normalizeLastLeftTree(Tree lastLeft) {
    while (lastLeft != null && lastLeft.getKind() == Kind.ARRAY_TYPE) {
        lastLeft = ((ArrayTypeTree) lastLeft).getType();
    }
    
    return lastLeft;
}
 
示例10
private ReplaceConstructorWithBuilderUI(TreePathHandle constructor, CompilationInfo info) {
    this.refactoring = new ReplaceConstructorWithBuilderRefactoring(constructor);
    ExecutableElement contructorElement = (ExecutableElement) constructor.resolveElement(info);
    this.name = contructorElement.getSimpleName().toString();
    MethodTree constTree = (MethodTree) constructor.resolve(info).getLeaf();
    paramaterNames = new ArrayList<String>();
    parameterTypes = new ArrayList<String>();
    parameterTypeVars = new ArrayList<Boolean>();
    boolean varargs = contructorElement.isVarArgs();
    List<? extends VariableElement> parameterElements = contructorElement.getParameters();
    List<? extends VariableTree> parameters = constTree.getParameters();
    for (int i = 0; i < parameters.size(); i++) {
        VariableTree var = parameters.get(i);
        paramaterNames.add(var.getName().toString());
        String type = contructorElement.getParameters().get(i).asType().toString();
        if(varargs && i+1 == parameters.size()) {
            if(var.getType().getKind() == Tree.Kind.ARRAY_TYPE) {
                ArrayTypeTree att = (ArrayTypeTree) var.getType();
                type = att.getType().toString();
                type += "..."; //NOI18N
            }
        }
        parameterTypes.add(type);
        parameterTypeVars.add(parameterElements.get(i).asType().getKind() == TypeKind.TYPEVAR);
    }
    TypeElement typeEl = (TypeElement) contructorElement.getEnclosingElement();
    if(typeEl.getNestingKind() != NestingKind.TOP_LEVEL) {
        PackageElement packageOf = info.getElements().getPackageOf(typeEl);
        builderFQN = packageOf.toString() + "." + typeEl.getSimpleName().toString();
    } else {
        builderFQN = typeEl.getQualifiedName().toString();
    }
    buildMethodName = "create" + typeEl.getSimpleName();
}
 
示例11
@Override
public Void visitArrayType(ArrayTypeTree tree, List<Node> d) {
    List<Node> below = new ArrayList<Node>();
    
    addCorrespondingElement(below);
    addCorrespondingType(below);
    addCorrespondingComments(below);
    
    super.visitArrayType(tree, below);
    
    d.add(new TreeNode(info, getCurrentPath(), below));
    return null;
}
 
示例12
private ExpressionTree transformInitializer(ExpressionTree initializer, Tree type, TreeMaker make) {
    if(initializer.getKind() == Tree.Kind.NEW_ARRAY) {
        NewArrayTree nat = (NewArrayTree) initializer;
        if(nat.getType() == null) {
            if(type.getKind() == Tree.Kind.ARRAY_TYPE) {
                ArrayTypeTree arrayTypeTree = (ArrayTypeTree) type;
                type = arrayTypeTree.getType();
            }
            return make.NewArray(type, nat.getDimensions(), nat.getInitializers());
        }
    }
    return initializer;
}
 
示例13
private int arrayLevels(Tree node) {
  int result = 0;
  while (node.getKind() == Tree.Kind.ARRAY_TYPE) {
    result++;
    node = ((ArrayTypeTree) node).getType();
  }
  return result;
}
 
示例14
private boolean checkTypePath(int i, Tree typeTree) {
        try {
loop:       while (typeTree != null && i < astPath.size()) {
                ASTPath.ASTEntry entry = astPath.get(i);
                Tree.Kind kind = entry.getTreeKind();
                switch (kind) {
                case ANNOTATED_TYPE:
                    typeTree = ((AnnotatedTypeTree) typeTree)
                        .getUnderlyingType();
                    continue;
                case ARRAY_TYPE:
                    typeTree = ((ArrayTypeTree) typeTree).getType();
                    break;
                case MEMBER_SELECT:
                    typeTree = ((MemberSelectTree) typeTree).getExpression();
                    break;
                case PARAMETERIZED_TYPE:
                    if (entry.childSelectorIs(ASTPath.TYPE_ARGUMENT)) {
                        int arg = entry.getArgument();
                        typeTree = ((ParameterizedTypeTree) typeTree)
                                .getTypeArguments().get(arg);
                    } else {  // TYPE
                        typeTree = ((ParameterizedTypeTree) typeTree).getType();
                    }
                    break;
                default:
                    if (isWildcard(kind)) {
                        return ++i == astPath.size();  // ???
                    }
                    break loop;
                }
                ++i;
            }
        } catch (RuntimeException ex) {}
        return false;
    }
 
示例15
@SuppressWarnings("signature") // com.sun.source.tree.Tree is not yet annotated
private static void treeToJVMLString(Tree typeTree, StringBuilder builder) {
    // FIXME: not robust in presence of comments
    switch (typeTree.getKind()) {
    case ARRAY_TYPE:
        builder.append('[');
        treeToJVMLString(((ArrayTypeTree) typeTree).getType(), builder);
        break;
    default:
        String str = typeTree.toString();
        builder.append("void".equals(str) ? "V"
            : Signatures.binaryNameToFieldDescriptor(typeTree.toString()));
        break;
    }
}
 
示例16
@Override
public Void visitArrayType(ArrayTypeTree expected, Tree actual) {
  Optional<ArrayTypeTree> other = checkTypeAndCast(expected, actual);
  if (!other.isPresent()) {
    addTypeMismatch(expected, actual);
    return null;
  }

  scan(expected.getType(), other.get().getType());
  return null;
}
 
示例17
@Override
public Void visitArrayType(ArrayTypeTree node, Void unused) {
    sync(node);
    visitAnnotatedArrayType(node);
    return null;
}
 
示例18
@Override
public Object visitArrayType(ArrayTypeTree node, Object p) {
    addDependency(node.getType());
    return super.visitArrayType(node, p);
}
 
示例19
public Boolean visitArrayType(ArrayTypeTree node, ConstructorData p) {
    super.visitArrayType(node, p);
    return null;
}
 
示例20
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
}
 
示例21
@Override
public Boolean visitArrayType(ArrayTypeTree node, Void p) {
    return super.visitArrayType(node, p); //To change body of generated methods, choose Tools | Templates.
}
 
示例22
@Override
public List<? extends TypeMirror> visitArrayType(ArrayTypeTree node, Object p) {
    return null;
}
 
示例23
@Override
public Void visitArrayType(ArrayTypeTree node, Void unused) {
    sync(node);
    visitAnnotatedArrayType(node);
    return null;
}
 
示例24
private List<Snippet> processVariables(String userSource, List<? extends Tree> units, String compileSource, ParseTask pt) {
    List<Snippet> snippets = new ArrayList<>();
    TreeDissector dis = TreeDissector.createByFirstClass(pt);
    for (Tree unitTree : units) {
        VariableTree vt = (VariableTree) unitTree;
        String name = vt.getName().toString();
        String typeName = EvalPretty.prettyExpr((JCTree) vt.getType(), false);
        Tree baseType = vt.getType();
        TreeDependencyScanner tds = new TreeDependencyScanner();
        tds.scan(baseType); // Not dependent on initializer
        StringBuilder sbBrackets = new StringBuilder();
        while (baseType instanceof ArrayTypeTree) {
            //TODO handle annotations too
            baseType = ((ArrayTypeTree) baseType).getType();
            sbBrackets.append("[]");
        }
        Range rtype = dis.treeToRange(baseType);
        Range runit = dis.treeToRange(vt);
        runit = new Range(runit.begin, runit.end - 1);
        ExpressionTree it = vt.getInitializer();
        Range rinit = null;
        int nameMax = runit.end - 1;
        SubKind subkind;
        if (it != null) {
            subkind = SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND;
            rinit = dis.treeToRange(it);
            nameMax = rinit.begin - 1;
        } else {
            subkind = SubKind.VAR_DECLARATION_SUBKIND;
        }
        int nameStart = compileSource.lastIndexOf(name, nameMax);
        if (nameStart < 0) {
            throw new AssertionError("Name '" + name + "' not found");
        }
        int nameEnd = nameStart + name.length();
        Range rname = new Range(nameStart, nameEnd);
        Wrap guts = Wrap.varWrap(compileSource, rtype, sbBrackets.toString(), rname, rinit);
        DiagList modDiag = modifierDiagnostics(vt.getModifiers(), dis, true);
        Snippet snip = new VarSnippet(state.keyMap.keyForVariable(name), userSource, guts,
                name, subkind, typeName,
                tds.declareReferences(), modDiag);
        snippets.add(snip);
    }
    return snippets;
}
 
示例25
private ArrayTypeTree largestContainingArray(Tree node) {
  TreePath p = getPath(node);
  Tree result = TreeFinder.largestContainingArray(p).getLeaf();
  assert result.getKind() == Tree.Kind.ARRAY_TYPE;
  return (ArrayTypeTree) result;
}
 
示例26
@Override
public Void visitArrayType(ArrayTypeTree node, Void unused) {
  sync(node);
  visitAnnotatedArrayType(node);
  return null;
}
 
示例27
private TreeNode convertArrayType(ArrayTypeTree node, TreePath parent) {
  TreePath path = getTreePath(parent, node);
  ArrayType newNode = new ArrayType();
  Type componentType = Type.newType(getTypeMirror(getTreePath(path, node.getType())));
  return newNode.setComponentType(componentType).setTypeMirror(getTypeMirror(path));
}
 
示例28
@Override
public JType visitArrayType(ArrayTypeTree at, Void __) {
    return visit(at.getType()).array();
}
 
示例29
@Override
public UArrayTypeTree visitArrayType(ArrayTypeTree tree, Void v) {
  return UArrayTypeTree.create(templateType(tree.getType()));
}
 
示例30
@Override
@Nullable
public Unifier visitArrayType(ArrayTypeTree tree, @Nullable Unifier unifier) {
  return getType().unify(tree.getType(), unifier);
}