`
dcaoyuan
  • 浏览: 300155 次
社区版块
存档分类
最新评论

A Regress Bug in java.awt.geom.Path2D (JDK 6.0 beta 2)

阅读更多

I tested AIOTrade on newly downloaded JDK 6.0 beta 2, and got an exception instantly:

java.lang.ArrayIndexOutOfBoundsException: 0
    at java.awt.geom.Path2D$Float.moveTo(Path2D.java:322)
    at java.awt.geom.Path2D$Float.append(Path2D.java:643)
    at java.awt.geom.Path2D.append(Path2D.java:1780)

The code run good in JDK 5.0, so will it be a regress bug in JDK 6.0?

I then checked the source code: 6.0 vs 5.0, and found there were likely a bit of code omited wrongly. That is, in method body of void needRoom(boolean needMove, int newCoords), should add

                if (grow < 1) {
                    grow = 1;
                }
at the next of:
            int size = pointTypes.length;
            if (numTypes >= size) {
                int grow = size;
                if (grow > EXPAND_MAX) {
                    grow = EXPAND_MAX;
                }

The following is the proper code I've tested OK:

        void needRoom(boolean needMove, int newCoords) {
            if (needMove && numTypes == 0) {
                throw new IllegalPathStateException("missing initial moveto "+
                        "in path definition");
            }
            int size = pointTypes.length;
            if (numTypes >= size) {
                int grow = size;
                if (grow > EXPAND_MAX) {
                    grow = EXPAND_MAX;
                }
                /** fix bug:
                 * java.lang.ArrayIndexOutOfBoundsException: 0
                 *     at java.awt.geom.Path2D$Float.moveTo(Path2D.java:322)
                 *     at java.awt.geom.Path2D$Float.append(Path2D.java:643)
                 *     at java.awt.geom.Path2D.append(Path2D.java:1780)
                 */
                if (grow < 1) {
                    grow = 1;
                }
                pointTypes = Arrays.copyOf(pointTypes, size+grow);
            }
            size = floatCoords.length;
            if (numCoords + newCoords > size) {
                int grow = size;
                if (grow > EXPAND_MAX * 2) {
                    grow = EXPAND_MAX * 2;
                }
                if (grow < newCoords) {
                    grow = newCoords;
                }
                floatCoords = Arrays.copyOf(floatCoords, size+grow);
            }
        }

As I can not wait for it be fixed in JDK, so I wrote another org.aiotrade.util.awt.geom.Path2D and org.aiotrade.util.awt.geom.GeneralPath, and replaced the java.awt.geom.GeneralPath in my source tree. you can get the code at:

GeneralPath.java
Path2D.java

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics