Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
Swap from jsr305 to checker
Browse files Browse the repository at this point in the history
  • Loading branch information
mmorrisontx committed Jan 24, 2024
1 parent 1cfb888 commit eaf516f
Show file tree
Hide file tree
Showing 29 changed files with 178 additions and 133 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
slf4jApi = "org.slf4j:slf4j-api:1.7.32"
junit = "junit:junit:4.8.2"
guava = "com.google.guava:guava:20.0"
jsr305 = "com.google.code.findbugs:jsr305:1.3.9"
servletApi = "org.apache.tomcat:tomcat-servlet-api:7.0.8"
freemarker = "org.freemarker:freemarker:2.3.16"
easymock = "org.easymock:easymock:4.1"
commonsLang = "commons-lang:commons-lang:2.6"
hamcrest = "org.hamcrest:hamcrest-core:2.2"
checkerQual = "org.checkerframework:checker-qual:3.42.0"
3 changes: 1 addition & 2 deletions io/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ dependencies {
implementation project(':util-core')
implementation libs.guava
implementation libs.slf4jApi
api libs.checkerQual

// This is a fake copy of the log4j1 Logger, which allows us to continue
// accepting it in our deprecated method signatures without accidentially using
// any of its functionality.
compileOnly project(':log4j1dummyshim')

compileOnly libs.jsr305

testImplementation libs.junit
}
10 changes: 4 additions & 6 deletions io/src/main/java/com/indeed/util/io/Directories.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
package com.indeed.util.io;

import com.google.common.collect.Iterables;
import org.checkerframework.checker.nullness.qual.NonNull;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.nio.file.DirectoryIteratorException;
import java.nio.file.DirectoryStream;
Expand All @@ -26,8 +25,7 @@ public final class Directories {
* @return number of inodes under it.
* @throws IOException
*/
@Nonnegative
public static int count(@Nonnull final Path dir) throws IOException {
public static int count(@NonNull final Path dir) throws IOException {
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
return Iterables.size(stream);
} catch (DirectoryIteratorException ex) {
Expand All @@ -48,8 +46,8 @@ public static int count(@Nonnull final Path dir) throws IOException {
* @return all files in that directory
* @throws IOException
*/
@Nonnull
public static List<Path> list(@Nonnull final Path dir) throws IOException {
@NonNull
public static List<Path> list(@NonNull final Path dir) throws IOException {
final List<Path> contents = new ArrayList<>();
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (final Path entry : stream) {
Expand Down
62 changes: 31 additions & 31 deletions io/src/main/java/com/indeed/util/io/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
import com.google.common.hash.Hashing;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
Expand Down Expand Up @@ -67,7 +67,7 @@ public static String buildPath(String... parts) {
* be written, flushed, synced, or closed
*/
public static void writeObjectToFileOrDie2(
@Nonnull final Object obj, @Nonnull final String file) throws IOException {
@NonNull final Object obj, @NonNull final String file) throws IOException {
Preconditions.checkNotNull(file, "file argument is required!");
Preconditions.checkArgument(!file.isEmpty(), "file argument is required!");

Expand Down Expand Up @@ -106,18 +106,18 @@ public static void writeObjectToFileOrDie2(
/** @deprecated Use {@link #writeObjectToFileOrDie2(java.lang.Object, java.lang.String)} */
@Deprecated
public static void writeObjectToFileOrDie(
@Nonnull final Object obj,
@Nonnull final String file,
@Nonnull final org.apache.log4j.Logger log)
@NonNull final Object obj,
@NonNull final String file,
final org.apache.log4j.@NonNull Logger log)
throws IOException {
writeObjectToFileOrDie2(obj, file);
}

private static class ObjectOutputStreamCallback implements OutputStreamCallback {
private long checksumForWrittenData = 0L;
@Nonnull private final Object obj;
@NonNull private final Object obj;

private ObjectOutputStreamCallback(@Nonnull Object obj) {
private ObjectOutputStreamCallback(@NonNull Object obj) {
this.obj = obj;
}

Expand All @@ -126,7 +126,7 @@ public long getChecksumValue() {
}

@Override
public void writeAndFlushData(@Nonnull OutputStream outputStream) throws IOException {
public void writeAndFlushData(@NonNull OutputStream outputStream) throws IOException {
final ChecksummingOutputStream checksummingOutputStream =
new ChecksummingOutputStream(new BufferedOutputStream(outputStream));
final ObjectOutputStream out = new ObjectOutputStream(checksummingOutputStream);
Expand All @@ -141,7 +141,7 @@ public void writeAndFlushData(@Nonnull OutputStream outputStream) throws IOExcep
}

private static class ChecksummingOutputStream extends FilterOutputStream {
@Nonnull private final Checksum checksummer;
@NonNull private final Checksum checksummer;

private ChecksummingOutputStream(OutputStream out) {
super(out);
Expand Down Expand Up @@ -177,13 +177,13 @@ public long getChecksumValue() {
}

private static interface OutputStreamCallback {
void writeAndFlushData(@Nonnull final OutputStream outputStream) throws IOException;
void writeAndFlushData(@NonNull final OutputStream outputStream) throws IOException;
}

// return a reference to a temp file that contains the written + flushed + fsynced + closed data
@Nonnull
@NonNull
private static File writeDataToTempFileOrDie2(
@Nonnull final OutputStreamCallback callback, @Nonnull final File targetFile)
@NonNull final OutputStreamCallback callback, @NonNull final File targetFile)
throws IOException {
Preconditions.checkNotNull(callback, "callback argument is required!");
Preconditions.checkNotNull(targetFile, "targetFile argument is required!");
Expand Down Expand Up @@ -229,18 +229,18 @@ private static File writeDataToTempFileOrDie2(
* java.io.File)}
*/
@Deprecated
@Nonnull
@NonNull
private static File writeDataToTempFileOrDie(
@Nonnull final OutputStreamCallback callback,
@Nonnull final File targetFile,
@Nonnull final org.apache.log4j.Logger log)
@NonNull final OutputStreamCallback callback,
@NonNull final File targetFile,
final org.apache.log4j.@NonNull Logger log)
throws IOException {
return writeDataToTempFileOrDie2(callback, targetFile);
}

@Nonnull
@NonNull
private static File writeTextToTempFileOrDie2(
@Nonnull final String[] text, @Nonnull final File targetFile) throws IOException {
@NonNull final String[] text, @NonNull final File targetFile) throws IOException {
Preconditions.checkNotNull(text, "callback argument is required!");
Preconditions.checkNotNull(targetFile, "targetFile argument is required!");

Expand Down Expand Up @@ -275,11 +275,11 @@ private static File writeTextToTempFileOrDie2(

/** @deprecated Use {@link #writeTextToTempFileOrDie2(java.lang.String[], java.io.File)} */
@Deprecated
@Nonnull
@NonNull
private static File writeTextToTempFileOrDie(
@Nonnull final String[] text,
@Nonnull final File targetFile,
@Nonnull final org.apache.log4j.Logger log)
@NonNull final String[] text,
@NonNull final File targetFile,
final org.apache.log4j.@NonNull Logger log)
throws IOException {
return writeTextToTempFileOrDie2(text, targetFile);
}
Expand All @@ -297,7 +297,7 @@ private static File writeTextToTempFileOrDie(
* synced, or closed
*/
public static boolean writeObjectIfChangedOrDie2(
@Nonnull final Object obj, @Nonnull final String file) throws IOException {
@NonNull final Object obj, @NonNull final String file) throws IOException {
Preconditions.checkNotNull(file, "file argument is required!");
Preconditions.checkArgument(!file.isEmpty(), "file argument is required!");

Expand Down Expand Up @@ -364,15 +364,15 @@ public static boolean writeObjectIfChangedOrDie2(
/** @deprecated Use {@link #writeObjectIfChangedOrDie2(java.lang.Object, java.lang.String)} */
@Deprecated
public static boolean writeObjectIfChangedOrDie(
@Nonnull final Object obj,
@Nonnull final String file,
@Nonnull final org.apache.log4j.Logger log)
@NonNull final Object obj,
@NonNull final String file,
final org.apache.log4j.@NonNull Logger log)
throws IOException {
return writeObjectIfChangedOrDie2(obj, file);
}

public static long computeFileChecksum(
@Nonnull final File file, @Nonnull final Checksum checksum) throws IOException {
@NonNull final File file, @NonNull final Checksum checksum) throws IOException {
return com.google.common.io.Files.asByteSource(file).hash(Hashing.crc32()).padToLong();
}

Expand Down Expand Up @@ -694,7 +694,7 @@ public static void writeToTextFile(String[] lines, String file) {
}

public static void writeToTextFileOrDie(
@Nonnull final String[] lines, @Nonnull final String file) throws IOException {
@NonNull final String[] lines, @NonNull final String file) throws IOException {
// Write out a temp file (or die)
final File f = new File(file);
final File temp = writeTextToTempFileOrDie2(lines, f);
Expand Down Expand Up @@ -778,7 +778,7 @@ public static boolean delete(String file) {
* @return true if all deletions were successful, false if file did not exist
* @throws IOException if deletion fails and the file still exists at the end
*/
public static boolean deleteOrDie(@Nonnull final String file) throws IOException {
public static boolean deleteOrDie(@NonNull final String file) throws IOException {
// this returns true if the file was actually deleted
// and false for 2 cases:
// 1. file did not exist to start with
Expand Down Expand Up @@ -893,8 +893,8 @@ public static String getFileHash(final String file, final String algorithm)
* Converts a byte array to a hex string. The String returned will be of length exactly {@code
* bytes.length * 2}.
*/
@Nonnull
public static String toHex(@Nonnull final byte[] bytes) {
@NonNull
public static String toHex(@NonNull final byte[] bytes) {
StringBuilder buf = new StringBuilder(bytes.length * 2);
for (byte b : bytes) {
String hexDigits = Integer.toHexString((int) b & 0x00ff);
Expand Down
26 changes: 12 additions & 14 deletions io/src/main/java/com/indeed/util/io/SafeFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import com.google.common.base.Charsets;
import com.google.common.base.Throwables;
import com.indeed.util.core.io.Closeables2;
import com.indeed.util.core.nullsafety.ParametersAreNonnullByDefault;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.NotThreadSafe;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -93,17 +91,15 @@ public static void ensureDirectoryExists(final Path path) throws IOException {
* @return number of NormalFiles fsynced (not including directories).
* @throws IOException in the event that we could not fsync the provided directory.
*/
@Nonnegative
public static int fsyncRecursive(final Path root) throws IOException {
final FsyncingSimpleFileVisitor visitor = new FsyncingSimpleFileVisitor();
Files.walkFileTree(root, visitor);
return visitor.getFileCount();
}

private static class FsyncingSimpleFileVisitor extends SimpleFileVisitor<Path> {
@Nonnegative private int fileCount = 0;
private int fileCount = 0;

@Nonnegative
public int getFileCount() {
return fileCount;
}
Expand Down Expand Up @@ -214,7 +210,7 @@ public static void write(final byte[] data, final Path path) throws IOException
* @return handle to opened temp file
* @throws IOException in the event that the file could not be created.
*/
@Nonnull
@NonNull
public static SafeOutputStream createAtomicFile(final Path path) throws IOException {
final Path dir = path.getParent();
final Path name = path.getFileName();
Expand Down Expand Up @@ -264,15 +260,17 @@ public static void deleteIfExistsQuietly(final Path path) {
}
}

/** @see SafeFiles#createAtomicFile(Path) */
/**
* @see SafeFiles#createAtomicFile(Path)
* <p>NotThreadSafe
*/
@ParametersAreNonnullByDefault
@NotThreadSafe
private static class SafeFileOutputStream extends SafeOutputStream {
@Nonnull private final Path path;
@Nonnull private final Path tempFile;
@NonNull private final Path path;
@NonNull private final Path tempFile;

@Nonnull private final OutputStream out; // do not close this
@Nonnull private final FileChannel fileChannel; // only close this
@NonNull private final OutputStream out; // do not close this
@NonNull private final FileChannel fileChannel; // only close this

private boolean closed = false;

Expand Down
12 changes: 7 additions & 5 deletions io/src/main/java/com/indeed/util/io/SafeOutputStream.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// Copyright 2015 Indeed
package com.indeed.util.io;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.NotThreadSafe;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Path;

/** @see SafeFiles#createAtomicFile(Path) */
@NotThreadSafe
/**
* @see SafeFiles#createAtomicFile(Path)
* <p>Not Thread Safe
*/
public abstract class SafeOutputStream extends OutputStream
implements WritableByteChannel, Closeable {
/**
Expand All @@ -25,7 +27,7 @@ public abstract class SafeOutputStream extends OutputStream
* @throws IOException in the event that the buffer could not be written.
*/
@Override
public abstract int write(@Nonnull final ByteBuffer src) throws IOException;
public abstract int write(@NonNull final ByteBuffer src) throws IOException;

/**
* Commit causes the current atomic file writing operation to conclude and the current temp file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.google.common.base.Preconditions;
import com.indeed.util.io.BufferedFileDataOutputStream;
import com.indeed.util.serialization.Stringifier;
import org.checkerframework.checker.nullness.qual.NonNull;

import javax.annotation.Nonnull;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand All @@ -23,17 +23,17 @@ public class FileBasedCheckpointer<T> implements Checkpointer<T> {
private volatile T value;

public FileBasedCheckpointer(
@Nonnull final File checkpointFile,
@Nonnull Stringifier<T> stringifier,
@Nonnull T defaultValue)
@NonNull final File checkpointFile,
@NonNull Stringifier<T> stringifier,
@NonNull T defaultValue)
throws IOException {
this(checkpointFile.toPath(), stringifier, defaultValue);
}

public FileBasedCheckpointer(
@Nonnull final Path checkpointFilePath,
@Nonnull Stringifier<T> stringifier,
@Nonnull T defaultValue)
@NonNull final Path checkpointFilePath,
@NonNull Stringifier<T> stringifier,
@NonNull T defaultValue)
throws IOException {
this.checkpointFilePath =
Preconditions.checkNotNull(checkpointFilePath, "no checkpoint file");
Expand Down
2 changes: 2 additions & 0 deletions log4j1dummyshim/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
plugins {
id 'java'
}

indeedOss.activateFeature 'java'
4 changes: 1 addition & 3 deletions util-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ dependencies {
implementation project(':varexport')
implementation libs.guava
implementation libs.slf4jApi
api libs.checkerQual

// This is a fake copy of the log4j1 Logger, which allows us to continue
// accepting it in our deprecated method signatures without accidentially using
// any of its functionality.
compileOnly project(':log4j1dummyshim')

compileOnly libs.jsr305
testCompileOnly libs.jsr305

testImplementation libs.junit
}
Loading

0 comments on commit eaf516f

Please sign in to comment.