@Override
    public Void visitNewArray(NewArrayTree tree, AnnotatedTypeMirror type) {

      List<? extends ExpressionTree> dimensions = tree.getDimensions();
      List<? extends ExpressionTree> initializers = tree.getInitializers();

      // Dimensions provided
      if (!dimensions.isEmpty()) {
        handleDimensions(dimensions, (AnnotatedArrayType) type);
      } else {
        // Initializer used
        handleInitalizers(initializers, (AnnotatedArrayType) type);

        AnnotationMirror newQual;
        Class<?> clazz = ValueCheckerUtils.getClassFromType(type.getUnderlyingType());
        String stringVal = null;
        if (clazz.equals(byte[].class)) {
          stringVal = getByteArrayStringVal(initializers);
        } else if (clazz.equals(char[].class)) {
          stringVal = getCharArrayStringVal(initializers);
        }

        if (stringVal != null) {
          newQual = createStringAnnotation(Collections.singletonList(stringVal));
          type.replaceAnnotation(newQual);
        }
      }

      return null;
    }
Пример #2
0
  private boolean isNewArrayInToArray(NewArrayTree node) {
    if (node.getDimensions().size() != 1) {
      return false;
    }

    ExpressionTree dim = node.getDimensions().get(0);
    ProcessingEnvironment env = checker.getProcessingEnvironment();

    if (!TreeUtils.isMethodInvocation(dim, collectionSize, env)) {
      return false;
    }

    ExpressionTree rcvsize = ((MethodInvocationTree) dim).getMethodSelect();
    if (!(rcvsize instanceof MemberSelectTree)) {
      return false;
    }
    rcvsize = ((MemberSelectTree) rcvsize).getExpression();
    if (!(rcvsize instanceof IdentifierTree)) {
      return false;
    }

    Tree encl = getCurrentPath().getParentPath().getLeaf();

    if (!TreeUtils.isMethodInvocation(encl, collectionToArray, env)) {
      return false;
    }

    ExpressionTree rcvtoarray = ((MethodInvocationTree) encl).getMethodSelect();
    if (!(rcvtoarray instanceof MemberSelectTree)) {
      return false;
    }
    rcvtoarray = ((MemberSelectTree) rcvtoarray).getExpression();
    if (!(rcvtoarray instanceof IdentifierTree)) {
      return false;
    }

    return ((IdentifierTree) rcvsize).getName() == ((IdentifierTree) rcvtoarray).getName();
  }
Пример #3
0
 /**
  * Determine whether all dimensions given in a new array expression have zero as length. For
  * example "new Object[0][0];". Also true for empty dimensions, as in "new Object[] {...}".
  */
 private static boolean isNewArrayAllZeroDims(NewArrayTree node) {
   boolean isAllZeros = true;
   for (ExpressionTree dim : node.getDimensions()) {
     if (dim instanceof LiteralTree) {
       Object val = ((LiteralTree) dim).getValue();
       if (!(val instanceof Number) || !(new Integer(0).equals(val))) {
         isAllZeros = false;
         break;
       }
     } else {
       isAllZeros = false;
       break;
     }
   }
   return isAllZeros;
 }
  private static List<? extends TypeMirror> computeNewArray(
      Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    NewArrayTree nat = (NewArrayTree) parent.getLeaf();

    if (nat.getType() == error) {
      types.add(ElementKind.CLASS);
      types.add(ElementKind.ENUM);
      types.add(ElementKind.INTERFACE);

      return null;
    }

    for (Tree dimension : nat.getDimensions()) {
      if (dimension == error) {
        types.add(ElementKind.PARAMETER);
        types.add(ElementKind.LOCAL_VARIABLE);
        types.add(ElementKind.FIELD);

        return Collections.singletonList(info.getTypes().getPrimitiveType(TypeKind.INT));
      }
    }

    for (Tree init : nat.getInitializers()) {
      if (init == error) {
        TypeMirror whole = info.getTrees().getTypeMirror(parent);

        if (whole == null || whole.getKind() != TypeKind.ARRAY) return null;

        types.add(ElementKind.PARAMETER);
        types.add(ElementKind.LOCAL_VARIABLE);
        types.add(ElementKind.FIELD);

        return Collections.singletonList(((ArrayType) whole).getComponentType());
      }
    }

    return null;
  }