use of com.sun.tools.javac.code.Type.UnionClassType in project ceylon-compiler by ceylon.
the class Attr method visitTypeUnion.
public void visitTypeUnion(JCTypeUnion tree) {
ListBuffer<Type> multicatchTypes = ListBuffer.lb();
// lazy, only if needed
ListBuffer<Type> all_multicatchTypes = null;
for (JCExpression typeTree : tree.alternatives) {
Type ctype = attribType(typeTree, env);
ctype = chk.checkType(typeTree.pos(), chk.checkClassType(typeTree.pos(), ctype), syms.throwableType);
if (!ctype.isErroneous()) {
// unrelated w.r.t. subtyping
if (chk.intersects(ctype, multicatchTypes.toList())) {
for (Type t : multicatchTypes) {
boolean sub = types.isSubtype(ctype, t);
boolean sup = types.isSubtype(t, ctype);
if (sub || sup) {
// assume 'a' <: 'b'
Type a = sub ? ctype : t;
Type b = sub ? t : ctype;
log.error(typeTree.pos(), "multicatch.types.must.be.disjoint", a, b);
}
}
}
multicatchTypes.append(ctype);
if (all_multicatchTypes != null)
all_multicatchTypes.append(ctype);
} else {
if (all_multicatchTypes == null) {
all_multicatchTypes = ListBuffer.lb();
all_multicatchTypes.appendList(multicatchTypes);
}
all_multicatchTypes.append(ctype);
}
}
Type t = check(tree, types.lub(multicatchTypes.toList()), TYP, pkind, pt);
if (t.tag == CLASS) {
List<Type> alternatives = ((all_multicatchTypes == null) ? multicatchTypes : all_multicatchTypes).toList();
t = new UnionClassType((ClassType) t, alternatives);
}
tree.type = result = t;
}
use of com.sun.tools.javac.code.Type.UnionClassType in project error-prone by google.
the class CatchFail method deleteFix.
// Extract the argument to a call to assertWithMessage, e.g. in:
// assertWithMessage("message").fail();
Optional<Fix> deleteFix(TryTree tree, ImmutableList<CatchTree> catchBlocks, VisitorState state) {
SuggestedFix.Builder fix = SuggestedFix.builder();
if (tree.getFinallyBlock() != null || catchBlocks.size() < tree.getCatches().size()) {
// If the try statement has a finally region, or other catch blocks, delete only the
// unnecessary blocks.
catchBlocks.stream().forEachOrdered(fix::delete);
} else {
// The try statement has no finally region and all catch blocks are unnecessary. Replace it
// with the try statements, deleting all catches.
List<? extends StatementTree> tryStatements = tree.getBlock().getStatements();
String source = state.getSourceCode().toString();
// Replace the full region to work around a GJF partial formatting bug that prevents it from
// re-indenting unchanged lines. This means that fixes may overlap, but that's (hopefully)
// unlikely.
// TODO(b/24140798): emit more precise replacements if GJF is fixed
fix.replace(tree, source.substring(((JCTree) tryStatements.get(0)).getStartPosition(), state.getEndPosition(Iterables.getLast(tryStatements))));
}
MethodTree enclosing = findEnclosing(state.getPath());
if (enclosing == null) {
// There isn't an enclosing method, possibly because we're in a lambda or initializer block.
return Optional.empty();
}
if (isExpectedExceptionTest(ASTHelpers.getSymbol(enclosing), state)) {
// tests, so don't use that fix for methods annotated with @Test(expected=...).
return Optional.empty();
}
// Fix up the enclosing method's throws declaration to include the new thrown exception types.
Collection<Type> thrownTypes = ASTHelpers.getSymbol(enclosing).getThrownTypes();
Types types = state.getTypes();
// Find all types in the deleted catch blocks that are not already in the throws declaration.
ImmutableList<Type> toThrow = catchBlocks.stream().map(c -> ASTHelpers.getType(c.getParameter())).flatMap(t -> t instanceof UnionClassType ? ImmutableList.copyOf(((UnionClassType) t).getAlternativeTypes()).stream() : Stream.of(t)).filter(t -> thrownTypes.stream().noneMatch(x -> types.isAssignable(t, x))).collect(toImmutableList());
if (!toThrow.isEmpty()) {
if (!TEST_CASE.matches(enclosing, state)) {
// not be a safe local refactoring.
return Optional.empty();
}
String throwsString = toThrow.stream().map(t -> SuggestedFixes.qualifyType(state, fix, t)).distinct().collect(joining(", "));
if (enclosing.getThrows().isEmpty()) {
// Add a new throws declaration.
fix.prefixWith(enclosing.getBody(), "throws " + throwsString);
} else {
// Append to an existing throws declaration.
fix.postfixWith(Iterables.getLast(enclosing.getThrows()), ", " + throwsString);
}
}
return Optional.of(fix.build());
}
use of com.sun.tools.javac.code.Type.UnionClassType in project ceylon-compiler by ceylon.
the class JavacTrees method getLub.
@Override
public TypeMirror getLub(CatchTree tree) {
JCCatch ct = (JCCatch) tree;
JCVariableDecl v = ct.param;
if (v.type != null && v.type.getKind() == TypeKind.UNION) {
UnionClassType ut = (UnionClassType) v.type;
return ut.getLub();
} else {
return v.type;
}
}
Aggregations