There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]).
In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation.
Signed-off-by: Lars Grefer <eclipse@larsgrefer.de>
}
args[p++] = "-xmlConfigured";
} else {
- args = l.toArray(new String[l.size()]);
+ args = l.toArray(new String[0]);
}
}
};
file.walk(walker);
- return (IProgramElement[]) nodes.toArray(new IProgramElement[nodes.size()]);
+ return (IProgramElement[]) nodes.toArray(new IProgramElement[0]);
}
/**
for (clazz = clazz.getSuperClass(); clazz != null; clazz = clazz.getSuperClass()) {
vec.add(clazz);
}
- return vec.toArray(new JavaClass[vec.size()]);
+ return vec.toArray(new JavaClass[0]);
}
/**
ConstantPool cp = this.cpool.getFinalConstantPool();
return new JavaClass(classnameIndex, superclassnameIndex, filename, major, minor, modifiers, cp, interfaces, fields,
- methods, attributes.toArray(new Attribute[attributes.size()]));// OPTIMIZE avoid toArray()?
+ methods, attributes.toArray(new Attribute[0]));// OPTIMIZE avoid toArray()?
}
public void addInterface(String name) {
source,
startLine,
endLine,
- flags.toArray(new String[flags.size()]));
+ flags.toArray(new String[0]));
sink.addSample(sample);
// back to seeking start
}
next = anchorName.substring(start);
result.add(next);
- return result.toArray(new String[result.size()]);
+ return result.toArray(new String[0]);
}
/**
* Replace literals with literals in source string
}
// Time to construct the method itself:
- LazyMethodGen advice = new LazyMethodGen(Modifier.PUBLIC, returnType, adviceName, paramTypes.toArray(new Type[paramTypes
- .size()]), EMPTY_STRINGS, cg);
+ LazyMethodGen advice = new LazyMethodGen(Modifier.PUBLIC, returnType, adviceName, paramTypes.toArray(new Type[0]), EMPTY_STRINGS, cg);
InstructionList adviceBody = advice.getBody();
// javaArgList.add("-bootclasspath");
// javaArgList.add(parser.bootclasspath == null ? System.getProperty("user.dir") : parser.bootclasspath);
javaArgList.addAll(parser.getUnparsedArgs());
- super.configure(javaArgList.toArray(new String[javaArgList.size()]));
+ super.configure(javaArgList.toArray(new String[0]));
if (parser.getModuleInfoArgument() != null) {
IModule moduleDesc = super.getModuleDesc(parser.getModuleInfoArgument());
}
public static void setStatements(MethodDeclaration ret, List statements) {
- ret.statements = (Statement[]) statements.toArray(new Statement[statements.size()]);
+ ret.statements = (Statement[]) statements.toArray(new Statement[0]);
}
public static SingleNameReference makeLocalVariableReference(LocalVariableBinding binding) {
importedNamesList.add(world.fromBinding(topType).getName());
}
- importedNames = importedNamesList.toArray(new String[importedNamesList.size()]);
+ importedNames = importedNamesList.toArray(new String[0]);
- importedPrefixes = importedPrefixesList.toArray(new String[importedPrefixesList.size()]);
+ importedPrefixes = importedPrefixesList.toArray(new String[0]);
}
private void addClassAndParentsToPrefixes(ReferenceBinding binding, List<String> importedPrefixesList) {
declaredFields.add(factory.makeResolvedMember(f));
}
- this.declaredPointcuts = declaredPointcuts.toArray(new ResolvedPointcutDefinition[declaredPointcuts.size()]);
- this.declaredMethods = declaredMethods.toArray(new ResolvedMember[declaredMethods.size()]);
- this.declaredFields = declaredFields.toArray(new ResolvedMember[declaredFields.size()]);
+ this.declaredPointcuts = declaredPointcuts.toArray(new ResolvedPointcutDefinition[0]);
+ this.declaredMethods = declaredMethods.toArray(new ResolvedMember[0]);
+ this.declaredFields = declaredFields.toArray(new ResolvedMember[0]);
}
private final static char[] valuesCharArray = "values".toCharArray();
if (ret.isEmpty()) {
return Binding.NO_METHODS;
}
- return ret.toArray(new MethodBinding[ret.size()]);
+ return ret.toArray(new MethodBinding[0]);
}
private void addPublicITDSFrom(SourceTypeBinding anInterface, List<MethodBinding> accumulator) {
// System.err.println("got methods: " + ret + " on " + sourceTypeBinding);
- return ret.toArray(new MethodBinding[ret.size()]);
+ return ret.toArray(new MethodBinding[0]);
}
@Override
args.add(getSandboxName());
args.add("-classpath");
- args.add(getRuntimeClasspath() + File.pathSeparator +\r "../lib/junit/junit.jar;../testing-client/bin;not_found_anywhere.jar");
+ args.add(getRuntimeClasspath() + File.pathSeparator +
+ "../lib/junit/junit.jar;../testing-client/bin;not_found_anywhere.jar");
args.add(Constants.TESTDATA_PATH + "/src1/ThisAndModifiers.java");
ICommand command = new AjdtCommand();
MessageHandler myHandler = new MessageHandler();
//myHandler.setInterceptor(org.aspectj.tools.ajc.Main.MessagePrinter.TERSE);
- /*boolean result = */command.runCommand((String[])args.toArray(new String[args.size()]), myHandler);
+ /*boolean result = */command.runCommand((String[])args.toArray(new String[0]), myHandler);
//System.err.println("messages: " + Arrays.asList(myHandler.getMessages(IMessage.INFO, true)));
// DON'T yet have a way of testing that we actually got a particular info message
ICommand command = new AjdtCommand();
MessageHandler myHandler = new MessageHandler();
myHandler.setInterceptor(org.aspectj.tools.ajc.Main.MessagePrinter.TERSE);
- /*boolean result = */command.runCommand((String[])args.toArray(new String[args.size()]), myHandler);
+ /*boolean result = */command.runCommand((String[])args.toArray(new String[0]), myHandler);
assertEquals("error for org.aspectj.lang.JoinPoint not found", 1, myHandler.getErrors().length);
}
ICommand command = new AjdtCommand();
MessageHandler myHandler = new MessageHandler();
myHandler.setInterceptor(org.aspectj.tools.ajc.Main.MessagePrinter.TERSE);
- boolean result = command.runCommand((String[]) args.toArray(new String[args.size()]), myHandler);
+ boolean result = command.runCommand((String[]) args.toArray(new String[0]), myHandler);
System.out.println("result: " + result);
// System.out.println("errors: " + Arrays.asList(myHandler.getErrors()));
// System.out.println("warnings: " + Arrays.asList(myHandler.getWarnings()));
l.add(UnresolvedType.forSignature(sig.substring(start, i)));
}
}
- UnresolvedType[] paramTypes = l.toArray(new UnresolvedType[l.size()]);
+ UnresolvedType[] paramTypes = l.toArray(new UnresolvedType[0]);
UnresolvedType returnType = UnresolvedType.forSignature(sig.substring(i + 1, sig.length()));
return new Object[] { returnType, paramTypes };
} else {
l.add(m);
}
}
- return l.toArray(new ResolvedMember[l.size()]);
+ return l.toArray(new ResolvedMember[0]);
}
public abstract ISourceContext getSourceContext();
}
public AnnotationPatternList(List<AnnotationTypePattern> l) {
- this((AnnotationTypePattern[]) l.toArray(new AnnotationTypePattern[l.size()]));
+ this((AnnotationTypePattern[]) l.toArray(new AnnotationTypePattern[0]));
}
protected AnnotationTypePattern[] getAnnotationPatterns() {
//System.out.println(tokens);
- return new BasicTokenSource((IToken[])tokens.toArray(new IToken[tokens.size()]), context);
+ return new BasicTokenSource((IToken[])tokens.toArray(new IToken[0]), context);
}
private static String makeString(char ch) {
}
}
- ret = Test.makeAnd(ret, Test.makeCall(testMethod, (Expr[]) args.toArray(new Expr[args.size()])));
+ ret = Test.makeAnd(ret, Test.makeCall(testMethod, (Expr[]) args.toArray(new Expr[0])));
// Remember...
ifLastMatchedShadowId = shadow.shadowId;
}
public TypePatternList(List<TypePattern> l) {
- this((TypePattern[]) l.toArray(new TypePattern[l.size()]));
+ this((TypePattern[]) l.toArray(new TypePattern[0]));
}
public int size() {
}
public WildTypePattern(List<NamePattern> names, boolean includeSubtypes, int dim) {
- this((NamePattern[]) names.toArray(new NamePattern[names.size()]), includeSubtypes, dim, false, TypePatternList.EMPTY);
+ this((NamePattern[]) names.toArray(new NamePattern[0]), includeSubtypes, dim, false, TypePatternList.EMPTY);
}
public WildTypePattern(List<NamePattern> names, boolean includeSubtypes, int dim, int endPos, boolean isVarArg, TypePatternList typeParams,
TypePattern upperBound, TypePattern[] additionalInterfaceBounds, TypePattern lowerBound) {
- this((NamePattern[]) names.toArray(new NamePattern[names.size()]), includeSubtypes, dim, isVarArg, typeParams);
+ this((NamePattern[]) names.toArray(new NamePattern[0]), includeSubtypes, dim, isVarArg, typeParams);
this.end = endPos;
this.upperBound = upperBound;
this.lowerBound = lowerBound;
}
public WildTypePattern(List<NamePattern> names, boolean includeSubtypes, int dim, int endPos, boolean isVarArg, TypePatternList typeParams) {
- this((NamePattern[]) names.toArray(new NamePattern[names.size()]), includeSubtypes, dim, isVarArg, typeParams);
+ this((NamePattern[]) names.toArray(new NamePattern[0]), includeSubtypes, dim, isVarArg, typeParams);
this.end = endPos;
}
}
}
}
- return ret.toArray(new String[ret.size()]);
+ return ret.toArray(new String[0]);
}
// public void postRead(ResolvedType enclosingType) {
protected void mungersTest(ResolvedType ty, ShadowMunger[] x) {
List<ShadowMunger> l = ty.getDeclaredShadowMungers();
- ShadowMunger[] array = (ShadowMunger[]) l.toArray(new ShadowMunger[l.size()]);
+ ShadowMunger[] array = (ShadowMunger[]) l.toArray(new ShadowMunger[0]);
TestUtil.assertSetEquals(ty + " mungers:", x, array);
}
l.add(str.substring(start, i).trim());
start = i + 1;
}
- return (String[]) l.toArray(new String[l.size()]);
+ return (String[]) l.toArray(new String[0]);
}
}
argsList.add(args[i]);
}
}
- return (String[])argsList.toArray(new String[argsList.size()]);
+ return (String[])argsList.toArray(new String[0]);
}
/**
}
}
}
- return (String[]) list.toArray(new String[list.size()]);
+ return (String[]) list.toArray(new String[0]);
}
private final Option.Value[] values;
if ((null != srcDir) && srcDir.canRead()) {
listFiles(srcDir, result, fileFilter);
}
- return result.toArray(new File[result.size()]);
+ return result.toArray(new File[0]);
}
/**
ignores.add(formalBinding.getName());
}
}
- pointcut.m_ignoreUnboundBindingForNames = ignores.toArray(new String[ignores.size()]);
+ pointcut.m_ignoreUnboundBindingForNames = ignores.toArray(new String[0]);
}
/**
if (pointcuts.size() == 0) {
this.pointcuts = ResolvedPointcutDefinition.NO_POINTCUTS;
} else {
- this.pointcuts = pointcuts.toArray(new ResolvedPointcutDefinition[pointcuts.size()]);
+ this.pointcuts = pointcuts.toArray(new ResolvedPointcutDefinition[0]);
}
resolveAnnotationDeclares(l);
public AnnotationAJ[] getAnnotations() {
initialize();
if (memberView == null && newAnnotations!=null && newAnnotations.size()!=0) {
- return newAnnotations.toArray(new AnnotationAJ[newAnnotations.size()]);
+ return newAnnotations.toArray(new AnnotationAJ[0]);
}
return null;
}
if (matches.isEmpty()) {
return EMPTY_KEYS;
} else {
- return matches.toArray(new String[matches.size()]);
+ return matches.toArray(new String[0]);
}
}