Skip to content

Package: FactoryFinder

FactoryFinder

nameinstructionbranchcomplexitylinemethod
FactoryFinder()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
find(Class)
M: 7 C: 15
68%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 5
71%
M: 0 C: 1
100%
newInstance(Class, String)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package org.jvnet.mimepull;
12:
13: import java.util.Iterator;
14: import java.util.ServiceLoader;
15:
16: class FactoryFinder {
17:
18: static <T> T find(Class<T> factoryId) throws ReflectiveOperationException {
19: String systemProp = System.getProperty(factoryId.getName());
20:• if (systemProp != null) {
21: return newInstance(factoryId, systemProp);
22: }
23:
24: Iterator<T> loader = ServiceLoader.load(factoryId).iterator();
25:• if (loader.hasNext()) {
26: return loader.next();
27: }
28:
29: return null;
30: }
31:
32: static <T> T newInstance(Class<T> cls, String className) throws ReflectiveOperationException {
33: @SuppressWarnings("unchecked")
34: Class<T> providerClass = (Class<T>) FactoryFinder.class.getClassLoader().loadClass(className);
35: return providerClass.getConstructor().newInstance();
36: }
37:
38: }