diff --git a/src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/TagletManager.java b/src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/TagletManager.java index 330243e84d9874eb4f5c845021cd27b92d003eca..70cee39964880a409d006b19d397db851b88b372 100644 --- a/src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/TagletManager.java +++ b/src/share/classes/com/sun/tools/doclets/internal/toolkit/taglets/TagletManager.java @@ -262,11 +262,7 @@ public class TagletManager { urls[count++] = url; } } - if (urls.length != count) { - URL[] tmp = new URL[count]; - System.arraycopy(urls, 0, tmp, 0, count); - urls = tmp; - } + urls = Arrays.copyOf(urls, count); return urls; } diff --git a/src/share/classes/com/sun/tools/javac/api/MultiTaskListener.java b/src/share/classes/com/sun/tools/javac/api/MultiTaskListener.java index 759dff38710323c278c8d3ebf662f9d59678414a..f1c21f99d54dd8923f78866b5ca0a689750e527a 100644 --- a/src/share/classes/com/sun/tools/javac/api/MultiTaskListener.java +++ b/src/share/classes/com/sun/tools/javac/api/MultiTaskListener.java @@ -79,10 +79,8 @@ public class MultiTaskListener implements TaskListener { if (ccw.unwrap(l) == listener) throw new IllegalStateException(); } - TaskListener[] newListeners = new TaskListener[listeners.length + 1]; - System.arraycopy(listeners, 0, newListeners, 0, listeners.length); - newListeners[newListeners.length - 1] = ccw.wrap(listener); - listeners = newListeners; + listeners = Arrays.copyOf(listeners, listeners.length + 1); + listeners[listeners.length - 1] = ccw.wrap(listener); } public void remove(TaskListener listener) { diff --git a/src/share/classes/com/sun/tools/javac/comp/Flow.java b/src/share/classes/com/sun/tools/javac/comp/Flow.java index 3dad6e3b2c3d197a8fec0eaf4a87a8ec7ee7c1f3..b914a225da324b686718c51a29f1eee9cae75699 100644 --- a/src/share/classes/com/sun/tools/javac/comp/Flow.java +++ b/src/share/classes/com/sun/tools/javac/comp/Flow.java @@ -1318,11 +1318,7 @@ public class Flow { * index into the vars array. */ void newVar(VarSymbol sym) { - if (nextadr == vars.length) { - VarSymbol[] newvars = new VarSymbol[nextadr * 2]; - System.arraycopy(vars, 0, newvars, 0, nextadr); - vars = newvars; - } + vars = ArrayUtils.ensureCapacity(vars, nextadr); if ((sym.flags() & FINAL) == 0) { sym.flags_field |= EFFECTIVELY_FINAL; } diff --git a/src/share/classes/com/sun/tools/javac/file/Locations.java b/src/share/classes/com/sun/tools/javac/file/Locations.java index 3a464ca818a54e232533e25f08ea46b8df32df58..1e29f9167426811350d25e394c418b5aea909581 100644 --- a/src/share/classes/com/sun/tools/javac/file/Locations.java +++ b/src/share/classes/com/sun/tools/javac/file/Locations.java @@ -733,11 +733,7 @@ public class Locations { urls[count++] = url; } } - if (urls.length != count) { - URL[] tmp = new URL[count]; - System.arraycopy(urls, 0, tmp, 0, count); - urls = tmp; - } + urls = Arrays.copyOf(urls, count); return urls; } diff --git a/src/share/classes/com/sun/tools/javac/jvm/Code.java b/src/share/classes/com/sun/tools/javac/jvm/Code.java index 15ef3812b43047889e68a210f63fb325cbd78d98..a3cffbc58e12d7b31bc11e1f63110adffb32e6e5 100644 --- a/src/share/classes/com/sun/tools/javac/jvm/Code.java +++ b/src/share/classes/com/sun/tools/javac/jvm/Code.java @@ -314,11 +314,7 @@ public class Code { */ private void emit1(int od) { if (!alive) return; - if (cp == code.length) { - byte[] newcode = new byte[cp * 2]; - System.arraycopy(code, 0, newcode, 0, cp); - code = newcode; - } + code = ArrayUtils.ensureCapacity(code, cp); code[cp++] = (byte)od; } @@ -1247,12 +1243,8 @@ public class Code { if (stackMapBuffer == null) { stackMapBuffer = new StackMapFrame[20]; - } else if (stackMapBuffer.length == stackMapBufferSize) { - StackMapFrame[] newStackMapBuffer = - new StackMapFrame[stackMapBufferSize << 1]; - System.arraycopy(stackMapBuffer, 0, newStackMapBuffer, - 0, stackMapBufferSize); - stackMapBuffer = newStackMapBuffer; + } else { + stackMapBuffer = ArrayUtils.ensureCapacity(stackMapBuffer, stackMapBufferSize); } StackMapFrame frame = stackMapBuffer[stackMapBufferSize++] = new StackMapFrame(); @@ -1320,12 +1312,10 @@ public class Code { if (stackMapTableBuffer == null) { stackMapTableBuffer = new StackMapTableFrame[20]; - } else if (stackMapTableBuffer.length == stackMapBufferSize) { - StackMapTableFrame[] newStackMapTableBuffer = - new StackMapTableFrame[stackMapBufferSize << 1]; - System.arraycopy(stackMapTableBuffer, 0, newStackMapTableBuffer, - 0, stackMapBufferSize); - stackMapTableBuffer = newStackMapTableBuffer; + } else { + stackMapTableBuffer = ArrayUtils.ensureCapacity( + stackMapTableBuffer, + stackMapBufferSize); } stackMapTableBuffer[stackMapBufferSize++] = StackMapTableFrame.getInstance(frame, lastFrame.pc, lastFrame.locals, types); @@ -1651,10 +1641,8 @@ public class Code { void lock(int register) { if (locks == null) { locks = new int[20]; - } else if (locks.length == nlocks) { - int[] newLocks = new int[locks.length << 1]; - System.arraycopy(locks, 0, newLocks, 0, locks.length); - locks = newLocks; + } else { + locks = ArrayUtils.ensureCapacity(locks, nlocks); } locks[nlocks] = register; nlocks++; @@ -1680,11 +1668,7 @@ public class Code { default: break; } - if (stacksize+2 >= stack.length) { - Type[] newstack = new Type[2*stack.length]; - System.arraycopy(stack, 0, newstack, 0, stack.length); - stack = newstack; - } + stack = ArrayUtils.ensureCapacity(stack, stacksize+2); stack[stacksize++] = t; switch (width(t)) { case 1: @@ -1871,13 +1855,7 @@ public class Code { /** Add a new local variable. */ private void addLocalVar(VarSymbol v) { int adr = v.adr; - if (adr+1 >= lvar.length) { - int newlength = lvar.length << 1; - if (newlength <= adr) newlength = adr + 10; - LocalVar[] new_lvar = new LocalVar[newlength]; - System.arraycopy(lvar, 0, new_lvar, 0, lvar.length); - lvar = new_lvar; - } + lvar = ArrayUtils.ensureCapacity(lvar, adr+1); Assert.checkNull(lvar[adr]); if (pendingJumps != null) resolvePending(); lvar[adr] = new LocalVar(v); @@ -1957,11 +1935,8 @@ public class Code { if ((var.sym.flags() & Flags.SYNTHETIC) != 0) return; if (varBuffer == null) varBuffer = new LocalVar[20]; - else if (varBufferSize >= varBuffer.length) { - LocalVar[] newVarBuffer = new LocalVar[varBufferSize*2]; - System.arraycopy(varBuffer, 0, newVarBuffer, 0, varBuffer.length); - varBuffer = newVarBuffer; - } + else + varBuffer = ArrayUtils.ensureCapacity(varBuffer, varBufferSize); varBuffer[varBufferSize++] = var; } diff --git a/src/share/classes/com/sun/tools/javac/jvm/Pool.java b/src/share/classes/com/sun/tools/javac/jvm/Pool.java index 446b1075ab2e9627f17368e0ed0d0526cf65754c..021eb0e5279b6299458024faccade2972110c8e7 100644 --- a/src/share/classes/com/sun/tools/javac/jvm/Pool.java +++ b/src/share/classes/com/sun/tools/javac/jvm/Pool.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2012, 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 @@ -32,6 +32,7 @@ import java.util.*; import com.sun.tools.javac.code.Symbol; import com.sun.tools.javac.code.Symbol.*; import com.sun.tools.javac.code.Type; +import com.sun.tools.javac.util.ArrayUtils; import com.sun.tools.javac.util.Assert; import com.sun.tools.javac.util.Filter; import com.sun.tools.javac.util.Name; @@ -91,14 +92,6 @@ public class Pool { indices.clear(); } - /** Double pool buffer in size. - */ - private void doublePool() { - Object[] newpool = new Object[pool.length * 2]; - System.arraycopy(pool, 0, newpool, 0, pool.length); - pool = newpool; - } - /** Place an object in the pool, unless it is already there. * If object is a symbol also enter its owner unless the owner is a * package. Return the object's index in the pool. @@ -114,10 +107,10 @@ public class Pool { // System.err.println("put " + value + " " + value.getClass());//DEBUG index = pp; indices.put(value, index); - if (pp == pool.length) doublePool(); + pool = ArrayUtils.ensureCapacity(pool, pp); pool[pp++] = value; if (value instanceof Long || value instanceof Double) { - if (pp == pool.length) doublePool(); + pool = ArrayUtils.ensureCapacity(pool, pp); pool[pp++] = null; } } diff --git a/src/share/classes/com/sun/tools/javac/parser/UnicodeReader.java b/src/share/classes/com/sun/tools/javac/parser/UnicodeReader.java index f1b6d8b12d6d6b8ee641575599d58db853c33e07..07b8c5cda8b752be54aca3c0c296f1c324cc7e29 100644 --- a/src/share/classes/com/sun/tools/javac/parser/UnicodeReader.java +++ b/src/share/classes/com/sun/tools/javac/parser/UnicodeReader.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2012, 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 @@ -25,13 +25,15 @@ package com.sun.tools.javac.parser; +import java.nio.CharBuffer; +import java.util.Arrays; + import com.sun.tools.javac.file.JavacFileManager; +import com.sun.tools.javac.util.ArrayUtils; import com.sun.tools.javac.util.Log; import com.sun.tools.javac.util.Name; import com.sun.tools.javac.util.Names; -import java.nio.CharBuffer; - import static com.sun.tools.javac.util.LayoutCharacters.*; /** The char reader used by the javac lexer/tokenizer. Returns the sequence of @@ -91,9 +93,7 @@ public class UnicodeReader { if (input.length > 0 && Character.isWhitespace(input[input.length - 1])) { inputLength--; } else { - char[] newInput = new char[inputLength + 1]; - System.arraycopy(input, 0, newInput, 0, input.length); - input = newInput; + input = Arrays.copyOf(input, inputLength + 1); } } buf = input; @@ -130,11 +130,7 @@ public class UnicodeReader { /** Append a character to sbuf. */ protected void putChar(char ch, boolean scan) { - if (sp == sbuf.length) { - char[] newsbuf = new char[sbuf.length * 2]; - System.arraycopy(sbuf, 0, newsbuf, 0, sbuf.length); - sbuf = newsbuf; - } + sbuf = ArrayUtils.ensureCapacity(sbuf, sp); sbuf[sp++] = ch; if (scan) scanChar(); diff --git a/src/share/classes/com/sun/tools/javac/util/ArrayUtils.java b/src/share/classes/com/sun/tools/javac/util/ArrayUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..2721fa95c34ff3f43fc074e0ab0e343655f66022 --- /dev/null +++ b/src/share/classes/com/sun/tools/javac/util/ArrayUtils.java @@ -0,0 +1,87 @@ +/* + * Copyright (c) 1999, 2012, 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 com.sun.tools.javac.util; + +import java.lang.reflect.Array; + +/**
This is NOT part of any supported API.
+ * If you write code that depends on this, you do so at your own risk.
+ * This code and its internal interfaces are subject to change or
+ * deletion without notice.
+ */
+public class ArrayUtils {
+
+ private static int calculateNewLength(int currentLength, int maxIndex) {
+ while (currentLength < maxIndex + 1)
+ currentLength = currentLength * 2;
+ return currentLength;
+ }
+
+ public static This is NOT part of any supported API.
@@ -62,9 +64,7 @@ public class Bits {
private void sizeTo(int len) {
if (bits.length < len) {
- int[] newbits = new int[len];
- System.arraycopy(bits, 0, newbits, 0, bits.length);
- bits = newbits;
+ bits = Arrays.copyOf(bits, len);
}
}
diff --git a/src/share/classes/com/sun/tools/javac/util/ByteBuffer.java b/src/share/classes/com/sun/tools/javac/util/ByteBuffer.java
index 509abac37eb4f2d03dcd32975c749c55c39dc76c..65a8c36907683b5bb4e71e4f91e131f7ca47dccb 100644
--- a/src/share/classes/com/sun/tools/javac/util/ByteBuffer.java
+++ b/src/share/classes/com/sun/tools/javac/util/ByteBuffer.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2012, 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
@@ -60,16 +60,10 @@ public class ByteBuffer {
length = 0;
}
- private void copy(int size) {
- byte[] newelems = new byte[size];
- System.arraycopy(elems, 0, newelems, 0, elems.length);
- elems = newelems;
- }
-
/** Append byte to this buffer.
*/
public void appendByte(int b) {
- if (length >= elems.length) copy(elems.length * 2);
+ elems = ArrayUtils.ensureCapacity(elems, length);
elems[length++] = (byte)b;
}
@@ -77,7 +71,7 @@ public class ByteBuffer {
* starting at given `start' offset.
*/
public void appendBytes(byte[] bs, int start, int len) {
- while (length + len > elems.length) copy(elems.length * 2);
+ elems = ArrayUtils.ensureCapacity(elems, length + len);
System.arraycopy(bs, start, elems, length, len);
length += len;
}
@@ -91,7 +85,7 @@ public class ByteBuffer {
/** Append a character as a two byte number.
*/
public void appendChar(int x) {
- while (length + 1 >= elems.length) copy(elems.length * 2);
+ elems = ArrayUtils.ensureCapacity(elems, length + 1);
elems[length ] = (byte)((x >> 8) & 0xFF);
elems[length+1] = (byte)((x ) & 0xFF);
length = length + 2;
@@ -100,7 +94,7 @@ public class ByteBuffer {
/** Append an integer as a four byte number.
*/
public void appendInt(int x) {
- while (length + 3 >= elems.length) copy(elems.length * 2);
+ elems = ArrayUtils.ensureCapacity(elems, length + 3);
elems[length ] = (byte)((x >> 24) & 0xFF);
elems[length+1] = (byte)((x >> 16) & 0xFF);
elems[length+2] = (byte)((x >> 8) & 0xFF);
diff --git a/src/share/classes/com/sun/tools/javac/util/SharedNameTable.java b/src/share/classes/com/sun/tools/javac/util/SharedNameTable.java
index 0314f8b94977513f7a5f1dc061238c1d3abc9bc9..6d34dc1fef20f80018897d40dbfcaae51a7d1430 100644
--- a/src/share/classes/com/sun/tools/javac/util/SharedNameTable.java
+++ b/src/share/classes/com/sun/tools/javac/util/SharedNameTable.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2012, 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
@@ -93,13 +93,7 @@ public class SharedNameTable extends Name.Table {
@Override
public Name fromChars(char[] cs, int start, int len) {
int nc = this.nc;
- byte[] bytes = this.bytes;
- while (nc + len * 3 >= bytes.length) {
- // System.err.println("doubling name buffer of length " + names.length + " to fit " + len + " chars");//DEBUG
- byte[] newnames = new byte[bytes.length * 2];
- System.arraycopy(bytes, 0, newnames, 0, bytes.length);
- bytes = this.bytes = newnames;
- }
+ byte[] bytes = this.bytes = ArrayUtils.ensureCapacity(this.bytes, nc + len * 3);
int nbytes = Convert.chars2utf(cs, start, bytes, nc, len) - nc;
int h = hashValue(bytes, nc, nbytes) & hashMask;
NameImpl n = hashes[h];
@@ -133,12 +127,7 @@ public class SharedNameTable extends Name.Table {
}
if (n == null) {
int nc = this.nc;
- while (nc + len > names.length) {
- // System.err.println("doubling name buffer of length + " + names.length + " to fit " + len + " bytes");//DEBUG
- byte[] newnames = new byte[names.length * 2];
- System.arraycopy(names, 0, newnames, 0, names.length);
- names = this.bytes = newnames;
- }
+ names = this.bytes = ArrayUtils.ensureCapacity(names, nc + len);
System.arraycopy(cs, start, names, nc, len);
n = new NameImpl(this);
n.index = nc;
diff --git a/src/share/classes/com/sun/tools/javap/StackMapWriter.java b/src/share/classes/com/sun/tools/javap/StackMapWriter.java
index aedcf31c1361ee0779923e7f9881dd1e180cd19e..084c536f51d1d4d35883d159d301f1cb43d90b9d 100644
--- a/src/share/classes/com/sun/tools/javap/StackMapWriter.java
+++ b/src/share/classes/com/sun/tools/javap/StackMapWriter.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2012, 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
@@ -25,10 +25,11 @@
package com.sun.tools.javap;
-import com.sun.tools.classfile.AccessFlags;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
+import com.sun.tools.classfile.AccessFlags;
import com.sun.tools.classfile.Attribute;
import com.sun.tools.classfile.Code_attribute;
import com.sun.tools.classfile.ConstantPool;
@@ -233,8 +234,7 @@ public class StackMapWriter extends InstructionDetailWriter {
StackMap prev = map.get(pc);
assert (prev != null);
int k = 251 - frame.frame_type;
- verification_type_info[] new_locals = new verification_type_info[prev.locals.length - k];
- System.arraycopy(prev.locals, 0, new_locals, 0, new_locals.length);
+ verification_type_info[] new_locals = Arrays.copyOf(prev.locals, prev.locals.length - k);
StackMap m = new StackMap(new_locals, empty);
map.put(new_pc, m);
return new_pc;