001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.log4j.spi;
018
019import java.lang.reflect.InvocationTargetException;
020import java.lang.reflect.Method;
021import java.util.List;
022
023import org.apache.logging.log4j.util.Strings;
024
025/**
026 * Class Description goes here.
027 */
028public class ThrowableInformation implements java.io.Serializable {
029
030    static final long serialVersionUID = -4748765566864322735L;
031
032    private transient Throwable throwable;
033    private Method toStringList;
034
035    public ThrowableInformation(Throwable throwable) {
036        this.throwable = throwable;
037        Method method = null;
038        try {
039            Class<?> throwables = Class.forName("org.apache.logging.log4j.core.util.Throwables");
040            method = throwables.getMethod("toStringList", Throwable.class);
041        } catch (ClassNotFoundException | NoSuchMethodException ex) {
042            // Ignore the exception if Log4j-core is not present.
043        }
044        this.toStringList = method;
045    }
046
047    public Throwable getThrowable() {
048        return throwable;
049    }
050
051    public synchronized String[] getThrowableStrRep() {
052        if (toStringList != null && throwable != null) {
053            try {
054                @SuppressWarnings("unchecked")
055                List<String> elements = (List<String>) toStringList.invoke(null, throwable);
056                if (elements != null) {
057                    return elements.toArray(Strings.EMPTY_ARRAY);
058                }
059            } catch (IllegalAccessException | InvocationTargetException ex) {
060                // Ignore the exception.
061            }
062        }
063        return null;
064    }
065}
066