Skip to content

Commit

Permalink
[2/6][java] More efficient col.toArray()
Browse files Browse the repository at this point in the history
 - zeroing step is skipped with col.toArray(new T[0]) or (T::new)
 - easier to optimize for the JVM
 - shorter/easier to read
  • Loading branch information
mbien committed Feb 21, 2024
1 parent d0f8ec2 commit fe5d373
Show file tree
Hide file tree
Showing 319 changed files with 469 additions and 508 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public void run() throws IOException {
// Run default target.
targets = null;
} else {
targets = (String[]) targetsL.toArray(new String[targetsL.size()]);
targets = (String[]) targetsL.toArray(new String[0]);
}
}
Properties props = new Properties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ private void stopHere(AntEvent event) {
currentFile = event.getScriptLocation();
// update variable values
Set<String> properties = event.getPropertyNames();
variables = properties.toArray(new String[properties.size()]);
variables = properties.toArray(new String[0]);
fireVariables ();
fireWatches ();
fireBreakpoints ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public void endElement (
Line l = lineCookie.getLineSet ().getCurrent (ln);
annotatables.add (l);
}
return annotatables.toArray (new Annotatable [annotatables.size ()]);
return annotatables.toArray (new Annotatable [0]);
}

private static int findIndexOf(String text, String target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public String[] getSupportedActions() {
names.add(COMMAND_MOVE);
names.add(COMMAND_COPY);
names.add(COMMAND_DELETE);
return names.toArray(new String[names.size()]);
return names.toArray(new String[0]);
}

public boolean isActionEnabled(String command, Lookup context) throws IllegalArgumentException {
Expand Down Expand Up @@ -463,7 +463,7 @@ public Union2<FileObject,String> run() {
} else if (scriptFile.hasFirst()) {
final String[] targetNameArray;
if (!targetNames.isEmpty()) {
targetNameArray = targetNames.toArray(new String[targetNames.size()]);
targetNameArray = targetNames.toArray(new String[0]);
} else {
// Run default target.
targetNameArray = null;
Expand Down Expand Up @@ -562,7 +562,7 @@ static Action[] contextMenuCustomActions(FreeformProject p) {
}
}
}
return actions.toArray(new Action[actions.size()]);
return actions.toArray(new Action[0]);
}

private static final class CustomAction extends AbstractAction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public AntArtifact[] getBuildArtifacts() {
artifact.configureId(id);
artifacts.add(artifact);
}
return artifacts.toArray(new AntArtifact[artifacts.size()]);
return artifacts.toArray(new AntArtifact[0]);
}

public static URI readArtifactLocation(Element export, PropertyEvaluator eval) {
Expand Down Expand Up @@ -214,7 +214,7 @@ public String getID() {
}

public URI[] getArtifactLocations() {
return locations.toArray(new URI[locations.size()]);
return locations.toArray(new URI[0]);
}

private void addLocation(URI u) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private PropertyEvaluator initEval() throws IOException {
String fname = XMLUtil.findText(e);
if (fname.contains("${")) { // NOI18N
// Tricky (#48230): need to listen to changes in the location of the file as well as its contents.
PropertyEvaluator intermediate = PropertyUtils.sequentialPropertyEvaluator(preprovider, defs.toArray(new PropertyProvider[defs.size()]));
PropertyEvaluator intermediate = PropertyUtils.sequentialPropertyEvaluator(preprovider, defs.toArray(new PropertyProvider[0]));
fname = intermediate.evaluate(fname);
if (fname == null) {
continue;
Expand All @@ -110,7 +110,7 @@ private PropertyEvaluator initEval() throws IOException {
}
}
}
return PropertyUtils.sequentialPropertyEvaluator(preprovider, defs.toArray(new PropertyProvider[defs.size()]));
return PropertyUtils.sequentialPropertyEvaluator(preprovider, defs.toArray(new PropertyProvider[0]));
}

public String getProperty(String prop) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public String[] getSupportedActions() {
actions.addAll(Arrays.asList(ap.getSupportedActions()));
}
LOG.log(Level.FINE, "delegates {0} report supported actions {1}", new Object[] {aps, actions});
return actions.toArray(new String[actions.size()]);
return actions.toArray(new String[0]);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private void listTargets() {
if (script != null) {
try {
List<String> targets = AntScriptUtils.getCallableTargetNames(script);
selectCombo.setModel(new DefaultComboBoxModel(targets.toArray(new String[targets.size()])));
selectCombo.setModel(new DefaultComboBoxModel(targets.toArray(new String[0])));
selectCombo.setSelectedItem("");
} catch (IOException x) {/* ignore */}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ private static String[] likelyPropertyNames(HintContext ctx) {
it.remove();
}
}
return choices.toArray(new String[choices.size()]);
return choices.toArray(new String[0]);
}

private static final String[] PROPERTY_NAME_VALUED_PROPERTY_NAMES = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
}
}
}
return (HintContext)Proxy.newProxyInstance(TestUtil.class.getClassLoader(), interfaces.toArray(new Class[interfaces.size()]), new Handler());
return (HintContext)Proxy.newProxyInstance(TestUtil.class.getClassLoader(), interfaces.toArray(new Class[0]), new Handler());
}

static void findAllInterfaces(Class c, Set<Class> interfaces) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ public static String[] getTypes() {
String type = st.nextToken().trim();
l.add(type);
}
TYPES = l.toArray(new String[l.size()]);
TYPES = l.toArray(new String[0]);
}
return TYPES;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ public static String[] getTypes() {
String type = st.nextToken().trim();
l.add(type);
}
TYPES = l.toArray(new String[l.size()]);
TYPES = l.toArray(new String[0]);
}
return TYPES;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static TreePath getTreePath(JTreeOperator treeOperator, String targetNode
path.add(0,actNode);
actNode = actNode.getParent();
}
TreeNode[] res = path.toArray(new TreeNode[path.size()]);
TreeNode[] res = path.toArray(new TreeNode[0]);
return new TreePath(res);
}
final Enumeration children = actNode.children();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static Parameter[] makeParams(Method method) {
List<Parameter> paramList = new ArrayList<Parameter>();
for (Iterator<Parameter> it = new ParamIterator(method); it.hasNext();)
paramList.add(it.next());
return paramList.toArray(new Parameter[paramList.size()]);
return paramList.toArray(new Parameter[0]);
}

private static Parameter createParameter (String name, String type, ClassFile classFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ public UniqueKeyElement[] getUniqueKeys() {
if (keys == null)
return null;

return (keys.toArray(new UniqueKeyElement[keys.size()]));
return (keys.toArray(new UniqueKeyElement[0]));
}

/** Finds a unique key by name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ private static ClassPath convertToClassPath (Project project, Path path) {
if (url == null) continue;
l.add (url);
}
URL[] urls = (URL[]) l.toArray (new URL [l.size ()]);
URL[] urls = (URL[]) l.toArray (new URL [0]);
return ClassPathSupport.createClassPath (urls);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,7 @@ public SourcePathProviderImpl (ContextProvider contextProvider) {
Set<String> disabledRoots = getRemoteDisabledSourceRoots();

synchronized (this) {
unorderedOriginalSourcePath = createClassPath (
allSourceRoots.toArray
(new FileObject [allSourceRoots.size()])
);
unorderedOriginalSourcePath = createClassPath(allSourceRoots.toArray(new FileObject[0]));

Map<String, Integer> orderIndexes = getRemoteSourceRootsOrder();
String[] unorderedOriginalRoots = getSourceRoots(unorderedOriginalSourcePath);
Expand Down Expand Up @@ -397,8 +394,7 @@ private ClassPath getAdditionalClassPath(File baseDir) {
}
}
this.additionalSourceRoots = new LinkedHashSet<String>(additionalSourceRoots);
return createClassPath(
additionalSourcePath.toArray(new FileObject[0]));
return createClassPath(additionalSourcePath.toArray(new FileObject[0]));
} catch (MalformedURLException ex) {
Exceptions.printStackTrace(ex);
return null;
Expand Down Expand Up @@ -1077,12 +1073,8 @@ private synchronized Set<String> setSourceRoots(Set<String> newRoots,
newSourcePathPermutation[i] = i;
}
sourcePathPermutation = newSourcePathPermutation;
originalSourcePath =
createClassPath(
sourcePathOriginal.toArray(new FileObject[0]));
unorderedOriginalSourcePath =
createClassPath(
unorderedSourcePathOriginal.toArray(new FileObject[0]));
originalSourcePath = createClassPath(sourcePathOriginal.toArray(new FileObject[0]));
unorderedOriginalSourcePath = createClassPath(unorderedSourcePathOriginal.toArray(new FileObject[0]));
projectSourceRoots = getSourceRoots(originalSourcePath);
}
if (newSteppingRoots.size() > 0 || removedSteppingRoots.size() > 0) {
Expand All @@ -1094,9 +1086,7 @@ private synchronized Set<String> setSourceRoots(Set<String> newRoots,
}
sourcePath.removeAll(removedSteppingRoots);
oldCP_ptr[0] = smartSteppingSourcePath;
smartSteppingSourcePath =
createClassPath(
sourcePath.toArray(new FileObject[0]));
smartSteppingSourcePath = createClassPath(sourcePath.toArray(new FileObject[0]));
newCP_ptr[0] = smartSteppingSourcePath;
}
Set<FileObject> disabledRoots = new HashSet<FileObject>(sourcePathOriginal);
Expand Down Expand Up @@ -1573,15 +1563,11 @@ private void rootsChanged() {
}
List<URL> sourcePaths = getURLRoots(originalSourcePath);
sourcePaths.addAll(added);
originalSourcePath =
SourcePathProviderImpl.createClassPath(
sourcePaths.toArray(new URL[0]));
originalSourcePath = SourcePathProviderImpl.createClassPath(sourcePaths.toArray(new URL[0]));

sourcePaths = getURLRoots(smartSteppingSourcePath);
sourcePaths.addAll(added);
smartSteppingSourcePath =
SourcePathProviderImpl.createClassPath(
sourcePaths.toArray(new URL[0]));
smartSteppingSourcePath = SourcePathProviderImpl.createClassPath(sourcePaths.toArray(new URL[0]));
}
changed = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ private Lookup getLookup () {
} catch (DataObjectNotFoundException ex) {}
}
}
return Lookups.fixed ((Object[]) l.toArray (new DataObject [l.size ()]));
return Lookups.fixed ((Object[]) l.toArray (new DataObject [0]));
}

static void notifyError(String error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ private static TruffleScope[] createScopes(JPDADebugger debugger, ObjectVariable
i = fillVars(debugger, variables, varsArr, hasReceiver, i);
scopes.add(new TruffleScope(scopeName, variables));
}
return scopes.toArray(new TruffleScope[scopes.size()]);
return scopes.toArray(new TruffleScope[0]);
}

private static int fillVars(JPDADebugger debugger, TruffleVariable[] vars, Field[] varsArr, boolean hasReceiver, int i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private TruffleStackFrame[] loadStackFrames(boolean includeInternal) {
i1 = i2 + frameSeparator.length();
depth++;
}
return truffleFrames.toArray(new TruffleStackFrame[truffleFrames.size()]);
return truffleFrames.toArray(new TruffleStackFrame[0]);
} catch (InvalidExpressionException | InvalidObjectException ex) {
return new TruffleStackFrame[] {};
} catch (NoSuchMethodException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public DebuggingTruffleTreeModel(ContextProvider lookupProvider) {
propListenerHolder = propEvent -> {
ModelListener[] mls;
synchronized (listeners) {
mls = listeners.toArray(new ModelListener[listeners.size()]);
mls = listeners.toArray(new ModelListener[0]);
}
ModelEvent event = new ModelEvent.TreeChanged(TreeModel.ROOT);
for (ModelListener ml : mls) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ final class FrameInfo {
stackFramesArray.add(sf);
}
frame = topStackFrame;
stackTrace = stackFramesArray.toArray(new DebugStackFrame[stackFramesArray.size()]);
stackTrace = stackFramesArray.toArray(new DebugStackFrame[0]);
LanguageInfo sfLang = topStackFrame.getLanguage();
boolean isHost = supportsJavaFrames && isHost(topStackFrame);
topFrame = topStackFrame.getName() + "\n" + isHost + "\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ private static void log(Connector c, Map<String, Argument> args) {
arguments.delete(arguments.length() - 2, arguments.length());
}
params.add(arguments);
record.setParameters(params.toArray(new Object[params.size()]));
record.setParameters(params.toArray(new Object[0]));
USG_LOGGER.log(record);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public Action[] getActions (NodeActionsProvider original, Object node)
} else
return actions;
myActions.addAll (Arrays.asList (actions));
return (Action[]) myActions.toArray (new Action [myActions.size ()]);
return (Action[]) myActions.toArray (new Action [0]);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void performDefaultAction(NodeActionsProvider original, Object node) thro
for (int i = 1; i < actions.length; i++) {
myActions.add(actions[i]);
}
return (Action[]) myActions.toArray(new Action[myActions.size()]);
return (Action[]) myActions.toArray(new Action[0]);
}

private String localize(String s) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public Action[] getActions (
}
}
myActions.addAll(Arrays.asList(actions));
return (Action[]) myActions.toArray(new Action[myActions.size()]);
return (Action[]) myActions.toArray(new Action[0]);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static void logSnapshotTaken(String language, JPDADebugger debugger) {
record.setLoggerName(USG_LOGGER.getName());
List<String> params = new ArrayList<>();
params.add(language);
record.setParameters(params.toArray(new Object[params.size()]));
record.setParameters(params.toArray(new Object[0]));
USG_LOGGER.log(record);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public String[] getExclusionPatterns() {
if (addedPatterns != null) {
newPatterns.addAll(addedPatterns);
}
return newPatterns.toArray(new String[newPatterns.size()]);
return newPatterns.toArray(new String[0]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1021,12 +1021,9 @@ private void initFieldsOfClass (
classStaticFields.clear();
allInheretedFields.clear();
}
this.fields = classFields.toArray (new Field [classFields.size ()]);
this.inheritedFields = allInheretedFields.toArray (
new Field [allInheretedFields.size ()]
);
this.staticFields = classStaticFields.toArray
(new Field [classStaticFields.size ()]);
this.fields = classFields.toArray(new Field[0]);
this.inheritedFields = allInheretedFields.toArray(new Field[0]);
this.staticFields = classStaticFields.toArray(new Field[0]);
}

org.netbeans.api.debugger.jpda.Field getField (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ private boolean replaceShortClassName(String[] oldNames, String[] newNames) {
}
}
if (!oldList.isEmpty()) {
oldNames = oldList.toArray(new String[oldList.size()]);
newNames = newList.toArray(new String[newList.size()]);
oldNames = oldList.toArray(new String[0]);
newNames = newList.toArray(new String[0]);
return replaceClassOrPkgName(oldNames, newNames, false);
}
return false;
Expand Down Expand Up @@ -473,8 +473,8 @@ private void processCustomCode() {
}
}
if (!oldList.isEmpty()) {
String[] oldNames = oldList.toArray(new String[oldList.size()]);
String[] newNames = newList.toArray(new String[newList.size()]);
String[] oldNames = oldList.toArray(new String[0]);
String[] newNames = newList.toArray(new String[0]);
replaced |= replaceClassOrPkgName(oldNames, newNames, false);
// also try to replace short class name
switch (refInfo.getChangeType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ public void run(CompilationController controller) throws Exception {
}

if (changeType != null) {
FileObject[] originalFiles = fileList.toArray(new FileObject[fileList.size()]);
String[] oldNames = nameList.toArray(new String[nameList.size()]);
FileObject[] originalFiles = fileList.toArray(new FileObject[0]);
String[] oldNames = nameList.toArray(new String[0]);
RefactoringInfo refInfo = new RefactoringInfo(refactoring, changeType, originalFiles, oldNames);
refactoring.getContext().add(refInfo); // to be accessible to the GuardedBlockHandlerFactoryImpl
return new RefactoringPluginImpl(refInfo);
Expand Down
Loading

0 comments on commit fe5d373

Please sign in to comment.