Java Code Examples for com.sun.source.tree.Tree.Kind#TRY

The following examples show how to use com.sun.source.tree.Tree.Kind#TRY . 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: MagicSurroundWithTryCatchFix.java    From netbeans with Apache License 2.0 6 votes vote down vote up
static TreePath enclosingTry(TreePath from) {
    TreePath tryPath = from;
    while (tryPath != null
            && tryPath.getLeaf().getKind() != Kind.TRY
            && !TreeUtilities.CLASS_TREE_KINDS.contains(tryPath.getLeaf().getKind())
            && tryPath.getLeaf().getKind() != Kind.CATCH
            && tryPath.getLeaf().getKind() != Kind.LAMBDA_EXPRESSION)
        tryPath = tryPath.getParentPath();

    if (tryPath.getLeaf().getKind() == Kind.TRY) {
        TryTree tt = (TryTree) tryPath.getLeaf();
        //#104085: if the statement to be wrapped is inside a finally block of the try-catch,
        //do not attempt to extend existing catches...:
        for (Tree t : from) {
            if (tt.getFinallyBlock() == t) {
                return null;
            }
        }
        
        return tryPath;
    }
    
    return null;
}
 
Example 2
Source File: ConvertToARM.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static TryTree findNestedARM(
        final Collection<? extends StatementTree> stms,
        final StatementTree var) {
    int state = var != null ? 0 : 1;
    for (StatementTree stm : stms) {
        if (stm == var) {
            state = 1;
        }
        if (state == 1) {
            if (stm.getKind() == Kind.TRY) {
                final TryTree tryTree = (TryTree)stm;
                if (tryTree.getResources() != null && !tryTree.getResources().isEmpty()) {
                    return tryTree;
                } else {
                    final Iterator<? extends StatementTree> blkStms = tryTree.getBlock().getStatements().iterator();
                    if (blkStms.hasNext()) {
                        StatementTree bstm = blkStms.next();
                        if (bstm.getKind() == Kind.TRY) {
                            return (TryTree)bstm;
                        }
                        if (bstm.getKind() == Kind.EXPRESSION_STATEMENT && blkStms.hasNext()) {
                            bstm = blkStms.next();
                            if (bstm.getKind() == Kind.TRY) {
                                return (TryTree)bstm;
                            }
                        }
                    }
                }
            }
            if (stm != var) {
                break;
            }
        }
    }
    return null;
}
 
Example 3
Source File: ConvertToARM.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static TreePath findEnclosingTryPath(final TreePath path) {
    TreePath parent = path.getParentPath();
    if (parent == null || parent.getLeaf().getKind() != Kind.BLOCK) {
        return null;
    }
    parent = parent.getParentPath();
    if (parent == null || parent.getLeaf().getKind() != Kind.TRY) {
        return null;
    }
    return parent;
}
 
Example 4
Source File: FinalizeDoesNotCallSuper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private TryTree soleTryWithoutFinally(BlockTree block) {
    if (block.getStatements().size() != 1) return null;
    StatementTree first = block.getStatements().get(0);
    if (first.getKind() != Kind.TRY) return null;
    TryTree tt = (TryTree) first;
    if (tt.getFinallyBlock() != null) return null;
    return tt;
}
 
Example 5
Source File: UncaughtException.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private List<? extends TypeMirror> findUncaughtExceptions(CompilationInfo info, TreePath path, List<? extends TypeMirror> exceptions) {
    List<TypeMirror> result = new ArrayList<TypeMirror>();
    
    result.addAll(exceptions);
    
    Tree lastTree = null;
    
    while (path != null) {
        Tree currentTree = path.getLeaf();

        if (currentTree.getKind() == Tree.Kind.METHOD) {
            TypeMirror tm = info.getTrees().getTypeMirror(path);
            if (tm != null && tm.getKind() == TypeKind.EXECUTABLE) {
                for (TypeMirror mirr : ((ExecutableType) tm).getThrownTypes()) {
                    for (Iterator<TypeMirror> it = result.iterator(); it.hasNext();)
                        if (info.getTypes().isSameType(it.next(), mirr))
                            it.remove();
                }
                break;
            }
        }            
        
        if (currentTree.getKind() == Tree.Kind.LAMBDA_EXPRESSION) {
            // no checked exceptions can be thrown out of Lambda, #243106
            break;
        }
        
        if (currentTree.getKind() == Kind.TRY) {
            TryTree tt = (TryTree) currentTree;
            
            if (tt.getBlock() == lastTree) {
                for (CatchTree c : tt.getCatches()) {
                    TreePath catchPath = new TreePath(new TreePath(path, c), c.getParameter());
                    VariableElement variable = (VariableElement) info.getTrees().getElement(catchPath);
                    if (variable == null) {
                        continue;
                    }
                    TypeMirror variableType = variable.asType();
                    if (variableType.getKind() == TypeKind.UNION) {
                        result.removeAll(((UnionType)variableType).getAlternatives());
                    } else {
                        result.remove(variableType);
                    }
                }
            }
        }
        
        lastTree = path.getLeaf();
        path = path.getParentPath();
    }
    
    List<TypeMirror> filtered = new ArrayList<>();
    
    OUTER: for (Iterator<TypeMirror> sourceIt = result.iterator(); sourceIt.hasNext(); ) {
        TypeMirror sourceType = sourceIt.next();
        
        for (Iterator<TypeMirror> filteredIt = filtered.iterator(); filteredIt.hasNext(); ) {
            TypeMirror filteredType = filteredIt.next();
            
            if (info.getTypes().isSubtype(sourceType, filteredType)) {
                sourceIt.remove();
                continue OUTER;
            }
            
            if (info.getTypes().isSubtype(filteredType, sourceType)) {
                filteredIt.remove();
                break;
            }
        }
        
        filtered.add(sourceType);
    }
    
    return filtered;
}
 
Example 6
Source File: CreateElementUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static List<? extends TypeMirror> computeAssignment(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    AssignmentTree at = (AssignmentTree) parent.getLeaf();
    TypeMirror     type = null;
    
    types.add(ElementKind.PARAMETER);
    types.add(ElementKind.LOCAL_VARIABLE);
    types.add(ElementKind.FIELD);

    if (at.getVariable() == error) {
        type = info.getTrees().getTypeMirror(new TreePath(parent, at.getExpression()));

        if (type != null) {
            //anonymous class?
            type = org.netbeans.modules.java.hints.errors.Utilities.convertIfAnonymous(type);

            if (type.getKind() == TypeKind.EXECUTABLE) {
                //TODO: does not actualy work, attempt to solve situations like:
                //t = Collections.emptyList()
                //t = Collections.<String>emptyList();
                //see also testCreateFieldMethod1 and testCreateFieldMethod2 tests:
                type = ((ExecutableType) type).getReturnType();
            }
        }

        if (parent.getParentPath() != null && parent.getParentPath().getLeaf().getKind() == Kind.TRY) {
            types.clear();
            types.add(ElementKind.RESOURCE_VARIABLE);
        }
    }
    
    if (at.getExpression() == error) {
        type = info.getTrees().getTypeMirror(new TreePath(parent, at.getVariable()));
    }
    
    //class or field:
    if (type == null) {
        if (ErrorHintsProvider.ERR.isLoggable(ErrorManager.INFORMATIONAL)) {
            ErrorHintsProvider.ERR.log(ErrorManager.INFORMATIONAL, "offset=" + offset);
            ErrorHintsProvider.ERR.log(ErrorManager.INFORMATIONAL, "errorTree=" + error);
            ErrorHintsProvider.ERR.log(ErrorManager.INFORMATIONAL, "type=null");
        }
        
        return null;
    }
    
    return Collections.singletonList(type);
}