本文整理汇总了Java中com.sun.tools.javac.code.Type.UnionClassType类的典型用法代码示例。如果您正苦于以下问题:Java UnionClassType类的具体用法?Java UnionClassType怎么用?Java UnionClassType使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
UnionClassType类属于com.sun.tools.javac.code.Type包,在下文中一共展示了UnionClassType类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: tryCatchesException
import com.sun.tools.javac.code.Type.UnionClassType; //导入依赖的package包/类
/** Return whether the given try-tree will catch the given exception type. */
private boolean tryCatchesException(TryTree tryTree, Type exceptionToCatch, VisitorState state) {
Types types = state.getTypes();
return tryTree
.getCatches()
.stream()
.anyMatch(
(CatchTree catchClause) -> {
Type catchesException = getType(catchClause.getParameter().getType());
// Examine all alternative types of a union type.
if (catchesException != null && catchesException.isUnion()) {
return Streams.stream(((UnionClassType) catchesException).getAlternativeTypes())
.anyMatch(caught -> types.isSuperType(caught, exceptionToCatch));
}
// Simple type, just check superclass.
return types.isSuperType(catchesException, exceptionToCatch);
});
}
示例2: getLub
import com.sun.tools.javac.code.Type.UnionClassType; //导入依赖的package包/类
@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;
}
}
示例3: getLub
import com.sun.tools.javac.code.Type.UnionClassType; //导入依赖的package包/类
@Override @DefinedBy(Api.COMPILER_TREE)
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;
}
}
示例4: catchesType
import com.sun.tools.javac.code.Type.UnionClassType; //导入依赖的package包/类
private static boolean catchesType(
JCTry tryStatement, Type assertionErrorType, VisitorState state) {
return tryStatement
.getCatches()
.stream()
.map(catchTree -> ASTHelpers.getType(catchTree.getParameter()))
.flatMap(
type ->
type.isUnion()
? Streams.stream(((UnionClassType) type).getAlternativeTypes())
: Stream.of(type))
.anyMatch(caught -> isSubtype(assertionErrorType, caught, state));
}
示例5: deleteFix
import com.sun.tools.javac.code.Type.UnionClassType; //导入依赖的package包/类
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)) {
// Replacing the original exception with fail() may break badly-structured expected-exception
// 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()))
// convert multi-catch to a list of component types
.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)) {
// Don't add throws declarations to methods that don't look like test cases, since it may
// 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());
}