/
AccessHelper.java
212 lines (195 loc) · 7.93 KB
/
AccessHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc..
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.internal.source.annotations.xml.mocker;
import java.util.List;
import java.util.Map;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.MethodInfo;
import org.jboss.logging.Logger;
import org.hibernate.AssertionFailure;
import org.hibernate.MappingException;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.jaxb.mapping.orm.JaxbAccessType;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.metamodel.internal.source.annotations.util.JPADotNames;
import org.hibernate.metamodel.internal.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.internal.source.annotations.xml.PseudoJpaDotNames;
/**
* @author Strong Liu
*/
class AccessHelper implements JPADotNames {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
AccessHelper.class.getName()
);
static JaxbAccessType getAccessFromDefault(IndexBuilder indexBuilder) {
AnnotationInstance annotationInstance = JandexHelper.getSingleAnnotation(
indexBuilder.getAnnotations(),
PseudoJpaDotNames.DEFAULT_ACCESS
);
if ( annotationInstance == null ) {
return null;
}
else {
return JandexHelper.getEnumValue( annotationInstance, "value", JaxbAccessType.class );
}
}
static JaxbAccessType getAccessFromIdPosition(DotName className, IndexBuilder indexBuilder) {
Map<DotName, List<AnnotationInstance>> indexedAnnotations = indexBuilder.getIndexedAnnotations( className );
Map<DotName, List<AnnotationInstance>> ormAnnotations = indexBuilder.getClassInfoAnnotationsMap( className );
JaxbAccessType accessType = getAccessFromIdPosition( ormAnnotations );
if ( accessType == null ) {
accessType = getAccessFromIdPosition( indexedAnnotations );
}
if ( accessType == null ) {
ClassInfo parent = indexBuilder.getClassInfo( className );
if ( parent == null ) {
parent = indexBuilder.getIndexedClassInfo( className );
}
if ( parent != null ) {
DotName parentClassName = parent.superName();
accessType = getAccessFromIdPosition( parentClassName, indexBuilder );
}
}
return accessType;
}
private static JaxbAccessType getAccessFromIdPosition(Map<DotName, List<AnnotationInstance>> annotations) {
if ( annotations == null || annotations.isEmpty() || !( annotations.containsKey( ID ) ) ) {
return null;
}
List<AnnotationInstance> idAnnotationInstances = annotations.get( ID );
if ( CollectionHelper.isNotEmpty( idAnnotationInstances ) ) {
return processIdAnnotations( idAnnotationInstances );
}
return null;
}
private static JaxbAccessType processIdAnnotations(List<AnnotationInstance> idAnnotations) {
JaxbAccessType accessType = null;
for ( AnnotationInstance annotation : idAnnotations ) {
AnnotationTarget tmpTarget = annotation.target();
if ( tmpTarget == null ) {
throw new AssertionFailure( "@Id has no AnnotationTarget, this is mostly a internal error." );
}
if ( accessType == null ) {
accessType = annotationTargetToAccessType( tmpTarget );
}
else {
if ( !accessType.equals( annotationTargetToAccessType( tmpTarget ) ) ) {
throw new MappingException( "Inconsistent placement of @Id annotation within hierarchy " );
}
}
}
return accessType;
}
static JaxbAccessType annotationTargetToAccessType(AnnotationTarget target) {
return ( target instanceof MethodInfo ) ? JaxbAccessType.PROPERTY : JaxbAccessType.FIELD;
}
static JaxbAccessType getEntityAccess(DotName className, IndexBuilder indexBuilder) {
Map<DotName, List<AnnotationInstance>> indexedAnnotations = indexBuilder.getIndexedAnnotations( className );
Map<DotName, List<AnnotationInstance>> ormAnnotations = indexBuilder.getClassInfoAnnotationsMap( className );
JaxbAccessType accessType = getAccess( ormAnnotations );
if ( accessType == null ) {
accessType = getAccess( indexedAnnotations );
}
if ( accessType == null ) {
ClassInfo parent = indexBuilder.getClassInfo( className );
if ( parent == null ) {
parent = indexBuilder.getIndexedClassInfo( className );
}
if ( parent != null ) {
DotName parentClassName = parent.superName();
accessType = getEntityAccess( parentClassName, indexBuilder );
}
}
return accessType;
}
private static JaxbAccessType getAccess(Map<DotName, List<AnnotationInstance>> annotations) {
if ( annotations == null || annotations.isEmpty() || !isEntityObject( annotations ) ) {
return null;
}
List<AnnotationInstance> accessAnnotationInstances = annotations.get( JPADotNames.ACCESS );
if ( CollectionHelper.isNotEmpty( accessAnnotationInstances ) ) {
for ( AnnotationInstance annotationInstance : accessAnnotationInstances ) {
if ( annotationInstance.target() != null && annotationInstance.target() instanceof ClassInfo ) {
return JandexHelper.getEnumValue(
annotationInstance,
"value",
JaxbAccessType.class
);
}
}
}
return null;
}
private static boolean isEntityObject(Map<DotName, List<AnnotationInstance>> annotations) {
return annotations.containsKey( ENTITY ) || annotations.containsKey( MAPPED_SUPERCLASS ) || annotations
.containsKey( EMBEDDABLE );
}
/**
* Get {@link javax.persistence.AccessType } from {@link javax.persistence.Access @Access} on the attribute of the given class
*/
static JaxbAccessType getAccessFromAttributeAnnotation(DotName className, String attributeName, IndexBuilder indexBuilder) {
Map<DotName, List<AnnotationInstance>> indexedAnnotations = indexBuilder.getIndexedAnnotations( className );
if ( indexedAnnotations != null && indexedAnnotations.containsKey( ACCESS ) ) {
List<AnnotationInstance> annotationInstances = indexedAnnotations.get( ACCESS );
if ( CollectionHelper.isNotEmpty( annotationInstances ) ) {
for ( AnnotationInstance annotationInstance : annotationInstances ) {
AnnotationTarget indexedPropertyTarget = annotationInstance.target();
if ( indexedPropertyTarget == null ) {
continue;
}
if ( JandexHelper.getPropertyName( indexedPropertyTarget ).equals( attributeName ) ) {
JaxbAccessType accessType = JandexHelper.getEnumValue(
annotationInstance,
"value",
JaxbAccessType.class
);
/**
* here we ignore @Access(FIELD) on property (getter) and @Access(PROPERTY) on field
*/
JaxbAccessType targetAccessType = annotationTargetToAccessType( indexedPropertyTarget );
if ( accessType.equals( targetAccessType ) ) {
return targetAccessType;
}
else {
LOG.warn(
String.format(
"%s.%s has @Access on %s, but it tries to assign the access type to %s, this is not allowed by JPA spec, and will be ignored.",
className,
attributeName,
targetAccessType,
accessType
)
);
}
}
}
}
}
return null;
}
}