コード例 #1
0
    @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 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;
  }