⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nestabledelegate.java

📁 人力资源管理系统主要包括:人员管理、招聘管理、培训管理、奖惩管理和薪金管理五大管理模块。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

    /**
     * Returns the <code>Throwable</code> in the chain of
     * <code>Throwable</code>s at the specified index, numbererd from 0.
     *
     * @param index the index, numbered from 0, of the <code>Throwable</code> in
     * the chain of <code>Throwable</code>s
     * @return the <code>Throwable</code>
     * @throws IndexOutOfBoundsException if the <code>index</code> argument is
     * negative or not less than the count of <code>Throwable</code>s in the
     * chain
     * @since 2.0
     */
    public Throwable getThrowable(int index) {
        if (index == 0) {
            return this.nestable;
        }
        Throwable[] throwables = this.getThrowables();
        return throwables[index];
    }

    /**
     * Returns the number of <code>Throwable</code>s contained in the
     * <code>Nestable</code> contained by this delegate.
     *
     * @return the throwable count
     * @since 2.0
     */
    public int getThrowableCount() {
        return ExceptionUtils.getThrowableCount(this.nestable);
    }

    /**
     * Returns this delegate's <code>Nestable</code> and any nested
     * <code>Throwable</code>s in an array of <code>Throwable</code>s, one
     * element for each <code>Throwable</code>.
     *
     * @return the <code>Throwable</code>s
     * @since 2.0
     */
    public Throwable[] getThrowables() {
        return ExceptionUtils.getThrowables(this.nestable);
    }

    /**
     * Returns the index, numbered from 0, of the first <code>Throwable</code>
     * that matches the specified type in the chain of <code>Throwable</code>s
     * held in this delegate's <code>Nestable</code> with an index greater than
     * or equal to the specified index, or -1 if the type is not found.
     *
     * @param type <code>Class</code> to be found
     * @param fromIndex the index, numbered from 0, of the starting position in
     * the chain to be searched
     * @return index of the first occurrence of the type in the chain, or -1 if
     * the type is not found
     * @throws IndexOutOfBoundsException if the <code>fromIndex</code> argument
     * is negative or not less than the count of <code>Throwable</code>s in the
     * chain
     * @since 2.0
     */
    public int indexOfThrowable(Class type, int fromIndex) {
        if (fromIndex < 0) {
            throw new IndexOutOfBoundsException("The start index was out of bounds: " + fromIndex);
        }
        Throwable[] throwables = ExceptionUtils.getThrowables(this.nestable);
        if (fromIndex >= throwables.length) {
            throw new IndexOutOfBoundsException("The start index was out of bounds: "
                + fromIndex + " >= " + throwables.length);
        }
        for (int i = fromIndex; i < throwables.length; i++) {
            if ( throwables[i].getClass().equals(type) ) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Prints the stack trace of this exception the the standar error
     * stream.
     */
    public void printStackTrace() {
        printStackTrace(System.err);
    }

    /**
     * Prints the stack trace of this exception to the specified
     * stream.
     *
     * @param out <code>PrintStream</code> to use for output.
     * @see #printStackTrace(PrintWriter)
     */
    public void printStackTrace(PrintStream out) {
        synchronized (out) {
            PrintWriter pw = new PrintWriter(out, false);
            printStackTrace(pw);
            // Flush the PrintWriter before it's GC'ed.
            pw.flush();
        }
    }

    /**
     * Prints the stack trace of this exception to the specified
     * writer. If the Throwable class has a <code>getCause</code>
     * method (i.e. running on jre1.4 or higher), this method just 
     * uses Throwable's printStackTrace() method. Otherwise, generates
     * the stack-trace, by taking into account the 'topDown' and 
     * 'trimStackFrames' parameters. The topDown and trimStackFrames 
     * are set to 'true' by default (produces jre1.4-like stack trace).
     *
     * @param out <code>PrintWriter</code> to use for output.
     */
    public void printStackTrace(PrintWriter out) {
        Throwable throwable = this.nestable;
        // if running on jre1.4 or higher, use default printStackTrace
        if ( ExceptionUtils.isThrowableNested() ) {
            if (throwable instanceof Nestable) {
                ( (Nestable)throwable ).printPartialStackTrace(out);
            } 
            else {
                throwable.printStackTrace(out);
            }
            return;
        }

        // generating the nested stack trace
        List stacks = new ArrayList();
        while (throwable != null) {
            String[] st = getStackFrames(throwable);
            stacks.add(st);
            throwable = ExceptionUtils.getCause(throwable);
        }

        // If NOT topDown, reverse the stack
        String separatorLine = "Caused by: ";
        if (!topDown) {
            separatorLine = "Rethrown as: ";
            Collections.reverse(stacks);
        }

        // Remove the repeated lines in the stack
        if (trimStackFrames) trimStackFrames(stacks);

        synchronized (out) {
            for (Iterator iter=stacks.iterator(); iter.hasNext();) {
                String[] st = (String[]) iter.next();
                for (int i=0, len=st.length; i < len; i++) {
                    out.println(st[i]);
                }
                if ( iter.hasNext() ) out.print(separatorLine);
            }
        }
    }

    /**
     * Captures the stack trace associated with the specified
     * <code>Throwable</code> object, decomposing it into a list of
     * stack frames.
     *
     * @param t The <code>Throwable</code>.
     * @return  An array of strings describing each stack frame.
     * @since 2.0
     */
    protected String[] getStackFrames(Throwable t) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);

        // Avoid infinite loop between decompose() and printStackTrace().
        if (t instanceof Nestable) {
            ( (Nestable) t ).printPartialStackTrace(pw);
        } 
        else {
            t.printStackTrace(pw);
        }
        return ExceptionUtils.getStackFrames( sw.getBuffer().toString() );
    }
    
    /**
     * Trims the stack frames. The first set is left untouched. The rest
     * of the frames are truncated from the bottom by comparing with
     * one just on top.
     *
     * @param stacks The list containing String[] elements
     * @since 2.0
     */
    protected void trimStackFrames(List stacks) {
         for (int size=stacks.size(), i=size-1; i > 0; i--) {
             String[] curr = (String[]) stacks.get(i);
             String[] next = (String[]) stacks.get(i-1); 
             
             List currList = new ArrayList( Arrays.asList(curr) );
             List nextList = new ArrayList( Arrays.asList(next) );
             ExceptionUtils.removeCommonFrames(currList, nextList);

             int trimmed = curr.length - currList.size();
             if (trimmed > 0) {
                 currList.add("\t... "+trimmed+" more");
                 stacks.set(
                     i, 
                     currList.toArray(new String[currList.size()])
                 );
             }
         }
     }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -