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

framework.src.org.checkerframework.framework.test.diagnostics.JavaDiagnosticReader Maven / Gradle / Ivy

Go to download

The Checker Framework enhances Java’s type system to make it more powerful and useful. This lets software developers detect and prevent errors in their Java programs. The Checker Framework includes compiler plug-ins ("checkers") that find bugs or verify their absence. It also permits you to write your own compiler plug-ins.

There is a newer version: 3.42.0
Show newest version
package org.checkerframework.framework.test.diagnostics;

import javax.tools.JavaFileObject;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * Reads a file that has serialized javac diagnostics and returns either a list of TestDiagnosticLines or
 * TestDiagnostics.  This file might either:
 *    a) a Java file, which is read by creating a JavaDiagnosticReader with the JAVA_COMMENT_CODEC as
 *    b) A "Diagnostic" file, which is read by creating a JavaDiagnosticReader with a DIAGNOSTIC_FILE_CODEC
 */
public class JavaDiagnosticReader implements Iterator {

    // This class begins with the most common static helper methods that are used to read diagnostics

    /**
     * Reads the entire input file using the given codec and returns the resulting line.
     * @param toRead the file (Java or Diagnostics format) to read
     * @param codec a codec corresponding to the file type being read
     * @param omitEmptyDiagnostics whether or not lines that do not contain any diagnostics should be
     *                             reported as empty TestDiagnosticLines
     * @return the List of TestDiagnosticLines from the input file
     */
    public static List readDiagnostics(File toRead, DiagnosticCodec codec, boolean omitEmptyDiagnostics) {
        List lines = new ArrayList<>();
        JavaDiagnosticReader reader = new JavaDiagnosticReader(toRead, codec);
        while (reader.hasNext()) {
            TestDiagnosticLine line = reader.next();
            if (!omitEmptyDiagnostics || line.hasDiagnostics()) {
                lines.add(line);
            }
        }
        reader.close();

        return lines;
    }

    /**
     * Reads diagnostic lines from the comments of the input Java file.
     * @param toRead a Java File
     * @param omitEmptyDiagnostics whether or not lines that do not contain any diagnostics should be
     *                             reported as empty TestDiagnosticLines
     * @return the List of TestDiagnosticLines from the input file
     */
    public static List readDiagnostics(File toRead, boolean omitEmptyDiagnostics) {
        return readDiagnostics(toRead, JAVA_COMMENT_CODEC, omitEmptyDiagnostics);
    }

    /**
     * Reads diagnostic lines from the comments of a set of Java file.
     * @param toRead java files to read using the JAVA_COMMENT_CODEC
     * @param omitEmptyDiagnostics whether or not lines that do not contain any diagnostics should be
     *                             reported as empty TestDiagnosticLines
     * @return the List of TestDiagnosticLines from the input Jav afiles
     */
    public static List readDiagnosticLines(Iterable toRead, boolean omitEmptyDiagnostics) {
        List lines = new ArrayList<>();
        for (File file : toRead) {
            lines.addAll(readDiagnostics(file, omitEmptyDiagnostics));
        }
        return lines;
    }

    /**
     * Reads diagnostics from the comments of a set of Java file.
     * @param toRead java files to read using the JAVA_COMMENT_CODEC
     * @param omitEmptyDiagnostics whether or not lines that do not contain any diagnostics should be
     *                             reported as empty TestDiagnosticLines
     * @return the List of TestDiagnostics (not lines) from the files ToRead
     */
    public static List readDiagnostics(Iterable toRead, boolean omitEmptyDiagnostics) {
        List lines = readDiagnosticLines(toRead, omitEmptyDiagnostics);

        List diagnostics = new ArrayList((int) (lines.size() + lines.size() * 0.1));
        for (TestDiagnosticLine line : lines) {
            diagnostics.addAll(line.getDiagnostics());
        }
        return diagnostics;
    }

    /**
     * Reads diagnostic lines from the comments of a set of Java file.
     * @param toRead the Java files to read using the JAVA_COMMENT_CODEC
     * @param omitEmptyDiagnostics whether or not lines that do not contain any diagnostics should be
     *                             reported as empty TestDiagnosticLines
     * @return the List of TestDiagnostics (not lines) from the files ToRead
     */
    public static List readDiagnosticsJfo(JavaFileObject toRead, boolean omitEmptyDiagnostics) {
        List lines = new ArrayList<>();
        JavaDiagnosticReader reader = new JavaDiagnosticReader(toRead, JAVA_COMMENT_CODEC);
        while (reader.hasNext()) {
            TestDiagnosticLine line = reader.next();
            if (!omitEmptyDiagnostics || line.hasDiagnostics()) {
                lines.add(line);
            }
        }
        reader.close();

        return lines;
    }


    /**
     * Reads diagnostic lines from the comments of a set of Java file.
     * @param toRead the Java files to read using the JAVA_COMMENT_CODEC
     * @param omitEmptyDiagnostics whether or not lines that do not contain any diagnostics should be
     *                             reported as empty TestDiagnosticLines
     * @return the List of TestDiagnosticLines from the input Jav afiles
     */
    public static List readExpectedDiagnosticLinesJfo(Iterable toRead, boolean omitEmptyDiagnostics) {
        List lines = new ArrayList<>();
        for (JavaFileObject file : toRead) {
            lines.addAll(readDiagnosticsJfo(file, omitEmptyDiagnostics));
        }
        return lines;
    }


    /**
     * Reads diagnostics from the comments of a set of Java file.
     * @param toRead the Java files to read using the JAVA_COMMENT_CODEC
     * @param omitEmptyDiagnostics whether or not lines that do not contain any diagnostics should be
     *                             reported as empty TestDiagnosticLines
     * @return the List of TestDiagnostics (not lines)
     */
    public static List readExpectedDiagnosticsJfo(Iterable toRead, boolean omitEmptyDiagnostics) {
        List lines = readExpectedDiagnosticLinesJfo(toRead, omitEmptyDiagnostics);

        List diagnostics = new ArrayList((int) (lines.size() + lines.size() * 0.1));
        for (TestDiagnosticLine line : lines) {
            diagnostics.addAll(line.getDiagnostics());
        }
        return diagnostics;
    }

    /**
     * Reads diagnostic lines line-by-line from the input Diagnostic file.
     * @param toRead a Diagnostic File
     * @param omitEmptyDiagnostics whether or not lines that do not contain any diagnostics should be
     *                             reported as empty TestDiagnosticLines
     * @return the List of TestDiagnosticLines from the input file
     */
    public static List readDiagnosticFile(File toRead, boolean omitEmptyDiagnostics) {
        return readDiagnostics(toRead, DIAGNOSTIC_FILE_CODEC, omitEmptyDiagnostics);
    }

    /**
     * Reads diagnostic lines line-by-line from the input Diagnostic files.
     * @param toRead a set of Diagnostic Files
     * @param omitEmptyDiagnostics whether or not lines that do not contain any diagnostics should be
     *                             reported as empty TestDiagnosticLines
     * @return the List of TestDiagnosticLines from the input files
     */
    public static List readDiagnosticFileLines(Iterable toRead, boolean omitEmptyDiagnostics) {
        List lines = new ArrayList<>();
        for (File file : toRead) {
            lines.addAll(readDiagnosticFile(file, omitEmptyDiagnostics));
        }
        return lines;
    }

    /**
     * Reads diagnostics line-by-line from the input Diagnostic files.
     * @param toRead a set of Diagnostic Files
     * @param omitEmptyDiagnostics whether or not lines that do not contain any diagnostics should be
     *                             reported as empty TestDiagnosticLines
     * @return the List of TestDiagnosticLines from the input files
     */
    public static List readDiagnosticFiles(Iterable toRead, boolean omitEmptyDiagnostics) {
        List lines = readDiagnosticFileLines(toRead, omitEmptyDiagnostics);

        List diagnostics = new ArrayList((int) (lines.size() + lines.size() * 0.1));
        for (TestDiagnosticLine line : lines) {
            diagnostics.addAll(line.getDiagnostics());
        }
        return diagnostics;
    }

    /**
     * Instances of DiagnosticCodec represent the various formats diagnostic strings can take
     */
    public interface DiagnosticCodec {
        public TestDiagnosticLine convertLine(long lineNumber, String line);
    }

    /**
     * Interprets a string that was written as a comment in a Java file
     */
    public static DiagnosticCodec JAVA_COMMENT_CODEC = new DiagnosticCodec() {
        @Override
        public TestDiagnosticLine convertLine(long lineNumber, String line) {
            return TestDiagnosticUtils.fromJavaSourceLine(line, lineNumber);
        }
    };

    /**
     * Interprets a string that was written as a line in a Diagnostic File
     */
    public static DiagnosticCodec DIAGNOSTIC_FILE_CODEC = new DiagnosticCodec() {
        @Override
        public TestDiagnosticLine convertLine(long lineNumber, String line) {
            return TestDiagnosticUtils.fromDiagnosticFileLine(line);
        }
    };

    public final File toRead;
    public final JavaFileObject toReadFileObject;
    public final DiagnosticCodec codec;

    private boolean initialized = false;
    private boolean closed = false;

    private LineNumberReader reader = null;

    public String nextLine = null;
    public int nextLineNumber = -1;

    public JavaDiagnosticReader(File toRead, DiagnosticCodec codec) {
        this.toRead = toRead;
        this.toReadFileObject = null;
        this.codec = codec;
    }

    public JavaDiagnosticReader(JavaFileObject toRead, DiagnosticCodec codec) {
        this.toRead = null;
        this.toReadFileObject = toRead;
        this.codec = codec;
    }

    private void init() throws IOException {
        if (!initialized && !closed) {
            initialized = true;

            Reader fileReader = (toRead != null) ? new FileReader(toRead) : toReadFileObject.openReader(true);
            reader = new LineNumberReader(fileReader);
            advance();
        }
    }

    public boolean hasNext() {
        if (closed) {
            return false;
        }

        try {
            init();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return nextLine != null;
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("Cannot remove elements using JavaDiagnosticFileReader.");
    }

    public TestDiagnosticLine next() {
        try {
            init();

            if (nextLine == null) {
                throw new NoSuchElementException();
            } else if (closed) {
                throw new RuntimeException("Reader has been closed: " + toRead.getAbsolutePath());
            }

            String current = nextLine;
            int currentLineNumber = nextLineNumber;

            advance();

            if (nextLine == null) {
                close();
            }

            return codec.convertLine(currentLineNumber, current);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    protected void advance() throws IOException {
        nextLine = reader.readLine();
        nextLineNumber = reader.getLineNumber();
    }

    public void close() {
        try {
            if (initialized) {
                reader.close();
            }

            closed = true;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy