From 05ab431c42f980f82767fb3b725e5fe8cb263977 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Mon, 18 Mar 2013 21:32:14 +0100 Subject: [PATCH] CR-IC-417 (bad API fixed) --- .../source/tree/java/PsiNewExpressionImpl.java | 8 +++++--- .../util/src/com/intellij/util/SmartList.java | 17 ----------------- .../intellij/util/containers/ContainerUtil.java | 13 ++++++++++++- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiNewExpressionImpl.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiNewExpressionImpl.java index 6d45a5f76ca1..dc8f0bb75f6e 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiNewExpressionImpl.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/tree/java/PsiNewExpressionImpl.java @@ -30,6 +30,7 @@ import com.intellij.psi.tree.TokenSet; import com.intellij.psi.util.PsiUtil; import com.intellij.util.ArrayUtil; import com.intellij.util.SmartList; +import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -72,19 +73,20 @@ public class PsiNewExpressionImpl extends ExpressionPsiElement implements PsiNew else if (ElementType.PRIMITIVE_TYPE_BIT_SET.contains(elementType)) { assert type == null : this; PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory(); - type = factory.createPrimitiveType(child.getText(), annotations.toArray(PsiAnnotation.ARRAY_FACTORY, true)); + type = factory.createPrimitiveType(child.getText(), ContainerUtil.copyAndClear(annotations, PsiAnnotation.ARRAY_FACTORY, true)); if (stop) return type; } else if (elementType == JavaTokenType.LBRACKET) { assert type != null : this; - type = type.createArrayType(annotations.toArray(PsiAnnotation.ARRAY_FACTORY, true)); + type = type.createArrayType(ContainerUtil.copyAndClear(annotations, PsiAnnotation.ARRAY_FACTORY, true)); if (stop) return type; } else if (elementType == JavaElementType.ANONYMOUS_CLASS) { PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory(); PsiClass aClass = (PsiClass)child.getPsi(); PsiSubstitutor substitutor = aClass instanceof PsiTypeParameter ? PsiSubstitutor.EMPTY : factory.createRawSubstitutor(aClass); - type = factory.createType(aClass, substitutor, PsiUtil.getLanguageLevel(aClass), annotations.toArray(PsiAnnotation.ARRAY_FACTORY, true)); + type = factory.createType(aClass, substitutor, PsiUtil.getLanguageLevel(aClass), + ContainerUtil.copyAndClear(annotations, PsiAnnotation.ARRAY_FACTORY, true)); if (stop) return type; } } diff --git a/platform/util/src/com/intellij/util/SmartList.java b/platform/util/src/com/intellij/util/SmartList.java index ed19ef4f7a16..c106e682df73 100644 --- a/platform/util/src/com/intellij/util/SmartList.java +++ b/platform/util/src/com/intellij/util/SmartList.java @@ -267,23 +267,6 @@ public class SmartList extends AbstractList { return super.toArray(a); } - /** - * Copies list elements into new array and clears the list if requested. - * - * @param factory a factory to allocate arrays. - * @param clear clear this lists after copying. - * @return allocated array. - */ - @NotNull - public T[] toArray(@NotNull ArrayFactory factory, boolean clear) { - T[] a = factory.create(mySize); - if (size() > 0) { - a = toArray(a); - if (clear) clear(); - } - return a; - } - /** * Trims the capacity of this list to be the * list's current size. An application can use this operation to minimize diff --git a/platform/util/src/com/intellij/util/containers/ContainerUtil.java b/platform/util/src/com/intellij/util/containers/ContainerUtil.java index 8c9435f0bd25..108c0e61c1ca 100644 --- a/platform/util/src/com/intellij/util/containers/ContainerUtil.java +++ b/platform/util/src/com/intellij/util/containers/ContainerUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2013 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -1927,6 +1927,17 @@ public class ContainerUtil extends ContainerUtilRt { return ContainerUtilRt.toArray(c, sample); } + @NotNull + public static T[] copyAndClear(@NotNull Collection collection, @NotNull ArrayFactory factory, boolean clear) { + int size = collection.size(); + T[] a = factory.create(size); + if (size > 0) { + a = collection.toArray(a); + if (clear) collection.clear(); + } + return a; + } + @NotNull public static Collection toCollection(@NotNull Iterable iterable) { return iterable instanceof Collection ? (Collection)iterable : newArrayList(iterable);