-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
InputStreamRules
Refaster rule collection (#963)
- Loading branch information
1 parent
09317ab
commit 641bb5c
Showing
4 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/InputStreamRules.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package tech.picnic.errorprone.refasterrules; | ||
|
||
import com.google.common.io.ByteStreams; | ||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; | ||
|
||
/** Refaster rules related to expressions dealing with {@link InputStream}s. */ | ||
// XXX: Add a rule for `ByteStreams.skipFully(in, n)` -> `in.skipNBytes(n)` once we have a way to | ||
// target JDK 12+ APIs. | ||
@OnlineDocumentation | ||
final class InputStreamRules { | ||
private InputStreamRules() {} | ||
|
||
static final class InputStreamTransferTo { | ||
@BeforeTemplate | ||
long before(InputStream in, OutputStream out) throws IOException { | ||
return ByteStreams.copy(in, out); | ||
} | ||
|
||
@AfterTemplate | ||
long after(InputStream in, OutputStream out) throws IOException { | ||
return in.transferTo(out); | ||
} | ||
} | ||
|
||
static final class InputStreamReadAllBytes { | ||
@BeforeTemplate | ||
byte[] before(InputStream in) throws IOException { | ||
return ByteStreams.toByteArray(in); | ||
} | ||
|
||
@AfterTemplate | ||
byte[] after(InputStream in) throws IOException { | ||
return in.readAllBytes(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...ib/src/test/resources/tech/picnic/errorprone/refasterrules/InputStreamRulesTestInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package tech.picnic.errorprone.refasterrules; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import com.google.common.io.ByteStreams; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; | ||
|
||
final class InputStreamRulesTest implements RefasterRuleCollectionTestCase { | ||
@Override | ||
public ImmutableSet<Object> elidedTypesAndStaticImports() { | ||
return ImmutableSet.of(ByteStreams.class); | ||
} | ||
|
||
long testInputStreamTransferTo() throws IOException { | ||
return ByteStreams.copy(new ByteArrayInputStream(new byte[0]), new ByteArrayOutputStream()); | ||
} | ||
|
||
byte[] testInputStreamReadAllBytes() throws IOException { | ||
return ByteStreams.toByteArray(new ByteArrayInputStream(new byte[0])); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...b/src/test/resources/tech/picnic/errorprone/refasterrules/InputStreamRulesTestOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package tech.picnic.errorprone.refasterrules; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import com.google.common.io.ByteStreams; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; | ||
|
||
final class InputStreamRulesTest implements RefasterRuleCollectionTestCase { | ||
@Override | ||
public ImmutableSet<Object> elidedTypesAndStaticImports() { | ||
return ImmutableSet.of(ByteStreams.class); | ||
} | ||
|
||
long testInputStreamTransferTo() throws IOException { | ||
return new ByteArrayInputStream(new byte[0]).transferTo(new ByteArrayOutputStream()); | ||
} | ||
|
||
byte[] testInputStreamReadAllBytes() throws IOException { | ||
return new ByteArrayInputStream(new byte[0]).readAllBytes(); | ||
} | ||
} |