Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
29 changes: 23 additions & 6 deletions src/java.desktop/share/classes/java/awt/image/Kernel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -59,21 +59,38 @@ public class Kernel implements Cloneable {
* @param width width of the kernel
* @param height height of the kernel
* @param data kernel data in row major order
* @throws IllegalArgumentException if {@code data} is null
Copy link
Member

Choose a reason for hiding this comment

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

Is there any particular reason why IllegalArgumentException was chosen instead of NullPointerException?

Copy link
Member Author

@jayathirthrao jayathirthrao Nov 6, 2025

Choose a reason for hiding this comment

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

Spec of NPE states multiple reasons of when an NPE is thrown while using null object and IAE clearly mentions that we are trying to validate input arguments and throw it when we see any issue.

Current spec already throws IAE in one of the validation scenario, so throwing the same IAE for other input argument validation is uniform.

We can say that in some parts of JDK we throw NPE for null argument verification, but using IAE in this scenario looks better.

Copy link
Member

Choose a reason for hiding this comment

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

Current spec already throws IAE in one of the validation scenario, so throwing the same IAE for other input argument validation is uniform.

We usually throw an NPE when the value is null, since you can’t validate any properties on a null reference, and it’s more specific than an IAE. Moreover, NPE was thrown before, so this won’t change the behavior.

Copy link
Member Author

Choose a reason for hiding this comment

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

NPE which was thrown before was undefined behaviour.
There is no objection in CSR request also. I would like to continue keeping usage of IAE.

If majority of reviewers have objections, we can take it up in follow-up issue.

Copy link
Member

Choose a reason for hiding this comment

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

NPE which was thrown before was undefined behaviour.
There is no objection in CSR request also. I would like to continue keeping usage of IAE.

Most of the new code in the JDK throws NullPointerException for null arguments. We already had a similar discussion recently, and most of the same arguments apply here as well:
https://mail.openjdk.org/pipermail/client-libs-dev/2025-May/028879.html
040e8dd

It also appears that this is the first usage of "@throws IllegalArgumentException" for null parameters in that package, but there are some usages of NPE for null.

If majority of reviewers have objections, we can take it up in follow-up issue.

I have filed: https://bugs.openjdk.org/browse/JDK-8371501

* @throws IllegalArgumentException if {@code width} or {@code height}
* is not positive
* @throws IllegalArgumentException if product of {@code width} and
* {@code height} overflows an int
* @throws IllegalArgumentException if the length of {@code data}
* is less than the product of {@code width} and
* {@code height}
*/
public Kernel(int width, int height, float[] data) {
this.width = width;
this.height = height;
this.xOrigin = (width-1)>>1;
this.yOrigin = (height-1)>>1;
int len = width*height;
if (data == null) {
throw new IllegalArgumentException("Data must not be null");
}
if ((width <= 0) || (height <= 0)) {
throw new IllegalArgumentException("Invalid width or height");
}
int len = 0;
try {
len = Math.multiplyExact(width, height);
} catch (ArithmeticException e) {
throw new IllegalArgumentException("width * height results in" +
" integer overflow");
}
if (data.length < len) {
throw new IllegalArgumentException("Data array too small "+
"(is "+data.length+
" and should be "+len);
}
this.width = width;
this.height = height;
this.xOrigin = (width - 1) >> 1;
this.yOrigin = (height - 1) >> 1;
this.data = new float[len];
System.arraycopy(data, 0, this.data, 0, len);

Expand Down
60 changes: 60 additions & 0 deletions test/jdk/java/awt/image/ConvolveOp/KernelInitialisationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8368729
* @summary Tests that passing invalid values to Kernel constructor
* throws only IllegalArgumentException
*/

import java.awt.image.Kernel;

public class KernelInitialisationTest {
private static void expectIllegalArgumentException(Runnable code) {
try {
code.run();
throw new RuntimeException("Expected IllegalArgumentException" +
" but no exception was thrown");
} catch (IllegalArgumentException e) {
// we expect IllegalArgumentException
}
}

private static void testKernel(int width, int height, float[] data) {
System.out.println("Testing for width: " + width + ", height: "
+ height + ", data: " + (data == null ? "null" : "not null"));
expectIllegalArgumentException(() -> new Kernel(width, height, data));
}

public static void main(String[] args) {
testKernel(-1, 1, new float[100]);
testKernel(1, -1, new float[100]);
testKernel(-1, -1, new float[100]);
testKernel(1, 1, null);

int width = 50;
int height = Integer.MAX_VALUE;
testKernel(width, height, new float[100]);
}
}