Skip to contentPackage: PropertyInfo
PropertyInfo
Coverage
1: /**
2: * Copyright (C) 2023 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 java.lang.annotation.Annotation;
18: import java.lang.reflect.Field;
19: import java.lang.reflect.Member;
20: import java.lang.reflect.Method;
21:
22: /**
23: * A simple wrapper that enables using annotations from methods while processing fields during metamodel creation.
24: * @see AnnotatedAccessor
25: */
26: public interface PropertyInfo {
27: <T extends Annotation> T getAnnotation(Class<T> annotationClass);
28:
29: Class<?> getType();
30:
31: String getName();
32:
33: Member getMember();
34:
35: Field getField();
36:
37: static PropertyInfo from(Field field) {
38: return new FieldInfo(field);
39: }
40:
41: static PropertyInfo from(Method method,Field field) {
42: return new MethodInfo(method,field);
43: }
44:
45: class FieldInfo implements PropertyInfo {
46: private final Field field;
47:
48: public FieldInfo(Field field) {
49: this.field = field;
50: }
51:
52: @Override
53: public Class<?> getType() {
54: return field.getType();
55: }
56:
57: @Override
58: public String getName() {
59: return field.getName();
60: }
61:
62: @Override
63: public Member getMember() {
64: return field;
65: }
66:
67: @Override
68: public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
69: return field.getAnnotation(annotationClass);
70: }
71:
72: @Override
73: public Field getField() {
74: return field;
75: }
76:
77: @Override
78: public String toString() {
79: return "FieldInfo{" +
80: "class =" + field.getDeclaringClass().getName() +
81: ", field=" + field.getName() +
82: '}';
83: }
84: }
85:
86: class MethodInfo implements PropertyInfo {
87: private final Method method;
88:
89: private final Field rawField;
90:
91: public MethodInfo(Method method, Field rawField) {
92: this.method = method;
93: this.rawField = rawField;
94: }
95:
96: @Override
97: public Class<?> getType() {
98: return rawField.getType();
99: }
100:
101: @Override
102: public String getName() {
103: return rawField.getName();
104: }
105: @Override
106: public Member getMember() {
107: return method;
108: }
109:
110: @Override
111: public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
112: return method.getAnnotation(annotationClass);
113: }
114:
115: @Override
116: public Field getField() {
117: return rawField;
118: }
119:
120: @Override
121: public String toString() {
122: return "MethodInfo{" +
123: "method=" + method +
124: ", rawField=" + rawField +
125: '}';
126: }
127: }
128: }
129: