Java Code Examples for com.sun.source.tree.UnionTypeTree#getTypeAlternatives()

The following examples show how to use com.sun.source.tree.UnionTypeTree#getTypeAlternatives() . 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: ExtraCatch.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void removeAlternativeFromMultiCatch(TransformationContext ctx) throws Exception {
    TreePath unionPath = ctx.getPath().getParentPath();
    UnionTypeTree union = (UnionTypeTree)unionPath.getLeaf();
    TreeMaker mk = ctx.getWorkingCopy().getTreeMaker();
    GeneratorUtilities gen = GeneratorUtilities.get(ctx.getWorkingCopy());
    union = gen.importComments(union, ctx.getWorkingCopy().getCompilationUnit());
    List<? extends Tree> alts = new ArrayList<>(union.getTypeAlternatives());
    alts.remove(ctx.getPath().getLeaf());
    if (alts.size() > 1) {
        // still remains a multi-catch
        
        Tree newUnion = mk.UnionType(alts);
        ctx.getWorkingCopy().rewrite(union, newUnion);
    } else {
        // replace union type with just ordinary type
        ctx.getWorkingCopy().rewrite(union, alts.get(0));
    }
}
 
Example 2
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Formats a union type declaration in a catch clause.
 */
private void visitUnionType(VariableTree declaration) {
    UnionTypeTree type = (UnionTypeTree) declaration.getType();
    builder.open(ZERO);
    sync(declaration);
    visitAndBreakModifiers(
            declaration.getModifiers(), Direction.HORIZONTAL, Optional.<BreakTag>absent());
    List<? extends Tree> union = type.getTypeAlternatives();
    boolean first = true;
    for (int i = 0; i < union.size() - 1; i++) {
        if (!first) {
            builder.breakOp(" ");
            token("|");
            builder.space();
        } else {
            first = false;
        }
        scan(union.get(i), null);
    }
    builder.breakOp(" ");
    token("|");
    builder.space();
    Tree last = union.get(union.size() - 1);
    declareOne(
            DeclarationKind.NONE,
            Direction.HORIZONTAL,
            Optional.<ModifiersTree>absent(),
            last,
            VarArgsOrNot.NO, // VarArgsOrNot.valueOf(declaration.isVarargs()),
            ImmutableList.<AnnotationTree>of(), // declaration.varargsAnnotations(),
            declaration.getName(),
            "",
            // declaration.extraDimensions(),
            "=",
            Optional.fromNullable(declaration.getInitializer()),
            Optional.<String>absent(),
            Optional.<ExpressionTree>absent(),
            Optional.<TypeWithDims>absent());
    builder.close();
}
 
Example 3
Source File: DependencyCollector.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Object visitUnionType(UnionTypeTree node, Object p) {
    for (Tree t : node.getTypeAlternatives()) {
        addDependency(info.getTrees().getTypeMirror(new TreePath(getCurrentPath(), t)));
    }
    return super.visitUnionType(node, p);
}
 
Example 4
Source File: JavaInputAstVisitor.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Formats a union type declaration in a catch clause.
 */
private void visitUnionType(VariableTree declaration) {
    UnionTypeTree type = (UnionTypeTree) declaration.getType();
    builder.open(ZERO);
    sync(declaration);
    visitAndBreakModifiers(
            declaration.getModifiers(), Direction.HORIZONTAL, Optional.<BreakTag>absent());
    List<? extends Tree> union = type.getTypeAlternatives();
    boolean first = true;
    for (int i = 0; i < union.size() - 1; i++) {
        if (!first) {
            builder.breakOp(" ");
            token("|");
            builder.space();
        } else {
            first = false;
        }
        scan(union.get(i), null);
    }
    builder.breakOp(" ");
    token("|");
    builder.space();
    Tree last = union.get(union.size() - 1);
    declareOne(
            DeclarationKind.NONE,
            Direction.HORIZONTAL,
            Optional.<ModifiersTree>absent(),
            last,
            VarArgsOrNot.NO, // VarArgsOrNot.valueOf(declaration.isVarargs()),
            ImmutableList.<AnnotationTree>of(), // declaration.varargsAnnotations(),
            declaration.getName(),
            "",
            // declaration.extraDimensions(),
            "=",
            Optional.fromNullable(declaration.getInitializer()),
            Optional.<String>absent(),
            Optional.<ExpressionTree>absent(),
            Optional.<TypeWithDims>absent());
    builder.close();
}
 
Example 5
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
/** Formats a union type declaration in a catch clause. */
private void visitUnionType(VariableTree declaration) {
  UnionTypeTree type = (UnionTypeTree) declaration.getType();
  builder.open(ZERO);
  sync(declaration);
  visitAndBreakModifiers(
      declaration.getModifiers(),
      Direction.HORIZONTAL,
      /* declarationAnnotationBreak= */ Optional.empty());
  List<? extends Tree> union = type.getTypeAlternatives();
  boolean first = true;
  for (int i = 0; i < union.size() - 1; i++) {
    if (!first) {
      builder.breakOp(" ");
      token("|");
      builder.space();
    } else {
      first = false;
    }
    scan(union.get(i), null);
  }
  builder.breakOp(" ");
  token("|");
  builder.space();
  Tree last = union.get(union.size() - 1);
  declareOne(
      DeclarationKind.NONE,
      Direction.HORIZONTAL,
      /* modifiers= */ Optional.empty(),
      last,
      /* name= */ declaration.getName(),
      /* op= */ "",
      "=",
      Optional.ofNullable(declaration.getInitializer()),
      /* trailing= */ Optional.empty(),
      /* receiverExpression= */ Optional.empty(),
      /* typeWithDims= */ Optional.empty());
  builder.close();
}