提交 7a8667bf 编写于 作者: J jjg

7193657: provide internal ArrayUtils class to simplify common usage of arrays in javac

Reviewed-by: mcimadamore, jjg
Contributed-by: vicenterz@yahoo.es
上级 c9d481ef
......@@ -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;
}
......
......@@ -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) {
......
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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;
}
......
/*
* 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;
}
}
......
/*
* 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();
......
/*
* 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;
/** <p><b>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.</b>
*/
public class ArrayUtils {
private static int calculateNewLength(int currentLength, int maxIndex) {
while (currentLength < maxIndex + 1)
currentLength = currentLength * 2;
return currentLength;
}
public static <T> T[] ensureCapacity(T[] array, int maxIndex) {
if (maxIndex < array.length) {
return array;
} else {
int newLength = calculateNewLength(array.length, maxIndex);
@SuppressWarnings("unchecked")
T[] result = (T[]) Array.newInstance(array.getClass().getComponentType(), newLength);
System.arraycopy(array, 0, result, 0, array.length);
return result;
}
}
public static byte[] ensureCapacity(byte[] array, int maxIndex) {
if (maxIndex < array.length) {
return array;
} else {
int newLength = calculateNewLength(array.length, maxIndex);
byte[] result = new byte[newLength];
System.arraycopy(array, 0, result, 0, array.length);
return result;
}
}
public static char[] ensureCapacity(char[] array, int maxIndex) {
if (maxIndex < array.length) {
return array;
} else {
int newLength = calculateNewLength(array.length, maxIndex);
char[] result = new char[newLength];
System.arraycopy(array, 0, result, 0, array.length);
return result;
}
}
public static int[] ensureCapacity(int[] array, int maxIndex) {
if (maxIndex < array.length) {
return array;
} else {
int newLength = calculateNewLength(array.length, maxIndex);
int[] result = new int[newLength];
System.arraycopy(array, 0, result, 0, array.length);
return result;
}
}
}
......@@ -25,6 +25,8 @@
package com.sun.tools.javac.util;
import java.util.Arrays;
/** A class for extensible, mutable bit sets.
*
* <p><b>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);
}
}
......
/*
* 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);
......
/*
* 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;
......
/*
* 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;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册