--- old/src/java.desktop/share/classes/java/awt/geom/Path2D.java 2015-03-06 22:55:22.759478703 +0100 +++ new/src/java.desktop/share/classes/java/awt/geom/Path2D.java 2015-03-06 22:55:22.567478706 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2006, 2015, 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 @@ -224,8 +224,8 @@ Path2D p2d = (Path2D) s; setWindingRule(p2d.windingRule); this.numTypes = p2d.numTypes; - this.pointTypes = Arrays.copyOf(p2d.pointTypes, - p2d.pointTypes.length); + // trim arrays: + this.pointTypes = Arrays.copyOf(p2d.pointTypes, this.numTypes); this.numCoords = p2d.numCoords; this.floatCoords = p2d.cloneCoordsFloat(at); } else { @@ -237,19 +237,23 @@ } } + @Override float[] cloneCoordsFloat(AffineTransform at) { + // trim arrays: float ret[]; if (at == null) { - ret = Arrays.copyOf(this.floatCoords, this.floatCoords.length); + ret = Arrays.copyOf(floatCoords, numCoords); } else { - ret = new float[floatCoords.length]; + ret = new float[numCoords]; at.transform(floatCoords, 0, ret, 0, numCoords / 2); } return ret; } + @Override double[] cloneCoordsDouble(AffineTransform at) { - double ret[] = new double[floatCoords.length]; + // trim arrays: + double ret[] = new double[numCoords]; if (at == null) { for (int i = 0; i < numCoords; i++) { ret[i] = floatCoords[i]; @@ -1061,8 +1065,8 @@ Path2D p2d = (Path2D) s; setWindingRule(p2d.windingRule); this.numTypes = p2d.numTypes; - this.pointTypes = Arrays.copyOf(p2d.pointTypes, - p2d.pointTypes.length); + // trim arrays: + this.pointTypes = Arrays.copyOf(p2d.pointTypes, this.numTypes); this.numCoords = p2d.numCoords; this.doubleCoords = p2d.cloneCoordsDouble(at); } else { @@ -1074,8 +1078,10 @@ } } + @Override float[] cloneCoordsFloat(AffineTransform at) { - float ret[] = new float[doubleCoords.length]; + // trim arrays: + float ret[] = new float[numCoords]; if (at == null) { for (int i = 0; i < numCoords; i++) { ret[i] = (float) doubleCoords[i]; @@ -1086,13 +1092,14 @@ return ret; } + @Override double[] cloneCoordsDouble(AffineTransform at) { + // trim arrays: double ret[]; if (at == null) { - ret = Arrays.copyOf(this.doubleCoords, - this.doubleCoords.length); + ret = Arrays.copyOf(doubleCoords, numCoords); } else { - ret = new double[doubleCoords.length]; + ret = new double[numCoords]; at.transform(doubleCoords, 0, ret, 0, numCoords / 2); } return ret; --- /dev/null 2015-03-06 21:19:36.009729995 +0100 +++ new/test/java/awt/geom/Path2DTrimCopy.java 2015-03-06 22:55:23.059478699 +0100 @@ -0,0 +1,269 @@ +/* + * Copyright (c) 2015, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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. + */ +package java.awt.geom; + +/** + * Using the java.awt.geom package is not allowed by jtreg but there is no + * practical mean to access Path2D fields (arrays) to check them. + * Run this test manually + * + * @summary Check Path2D copy constructor modified to trim arrays + * @run main Path2DTrimCopy + */ +public class Path2DTrimCopy { + + private static final int LEN = 87; + + private static final int GROWTH_LEN = 128; + + public static void main(String[] args) { + testPath2D_Float(); + testPath2D_Float_empty(); + + testPath2D_Double(); + testPath2D_Double_empty(); + } + + public static void testPath2D_Float() { + System.out.println("testPath2D_Float()"); + Path2D.Float cf; + + // Fill new path: + final Path2D.Float pf = new Path2D.Float(); + fillPath(pf, LEN); + + System.out.println("numTypes: " + pf.numTypes); + System.out.println("pointTypes.length: " + pf.pointTypes.length); + System.out.println("numCoords: " + pf.numCoords); + System.out.println("floatCoords.length: " + pf.floatCoords.length); + + // Test cloned path: + cf = new Path2D.Float(pf); + System.out.println("cloned pointTypes.length: " + + cf.pointTypes.length); + System.out.println("cloned floatCoords.length: " + + cf.floatCoords.length); + + if (cf.numTypes != pf.numTypes) { + fail("Invalid cloned numTypes: " + + cf.numTypes + " != " + pf.numTypes); + } + if (cf.pointTypes.length != cf.numTypes) { + fail("Invalid cloned pointTypes: " + + cf.pointTypes.length + " != " + cf.numTypes); + } + if (cf.numCoords != pf.numCoords) { + fail("Invalid cloned numCoords: " + + cf.numCoords + " != " + pf.numCoords); + } + if (cf.floatCoords.length != cf.numCoords) { + fail("Invalid cloned floatCoords: " + + cf.floatCoords.length + " != " + cf.numCoords); + } + } + + public static void testPath2D_Float_empty() { + System.out.println("testPath2D_Float_empty()"); + Path2D.Float cf; + + { + // Create empty path: + Path2D.Float pf = new Path2D.Float(); + + System.out.println("numTypes: " + pf.numTypes); + System.out.println("pointTypes.length: " + pf.pointTypes.length); + System.out.println("numCoords: " + pf.numCoords); + System.out.println("floatCoords.length: " + pf.floatCoords.length); + + // Test cloned path: + cf = new Path2D.Float(pf); + System.out.println("cloned pointTypes.length: " + + cf.pointTypes.length); + System.out.println("cloned floatCoords.length: " + + cf.floatCoords.length); + + if (cf.numTypes != pf.numTypes) { + fail("Invalid cloned numTypes: " + + cf.numTypes + " != " + pf.numTypes); + } + if (cf.pointTypes.length != cf.numTypes) { + fail("Invalid cloned pointTypes: " + + cf.pointTypes.length + " != " + cf.numTypes); + } + if (cf.numCoords != pf.numCoords) { + fail("Invalid cloned numCoords: " + + cf.numCoords + " != " + pf.numCoords); + } + if (cf.floatCoords.length != cf.numCoords) { + fail("Invalid cloned floatCoords: " + + cf.floatCoords.length + " != " + cf.numCoords); + } + } + + // Fill path: + fillPath(cf, LEN); + + System.out.println("numTypes: " + cf.numTypes); + System.out.println("pointTypes.length: " + cf.pointTypes.length); + System.out.println("numCoords: " + cf.numCoords); + System.out.println("floatCoords.length: " + cf.floatCoords.length); + + final int n = LEN + 1; + + if (cf.numTypes != n) { + fail("Invalid cloned numTypes: " + + cf.numTypes + " != " + n); + } + if (cf.pointTypes.length != GROWTH_LEN) { + fail("Invalid cloned pointTypes: " + + cf.pointTypes.length + " != " + GROWTH_LEN); + } + if (cf.numCoords != n * 2) { + fail("Invalid cloned numCoords: " + + cf.numCoords + " != " + (n * 2)); + } + if (cf.floatCoords.length != 2 * GROWTH_LEN) { + fail("Invalid cloned floatCoords: " + + cf.floatCoords.length + " != " + (2 * GROWTH_LEN)); + } + } + + public static void testPath2D_Double() { + System.out.println("testPath2D_Double()"); + Path2D.Double cd; + + // Fill new path: + final Path2D.Double pd = new Path2D.Double(); + fillPath(pd, LEN); + + System.out.println("numTypes: " + pd.numTypes); + System.out.println("pointTypes.length: " + pd.pointTypes.length); + System.out.println("numCoords: " + pd.numCoords); + System.out.println("doubleCoords.length: " + pd.doubleCoords.length); + + // Test cloned path: + cd = new Path2D.Double(pd); + System.out.println("cloned pointTypes.length: " + + cd.pointTypes.length); + System.out.println("cloned doubleCoords.length: " + + cd.doubleCoords.length); + + if (cd.numTypes != pd.numTypes) { + fail("Invalid cloned numTypes: " + + cd.numTypes + " != " + pd.numTypes); + } + if (cd.pointTypes.length != cd.numTypes) { + fail("Invalid cloned pointTypes: " + + cd.pointTypes.length + " != " + cd.numTypes); + } + if (cd.numCoords != pd.numCoords) { + fail("Invalid cloned numCoords: " + + cd.numCoords + " != " + pd.numCoords); + } + if (cd.doubleCoords.length != cd.numCoords) { + fail("Invalid cloned doubleCoords: " + + cd.doubleCoords.length + " != " + cd.numCoords); + } + } + + public static void testPath2D_Double_empty() { + System.out.println("testPath2D_Double_empty()"); + + Path2D.Double cf; + + { + // Create empty path: + Path2D.Double pf = new Path2D.Double(); + + System.out.println("numTypes: " + pf.numTypes); + System.out.println("pointTypes.length: " + pf.pointTypes.length); + System.out.println("numCoords: " + pf.numCoords); + System.out.println("floatCoords.length: " + pf.doubleCoords.length); + + // Test cloned path: + cf = new Path2D.Double(pf); + System.out.println("cloned pointTypes.length: " + + cf.pointTypes.length); + System.out.println("cloned doubleCoords.length: " + + cf.doubleCoords.length); + + if (cf.numTypes != pf.numTypes) { + fail("Invalid cloned numTypes: " + + cf.numTypes + " != " + pf.numTypes); + } + if (cf.pointTypes.length != cf.numTypes) { + fail("Invalid cloned pointTypes: " + + cf.pointTypes.length + " != " + cf.numTypes); + } + if (cf.numCoords != pf.numCoords) { + fail("Invalid cloned numCoords: " + + cf.numCoords + " != " + pf.numCoords); + } + if (cf.doubleCoords.length != cf.numCoords) { + fail("Invalid cloned doubleCoords: " + + cf.doubleCoords.length + " != " + cf.numCoords); + } + } + + // Fill path: + fillPath(cf, LEN); + + System.out.println("numTypes: " + cf.numTypes); + System.out.println("pointTypes.length: " + cf.pointTypes.length); + System.out.println("numCoords: " + cf.numCoords); + System.out.println("doubleCoords.length: " + cf.doubleCoords.length); + + final int n = LEN + 1; + + if (cf.numTypes != n) { + fail("Invalid cloned numTypes: " + + cf.numTypes + " != " + n); + } + if (cf.pointTypes.length != GROWTH_LEN) { + fail("Invalid cloned pointTypes: " + + cf.pointTypes.length + " != " + GROWTH_LEN); + } + if (cf.numCoords != n * 2) { + fail("Invalid cloned numCoords: " + + cf.numCoords + " != " + (n * 2)); + } + if (cf.doubleCoords.length != 2 * GROWTH_LEN) { + fail("Invalid cloned doubleCoords: " + + cf.doubleCoords.length + " != " + (2 * GROWTH_LEN)); + } + } + + private static void fillPath(Path2D p, int len) { + p.moveTo(0, 0); + for (int i = 0; i < len; i++) { + p.lineTo(i, i); + } + } + + private static void fail(String msg) { + System.out.println("Test Failed"); + throw new RuntimeException(msg); + } +}