diff options
Diffstat (limited to 'platform/util/src/com/intellij/util')
11 files changed, 193 insertions, 50 deletions
diff --git a/platform/util/src/com/intellij/util/EventDispatcher.java b/platform/util/src/com/intellij/util/EventDispatcher.java index 34361a8ff31b..e17c6e46e5f3 100644 --- a/platform/util/src/com/intellij/util/EventDispatcher.java +++ b/platform/util/src/com/intellij/util/EventDispatcher.java @@ -43,6 +43,7 @@ public class EventDispatcher<T extends EventListener> { } private EventDispatcher(@NotNull Class<T> listenerClass) { + LOG.assertTrue(listenerClass.isInterface(), "listenerClass must be an interface"); InvocationHandler handler = new InvocationHandler() { @Override @NonNls @@ -71,10 +72,7 @@ public class EventDispatcher<T extends EventListener> { }; //noinspection unchecked - myMulticaster = (T)Proxy.newProxyInstance(listenerClass.getClassLoader(), - new Class[]{listenerClass}, - handler - ); + myMulticaster = (T)Proxy.newProxyInstance(listenerClass.getClassLoader(), new Class[]{listenerClass}, handler); } @NotNull @@ -82,7 +80,7 @@ public class EventDispatcher<T extends EventListener> { return myMulticaster; } - private void dispatch(final Method method, final Object[] args) { + private void dispatch(@NotNull Method method, Object[] args) { method.setAccessible(true); for (T listener : myListeners) { diff --git a/platform/util/src/com/intellij/util/PatternUtil.java b/platform/util/src/com/intellij/util/PatternUtil.java index 7159a90d1601..0b683dddc1e6 100644 --- a/platform/util/src/com/intellij/util/PatternUtil.java +++ b/platform/util/src/com/intellij/util/PatternUtil.java @@ -17,8 +17,9 @@ package com.intellij.util; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.text.StringUtil; -import com.intellij.util.containers.HashMap; -import org.jetbrains.annotations.NonNls; +import com.intellij.util.containers.ContainerUtil; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; @@ -29,30 +30,22 @@ import java.util.regex.PatternSyntaxException; public class PatternUtil { private static final Logger LOG = Logger.getInstance("#com.intellij.util.PatternUtil"); - private static final HashMap<String, String> ourEscapeRules = new HashMap<String, String>(); + + public static final Pattern NOTHING = Pattern.compile("(a\\A)"); + + private static final Map<String, String> ourEscapeRules = ContainerUtil.newLinkedHashMap(); static { // '.' should be escaped first ourEscapeRules.put("*", ".*"); ourEscapeRules.put("?", "."); - escape2('+'); - escape2('('); - escape2(')'); - escape2('['); - escape2(']'); - escape2('/'); - escape2('^'); - escape2('$'); - escape2('{'); - escape2('}'); - escape2('|'); - } - - private static void escape2(char symbol) { - ourEscapeRules.put(String.valueOf(symbol), "\\" + symbol); + for (char c : "+()[]/^${}|".toCharArray()) { + ourEscapeRules.put(String.valueOf(c), "\\" + c); + } } - public static String convertToRegex(String mask) { + @NotNull + public static String convertToRegex(@NotNull String mask) { List<String> strings = StringUtil.split(mask, "\\"); StringBuilder pattern = new StringBuilder(); String separator = ""; @@ -69,17 +62,26 @@ public class PatternUtil { return pattern.toString(); } - public static Pattern fromMask(@NonNls String mask) { -// String pattern = mask.replaceAll("\\.", "\\.").replaceAll("\\*", ".*").replaceAll("\\?", "."); + @NotNull + public static Pattern fromMask(@NotNull String mask) { try { return Pattern.compile(convertToRegex(mask)); } catch (PatternSyntaxException e) { LOG.error(mask, e); - return Pattern.compile(""); + return NOTHING; } } + @Contract("_, !null->!null") + public static Pattern compileSafe(String pattern, Pattern def) { + try { + return Pattern.compile(pattern); + } + catch (Exception e) { + return def; + } + } /** * Finds the first match in a list os Strings. * diff --git a/platform/util/src/com/intellij/util/ReflectionUtil.java b/platform/util/src/com/intellij/util/ReflectionUtil.java index a4bf4318a76e..df33a80d4cba 100644 --- a/platform/util/src/com/intellij/util/ReflectionUtil.java +++ b/platform/util/src/com/intellij/util/ReflectionUtil.java @@ -426,6 +426,26 @@ public class ReflectionUtil { } } + /** + * {@link Class#newInstance()} cannot instantiate private classes + */ + @NotNull + public static <T> T newInstance(@NotNull Class<T> aClass, @NotNull Class... parameterTypes) { + try { + Constructor<T> constructor = aClass.getDeclaredConstructor(parameterTypes); + try { + constructor.setAccessible(true); + } + catch (SecurityException e) { + return aClass.newInstance(); + } + return constructor.newInstance(); + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + @NotNull public static <T> T createInstance(@NotNull Constructor<T> constructor, @NotNull Object... args) { try { diff --git a/platform/util/src/com/intellij/util/Restarter.java b/platform/util/src/com/intellij/util/Restarter.java index 7f1584dc73dc..01e7fc494f5b 100644 --- a/platform/util/src/com/intellij/util/Restarter.java +++ b/platform/util/src/com/intellij/util/Restarter.java @@ -153,12 +153,11 @@ public class Restarter { } public static File createTempExecutable(File executable) throws IOException { - String ext = FileUtilRt.getExtension(executable.getName()); - File copy = FileUtilRt.createTempFile(FileUtilRt.getNameWithoutExtension(executable.getName()), - StringUtil.isEmptyOrSpaces(ext) ? ".tmp" : ("." + ext), - false); - FileUtilRt.copy(executable, copy); - if (!copy.setExecutable(executable.canExecute())) throw new IOException("Cannot make file executable: " + copy); + File copy = new File(System.getProperty("user.home") + "/." + System.getProperty("idea.paths.selector") + "/restart/" + executable.getName()); + if (FileUtilRt.ensureCanCreateFile(copy)) { + FileUtilRt.copy(executable, copy); + if (!copy.setExecutable(executable.canExecute())) throw new IOException("Cannot make file executable: " + copy); + } return copy; } diff --git a/platform/util/src/com/intellij/util/containers/ContainerUtil.java b/platform/util/src/com/intellij/util/containers/ContainerUtil.java index 6052df2b701e..171cfababfa9 100644 --- a/platform/util/src/com/intellij/util/containers/ContainerUtil.java +++ b/platform/util/src/com/intellij/util/containers/ContainerUtil.java @@ -828,6 +828,7 @@ public class ContainerUtil extends ContainerUtilRt { @NotNull public static <T> List<T> findAll(@NotNull Collection<? extends T> collection, @NotNull Condition<? super T> condition) { + if (collection.isEmpty()) return emptyList(); final List<T> result = new SmartList<T>(); for (final T t : collection) { if (condition.value(t)) { diff --git a/platform/util/src/com/intellij/util/containers/LongStack.java b/platform/util/src/com/intellij/util/containers/LongStack.java new file mode 100644 index 000000000000..8e1005bd0af2 --- /dev/null +++ b/platform/util/src/com/intellij/util/containers/LongStack.java @@ -0,0 +1,75 @@ +/* + * Copyright 2000-2014 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.util.containers; + +/** + * @author lambdamix + */ +import java.util.EmptyStackException; + +public class LongStack { + private long[] data; + private int size; + public LongStack(int initialCapacity) { + data = new long[initialCapacity]; + size = 0; + } + + public LongStack() { + this(5); + } + + public void push(long t) { + if (size >= data.length) { + long[] newdata = new long[data.length * 3 / 2]; + System.arraycopy(data, 0, newdata, 0, size); + data = newdata; + } + data[size++] = t; + } + + public long peek() { + if (size == 0) throw new EmptyStackException(); + return data[size - 1]; + } + + public long pop() { + if (size == 0) throw new EmptyStackException(); + return data[--size]; + } + + public boolean empty() { + return size == 0; + } + + @Override + public boolean equals(Object o) { + if (o instanceof LongStack) { + LongStack otherStack = (LongStack)o; + if (size != otherStack.size) return false; + for (int i = 0; i < otherStack.size; i++) { + if (data[i] != otherStack.data[i]) return false; + } + return true; + } + + return false; + } + + public void clear() { + size = 0; + } +} diff --git a/platform/util/src/com/intellij/util/diff/DiffTree.java b/platform/util/src/com/intellij/util/diff/DiffTree.java index 0bab60dc87da..680b15b7d1de 100644 --- a/platform/util/src/com/intellij/util/diff/DiffTree.java +++ b/platform/util/src/com/intellij/util/diff/DiffTree.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2014 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. @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.intellij.util.diff; import com.intellij.openapi.util.Ref; @@ -39,7 +38,6 @@ public class DiffTree<OT, NT> { final FlyweightCapableTreeStructure<NT> newTree, final ShallowNodeComparator<OT, NT> comparator, final DiffTreeChangeBuilder<OT, NT> consumer) { - myOldTree = oldTree; myNewTree = newTree; myComparator = comparator; @@ -53,7 +51,7 @@ public class DiffTree<OT, NT> { new DiffTree<OT, NT>(oldTree, newTree, comparator, consumer).build(oldTree.getRoot(), newTree.getRoot(), 0); } - private static enum CompareResult { + private enum CompareResult { EQUAL, // 100% equal DRILL_DOWN_NEEDED, // element types are equal, but elements are composite TYPE_ONLY, // only element types are equal @@ -146,6 +144,7 @@ public class DiffTree<OT, NT> { newIndex++; continue; } + CompareResult c12 = looksEqual(comparator, oldChild1, newChild2); if (c12 == CompareResult.EQUAL || c12 == CompareResult.DRILL_DOWN_NEEDED || c12 == CompareResult.TYPE_ONLY) { myConsumer.nodeInserted(oldNode, newChild1, newIndex); @@ -170,6 +169,7 @@ public class DiffTree<OT, NT> { oldIndex++; continue; } + myConsumer.nodeReplaced(oldChild1, newChild1); oldIndex++; newIndex++; diff --git a/platform/util/src/com/intellij/util/io/LongInlineKeyDescriptor.java b/platform/util/src/com/intellij/util/io/LongInlineKeyDescriptor.java new file mode 100644 index 000000000000..78f1fdd02f78 --- /dev/null +++ b/platform/util/src/com/intellij/util/io/LongInlineKeyDescriptor.java @@ -0,0 +1,50 @@ +/* + * Copyright 2000-2014 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.util.io; + +import org.jetbrains.annotations.NotNull; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; + +/** + * @author lambdamix + */ +public class LongInlineKeyDescriptor implements KeyDescriptor<Long> { + public static LongInlineKeyDescriptor INSTANCE = new LongInlineKeyDescriptor(); + + @Override + public final int getHashCode(Long value) { + return value.hashCode(); + } + + @Override + public final boolean isEqual(Long val1, Long val2) { + return val1.longValue() == val2.longValue(); + } + + @Override + public final void save(@NotNull DataOutput out, Long value) throws IOException { + out.writeLong(value.longValue()); + } + + @Override + public final Long read(@NotNull DataInput in) throws IOException { + return in.readLong(); + } + +} diff --git a/platform/util/src/com/intellij/util/text/StringSearcher.java b/platform/util/src/com/intellij/util/text/StringSearcher.java index 22e9db2d9428..d5bc95f99cef 100644 --- a/platform/util/src/com/intellij/util/text/StringSearcher.java +++ b/platform/util/src/com/intellij/util/text/StringSearcher.java @@ -21,6 +21,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Arrays; +import java.util.Locale; public class StringSearcher { private static final Logger LOG = Logger.getInstance("#com.intellij.util.text.StringSearcher"); @@ -48,7 +49,7 @@ public class StringSearcher { myPattern = pattern; myCaseSensitive = caseSensitive; myForwardDirection = forwardDirection; - myPatternArray = myCaseSensitive ? myPattern.toCharArray() : myPattern.toLowerCase().toCharArray(); + myPatternArray = myCaseSensitive ? myPattern.toCharArray() : myPattern.toLowerCase(Locale.US).toCharArray(); myPatternLength = myPatternArray.length; Arrays.fill(mySearchTable, -1); myJavaIdentifier = pattern.isEmpty() || diff --git a/platform/util/src/com/intellij/util/ui/UIUtil.java b/platform/util/src/com/intellij/util/ui/UIUtil.java index 7ed8021bd94b..aec2a356ded0 100644 --- a/platform/util/src/com/intellij/util/ui/UIUtil.java +++ b/platform/util/src/com/intellij/util/ui/UIUtil.java @@ -1586,7 +1586,7 @@ public class UIUtil { @NotNull Graphics g, boolean useRetinaCondition, Consumer<Graphics2D> paintRoutine) { - if (!useRetinaCondition || !isRetina() || Registry.is("ide.mac.retina.disableDrawingFix", false)) { + if (!useRetinaCondition || !isRetina() || Registry.is("ide.mac.retina.disableDrawingFix")) { paintRoutine.consume((Graphics2D)g); } else { @@ -1904,6 +1904,10 @@ public class UIUtil { return INACTIVE_HEADER_COLOR; } + /** + * @deprecated + * @use JBColor.border() + */ public static Color getBorderColor() { return isUnderDarcula() ? Gray._50 : BORDER_COLOR; } @@ -2769,7 +2773,7 @@ public class UIUtil { @NotNull public static Paint getGradientPaint(float x1, float y1, @NotNull Color c1, float x2, float y2, @NotNull Color c2) { - return (Registry.is("ui.no.bangs.and.whistles", false)) ? ColorUtil.mix(c1, c2, .5) : new GradientPaint(x1, y1, c1, x2, y2, c2); + return (Registry.is("ui.no.bangs.and.whistles")) ? ColorUtil.mix(c1, c2, .5) : new GradientPaint(x1, y1, c1, x2, y2, c2); } @Nullable diff --git a/platform/util/src/com/intellij/util/xmlb/XmlSerializerImpl.java b/platform/util/src/com/intellij/util/xmlb/XmlSerializerImpl.java index c25ea4eda084..6cc2124c695a 100644 --- a/platform/util/src/com/intellij/util/xmlb/XmlSerializerImpl.java +++ b/platform/util/src/com/intellij/util/xmlb/XmlSerializerImpl.java @@ -16,13 +16,13 @@ package com.intellij.util.xmlb; import com.intellij.openapi.util.Pair; +import com.intellij.util.ReflectionUtil; import org.jdom.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.lang.annotation.Annotation; import java.lang.ref.SoftReference; -import java.lang.reflect.Constructor; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.*; @@ -184,16 +184,9 @@ class XmlSerializerImpl { /** * {@link Class#newInstance()} cannot instantiate private classes */ - static <T> T newInstance(Class<T> aClass) { + static <T> T newInstance(@NotNull Class<T> aClass) { try { - Constructor<T> constructor = aClass.getDeclaredConstructor(); - try { - constructor.setAccessible(true); - } - catch (SecurityException e) { - return aClass.newInstance(); - } - return constructor.newInstance(); + return ReflectionUtil.newInstance(aClass); } catch (Exception e) { throw new XmlSerializationException(e); |