-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added annotations to org.apache.dubbo.common.io #1
base: master
Are you sure you want to change the base?
Changes from 10 commits
bdc4403
c42444f
34eb60e
34b3627
90f08fe
37ecbee
489a4e1
95b7927
702acd8
c6fddfb
100aac8
e861190
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,35 +19,48 @@ | |
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import org.checkerframework.checker.index.qual.GTENegativeOne; | ||
import org.checkerframework.checker.index.qual.IndexFor; | ||
import org.checkerframework.checker.index.qual.IndexOrHigh; | ||
import org.checkerframework.checker.index.qual.LTEqLengthOf; | ||
import org.checkerframework.checker.index.qual.LTLengthOf; | ||
import org.checkerframework.checker.index.qual.NonNegative; | ||
|
||
/** | ||
* UnsafeByteArrayInputStream. | ||
*/ | ||
public class UnsafeByteArrayInputStream extends InputStream { | ||
protected byte[] mData; | ||
|
||
protected int mPosition, mLimit, mMark = 0; | ||
protected @IndexOrHigh("this.mData") int mPosition, mMark, mLimit = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not annotated |
||
|
||
public UnsafeByteArrayInputStream(byte[] buf) { | ||
this(buf, 0, buf.length); | ||
} | ||
|
||
public UnsafeByteArrayInputStream(byte[] buf, int offset) { | ||
@SuppressWarnings("argument.type.incompatible") // The length of the array is always greater than the index used to access it. | ||
public UnsafeByteArrayInputStream(byte[] buf, @IndexOrHigh("#1") int offset) { | ||
this(buf, offset, buf.length - offset); | ||
} | ||
|
||
public UnsafeByteArrayInputStream(byte[] buf, int offset, int length) { | ||
public UnsafeByteArrayInputStream(byte[] buf, @IndexOrHigh("#1") int offset, @NonNegative int length) { | ||
mData = buf; | ||
mPosition = mMark = offset; | ||
mLimit = Math.min(offset + length, buf.length); | ||
} | ||
|
||
@Override | ||
public int read() { | ||
public @GTENegativeOne int read() { | ||
return (mPosition < mLimit) ? (mData[mPosition++] & 0xff) : -1; | ||
} | ||
|
||
@Override | ||
public int read(byte[] b, int off, int len) { | ||
@SuppressWarnings({"assignment.type.incompatible", "argument.type.incompatible", "return.type.incompatible"}) /* | ||
#1. mLimit is greater than mPosition and after the assignment the value can only decrease, so it stays valid. | ||
#2. The call is safe because len has been verified before. | ||
#3. The return type is safe because len has been verified. | ||
*/ | ||
public @GTENegativeOne @LTEqLengthOf("#1") int read(byte[] b, @IndexOrHigh("#1") int off, @NonNegative @LTLengthOf(value = "#1", offset = "#2 - 1") int len) { | ||
if (b == null) { | ||
throw new NullPointerException(); | ||
} | ||
|
@@ -58,18 +71,18 @@ public int read(byte[] b, int off, int len) { | |
return -1; | ||
} | ||
if (mPosition + len > mLimit) { | ||
len = mLimit - mPosition; | ||
len = mLimit - mPosition; // #1 | ||
} | ||
if (len <= 0) { | ||
return 0; | ||
} | ||
System.arraycopy(mData, mPosition, b, off, len); | ||
System.arraycopy(mData, mPosition, b, off, len); // #2 | ||
mPosition += len; | ||
return len; | ||
return len; // #3 | ||
} | ||
|
||
@Override | ||
public long skip(long len) { | ||
public @NonNegative long skip(long len) { | ||
if (mPosition + len > mLimit) { | ||
len = mLimit - mPosition; | ||
} | ||
|
@@ -81,7 +94,8 @@ public long skip(long len) { | |
} | ||
|
||
@Override | ||
public int available() { | ||
@SuppressWarnings("return.type.incompatible") // mPosition is always lower than mLimit | ||
public @NonNegative int available() { | ||
return mLimit - mPosition; | ||
} | ||
|
||
|
@@ -108,7 +122,7 @@ public int position() { | |
return mPosition; | ||
} | ||
|
||
public void position(int newPosition) { | ||
public void position(@IndexFor("this.mData") int newPosition) { | ||
mPosition = newPosition; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,13 +19,19 @@ | |
import java.io.IOException; | ||
import java.io.Reader; | ||
|
||
import org.checkerframework.checker.index.qual.GTENegativeOne; | ||
import org.checkerframework.checker.index.qual.IndexOrHigh; | ||
import org.checkerframework.checker.index.qual.LTEqLengthOf; | ||
import org.checkerframework.checker.index.qual.LTLengthOf; | ||
import org.checkerframework.checker.index.qual.NonNegative; | ||
|
||
/** | ||
* Thread-unsafe StringReader. | ||
*/ | ||
public class UnsafeStringReader extends Reader { | ||
private String mString; | ||
|
||
private int mPosition, mLimit, mMark; | ||
private @IndexOrHigh("this.mString") int mPosition,mLimit, mMark; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing space in this line |
||
|
||
public UnsafeStringReader(String str) { | ||
mString = str; | ||
|
@@ -34,7 +40,9 @@ public UnsafeStringReader(String str) { | |
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
@SuppressWarnings({"return.type.incompatible", "argument.type.incompatible", "compound.assignment.type.incompatible"}) | ||
// A char is always greater than 0 and it has been previously verified that mPosition is less than the length of mString | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where/how was that verified? If it's a precondition of the method, you should express it as an annotation instead. |
||
public @GTENegativeOne int read() throws IOException { | ||
ensureOpen(); | ||
if (mPosition >= mLimit) { | ||
return -1; | ||
|
@@ -44,7 +52,11 @@ public int read() throws IOException { | |
} | ||
|
||
@Override | ||
public int read(char[] cs, int off, int len) throws IOException { | ||
@SuppressWarnings({"argument.type.incompatible", "compound.assignment.type.incompatible", "return.type.incompatible"}) /* | ||
#1 and #2. mPosition + n is at most mLimit, which is a valid index | ||
#3. Both mLimit - mPosition and len have been verified, so the returned variable is correct | ||
*/ | ||
public @GTENegativeOne @LTEqLengthOf("#1") int read(char[] cs, @IndexOrHigh("#1") int off, @NonNegative @LTLengthOf(value = "#1", offset = "#2 - 1") int len) throws IOException { | ||
ensureOpen(); | ||
if ((off < 0) || (off > cs.length) || (len < 0) || | ||
((off + len) > cs.length) || ((off + len) < 0)) { | ||
|
@@ -60,13 +72,14 @@ public int read(char[] cs, int off, int len) throws IOException { | |
} | ||
|
||
int n = Math.min(mLimit - mPosition, len); | ||
mString.getChars(mPosition, mPosition + n, cs, off); | ||
mPosition += n; | ||
return n; | ||
mString.getChars(mPosition, mPosition + n, cs, off); // #1 | ||
mPosition += n; // #2 | ||
return n; // #3 | ||
} | ||
|
||
@Override | ||
public long skip(long ns) throws IOException { | ||
@SuppressWarnings("compound.assignment.type.incompatible") // n is valid because it was previously verified | ||
public @NonNegative long skip(long ns) throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not convinced that this is the right specification for this method: I would expect that the argument |
||
ensureOpen(); | ||
if (mPosition >= mLimit) { | ||
return 0; | ||
|
@@ -90,7 +103,7 @@ public boolean markSupported() { | |
} | ||
|
||
@Override | ||
public void mark(int readAheadLimit) throws IOException { | ||
public void mark(@NonNegative int readAheadLimit) throws IOException { | ||
if (readAheadLimit < 0) { | ||
throw new IllegalArgumentException("Read-ahead limit < 0"); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,10 @@ | |
import java.io.IOException; | ||
import java.io.Writer; | ||
|
||
import org.checkerframework.checker.index.qual.IndexOrHigh; | ||
import org.checkerframework.checker.index.qual.LTLengthOf; | ||
import org.checkerframework.checker.index.qual.NonNegative; | ||
|
||
/** | ||
* Thread-unsafe StringWriter. | ||
*/ | ||
|
@@ -29,7 +33,7 @@ public UnsafeStringWriter() { | |
lock = mBuffer = new StringBuilder(); | ||
} | ||
|
||
public UnsafeStringWriter(int size) { | ||
public UnsafeStringWriter(@NonNegative int size) { | ||
if (size < 0) { | ||
throw new IllegalArgumentException("Negative buffer size"); | ||
} | ||
|
@@ -48,7 +52,7 @@ public void write(char[] cs) throws IOException { | |
} | ||
|
||
@Override | ||
public void write(char[] cs, int off, int len) throws IOException { | ||
public void write(char[] cs, @IndexOrHigh("#1") int off, @NonNegative @LTLengthOf(value = "#1", offset = "#2 - 1") int len) throws IOException { | ||
if ((off < 0) || (off > cs.length) || (len < 0) || | ||
((off + len) > cs.length) || ((off + len) < 0)) { | ||
throw new IndexOutOfBoundsException(); | ||
|
@@ -65,7 +69,7 @@ public void write(String str) { | |
} | ||
|
||
@Override | ||
public void write(String str, int off, int len) { | ||
public void write(String str, @IndexOrHigh("#1") int off, @NonNegative @LTLengthOf(value = "#1", offset = "#2 - 1") int len) { | ||
mBuffer.append(str.substring(off, off + len)); | ||
} | ||
|
||
|
@@ -80,7 +84,8 @@ public Writer append(CharSequence csq) { | |
} | ||
|
||
@Override | ||
public Writer append(CharSequence csq, int start, int end) { | ||
@SuppressWarnings("argument.type.incompatible") // The documentation is inherited from the overridden method. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand what you mean by this? Are you saying that the spec on the inherited method is wrong? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to say that the implementation of this method is similar to the overridden one, which is part of the JDK. That one has a documentation and specifies that the method can throw an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you clarify in the comment exactly where the warning is being suppressed, and how the exception can be thrown? Alternatively, you could add a link to the JDK documentation on the web so that a reader can easily read it themselves. As it is now, it's very confusing. |
||
public Writer append(CharSequence csq, @IndexOrHigh("#1") int start, @IndexOrHigh("#1") int end) { | ||
CharSequence cs = (csq == null ? "null" : csq); | ||
write(cs.subSequence(start, end).toString()); | ||
return this; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be latest release when this is merged (so
2.8.2
right now)