From 9d4354333da6c0d81670ee0cab525d35b203a9fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Sun, 15 Apr 2018 02:05:45 -0300 Subject: [PATCH] Use atomic operations on concurrentmap - Take the chance to actually use the lazy loading --- .../net/sourceforge/pmd/lang/ast/xpath/Attribute.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/Attribute.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/Attribute.java index 7a759c8c48..0e7a9b721f 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/Attribute.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/Attribute.java @@ -6,8 +6,8 @@ package net.sourceforge.pmd.lang.ast.xpath; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import java.util.logging.Level; import java.util.logging.Logger; @@ -20,7 +20,7 @@ public class Attribute { private static final Logger LOG = Logger.getLogger(Attribute.class.getName()); - private static final Map DETECTED_DEPRECATED_ATTRIBUTES = new ConcurrentHashMap<>(); + private static final ConcurrentMap DETECTED_DEPRECATED_ATTRIBUTES = new ConcurrentHashMap<>(); private static final Object[] EMPTY_OBJ_ARRAY = new Object[0]; @@ -48,14 +48,14 @@ public class Attribute { return value; } - if (method.isAnnotationPresent(Deprecated.class) && LOG.isLoggable(Level.WARNING) && !DETECTED_DEPRECATED_ATTRIBUTES.containsKey(getLoggableAttributeName())) { - DETECTED_DEPRECATED_ATTRIBUTES.put(getLoggableAttributeName(), Boolean.TRUE); + if (method.isAnnotationPresent(Deprecated.class) && LOG.isLoggable(Level.WARNING) + && DETECTED_DEPRECATED_ATTRIBUTES.putIfAbsent(getLoggableAttributeName(), Boolean.TRUE) == null) { LOG.warning("Use of deprecated attribute '" + getLoggableAttributeName() + "' in xpath query"); } // this lazy loading reduces calls to Method.invoke() by about 90% try { - return method.invoke(parent, EMPTY_OBJ_ARRAY); + return value = method.invoke(parent, EMPTY_OBJ_ARRAY); } catch (IllegalAccessException | InvocationTargetException iae) { iae.printStackTrace(); }