Skip to content

Package: PersistenceUnitClassFinder

PersistenceUnitClassFinder

nameinstructionbranchcomplexitylinemethod
PersistenceUnitClassFinder()
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getAttributeConverters()
M: 4 C: 8
67%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 2
100%
M: 0 C: 1
100%
getEntities()
M: 4 C: 8
67%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 2
100%
M: 0 C: 1
100%
getResultSetMappings()
M: 4 C: 8
67%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 2
100%
M: 0 C: 1
100%
resolveClasspathScanner(Configuration)
M: 7 C: 13
65%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 4
67%
M: 0 C: 1
100%
scanClasspath(Configuration)
M: 0 C: 46
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.loaders;
14:
15: import cz.cvut.kbss.jopa.exception.MetamodelInitializationException;
16: import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
17: import cz.cvut.kbss.jopa.model.annotations.SparqlResultSetMapping;
18: import cz.cvut.kbss.jopa.oom.converter.ConverterWrapper;
19: import cz.cvut.kbss.jopa.utils.Configuration;
20: import cz.cvut.kbss.jopa.utils.ReflectionUtils;
21:
22: import java.util.Map;
23: import java.util.Objects;
24: import java.util.Set;
25:
26: /**
27: * Scans classpath to discover classes relevant to persistence unit building.
28: * <p>
29: * Only classes under the package configured via {@link JOPAPersistenceProperties#SCAN_PACKAGE} are processed.
30: */
31: public class PersistenceUnitClassFinder {
32:
33: private final EntityLoader entityLoader = new EntityLoader();
34: private final ResultSetMappingLoader resultSetMappingLoader = new ResultSetMappingLoader();
35: private final ConverterLoader converterLoader = new ConverterLoader();
36:
37: private boolean scanned = false;
38:
39: /**
40: * Scans application classpath based on the {@link JOPAPersistenceProperties#SCAN_PACKAGE}, looking for classes
41: * relevant for the persistence provider.
42: * <p>
43: * These classes include:
44: * <ul> <li>Entities, i.e. classes annotated with {@link cz.cvut.kbss.jopa.model.annotations.OWLClass},</li>
45: * <li>Result result mapping classes, i.e. classes annotated with {@link cz.cvut.kbss.jopa.model.annotations.SparqlResultSetMapping}
46: * or {@link cz.cvut.kbss.jopa.model.annotations.SparqlResultSetMappings}</li> </ul>
47: *
48: * @param configuration Persistence configuration, should contain value for the {@link
49: * JOPAPersistenceProperties#SCAN_PACKAGE} property
50: * @throws IllegalArgumentException If {@link JOPAPersistenceProperties#SCAN_PACKAGE} values is missing
51: */
52: public void scanClasspath(Configuration configuration) {
53: Objects.requireNonNull(configuration);
54:• if (!configuration.contains(JOPAPersistenceProperties.SCAN_PACKAGE)) {
55: throw new IllegalArgumentException("Missing the " + JOPAPersistenceProperties.SCAN_PACKAGE + " property.");
56: }
57: String toScan = configuration.get(JOPAPersistenceProperties.SCAN_PACKAGE);
58:• if (toScan.isEmpty()) {
59: throw new IllegalArgumentException(JOPAPersistenceProperties.SCAN_PACKAGE + " property cannot be empty.");
60: }
61: final ClasspathScanner classpathScanner = resolveClasspathScanner(configuration);
62: classpathScanner.addListener(entityLoader);
63: classpathScanner.addListener(resultSetMappingLoader);
64: classpathScanner.addListener(converterLoader);
65: classpathScanner.processClasses(toScan);
66: this.scanned = true;
67: }
68:
69: private static ClasspathScanner resolveClasspathScanner(Configuration config) {
70: try {
71: final String scannerType = config.get(JOPAPersistenceProperties.CLASSPATH_SCANNER_CLASS,
72: DefaultClasspathScanner.class.getName());
73: final Class<?> scannerCls = Class.forName(scannerType);
74: return (ClasspathScanner) ReflectionUtils.instantiateUsingDefaultConstructor(scannerCls);
75: } catch (ClassNotFoundException | cz.cvut.kbss.jopa.exception.InstantiationException e) {
76: throw new MetamodelInitializationException("Unable to instantiate configured ClasspathScanner.", e);
77: }
78: }
79:
80: /**
81: * Gets entity classes found during classpath scanning.
82: *
83: * @return Set of entity classes discovered on classpath
84: */
85: public Set<Class<?>> getEntities() {
86:• assert scanned;
87: return entityLoader.getEntities();
88: }
89:
90: /**
91: * Gets {@link SparqlResultSetMapping}s found during classpath scanning.
92: *
93: * @return Set of result set mapping annotations discovered on classpath
94: */
95: public Set<SparqlResultSetMapping> getResultSetMappings() {
96:• assert scanned;
97: return resultSetMappingLoader.getMappings();
98: }
99:
100: /**
101: * Gets {@link cz.cvut.kbss.jopa.model.AttributeConverter} implementations found during classpath scanning.
102: *
103: * The converters are wrapped in an internal helper class {@link ConverterWrapper}.
104: * @return Set of custom converters
105: */
106: public Map<Class<?>, ConverterWrapper<?, ?>> getAttributeConverters() {
107:• assert scanned;
108: return converterLoader.getConverters();
109: }
110: }