All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.inject.internal.util.StackTraceElements Maven / Gradle / Ivy

package com.google.inject.internal.util;

import com.google.common.collect.MapMaker;

import java.lang.reflect.Constructor;
import java.lang.reflect.Member;
import java.util.Map;

/**
 * Creates stack trace elements for members.
 */
public class StackTraceElements {

    private static final StackTraceElement[] EMPTY_STACK_TRACE = new StackTraceElement[0];
    private static final InMemoryStackTraceElement[] EMPTY_INMEMORY_STACK_TRACE =
            new InMemoryStackTraceElement[0];
    private static final String UNKNOWN_SOURCE = "Unknown Source";
    private static Map cache = new MapMaker().makeMap();

    public static Object forMember(Member member) {
        if (member == null) {
            return SourceProvider.UNKNOWN_SOURCE;
        }

        Class declaringClass = member.getDeclaringClass();

        String fileName = null;
        int lineNumber = -1;

        Class memberType = Classes.memberType(member);
        String memberName = memberType == Constructor.class ? "" : member.getName();
        return new StackTraceElement(declaringClass.getName(), memberName, fileName, lineNumber);
    }

    public static Object forType(Class implementation) {
        String fileName = null;
        int lineNumber = -1;

        return new StackTraceElement(implementation.getName(), "class", fileName, lineNumber);
    }

    /**
     * Clears the internal cache for {@link StackTraceElement StackTraceElements}.
     */
    public static void clearCache() {
        cache.clear();
    }

    /**
     * Returns encoded in-memory version of {@link StackTraceElement StackTraceElements}.
     */
    public static InMemoryStackTraceElement[] convertToInMemoryStackTraceElement(
            StackTraceElement[] stackTraceElements) {
        if (stackTraceElements.length == 0) {
            return EMPTY_INMEMORY_STACK_TRACE;
        }
        InMemoryStackTraceElement[] inMemoryStackTraceElements =
                new InMemoryStackTraceElement[stackTraceElements.length];
        for (int i = 0; i < stackTraceElements.length; i++) {
            inMemoryStackTraceElements[i] =
                    weakIntern(new InMemoryStackTraceElement(stackTraceElements[i]));
        }
        return inMemoryStackTraceElements;
    }

    /**
     * Decodes in-memory stack trace elements to regular {@link StackTraceElement StackTraceElements}.
     */
    public static StackTraceElement[] convertToStackTraceElement(
            InMemoryStackTraceElement[] inMemoryStackTraceElements) {
        if (inMemoryStackTraceElements.length == 0) {
            return EMPTY_STACK_TRACE;
        }
        StackTraceElement[] stackTraceElements =
                new StackTraceElement[inMemoryStackTraceElements.length];
        for (int i = 0; i < inMemoryStackTraceElements.length; i++) {
            String declaringClass = inMemoryStackTraceElements[i].getClassName();
            String methodName = inMemoryStackTraceElements[i].getMethodName();
            int lineNumber = inMemoryStackTraceElements[i].getLineNumber();
            stackTraceElements[i] =
                    new StackTraceElement(declaringClass, methodName, UNKNOWN_SOURCE, lineNumber);
        }
        return stackTraceElements;
    }

    private static InMemoryStackTraceElement weakIntern(
            InMemoryStackTraceElement inMemoryStackTraceElement) {
        InMemoryStackTraceElement cached =
                (InMemoryStackTraceElement) cache.get(inMemoryStackTraceElement);
        if (cached != null) {
            return cached;
        }
        inMemoryStackTraceElement = new InMemoryStackTraceElement(
                weakIntern(inMemoryStackTraceElement.getClassName()),
                weakIntern(inMemoryStackTraceElement.getMethodName()),
                inMemoryStackTraceElement.getLineNumber());
        cache.put(inMemoryStackTraceElement, inMemoryStackTraceElement);
        return inMemoryStackTraceElement;
    }

    private static String weakIntern(String s) {
        String cached = (String) cache.get(s);
        if (cached != null) {
            return cached;
        }
        cache.put(s, s);
        return s;
    }

    /**
     * In-Memory version of {@link StackTraceElement} that does not store the file name.
     */
    public static class InMemoryStackTraceElement {
        private String declaringClass;
        private String methodName;
        private int lineNumber;

        InMemoryStackTraceElement(StackTraceElement ste) {
            this(ste.getClassName(), ste.getMethodName(), ste.getLineNumber());
        }

        InMemoryStackTraceElement(String declaringClass, String methodName, int lineNumber) {
            this.declaringClass = declaringClass;
            this.methodName = methodName;
            this.lineNumber = lineNumber;
        }

        String getClassName() {
            return declaringClass;
        }

        String getMethodName() {
            return methodName;
        }

        int getLineNumber() {
            return lineNumber;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == this) {
                return true;
            }
            if (!(obj instanceof InMemoryStackTraceElement)) {
                return false;
            }
            InMemoryStackTraceElement e = (InMemoryStackTraceElement) obj;
            return e.declaringClass.equals(declaringClass) && e.lineNumber == lineNumber &&
                    methodName.equals(e.methodName);
        }

        @Override
        public int hashCode() {
            int result = 31 * declaringClass.hashCode() + methodName.hashCode();
            result = 31 * result + lineNumber;
            return result;
        }

        @Override
        public String toString() {
            return declaringClass + "." + methodName + "(" + lineNumber + ")";
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy