View Javadoc

    1   /*
    2    * Copyright 2009 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * License.  You may obtain a copy of the License at:
    7    *
    8    *    http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16  package org.jboss.netty.util.internal;
    17
    18  import java.util.ArrayList;
    19  import java.util.List;
    20  import java.util.regex.Pattern;
    21
    22  import org.jboss.netty.channel.DefaultChannelPipeline;
    23  import org.jboss.netty.channel.SimpleChannelHandler;
    24  import org.jboss.netty.channel.StaticChannelPipeline;
    25  import org.jboss.netty.util.DebugUtil;
    26  import org.jboss.netty.util.ThreadRenamingRunnable;
    27
    28  /**
    29   * Simplifies an exception stack trace by removing unnecessary
    30   * {@link StackTraceElement}s.  Please note that the stack trace simplification
    31   * is disabled if {@linkplain DebugUtil debug mode} is turned on.
    32   *
    33   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    34   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    35   *
    36   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    37   *
    38   */
    39  public class StackTraceSimplifier {
    40
    41      private static final boolean SIMPLIFY_STACK_TRACE = !DebugUtil.isDebugEnabled();
    42      private static final Pattern EXCLUDED_STACK_TRACE =
    43          Pattern.compile(
    44                  "^org\\.jboss\\.netty\\." +
    45                  "(util\\.(ThreadRenamingRunnable)" +
    46                  "|channel\\.(SimpleChannel(Upstream|Downstream)?Handler|(Default|Static)ChannelPipeline.*))$");
    47
    48      /**
    49       * Removes unnecessary {@link StackTraceElement}s from the specified
    50       * exception. {@link ThreadRenamingRunnable}, {@link SimpleChannelHandler},
    51       * {@link DefaultChannelPipeline}, and {@link StaticChannelPipeline}
    52       * will be dropped from the trace.
    53       */
    54      public static void simplify(Throwable e) {
    55          if (!SIMPLIFY_STACK_TRACE) {
    56              return;
    57          }
    58
    59          if (e.getCause() != null) {
    60              simplify(e.getCause());
    61          }
    62
    63          StackTraceElement[] trace = e.getStackTrace();
    64          if (trace == null || trace.length == 0) {
    65              return;
    66          }
    67
    68          // Perhaps Netty bug.  Let us not strip things out.
    69          if (EXCLUDED_STACK_TRACE.matcher(trace[0].getClassName()).matches()) {
    70              return;
    71          }
    72
    73          List<StackTraceElement> simpleTrace =
    74              new ArrayList<StackTraceElement>(trace.length);
    75
    76          simpleTrace.add(trace[0]);
    77
    78          // Remove unnecessary stack trace elements.
    79          for (int i = 1; i < trace.length; i ++) {
    80              if (EXCLUDED_STACK_TRACE.matcher(trace[i].getClassName()).matches()) {
    81                  continue;
    82              }
    83              simpleTrace.add(trace[i]);
    84          }
    85
    86          e.setStackTrace(
    87                  simpleTrace.toArray(new StackTraceElement[simpleTrace.size()]));
    88      }
    89  }