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.message;
18  
19  import org.apache.logging.log4j.util.PerformanceSensitive;
20  import org.apache.logging.log4j.util.StringBuilderFormattable;
21  
22  /**
23   * Mutable Message wrapper around an Object message.
24   * @since 2.6
25   */
26  @PerformanceSensitive("allocation")
27  public class ReusableObjectMessage implements ReusableMessage {
28      private static final long serialVersionUID = 6922476812535519960L;
29  
30      private transient Object obj;
31      private transient String objectString;
32  
33      public void set(final Object object) {
34          this.obj = object;
35      }
36  
37      /**
38       * Returns the formatted object message.
39       *
40       * @return the formatted object message.
41       */
42      @Override
43      public String getFormattedMessage() {
44          return String.valueOf(obj);
45      }
46  
47      @Override
48      public void formatTo(final StringBuilder buffer) {
49          if (obj == null || obj instanceof String) {
50              buffer.append((String) obj);
51          } else if (obj instanceof StringBuilderFormattable) {
52              ((StringBuilderFormattable) obj).formatTo(buffer);
53          } else if (obj instanceof CharSequence) {
54              buffer.append((CharSequence) obj);
55          } else if (obj instanceof Integer) { // LOG4J2-1437 unbox auto-boxed primitives to avoid calling toString()
56              buffer.append(((Integer) obj).intValue());
57          } else if (obj instanceof Long) {
58              buffer.append(((Long) obj).longValue());
59          } else if (obj instanceof Double) {
60              buffer.append(((Double) obj).doubleValue());
61          } else if (obj instanceof Boolean) {
62              buffer.append(((Boolean) obj).booleanValue());
63          } else if (obj instanceof Character) {
64              buffer.append(((Character) obj).charValue());
65          } else if (obj instanceof Short) {
66              buffer.append(((Short) obj).shortValue());
67          } else if (obj instanceof Float) {
68              buffer.append(((Float) obj).floatValue());
69          } else {
70              buffer.append(obj);
71          }
72      }
73  
74      /**
75       * Returns the object formatted using its toString method.
76       *
77       * @return the String representation of the object.
78       */
79      @Override
80      public String getFormat() {
81          return getFormattedMessage();
82      }
83  
84      /**
85       * Returns the object parameter.
86       *
87       * @return The object.
88       * @since 2.7
89       */
90      public Object getParameter() {
91          return obj;
92      }
93  
94      /**
95       * Returns the object as if it were a parameter.
96       *
97       * @return The object.
98       */
99      @Override
100     public Object[] getParameters() {
101         return new Object[] {obj};
102     }
103 
104     @Override
105     public String toString() {
106         return getFormattedMessage();
107     }
108 
109     /**
110      * Gets the message if it is a throwable.
111      *
112      * @return the message if it is a throwable.
113      */
114     @Override
115     public Throwable getThrowable() {
116         return obj instanceof Throwable ? (Throwable) obj : null;
117     }
118 
119     /**
120      * This message does not have any parameters, so this method returns the specified array.
121      * @param emptyReplacement the parameter array to return
122      * @return the specified array
123      */
124     @Override
125     public Object[] swapParameters(final Object[] emptyReplacement) {
126         return emptyReplacement;
127     }
128 
129     /**
130      * This message does not have any parameters so this method always returns zero.
131      * @return 0 (zero)
132      */
133     @Override
134     public short getParameterCount() {
135         return 0;
136     }
137 
138     @Override
139     public Message memento() {
140         return new ObjectMessage(obj);
141     }
142 }