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

edu.umd.cs.findbugs.sarif.Stack Maven / Gradle / Ivy

There is a newer version: 4.8.6
Show newest version
package edu.umd.cs.findbugs.sarif;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.ba.SourceFinder;
import com.google.gson.JsonObject;
import com.google.gson.JsonArray;

import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * A class representing {@code stack} object (§3.44)
 * @see 3.44 stack object
 */
class Stack {
    final String message;
    final List frames;

    Stack(@NonNull String message, @NonNull List frames) {
        this.message = Objects.requireNonNull(Objects.requireNonNull(message));
        this.frames = Collections.unmodifiableList(Objects.requireNonNull(frames));
    }

    static Stack fromThrowable(Throwable throwable, @NonNull SourceFinder sourceFinder, @NonNull Map baseToId) {
        List frames = Arrays.stream(Objects.requireNonNull(throwable).getStackTrace()).map(element -> StackFrame.fromStackTraceElement(
                element, sourceFinder, baseToId)).collect(
                        Collectors.toList());
        String message = throwable.getMessage();
        if (message == null) {
            message = "no message given";
        }
        return new Stack(message, frames);
    }

    JsonObject toJsonObject() {
        JsonObject textJson = new JsonObject();
        textJson.addProperty("text", message);

        JsonObject result = new JsonObject();
        result.add("message", textJson);

        JsonArray frameArray = new JsonArray();
        frames.forEach(stackFrame -> frameArray.add(stackFrame.toJsonObject()));
        if (frameArray.size() > 0) {
            result.add("frames", frameArray);
        }
        return result;
    }

    /**
     * A class representing {@code stackFrame} object (§3.45)
     * @see 3.45 stackFrame object
     */
    static class StackFrame {
        @NonNull
        final Location location;

        StackFrame(@NonNull Location location) {
            this.location = Objects.requireNonNull(location);
        }

        static StackFrame fromStackTraceElement(@NonNull StackTraceElement element, @NonNull SourceFinder sourceFinder,
                @NonNull Map baseToId) {
            Location location = Location.fromStackTraceElement(element, sourceFinder, baseToId);
            return new StackFrame(location);
        }

        JsonObject toJsonObject() {
            JsonObject result = new JsonObject();
            result.add("location", location.toJsonObject());
            return result;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy