Skip to content

Package: AnnotatedAccessor$AnnotatedSetter

AnnotatedAccessor$AnnotatedSetter

nameinstructionbranchcomplexitylinemethod
AnnotatedAccessor.AnnotatedSetter(Method)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
extractPropertyNameFromMethod(Method)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
extractPropertyTypeNameFromMethod(Method)
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%

Coverage

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.model.metamodel;
19:
20: import cz.cvut.kbss.jopa.exception.MetamodelInitializationException;
21:
22: import java.beans.Introspector;
23: import java.lang.reflect.Method;
24:
25: /**
26: * Class used for extracting field name from getter or setter.
27: */
28: public abstract class AnnotatedAccessor {
29: public static final String GET_PREFIX = "get";
30: public static final String SET_PREFIX = "set";
31: public static final String IS_PREFIX = "is";
32: public static final String HAS_PREFIX = "has";
33:
34: protected final Method method;
35: protected final String propertyName;
36: protected final Class<?> propertyType;
37:
38: protected AnnotatedAccessor(Method method) {
39: this.method = method;
40: this.propertyName = extractPropertyNameFromMethod(method);
41: this.propertyType = extractPropertyTypeNameFromMethod(method);
42: }
43:
44: public static AnnotatedAccessor from(Method method) {
45: if (isSetter(method)) {
46: return new AnnotatedSetter(method);
47: } else if (isGetter(method)) {
48: return new AnnotatedGetter(method);
49: } else {
50: throw new MetamodelInitializationException("Method " + method + " is neither a setter nor a getter. Only simple getters and setters following the Java naming convection can be used for OWL property annotations.");
51: }
52:
53: }
54:
55: protected abstract String extractPropertyNameFromMethod(Method m);
56:
57: protected abstract Class<?> extractPropertyTypeNameFromMethod(Method m);
58:
59: private static boolean isSetter(Method m) {
60: return m.getName().startsWith(SET_PREFIX) && m.getParameterCount() == 1;
61: }
62:
63: private static boolean isGetter(Method m) {
64: if (m.getParameterCount() != 0 || m.getReturnType()
65: .equals(Void.TYPE)) { /// getter has 0 arguments and returns something
66: return false;
67: }
68:
69: if (m.getName().startsWith(GET_PREFIX)) {
70: return true;
71: } else if (m.getReturnType().isAssignableFrom(Boolean.class) || m.getReturnType()
72: .isAssignableFrom(boolean.class)) {/// getter on bools - can start with is,get or has
73: return m.getName().startsWith(IS_PREFIX) || m.getName().startsWith(HAS_PREFIX);
74: } else {
75: return false;
76: }
77: }
78:
79: public Method getMethod() {
80: return method;
81: }
82:
83: public String getPropertyName() {
84: return propertyName;
85: }
86:
87: public Class<?> getPropertyType() {
88: return propertyType;
89: }
90:
91: private static class AnnotatedSetter extends AnnotatedAccessor {
92:
93: public AnnotatedSetter(Method method) {
94: super(method);
95: }
96:
97: @Override
98: protected Class<?> extractPropertyTypeNameFromMethod(Method m) {
99: return m.getParameterTypes()[0];
100: }
101:
102: @Override
103: protected String extractPropertyNameFromMethod(Method m) {
104: String trimmedName = m.getName().substring(SET_PREFIX.length()); /// always starts with set
105: return Introspector.decapitalize(trimmedName);
106: }
107: }
108:
109: private static class AnnotatedGetter extends AnnotatedAccessor {
110: public AnnotatedGetter(Method method) {
111: super(method);
112: }
113:
114: @Override
115: protected String extractPropertyNameFromMethod(Method m) {
116: String trimmedName;
117:
118: if (m.getName().startsWith(IS_PREFIX)) {
119: trimmedName = m.getName().substring(IS_PREFIX.length());
120: } else if (m.getName().startsWith(GET_PREFIX) || m.getName().startsWith(HAS_PREFIX)) {
121: trimmedName = m.getName().substring(GET_PREFIX.length());
122: } else {
123: throw new MetamodelInitializationException("Failed to extract property name from annotated getter.");
124: }
125: return Introspector.decapitalize(trimmedName);
126: }
127:
128: @Override
129: protected Class<?> extractPropertyTypeNameFromMethod(Method m) {
130: return m.getReturnType();
131: }
132: }
133:
134: @Override
135: public String toString() {
136: return "AnnotatedAccessor{" +
137: "method=" + method +
138: ", propertyType=" + propertyType +
139: '}';
140: }
141: }