Skip to contentMethod: checkForNoArgConstructor(Class)
1: /**
2: * Copyright (C) 2016 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.metamodel;
16:
17: import cz.cvut.kbss.jopa.exception.MetamodelInitializationException;
18: import cz.cvut.kbss.jopa.model.IRI;
19: import cz.cvut.kbss.jopa.model.annotations.MappedSuperclass;
20: import cz.cvut.kbss.jopa.model.annotations.OWLClass;
21:
22: /**
23: * Utility methods for processing managed types for metamodel construction.
24: */
25: class ManagedClassProcessor {
26:
27: private ManagedClassProcessor() {
28: }
29:
30: static <T> AbstractIdentifiableType<T> processManagedType(Class<T> cls) {
31: assert isManagedType(cls);
32: if (isEntityType(cls)) {
33: return processEntityType(cls);
34: } else if (isMappedSuperclassType(cls)) {
35: return processMappedSuperclassType(cls);
36: }
37: throw new IllegalArgumentException("Type " + cls + " is not a managed type.");
38: }
39:
40: static <T> EntityTypeImpl<T> processEntityType(Class<T> cls) {
41: final OWLClass c = cls.getDeclaredAnnotation(OWLClass.class);
42:
43: if (c == null) {
44: throw new MetamodelInitializationException("The class " + cls + " is not an OWLPersistence entity!");
45: }
46:
47: checkForNoArgConstructor(cls);
48:
49: return new EntityTypeImpl<>(cls.getSimpleName(), cls, IRI.create(c.iri()));
50: }
51:
52: private static <T> void checkForNoArgConstructor(Class<T> cls) {
53: try {
54: cls.getDeclaredConstructor();
55: } catch (NoSuchMethodException e) {
56: throw new MetamodelInitializationException("Class " + cls + " is missing required no-arg constructor.");
57: }
58: }
59:
60: private static <T> MappedSuperclassTypeImpl<T> processMappedSuperclassType(Class<T> cls) {
61: assert cls.getDeclaredAnnotation(MappedSuperclass.class) != null;
62:
63: return new MappedSuperclassTypeImpl<>(cls);
64: }
65:
66: static <T> Class<? super T> getManagedSupertype(Class<T> cls) {
67: if (cls.getSuperclass() != null && isManagedType(cls.getSuperclass())) {
68: return cls.getSuperclass();
69: }
70: return null;
71: }
72:
73: private static boolean isManagedType(Class<?> cls) {
74: return isEntityType(cls) || isMappedSuperclassType(cls);
75: }
76:
77: private static boolean isEntityType(Class<?> cls) {
78: return cls.getDeclaredAnnotation(OWLClass.class) != null;
79: }
80:
81: private static boolean isMappedSuperclassType(Class<?> cls) {
82: return cls.getDeclaredAnnotation(MappedSuperclass.class) != null;
83: }
84: }