Fix checkstyle in mdsal-binding-generator-impl
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / mdsal / binding / generator / util / JavassistUtils.java
index 49a348b3d70fb72c1196b4c6a6b2e33fff33755d..a2c6456c0159cfa41ebd67ad7ff7e020df5e4d52 100644 (file)
@@ -20,6 +20,8 @@ import javassist.CtClass;
 import javassist.LoaderClassPath;
 import javassist.NotFoundException;
 import javax.annotation.concurrent.GuardedBy;
+import javax.annotation.concurrent.ThreadSafe;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -27,11 +29,15 @@ import org.slf4j.LoggerFactory;
  * Users of this utility class are expected to synchronize on this instance
  * it they need to ensure atomic operations on it.
  */
+@NonNullByDefault
+@ThreadSafe
 public final class JavassistUtils {
     private static final Logger LOG = LoggerFactory.getLogger(JavassistUtils.class);
     private static final Map<ClassPool, JavassistUtils> INSTANCES = new WeakHashMap<>();
 
+    @GuardedBy("this")
     private final Map<ClassLoader, ClassPath> loaderClassPaths = new WeakHashMap<>();
+    @GuardedBy("this")
     private final ClassPool classPool;
 
     private JavassistUtils(final ClassPool pool) {
@@ -47,12 +53,7 @@ public final class JavassistUtils {
      * @return shared utility instance for specified pool
      */
     public static synchronized JavassistUtils forClassPool(final ClassPool pool) {
-        JavassistUtils ret = INSTANCES.get(requireNonNull(pool));
-        if (ret == null) {
-            ret = new JavassistUtils(pool);
-            INSTANCES.put(pool, ret);
-        }
-        return ret;
+        return INSTANCES.computeIfAbsent(requireNonNull(pool), JavassistUtils::new);
     }
 
     /**
@@ -66,6 +67,7 @@ public final class JavassistUtils {
      * @throws NotFoundException when the prototype class is not found
      */
     @Beta
+    @SuppressWarnings("checkstyle:illegalCatch")
     public synchronized CtClass instantiatePrototype(final String prototype, final String fqn,
             final ClassCustomizer customizer) throws CannotCompileException, NotFoundException {
         final CtClass result = classPool.getAndRename(prototype, fqn);
@@ -86,24 +88,20 @@ public final class JavassistUtils {
     }
 
     @GuardedBy("this")
-    public CtClass asCtClass(final Class<?> class1) {
-        return get(this.classPool, class1);
-    }
-
-    public CtClass get(final ClassPool pool, final Class<? extends Object> cls) {
+    public CtClass asCtClass(final Class<?> cls) {
         try {
-            return pool.get(cls.getName());
+            return classPool.get(cls.getName());
         } catch (NotFoundException nfe1) {
             appendClassLoaderIfMissing(cls.getClassLoader());
             try {
-                return pool.get(cls.getName());
+                return classPool.get(cls.getName());
             } catch (final NotFoundException nfe2) {
                 LOG.warn("Appending ClassClassPath for {}", cls, nfe2);
-                pool.appendClassPath(new ClassClassPath(cls));
+                classPool.appendClassPath(new ClassClassPath(cls));
                 try {
-                    return pool.get(cls.getName());
+                    return classPool.get(cls.getName());
                 } catch (NotFoundException e) {
-                    LOG.warn("Failed to load class {} from pool {}", cls, pool, e);
+                    LOG.warn("Failed to load class {} from pool {}", cls, classPool, e);
                     throw new IllegalStateException("Failed to load class", e);
                 }
             }