Skip to content
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

8270058: Use Objects.check{Index,FromIndexSize} for java.desktop #4718

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Objects;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
Expand Down Expand Up @@ -81,8 +82,7 @@ public long getFrameLength() {
public int read(float[] b, int off, int len) throws IOException {
if (b == null)
throw new NullPointerException();
if (off < 0 || len < 0 || len > b.length - off)
throw new IndexOutOfBoundsException();
Objects.checkFromIndexSize(off, len, b.length);
if (pos >= buffer_len)
return -1;
if (len == 0)
Expand Down
5 changes: 2 additions & 3 deletions src/java.desktop/share/classes/javax/swing/JTabbedPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Objects;

import javax.accessibility.Accessible;
import javax.accessibility.AccessibleComponent;
Expand Down Expand Up @@ -1789,9 +1790,7 @@ public String getToolTipText(MouseEvent event) {
}

private void checkIndex(int index) {
if (index < 0 || index >= pages.size()) {
throw new IndexOutOfBoundsException("Index: "+index+", Tab count: "+pages.size());
}
Objects.checkIndex(index, pages.size());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not replace IOOBE checks with messages. Its better to keep it old way.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Objects.checkIndex itself throws exceptions for wrong values. So this should be ok. No?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have specific message here like "Tab count" which we are not passing and will be helpful for debugging in future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restored.

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/

import java.io.*;
import java.util.Objects;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
Expand Down Expand Up @@ -196,10 +197,7 @@ void setPrinterJob(PrinterJob job, boolean pageable) {

public PageFormat getPageFormat(int pageIndex)
throws IndexOutOfBoundsException {

if (pageIndex < 0 || pageIndex >= getNumberOfPages()) {
throw new IndexOutOfBoundsException();
}
Objects.checkIndex(pageIndex, getNumberOfPages());

PageFormat pf = myPrinterJob.defaultPage();
switch (pageIndex % 2) {
Expand All @@ -225,10 +223,8 @@ String getOrientStr(PageFormat pf) {

public Printable getPrintable(int pageIndex)
throws IndexOutOfBoundsException {
Objects.checkIndex(pageIndex, getNumberOfPages());

if (pageIndex < 0 || pageIndex >= getNumberOfPages()) {
throw new IndexOutOfBoundsException();
}
if (pageIndex < 2) {
paintSquares = true;
} else {
Expand Down
16 changes: 6 additions & 10 deletions test/jdk/javax/imageio/AppletResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.ListResourceBundle;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Objects;
import java.util.Vector;

import javax.imageio.IIOException;
Expand Down Expand Up @@ -114,26 +115,23 @@ public int getNumImages(boolean allowSearch) throws IOException {
public int getWidth(int imageIndex) throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);

return 10;
}

public int getHeight(int imageIndex) throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);

return 15;
}

public Iterator getImageTypes(int imageIndex) throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);

Vector imageTypes = new Vector();
imageTypes.add(ImageTypeSpecifier.createFromBufferedImageType
Expand All @@ -150,8 +148,7 @@ public IIOMetadata getImageMetadata(int imageIndex)

if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);
if (seekForwardOnly) {
if (imageIndex < minIndex)
throw new IndexOutOfBoundsException();
Expand All @@ -165,8 +162,7 @@ public BufferedImage read(int imageIndex, ImageReadParam param)
throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);
if (seekForwardOnly) {
if (imageIndex < minIndex)
throw new IndexOutOfBoundsException();
Expand Down
7 changes: 3 additions & 4 deletions test/jdk/javax/imageio/ImageReaderReadAll.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Objects;
import java.util.Vector;

import javax.imageio.IIOImage;
Expand Down Expand Up @@ -87,8 +88,7 @@ public BufferedImage read(int imageIndex, ImageReadParam param)
throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 1 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 1);
if (seekForwardOnly) {
if (imageIndex < minIndex)
throw new IndexOutOfBoundsException();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we use checkFromToIndex​ for this IOOBE too? Used in many other places in this PR...

Please also confirm all tests modified are green after this modification..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkFromToIndex​ has different semantice with checkIndex:

  • checkFromToIndex
     * <ul>
     *  <li>{@code fromIndex < 0}</li>
     *  <li>{@code fromIndex > toIndex}</li>
     *  <li>{@code toIndex > length}</li>
     *  <li>{@code length < 0}, which is implied from the former inequalities</li>
     * </ul>
  • checkIndex
     * <ul>
     *  <li>{@code index < 0}</li>
     *  <li>{@code index >= length}</li>
     *  <li>{@code length < 0}, which is implied from the former inequalities</li>
     * </ul>

The following tests are passed after applying this patch:

test/jdk/javax/imageio/AppletResourceTest.java
test/jdk/javax/imageio/ImageReaderReadAll.java
test/jdk/javax/imageio/metadata/IIOMetadataFormat/UserPluginMetadataFormatTest.java

ClippedImages.java is a manual test, I have no environment to test it, but since it's a trivial replacement, I think it's okay..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant

if (imageIndex < minIndex)
                    throw new IndexOutOfBoundsException();

can't it be replaced with Objects.checkFromToIndex(minIndex, imageIndex, ..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, sorry for the late response. checkFromToIndex has many constraints, this code may not satisfy those constraints, so I don't think we should do that in this PR.(Maybe we can file a new PR to do replacement carefully)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK...The subject line of this PR states that checkFromToIndex will also be used but I see only checkIndex so I intervened. Anyways, the code in question is in test so not much of an issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @prsadhuk for review! I've updated the title to exclude checkFromToIndex.

Expand All @@ -101,8 +101,7 @@ public BufferedImage read(int imageIndex, ImageReadParam param)
public Iterator getImageTypes(int imageIndex) throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 1 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 1);

Vector imageTypes = new Vector();
imageTypes.add(ImageTypeSpecifier.createFromBufferedImageType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.ListResourceBundle;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Objects;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
Expand Down Expand Up @@ -115,26 +116,23 @@ public int getNumImages(boolean allowSearch) throws IOException {
public int getWidth(int imageIndex) throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);

return 10;
}

public int getHeight(int imageIndex) throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);

return 15;
}

public Iterator getImageTypes(int imageIndex) throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);

Vector imageTypes = new Vector();
imageTypes.add(ImageTypeSpecifier.createFromBufferedImageType
Expand All @@ -150,8 +148,7 @@ public IIOMetadata getImageMetadata(int imageIndex) throws IOException {

if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);
if (seekForwardOnly) {
if (imageIndex < minIndex)
throw new IndexOutOfBoundsException();
Expand All @@ -169,8 +166,7 @@ public BufferedImage read(int imageIndex, ImageReadParam param)
throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);
if (seekForwardOnly) {
if (imageIndex < minIndex)
throw new IndexOutOfBoundsException();
Expand Down