View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache license, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License. You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the license for the specific language governing permissions and
    15   * limitations under the license.
    16   */
    17  package org.apache.logging.log4j.core.impl;
    18
    19  import java.io.Serializable;
    20
    21  /**
    22   * Wraps and extends the concept of the JRE's final class {@link StackTraceElement} by adding more location information.
    23   * <p>
    24   * Complements a StackTraceElement with:
    25   * </p>
    26   * <ul>
    27   * <li>exact: whether the class was obtained via {@link sun.reflect.Reflection#getCallerClass(int)}</li>
    28   * <li>location: a classpath element or a jar</li>
    29   * <li>version</li>
    30   * </ul>
    31   */
    32  public final class ExtendedStackTraceElement implements Serializable {
    33
    34      private static final long serialVersionUID = -2171069569241280505L;
    35
    36      private final ExtendedClassInfo extraClassInfo;
    37
    38      private final StackTraceElement stackTraceElement;
    39
    40      public ExtendedStackTraceElement(final StackTraceElement stackTraceElement, final ExtendedClassInfo extraClassInfo) {
    41          this.stackTraceElement = stackTraceElement;
    42          this.extraClassInfo = extraClassInfo;
    43      }
    44
    45      /**
    46       * Called from Jackson for XML and JSON IO.
    47       */
    48      public ExtendedStackTraceElement(final String declaringClass, final String methodName, final String fileName,
    49              final int lineNumber, final boolean exact, final String location, final String version) {
    50          this(new StackTraceElement(declaringClass, methodName, fileName, lineNumber), new ExtendedClassInfo(exact,
    51                  location, version));
    52      }
    53
    54      @Override
    55      public boolean equals(final Object obj) {
    56          if (this == obj) {
    57              return true;
    58          }
    59          if (obj == null) {
    60              return false;
    61          }
    62          if (!(obj instanceof ExtendedStackTraceElement)) {
    63              return false;
    64          }
    65          final ExtendedStackTraceElement other = (ExtendedStackTraceElement) obj;
    66          if (this.extraClassInfo == null) {
    67              if (other.extraClassInfo != null) {
    68                  return false;
    69              }
    70          } else if (!this.extraClassInfo.equals(other.extraClassInfo)) {
    71              return false;
    72          }
    73          if (this.stackTraceElement == null) {
    74              if (other.stackTraceElement != null) {
    75                  return false;
    76              }
    77          } else if (!this.stackTraceElement.equals(other.stackTraceElement)) {
    78              return false;
    79          }
    80          return true;
    81      }
    82
    83      public String getClassName() {
    84          return this.stackTraceElement.getClassName();
    85      }
    86
    87      public boolean getExact() {
    88          return this.extraClassInfo.getExact();
    89      }
    90
    91      public ExtendedClassInfo getExtraClassInfo() {
    92          return this.extraClassInfo;
    93      }
    94
    95      public String getFileName() {
    96          return this.stackTraceElement.getFileName();
    97      }
    98
    99      public int getLineNumber() {
    100         return this.stackTraceElement.getLineNumber();
    101     }
    102
    103     public String getLocation() {
    104         return this.extraClassInfo.getLocation();
    105     }
    106
    107     public String getMethodName() {
    108         return this.stackTraceElement.getMethodName();
    109     }
    110
    111     public StackTraceElement getStackTraceElement() {
    112         return this.stackTraceElement;
    113     }
    114
    115     public String getVersion() {
    116         return this.extraClassInfo.getVersion();
    117     }
    118
    119     @Override
    120     public int hashCode() {
    121         final int prime = 31;
    122         int result = 1;
    123         result = prime * result + ((this.extraClassInfo == null) ? 0 : this.extraClassInfo.hashCode());
    124         result = prime * result + ((this.stackTraceElement == null) ? 0 : this.stackTraceElement.hashCode());
    125         return result;
    126     }
    127
    128     public boolean isNativeMethod() {
    129         return this.stackTraceElement.isNativeMethod();
    130     }
    131
    132     @Override
    133     public String toString() {
    134         final StringBuilder sb = new StringBuilder();
    135         sb.append(this.stackTraceElement);
    136         sb.append(" ");
    137         sb.append(this.extraClassInfo);
    138         return sb.toString();
    139     }
    140 }