optimize — use getAnnotation directly

This commit is contained in:
Vladimir Krivosheev
2014-10-17 16:29:40 +02:00
parent 6ed1e0cad6
commit aa8537e5b0
9 changed files with 36 additions and 30 deletions
@@ -31,8 +31,6 @@ import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.annotation.Annotation;
@SuppressWarnings({"deprecation"})
public class DefaultStateSerializer {
private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.components.impl.stores.DefaultStateSerializer");
@@ -59,10 +57,9 @@ public class DefaultStateSerializer {
}
if (storage != null) {
for (Annotation annotation : accessor.getAnnotations()) {
if (StorageId.class.isAssignableFrom(annotation.annotationType()) && !((StorageId)annotation).value().equals(storage.id())) {
return false;
}
StorageId storageId = accessor.getAnnotation(StorageId.class);
if (storageId != null && !storageId.value().equals(storage.id())) {
return false;
}
return storage.isDefault();
}
@@ -41,7 +41,7 @@ abstract class AbstractCollectionBinding implements Binding {
myElementType = elementType;
myTagName = tagName;
myAccessor = accessor;
myAnnotation = accessor == null ? null : XmlSerializerImpl.findAnnotation(accessor.getAnnotations(), AbstractCollection.class);
myAnnotation = accessor == null ? null : accessor.getAnnotation(AbstractCollection.class);
}
@Override
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.util.xmlb;
import org.jetbrains.annotations.NotNull;
@@ -26,9 +25,15 @@ public interface Accessor {
void write(Object o, Object value);
@Deprecated
@NotNull
/**
* @deprecated to remove in IDEA 15
*/
Annotation[] getAnnotations();
<T extends Annotation> T getAnnotation(@NotNull Class<T> annotationClass);
String getName();
Class<?> getValueClass();
@@ -85,7 +85,7 @@ class BeanBinding implements Binding {
}
//todo: optimize. Cache it.
Property property = XmlSerializerImpl.findAnnotation(accessor.getAnnotations(), Property.class);
Property property = accessor.getAnnotation(Property.class);
if (property != null && property.filter() != SerializationFilter.class) {
try {
if (!ReflectionUtil.newInstance(property.filter()).accepts(accessor, o)) {
@@ -277,23 +277,23 @@ class BeanBinding implements Binding {
return binding;
}
Attribute attribute = XmlSerializerImpl.findAnnotation(accessor.getAnnotations(), Attribute.class);
Attribute attribute = accessor.getAnnotation(Attribute.class);
if (attribute != null) {
return new AttributeBinding(accessor, attribute);
}
Tag tag = XmlSerializerImpl.findAnnotation(accessor.getAnnotations(), Tag.class);
Tag tag = accessor.getAnnotation(Tag.class);
if (tag != null && !tag.value().isEmpty()) {
return new TagBinding(accessor, tag);
}
Text text = XmlSerializerImpl.findAnnotation(accessor.getAnnotations(), Text.class);
Text text = accessor.getAnnotation(Text.class);
if (text != null) {
return new TextBinding(accessor);
}
boolean surroundWithTag = true;
Property property = XmlSerializerImpl.findAnnotation(accessor.getAnnotations(), Property.class);
Property property = accessor.getAnnotation(Property.class);
if (property != null) {
surroundWithTag = property.surroundWithTag();
}
@@ -305,7 +305,6 @@ class BeanBinding implements Binding {
return new AccessorBindingWrapper(accessor, binding);
}
OptionTag optionTag = XmlSerializerImpl.findAnnotation(accessor.getAnnotations(), OptionTag.class);
return new OptionTagBinding(accessor, optionTag);
return new OptionTagBinding(accessor, accessor.getAnnotation(OptionTag.class));
}
}
@@ -61,6 +61,11 @@ class FieldAccessor implements Accessor {
return myField.getAnnotations();
}
@Override
public <T extends Annotation> T getAnnotation(@NotNull Class<T> annotationClass) {
return myField.getAnnotation(annotationClass);
}
@Override
public String getName() {
return myField.getName();
@@ -29,7 +29,7 @@ class JDOMElementBinding implements Binding {
public JDOMElementBinding(final Accessor accessor) {
myAccessor = accessor;
final Tag tag = XmlSerializerImpl.findAnnotation(myAccessor.getAnnotations(), Tag.class);
Tag tag = myAccessor.getAnnotation(Tag.class);
assert tag != null : "jdom.Element property without @Tag annotation: " + accessor;
myTagName = tag.value();
}
@@ -61,7 +61,7 @@ class MapBinding implements Binding {
myKeyBinding = XmlSerializerImpl.getBinding(keyType);
myValueBinding = XmlSerializerImpl.getBinding(valueType);
myMapAnnotation = XmlSerializerImpl.findAnnotation(accessor.getAnnotations(), MapAnnotation.class);
myMapAnnotation = accessor.getAnnotation(MapAnnotation.class);
}
@Nullable
@@ -16,6 +16,7 @@
package com.intellij.util.xmlb;
import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -25,7 +26,6 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
class PropertyAccessor implements Accessor {
@@ -80,25 +80,21 @@ class PropertyAccessor implements Accessor {
}
}
private Annotation[] myAnnotationCache;
@Override
@NotNull
public Annotation[] getAnnotations() {
Annotation[] annotations = myAnnotationCache;
if (annotations == null) {
annotations = myAnnotationCache = calcAnnotations();
}
return annotations;
}
private Annotation[] calcAnnotations() {
List<Annotation> result = new ArrayList<Annotation>();
List<Annotation> result = new SmartList<Annotation>();
ContainerUtil.addAll(result, myReadMethod.getAnnotations());
ContainerUtil.addAll(result, myWriteMethod.getAnnotations());
return result.toArray(new Annotation[result.size()]);
}
@Override
public <T extends Annotation> T getAnnotation(@NotNull Class<T> annotationClass) {
T annotation = myReadMethod.getAnnotation(annotationClass);
return annotation == null ? myWriteMethod.getAnnotation(annotationClass) : annotation;
}
@Override
public String getName() {
return myName;
@@ -127,7 +127,11 @@ class XmlSerializerImpl {
}
@Nullable
@SuppressWarnings({"unchecked"})
@Deprecated
@SuppressWarnings({"unchecked", "unused"})
/**
* @deprecated to remove in IDEA 15
*/
static <T> T findAnnotation(Annotation[] annotations, Class<T> aClass) {
if (annotations == null) return null;