cz.muni.stanse.checker.CheckerError.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.stanse.checker.CheckerError.java

Source

/**
 *  @brief Defines public final class CheckerError. It represents the error
 *         found in source program (reprezented by set of CFGs).
 *
 * Copyright (c) 2008-2009 Marek Trtik
 *
 * Licensed under GPLv2.
 */
package cz.muni.stanse.checker;

import cz.muni.stanse.Stanse;
import cz.muni.stanse.codestructures.CFGNode;
import cz.muni.stanse.codestructures.LazyInternalStructures;

import java.io.File;
import java.text.NumberFormat;
import java.util.Collections;
import java.util.List;
import java.util.Vector;

import org.dom4j.DocumentFactory;
import org.dom4j.Element;

/**
 * @brief Represents output from checkers, which is the error found in the
 *        source file (represented by a set of CFGs).
 *
 * A found error is described by short and full string descriptions, level of
 * error importance and by a list of error-traces. Each checker is under
 * obligation to report all its errors via instances of this class.  
 *
 * @see cz.muni.stanse.checker#Checker
 *      cz.muni.stanse.checker#CheckerErrorTrace
 */
public final class CheckerError implements Comparable<CheckerError> {

    // public section

    public CheckerError(final String shortDesc, final String fullDesc, final int importance,
            final String checkerName, final List<CheckerErrorTrace> traces) {
        this.shortDesc = shortDesc;
        this.fullDesc = fullDesc;
        this.importance = importance;
        this.checkerName = checkerName;
        this.traces = traces;
    }

    public CheckerError(final String shortDesc, final String fullDesc, final int importance,
            final String checkerName, final List<CFGNode> trace, final String startMsg, final String innerMsg,
            final String endMsg, final LazyInternalStructures internals) {
        this.shortDesc = shortDesc;
        this.fullDesc = fullDesc;
        this.importance = importance;
        this.checkerName = checkerName;
        this.traces = new Vector();
        traces.add(new CheckerErrorTrace(trace, startMsg, innerMsg, endMsg, internals));
    }

    @Override
    public boolean equals(Object obj) {
        return (obj == null || getClass() != obj.getClass()) ? false : isEqualWith((CheckerError) obj);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + getShortDesc().hashCode();
        hash = 59 * hash + getFullDesc().hashCode();
        hash = 59 * hash + getImportance();
        hash = 59 * hash + getCheckerName().hashCode();
        hash = 59 * hash + getTraces().hashCode();
        return hash;
    }

    @Override
    public String toString() {
        final CheckerErrorTraceLocation errorLocation = getErrorLocation();
        final StringBuilder sb = new StringBuilder("[");
        sb.append(NumberFormat.getIntegerInstance().format(getImportance())).append("] ");
        final String path = new File(errorLocation.getUnitName()).getAbsolutePath();
        // relativize
        path.replaceFirst(Stanse.getInstance().getRootDirectory() + '/', "");
        sb.append(path).append(", line ").append(errorLocation.getLineNumber()).append(": ").append(getShortDesc());
        return sb.toString();
    }

    public String dump() {
        StringBuilder result = new StringBuilder();
        result.append(toString());
        result.append('\n');
        for (final CheckerErrorTrace trace : getTraces())
            result.append(trace.dump());
        return result.toString();
    }

    public Element xmlDump() {
        Element result = DocumentFactory.getInstance().createElement("error");
        result.addElement("short_desc").addText(getShortDesc());
        result.addElement("full_desc").addText(getFullDesc());
        result.addElement("importance").addText(Integer.toString(getImportance()));
        result.addElement("checker_name").addText(getCheckerName());
        Element eTraces = result.addElement("traces").addText(getCheckerName());

        for (final CheckerErrorTrace trace : getTraces())
            eTraces.add(trace.xmlDump());
        return result;
    }

    @Override
    public int compareTo(CheckerError other) {
        return getImportance() - other.getImportance();
    }

    public CheckerErrorTraceLocation getCauseLocation() {
        return getTraces().iterator().next().getCauseLocation();
    }

    public CheckerErrorTraceLocation getErrorLocation() {
        return getTraces().iterator().next().getErrorLocation();
    }

    public boolean isEqualWith(final CheckerError other) {
        return getShortDesc().equals(other.getShortDesc()) && getFullDesc().equals(other.getFullDesc())
                && getImportance() == other.getImportance() && getCheckerName().equals(other.getCheckerName())
                && getTraces().equals(other.getTraces());
    }

    public String getShortDesc() {
        return shortDesc;
    }

    public String getFullDesc() {
        return fullDesc;
    }

    public int getImportance() {
        return importance;
    }

    public String getCheckerName() {
        return checkerName;
    }

    public List<CheckerErrorTrace> getTraces() {
        return Collections.unmodifiableList(traces);
    }

    // private section

    private final String shortDesc;
    private final String fullDesc;
    private final int importance;
    private final String checkerName;
    private final List<CheckerErrorTrace> traces;
}