LCOV - code coverage report
Current view: top level - server/rules - PredicateClassLoader.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 12 20 60.0 %
Date: 2022-11-19 15:00:39 Functions: 3 4 75.0 %

          Line data    Source code
       1             : // Copyright (C) 2012 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.rules;
      16             : 
      17             : import com.google.common.collect.LinkedHashMultimap;
      18             : import com.google.common.collect.SetMultimap;
      19             : import com.google.gerrit.server.plugincontext.PluginSetContext;
      20             : import java.util.Collection;
      21             : 
      22             : /** Loads the classes for Prolog predicates. */
      23             : public class PredicateClassLoader extends ClassLoader {
      24             : 
      25         146 :   private final SetMultimap<String, ClassLoader> packageClassLoaderMap =
      26         146 :       LinkedHashMultimap.create();
      27             : 
      28             :   public PredicateClassLoader(
      29             :       PluginSetContext<PredicateProvider> predicateProviders, ClassLoader parent) {
      30         146 :     super(parent);
      31             : 
      32         146 :     predicateProviders.runEach(
      33             :         predicateProvider -> {
      34           0 :           for (String pkg : predicateProvider.getPackages()) {
      35           0 :             packageClassLoaderMap.put(pkg, predicateProvider.getClass().getClassLoader());
      36           0 :           }
      37           0 :         });
      38         146 :   }
      39             : 
      40             :   @Override
      41             :   protected Class<?> findClass(String className) throws ClassNotFoundException {
      42         146 :     final Collection<ClassLoader> classLoaders =
      43         146 :         packageClassLoaderMap.get(getPackageName(className));
      44         146 :     for (ClassLoader cl : classLoaders) {
      45             :       try {
      46           0 :         return Class.forName(className, true, cl);
      47           0 :       } catch (ClassNotFoundException e) {
      48             :         // ignore
      49             :       }
      50           0 :     }
      51         146 :     throw new ClassNotFoundException(className);
      52             :   }
      53             : 
      54             :   private static String getPackageName(String className) {
      55         146 :     final int pos = className.lastIndexOf('.');
      56         146 :     if (pos < 0) {
      57           0 :       return "";
      58             :     }
      59         146 :     return className.substring(0, pos);
      60             :   }
      61             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750