From ddb7a73994f0fb1c5cfb7c5c2be346b6971065e6 Mon Sep 17 00:00:00 2001 From: peter Date: Thu, 20 Oct 2016 09:05:42 +0200 Subject: [PATCH] remove InstanceofCheckerGenerator since JMH shows no advantages of "instanceof" vs Class#isInstance (IDEA-CR-14734) --- platform/core-api/core-api.iml | 1 - .../com/intellij/patterns/ObjectPattern.java | 5 +- .../util/InstanceofCheckerGenerator.java | 129 ------------------ .../openapi/util/ClassConditionKey.java | 6 +- .../src/META-INF/PlatformExtensions.xml | 2 - 5 files changed, 2 insertions(+), 141 deletions(-) delete mode 100644 platform/core-api/src/com/intellij/util/InstanceofCheckerGenerator.java diff --git a/platform/core-api/core-api.iml b/platform/core-api/core-api.iml index d8bbd4ebc7c8..b56ccffb06aa 100644 --- a/platform/core-api/core-api.iml +++ b/platform/core-api/core-api.iml @@ -16,6 +16,5 @@ - \ No newline at end of file diff --git a/platform/core-api/src/com/intellij/patterns/ObjectPattern.java b/platform/core-api/src/com/intellij/patterns/ObjectPattern.java index b1751e1b481b..20417a24f858 100644 --- a/platform/core-api/src/com/intellij/patterns/ObjectPattern.java +++ b/platform/core-api/src/com/intellij/patterns/ObjectPattern.java @@ -15,9 +15,7 @@ */ package com.intellij.patterns; -import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.Key; -import com.intellij.util.InstanceofCheckerGenerator; import com.intellij.util.PairProcessor; import com.intellij.util.ProcessingContext; import org.jetbrains.annotations.NonNls; @@ -40,9 +38,8 @@ public abstract class ObjectPattern> impl protected ObjectPattern(final Class aClass) { this(new InitialPatternCondition(aClass) { - final Condition checker = InstanceofCheckerGenerator.getInstance().getInstanceofChecker(aClass); public boolean accepts(@Nullable final Object o, final ProcessingContext context) { - return checker.value(o); + return aClass.isInstance(o); } }); } diff --git a/platform/core-api/src/com/intellij/util/InstanceofCheckerGenerator.java b/platform/core-api/src/com/intellij/util/InstanceofCheckerGenerator.java deleted file mode 100644 index 4100e19d2137..000000000000 --- a/platform/core-api/src/com/intellij/util/InstanceofCheckerGenerator.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * 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. - * 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; - -import com.intellij.openapi.util.Condition; -import com.intellij.util.containers.ConcurrentFactoryMap; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.org.objectweb.asm.ClassWriter; -import org.jetbrains.org.objectweb.asm.MethodVisitor; -import org.jetbrains.org.objectweb.asm.Opcodes; -import org.jetbrains.org.objectweb.asm.Type; - -import java.lang.reflect.Modifier; - -/** - * @author peter - */ -public class InstanceofCheckerGenerator { - private static final InstanceofCheckerGenerator ourInstance; - - static { - try { - ourInstance = new InstanceofCheckerGenerator(); - } - catch (Throwable e) { - throw new RuntimeException(e); - } - } - - public static InstanceofCheckerGenerator getInstance() { - return ourInstance; - } - - @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") - private final ConcurrentFactoryMap> myCache = new ConcurrentFactoryMap>() { - @Override - protected Condition create(final Class key) { - if (key.isAnonymousClass() || Modifier.isPrivate(key.getModifiers())) { - return new Condition() { - @Override - public boolean value(Object o) { - return key.isInstance(o); - } - }; - } - - String name = "com.intellij.util.InstanceofChecker$$$$$" + key.getName().replace('.', '$'); - //noinspection unchecked - return (Condition)ReflectionUtil.newInstance(obtainClass(key, name, generateConditionClass(key, name))); - } - }; - - private synchronized Class obtainClass(Class checkedClass, String name, byte[] bytes) { - ClassLoader loader = checkedClass.getClassLoader(); - if (loader == null) loader = InstanceofCheckerGenerator.class.getClassLoader(); - try { - return loader.loadClass(name); - } - catch (ClassNotFoundException ignore) { - } - - try { - return ReflectionUtil.defineClass(name, bytes, loader); - } - catch (Exception e) { - throw new RuntimeException(e); - } - } - - @NotNull - public Condition getInstanceofChecker(final Class someClass) { - return myCache.get(someClass); - } - - private static String toInternalName(Class someClass) { - return toInternalName(someClass.getName()); - } - - @NotNull - private static String toInternalName(String name) { - return name.replace('.', '/'); - } - - private static byte[] generateConditionClass(Class checkedClass, final String generatedName) { - ClassWriter cv = new ClassWriter(ClassWriter.COMPUTE_FRAMES); - cv.visit(Opcodes.V1_2, Modifier.PUBLIC, toInternalName(generatedName), null, toInternalName(Object.class), new String[]{toInternalName(Condition.class)}); - - defaultConstructor(cv); - - conditionValue(checkedClass, cv); - - cv.visitEnd(); - return cv.toByteArray(); - } - - private static void defaultConstructor(ClassWriter cv) { - MethodVisitor mv = cv.visitMethod(Modifier.PUBLIC, "", "()V", null, null); - mv.visitCode(); - mv.visitVarInsn(Opcodes.ALOAD, 0); - mv.visitMethodInsn(Opcodes.INVOKESPECIAL, toInternalName(Object.class), "", "()V", false); - mv.visitInsn(Opcodes.RETURN); - mv.visitMaxs(0, 0); - mv.visitEnd(); - } - - private static void conditionValue(Class checkedClass, ClassWriter cv) { - MethodVisitor mv = cv.visitMethod(Modifier.PUBLIC, "value", "(L" + toInternalName(Object.class) + ";)Z", null, null); - mv.visitCode(); - mv.visitVarInsn(Opcodes.ALOAD, 1); - mv.visitTypeInsn(Opcodes.INSTANCEOF, Type.getType(checkedClass).getInternalName()); - mv.visitInsn(Opcodes.IRETURN); - mv.visitMaxs(0, 0); - mv.visitEnd(); - } - -} diff --git a/platform/platform-api/src/com/intellij/openapi/util/ClassConditionKey.java b/platform/platform-api/src/com/intellij/openapi/util/ClassConditionKey.java index 43e47954de6c..56e80168e7a5 100644 --- a/platform/platform-api/src/com/intellij/openapi/util/ClassConditionKey.java +++ b/platform/platform-api/src/com/intellij/openapi/util/ClassConditionKey.java @@ -15,17 +15,13 @@ */ package com.intellij.openapi.util; -import com.intellij.util.InstanceofCheckerGenerator; - /** * @author peter */ public class ClassConditionKey { - private final Condition myCondition; private final Class myConditionClass; private ClassConditionKey(Class aClass) { - myCondition = InstanceofCheckerGenerator.getInstance().getInstanceofChecker(aClass); myConditionClass = aClass; } @@ -34,7 +30,7 @@ public class ClassConditionKey { } public boolean isInstance(Object o) { - return myCondition.value(o); + return myConditionClass.isInstance(o); } @Override diff --git a/platform/platform-resources/src/META-INF/PlatformExtensions.xml b/platform/platform-resources/src/META-INF/PlatformExtensions.xml index 2a2b6067ffc0..7cf3e81acdaf 100644 --- a/platform/platform-resources/src/META-INF/PlatformExtensions.xml +++ b/platform/platform-resources/src/META-INF/PlatformExtensions.xml @@ -151,8 +151,6 @@ serviceImplementation="com.intellij.reporting.FreezeLoggerImpl"/> -