|
6 | 6 |
|
7 | 7 | import static org.hibernate.search.metamodel.processor.impl.ProcessorElementUtils.flattenedAnnotations;
|
8 | 8 | import static org.hibernate.search.metamodel.processor.impl.ProcessorElementUtils.propertyElements;
|
| 9 | +import static org.hibernate.search.metamodel.processor.impl.ProcessorElementUtils.propertyName; |
9 | 10 |
|
10 | 11 | import java.io.IOException;
|
11 | 12 | import java.io.Writer;
|
|
20 | 21 |
|
21 | 22 | import org.hibernate.search.engine.backend.metamodel.IndexObjectFieldDescriptor;
|
22 | 23 | import org.hibernate.search.engine.backend.metamodel.IndexValueFieldDescriptor;
|
| 24 | +import org.hibernate.search.engine.environment.bean.BeanHolder; |
23 | 25 | import org.hibernate.search.engine.environment.bean.BeanReference;
|
| 26 | +import org.hibernate.search.mapper.pojo.bridge.IdentifierBridge; |
| 27 | +import org.hibernate.search.mapper.pojo.bridge.binding.IdentifierBindingContext; |
| 28 | +import org.hibernate.search.mapper.pojo.bridge.binding.impl.DefaultIdentifierBindingContext; |
| 29 | +import org.hibernate.search.mapper.pojo.bridge.mapping.programmatic.IdentifierBinder; |
| 30 | +import org.hibernate.search.mapper.pojo.bridge.runtime.IdentifierBridgeFromDocumentIdentifierContext; |
| 31 | +import org.hibernate.search.mapper.pojo.bridge.runtime.IdentifierBridgeToDocumentIdentifierContext; |
24 | 32 | import org.hibernate.search.mapper.pojo.mapping.definition.programmatic.ProgrammaticMappingConfigurationContext;
|
25 | 33 | import org.hibernate.search.mapper.pojo.mapping.definition.programmatic.PropertyMappingStep;
|
26 | 34 | import org.hibernate.search.mapper.pojo.mapping.definition.programmatic.TypeMappingStep;
|
|
35 | 43 | import org.hibernate.search.metamodel.processor.annotation.processing.impl.ProcessorTypeMappingAnnotationProcessor;
|
36 | 44 | import org.hibernate.search.metamodel.processor.mapping.impl.ProcessorIntrospectorContext;
|
37 | 45 | import org.hibernate.search.metamodel.processor.mapping.impl.ProcessorPojoModelsBootstrapIntrospector;
|
| 46 | +import org.hibernate.search.metamodel.processor.model.impl.HibernateSearchProcessorEnum; |
38 | 47 | import org.hibernate.search.metamodel.processor.writer.impl.MetamodelClassWriter;
|
39 | 48 | import org.hibernate.search.metamodel.processor.writer.impl.MetamodelNamesFormatter;
|
| 49 | +import org.hibernate.search.util.common.annotation.impl.SuppressJQAssistant; |
40 | 50 |
|
41 | 51 | public class IndexedEntityMetamodelAnnotationProcessor implements MetamodelAnnotationProcessor {
|
42 | 52 |
|
@@ -72,6 +82,11 @@ public void process(RoundEnvironment roundEnv) {
|
72 | 82 | .discoverJandexIndexesFromAddedTypes( false )
|
73 | 83 | .buildMissingDiscoveredJandexIndexes( false );
|
74 | 84 |
|
| 85 | + configurationContext.bridges() |
| 86 | + .subTypesOf( HibernateSearchProcessorEnum.class ) |
| 87 | + .valueBinder( HibernateSearchProcessorEnum.BINDER ) |
| 88 | + .identifierBinder( HibernateSearchProcessorEnum.BINDER ); |
| 89 | + |
75 | 90 | ProgrammaticMappingConfigurationContext programmaticMapping =
|
76 | 91 | configurationContext.programmaticMapping();
|
77 | 92 |
|
@@ -167,36 +182,83 @@ public static void processTypeAndProperties(TypeElement typeElement, TypeMapping
|
167 | 182 | AtomicReference<PropertyMappingStep> ormId = new AtomicReference<>();
|
168 | 183 | propertyElements( ctx.elements(), typeElement )
|
169 | 184 | .forEach( element -> {
|
170 |
| - PropertyMappingStep step = typeMappingContext.property( element.getSimpleName().toString() ); |
| 185 | + PropertyMappingStep step = typeMappingContext.property( propertyName( element ) ); |
171 | 186 |
|
172 |
| - flattenedAnnotations( ctx.types(), element ) |
173 |
| - .forEach( annotationMirror -> { |
174 |
| - if ( ProcessorPropertyMappingAnnotationProcessor.documentId( annotationMirror ) ) { |
175 |
| - documentId.set( step ); |
176 |
| - } |
177 |
| - else if ( ProcessorPropertyMappingAnnotationProcessor.ormId( annotationMirror ) ) { |
178 |
| - ormId.set( step ); |
179 |
| - } |
180 |
| - else { |
181 |
| - ProcessorPropertyMappingAnnotationProcessor.processor( annotationMirror ) |
182 |
| - .ifPresent( p -> p.process( |
183 |
| - step, |
184 |
| - annotationMirror, |
185 |
| - element, |
186 |
| - ctx |
187 |
| - ) ); |
188 |
| - } |
189 |
| - } ); |
| 187 | + try { |
| 188 | + flattenedAnnotations( ctx.types(), element ) |
| 189 | + .forEach( annotationMirror -> { |
| 190 | + if ( ProcessorPropertyMappingAnnotationProcessor.documentId( annotationMirror ) ) { |
| 191 | + documentId.set( step ); |
| 192 | + } |
| 193 | + else if ( ProcessorPropertyMappingAnnotationProcessor.ormId( annotationMirror ) ) { |
| 194 | + ormId.set( step ); |
| 195 | + } |
| 196 | + else { |
| 197 | + ProcessorPropertyMappingAnnotationProcessor.processor( annotationMirror ) |
| 198 | + .ifPresent( p -> p.process( |
| 199 | + step, |
| 200 | + annotationMirror, |
| 201 | + element, |
| 202 | + ctx |
| 203 | + ) ); |
| 204 | + } |
| 205 | + } ); |
| 206 | + } |
| 207 | + catch (Exception e) { |
| 208 | + ExceptionUtils.logError( ctx.messager(), e, |
| 209 | + "Unable to process Hibernate Search metamodel annotations: ", element ); |
| 210 | + } |
190 | 211 | } );
|
| 212 | + PropertyMappingStep docIdStep = null; |
191 | 213 | if ( documentId.get() != null ) {
|
192 |
| - documentId.get().documentId(); |
| 214 | + docIdStep = documentId.get(); |
193 | 215 | }
|
194 | 216 | if ( ormId.get() != null ) {
|
195 |
| - ormId.get().documentId(); |
| 217 | + docIdStep = ormId.get(); |
| 218 | + } |
| 219 | + if ( docIdStep != null ) { |
| 220 | + // Users can have custom identifier binders/bridges, but we don't care much about their impl |
| 221 | + // and since for now we are ignoring these impls we still want users to be able to generate |
| 222 | + // a partial model; without an id that wouldn't be possible... hence: |
| 223 | + docIdStep.documentId().identifierBinder( ProcessorIdentifierBinder.INSTANCE ); |
196 | 224 | }
|
197 | 225 | }
|
198 | 226 |
|
199 | 227 | private ProcessorPojoModelsBootstrapIntrospector wrapIntrospector(PojoBootstrapIntrospector introspector) {
|
200 | 228 | return new ProcessorPojoModelsBootstrapIntrospector( introspectorContext, introspector );
|
201 | 229 | }
|
| 230 | + |
| 231 | + @SuppressJQAssistant(reason = "Need to cast to an impl type to get access to not-yet exposed method") |
| 232 | + private static class ProcessorIdentifierBinder implements IdentifierBinder { |
| 233 | + |
| 234 | + static ProcessorIdentifierBinder INSTANCE = new ProcessorIdentifierBinder(); |
| 235 | + |
| 236 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 237 | + @Override |
| 238 | + public void bind(IdentifierBindingContext<?> context) { |
| 239 | + if ( context instanceof DefaultIdentifierBindingContext ctx ) { |
| 240 | + ctx.applyBridge( Object.class, BeanHolder.of( ProcessorIdentifierBridge.INSTANCE ) ); |
| 241 | + } |
| 242 | + else { |
| 243 | + context.bridge( Object.class, ProcessorIdentifierBridge.INSTANCE ); |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + private static class ProcessorIdentifierBridge implements IdentifierBridge<Object> { |
| 248 | + |
| 249 | + static ProcessorIdentifierBridge INSTANCE = new ProcessorIdentifierBridge(); |
| 250 | + |
| 251 | + @Override |
| 252 | + public String toDocumentIdentifier(Object propertyValue, IdentifierBridgeToDocumentIdentifierContext context) { |
| 253 | + return ""; |
| 254 | + } |
| 255 | + |
| 256 | + @Override |
| 257 | + public Object fromDocumentIdentifier(String documentIdentifier, |
| 258 | + IdentifierBridgeFromDocumentIdentifierContext context) { |
| 259 | + return null; |
| 260 | + } |
| 261 | + } |
| 262 | + } |
| 263 | + |
202 | 264 | }
|
0 commit comments