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  import org.apache.logging.log4j.core.pattern.PlainTextRenderer;
22  import org.apache.logging.log4j.core.pattern.TextRenderer;
23  
24  /**
25   * Wraps and extends the concept of the JRE's final class {@link StackTraceElement} by adding more location information.
26   * <p>
27   * Complements a StackTraceElement with:
28   * </p>
29   * <ul>
30   * <li>exact: whether the class was obtained via {@link sun.reflect.Reflection#getCallerClass(int)}</li>
31   * <li>location: a classpath element or a jar</li>
32   * <li>version</li>
33   * </ul>
34   */
35  public final class ExtendedStackTraceElement implements Serializable {
36  
37      private static final long serialVersionUID = -2171069569241280505L;
38  
39      private final ExtendedClassInfo extraClassInfo;
40  
41      private final StackTraceElement stackTraceElement;
42  
43      public ExtendedStackTraceElement(final StackTraceElement stackTraceElement,
44              final ExtendedClassInfo extraClassInfo) {
45          this.stackTraceElement = stackTraceElement;
46          this.extraClassInfo = extraClassInfo;
47      }
48  
49      /**
50       * Called from Jackson for XML and JSON IO.
51       */
52      public ExtendedStackTraceElement(final String declaringClass, final String methodName, final String fileName,
53              final int lineNumber, final boolean exact, final String location, final String version) {
54          this(new StackTraceElement(declaringClass, methodName, fileName, lineNumber),
55                  new ExtendedClassInfo(exact, location, version));
56      }
57  
58      @Override
59      public boolean equals(final Object obj) {
60          if (this == obj) {
61              return true;
62          }
63          if (obj == null) {
64              return false;
65          }
66          if (!(obj instanceof ExtendedStackTraceElement)) {
67              return false;
68          }
69          final ExtendedStackTraceElement other = (ExtendedStackTraceElement) obj;
70          if (this.extraClassInfo == null) {
71              if (other.extraClassInfo != null) {
72                  return false;
73              }
74          } else if (!this.extraClassInfo.equals(other.extraClassInfo)) {
75              return false;
76          }
77          if (this.stackTraceElement == null) {
78              if (other.stackTraceElement != null) {
79                  return false;
80              }
81          } else if (!this.stackTraceElement.equals(other.stackTraceElement)) {
82              return false;
83          }
84          return true;
85      }
86  
87      public String getClassName() {
88          return this.stackTraceElement.getClassName();
89      }
90  
91      public boolean getExact() {
92          return this.extraClassInfo.getExact();
93      }
94  
95      public ExtendedClassInfo getExtraClassInfo() {
96          return this.extraClassInfo;
97      }
98  
99      public String getFileName() {
100         return this.stackTraceElement.getFileName();
101     }
102 
103     public int getLineNumber() {
104         return this.stackTraceElement.getLineNumber();
105     }
106 
107     public String getLocation() {
108         return this.extraClassInfo.getLocation();
109     }
110 
111     public String getMethodName() {
112         return this.stackTraceElement.getMethodName();
113     }
114 
115     public StackTraceElement getStackTraceElement() {
116         return this.stackTraceElement;
117     }
118 
119     public String getVersion() {
120         return this.extraClassInfo.getVersion();
121     }
122 
123     @Override
124     public int hashCode() {
125         final int prime = 31;
126         int result = 1;
127         result = prime * result + ((this.extraClassInfo == null) ? 0 : this.extraClassInfo.hashCode());
128         result = prime * result + ((this.stackTraceElement == null) ? 0 : this.stackTraceElement.hashCode());
129         return result;
130     }
131 
132     public boolean isNativeMethod() {
133         return this.stackTraceElement.isNativeMethod();
134     }
135 
136     void renderOn(final StringBuilder output, final TextRenderer textRenderer) {
137         render(this.stackTraceElement, output, textRenderer);
138         textRenderer.render(" ", output, "Text");
139         this.extraClassInfo.renderOn(output, textRenderer);
140     }
141 
142     private void render(final StackTraceElement stElement, final StringBuilder output, final TextRenderer textRenderer) {
143         final String fileName = stElement.getFileName();
144         final int lineNumber = stElement.getLineNumber();
145         textRenderer.render(getClassName(), output, "StackTraceElement.ClassName");
146         textRenderer.render(".", output, "StackTraceElement.ClassMethodSeparator");
147         textRenderer.render(stElement.getMethodName(), output, "StackTraceElement.MethodName");
148         if (stElement.isNativeMethod()) {
149             textRenderer.render("(Native Method)", output, "StackTraceElement.NativeMethod");
150         } else if (fileName != null && lineNumber >= 0) {
151             textRenderer.render("(", output, "StackTraceElement.Container");
152             textRenderer.render(fileName, output, "StackTraceElement.FileName");
153             textRenderer.render(":", output, "StackTraceElement.ContainerSeparator");
154             textRenderer.render(Integer.toString(lineNumber), output, "StackTraceElement.LineNumber");
155             textRenderer.render(")", output, "StackTraceElement.Container");
156         } else if (fileName != null) {
157             textRenderer.render("(", output, "StackTraceElement.Container");
158             textRenderer.render(fileName, output, "StackTraceElement.FileName");
159             textRenderer.render(")", output, "StackTraceElement.Container");
160         } else {
161             textRenderer.render("(", output, "StackTraceElement.Container");
162             textRenderer.render("Unknown Source", output, "StackTraceElement.UnknownSource");
163             textRenderer.render(")", output, "StackTraceElement.Container");
164         }
165     }
166 
167     @Override
168     public String toString() {
169         final StringBuilder sb = new StringBuilder();
170         renderOn(sb, PlainTextRenderer.getInstance());
171         return sb.toString();
172     }
173 
174 }