Skip to content

Commit

Permalink
Improve handling of union types
Browse files Browse the repository at this point in the history
* don't break after `} catch (`
* unify breaks between union type elements
* associate the identifier with the last union type element
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=115271047
  • Loading branch information
cushon committed Feb 23, 2016
1 parent 5ddd761 commit bccce1a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 24 deletions.
Expand Up @@ -2340,17 +2340,62 @@ private void visitCatchClause(CatchClause node, AllowTrailingBlankLine allowTrai
builder.space();
token("(");
builder.open(plusFour);
builder.breakOp();
builder.open(ZERO);
visit(node.getException());
builder.close();
SingleVariableDeclaration ex = node.getException();
if (ex.getType().getNodeType() == ASTNode.UNION_TYPE) {
builder.open(ZERO);
visitUnionType(ex);
builder.close();
} else {
builder.breakToFill();
builder.open(ZERO);
visit(ex);
builder.close();
}
builder.close();
token(")");
builder.space();
visitBlock(
node.getBody(), CollapseEmptyOrNot.NO, AllowLeadingBlankLine.YES, allowTrailingBlankLine);
}

/** Formats a union type declaration in a catch clause. */
private void visitUnionType(SingleVariableDeclaration declaration) {
UnionType type = (UnionType) declaration.getType();
builder.open(ZERO);
sync(declaration);
List<Type> union = type.types();
boolean first = true;
for (int i = 0; i < union.size() - 1; i++) {
if (!first) {
builder.breakOp(" ");
token("|");
builder.space();
} else {
first = false;
}
union.get(i).accept(this);
}
builder.breakOp(" ");
token("|");
builder.space();
Type last = union.get(union.size() - 1);
declareOne(
declaration,
Direction.HORIZONTAL,
declaration.modifiers(),
last,
VarArgsOrNot.valueOf(declaration.isVarargs()),
declaration.varargsAnnotations(),
declaration.getName(),
"",
declaration.extraDimensions(),
"=",
Optional.fromNullable(declaration.getInitializer()),
Optional.<String>absent(),
ReceiverParameter.NO);
builder.close();
}

/**
* Helper method for {@link InfixExpression}s. Visit this {@link Expression} node, and its
* children, as long as they are {@link InfixExpression} nodes of the same precedence. Accumulate
Expand Down
Expand Up @@ -2,10 +2,13 @@ class B22169269 {
{
try {
} catch (
ClassNotFoundException
| NoSuchMethodException
ClassNotFoundException
| NoSuchMethodException
| InvocationTargetException
| IllegalAccessException e) {
}
try {
} catch (ClassNotFoundException | NoSuchMethodException e) {
}
}
}
@@ -1,12 +1,13 @@
class B22169269 {
{
try {
} catch (
ClassNotFoundException
| NoSuchMethodException
| InvocationTargetException
| IllegalAccessException
e) {
} catch (ClassNotFoundException
| NoSuchMethodException
| InvocationTargetException
| IllegalAccessException e) {
}
try {
} catch (ClassNotFoundException | NoSuchMethodException e) {
}
}
}
Expand Up @@ -38,18 +38,16 @@ class U {
default:
throw new Exception9();
}
} catch (
Exception0
| Exception1
| Exception2
| Exception3
| Exception4
| Exception5
| Exception6
| Exception7
| Exception8
| Exception9
e) {
} catch (Exception0
| Exception1
| Exception2
| Exception3
| Exception4
| Exception5
| Exception6
| Exception7
| Exception8
| Exception9 e) {
}
}
}

0 comments on commit bccce1a

Please sign in to comment.