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