Skip to content

Package: MetamodelImpl

MetamodelImpl

nameinstructionbranchcomplexitylinemethod
MetamodelImpl()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
MetamodelImpl(Configuration)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
addUriToModuleExtractionSignature(URI)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
build(PersistenceUnitClassFinder)
M: 0 C: 50
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
checkForWeaver()
M: 9 C: 7
44%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 3 C: 3
50%
M: 0 C: 1
100%
entity(Class)
M: 0 C: 25
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getEntities()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getInferredClasses()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getManagedTypes()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getMappedEntities(String)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getMetamodel()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getModuleExtractionExtraSignature()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getNamedQueryManager()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getReferringTypes(Class)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getResultSetMappingManager()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getSignatureInternal()
M: 0 C: 8
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
initModuleExtractionSignature()
M: 11 C: 55
83%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 2 C: 10
83%
M: 0 C: 1
100%
isEntityType(Class)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$getMappedEntities$0(String, EntityType)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
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: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.model;
16:
17: import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
18: import cz.cvut.kbss.jopa.loaders.PersistenceUnitClassFinder;
19: import cz.cvut.kbss.jopa.model.metamodel.*;
20: import cz.cvut.kbss.jopa.query.NamedQueryManager;
21: import cz.cvut.kbss.jopa.query.ResultSetMappingManager;
22: import cz.cvut.kbss.jopa.sessions.MetamodelProvider;
23: import cz.cvut.kbss.jopa.utils.Configuration;
24: import cz.cvut.kbss.ontodriver.config.OntoDriverProperties;
25: import org.slf4j.Logger;
26: import org.slf4j.LoggerFactory;
27:
28: import java.net.URI;
29: import java.util.*;
30: import java.util.regex.Pattern;
31: import java.util.stream.Collectors;
32:
33: public class MetamodelImpl implements Metamodel, MetamodelProvider {
34:
35: private static final Logger LOG = LoggerFactory.getLogger(Metamodel.class);
36:
37: private static final String ASPECTJ_CLASS = "org.aspectj.weaver.loadtime.Agent";
38:
39: private Map<Class<?>, ManagedType<?>> typeMap;
40: private Map<Class<?>, EntityType<?>> entities;
41: private Set<Class<?>> inferredClasses;
42: private TypeReferenceMap typeReferenceMap;
43:
44: private NamedQueryManager namedQueryManager;
45: private ResultSetMappingManager resultSetMappingManager;
46:
47: private final Configuration configuration;
48:
49: private Set<URI> moduleExtractionSignature;
50:
51: protected MetamodelImpl() {
52: // Protected constructor for easier mocking
53: this.configuration = null;
54: }
55:
56: public MetamodelImpl(Configuration configuration) {
57: this.configuration = Objects.requireNonNull(configuration);
58: }
59:
60: /**
61: * Builds the metamodel for entities discovered by the specified entity loader.
62: *
63: * @param classFinder Finder of PU classes
64: */
65: public void build(PersistenceUnitClassFinder classFinder) {
66: Objects.requireNonNull(classFinder);
67: LOG.debug("Building metamodel...");
68: checkForWeaver();
69: classFinder.scanClasspath(configuration);
70:
71: final MetamodelBuilder metamodelBuilder = new MetamodelBuilder(configuration);
72: metamodelBuilder.buildMetamodel(classFinder);
73:
74: this.typeMap = metamodelBuilder.getTypeMap();
75: this.entities = metamodelBuilder.getEntities();
76: this.inferredClasses = metamodelBuilder.getInferredClasses();
77: this.namedQueryManager = metamodelBuilder.getNamedQueryManager();
78: this.resultSetMappingManager = metamodelBuilder.getResultSetMappingManager();
79: this.typeReferenceMap = metamodelBuilder.getTypeReferenceMap();
80: new StaticMetamodelInitializer(this).initializeStaticMetamodel();
81: }
82:
83: /**
84: * Check the class path for aspectj weaver, which is vital for using lazy loading.
85: */
86: private static void checkForWeaver() {
87: try {
88: MetamodelImpl.class.getClassLoader().loadClass(ASPECTJ_CLASS);
89: } catch (ClassNotFoundException e) {
90: LOG.error("AspectJ not found on classpath. Cannot run without AspectJ.");
91: throw new OWLPersistenceException(e);
92: }
93: }
94:
95: @Override
96: public <X> EntityTypeImpl<X> entity(Class<X> cls) {
97:• if (!isEntityType(cls)) {
98: throw new IllegalArgumentException(
99: "Class " + cls.getName() + " is not a known entity in this persistence unit.");
100: }
101: return (EntityTypeImpl<X>) entities.get(cls);
102: }
103:
104: @Override
105: public Set<EntityType<?>> getMappedEntities(String classIri) {
106: Objects.requireNonNull(classIri);
107: return entities.values().stream()
108: .filter(et -> Objects.equals(et.getIRI().toString(), classIri))
109: .collect(Collectors.toSet());
110: }
111:
112: @Override
113: public Set<EntityType<?>> getEntities() {
114: return new HashSet<>(entities.values());
115: }
116:
117: @Override
118: public Set<ManagedType<?>> getManagedTypes() {
119: return new HashSet<>(typeMap.values());
120: }
121:
122: @Override
123: public Set<Class<?>> getInferredClasses() {
124: return Collections.unmodifiableSet(inferredClasses);
125: }
126:
127: public NamedQueryManager getNamedQueryManager() {
128: return namedQueryManager;
129: }
130:
131: public ResultSetMappingManager getResultSetMappingManager() {
132: return resultSetMappingManager;
133: }
134:
135: @Override
136: public Set<URI> getModuleExtractionExtraSignature() {
137: return Collections.unmodifiableSet(getSignatureInternal());
138: }
139:
140: @Override
141: public void addUriToModuleExtractionSignature(URI uri) {
142: Objects.requireNonNull(uri);
143: synchronized (this) {
144: getSignatureInternal().add(uri);
145: }
146: }
147:
148: private synchronized Set<URI> getSignatureInternal() {
149: // This can be lazily loaded since we don't know if we'll need it
150:• if (moduleExtractionSignature == null) {
151: initModuleExtractionSignature();
152: }
153: return moduleExtractionSignature;
154: }
155:
156: private void initModuleExtractionSignature() {
157:• assert configuration != null;
158: final String sig = configuration.get(OntoDriverProperties.MODULE_EXTRACTION_SIGNATURE, "");
159:• if (sig.isEmpty()) {
160: this.moduleExtractionSignature = new HashSet<>();
161: } else {
162: final String[] signature = sig.split(Pattern.quote(OntoDriverProperties.SIGNATURE_DELIMITER));
163: this.moduleExtractionSignature = new HashSet<>(signature.length);
164: try {
165:• for (String uri : signature) {
166: moduleExtractionSignature.add(URI.create(uri));
167: }
168: } catch (IllegalArgumentException e) {
169: throw new OWLPersistenceException("Invalid URI encountered in module extraction signature.", e);
170: }
171: }
172: }
173:
174: @Override
175: public Metamodel getMetamodel() {
176: return this;
177: }
178:
179: @Override
180: public boolean isEntityType(Class<?> cls) {
181: Objects.requireNonNull(cls);
182: return entities.containsKey(cls);
183: }
184:
185: /**
186: * Gets types which contain an attribute of the specified type.
187: *
188: * @param cls Type referred to
189: * @return Set of referring types, possibly empty
190: */
191: public Set<Class<?>> getReferringTypes(Class<?> cls) {
192: return typeReferenceMap.getReferringTypes(cls);
193: }
194: }