1 19 20 package org.apache.cayenne.jpa.conf; 21 22 import java.lang.reflect.AnnotatedElement ; 23 24 import javax.persistence.AttributeOverride; 25 import javax.persistence.AttributeOverrides; 26 import javax.persistence.DiscriminatorColumn; 27 import javax.persistence.DiscriminatorValue; 28 import javax.persistence.Entity; 29 import javax.persistence.EntityListeners; 30 import javax.persistence.IdClass; 31 import javax.persistence.Inheritance; 32 import javax.persistence.NamedNativeQueries; 33 import javax.persistence.NamedNativeQuery; 34 import javax.persistence.NamedQueries; 35 import javax.persistence.NamedQuery; 36 import javax.persistence.PrimaryKeyJoinColumn; 37 import javax.persistence.PrimaryKeyJoinColumns; 38 import javax.persistence.SecondaryTable; 39 import javax.persistence.SecondaryTables; 40 import javax.persistence.SequenceGenerator; 41 import javax.persistence.SqlResultSetMapping; 42 import javax.persistence.Table; 43 import javax.persistence.TableGenerator; 44 45 import org.apache.cayenne.jpa.map.JpaAttributeOverride; 46 import org.apache.cayenne.jpa.map.JpaAttributes; 47 import org.apache.cayenne.jpa.map.JpaDiscriminatorColumn; 48 import org.apache.cayenne.jpa.map.JpaEmbeddable; 49 import org.apache.cayenne.jpa.map.JpaEntity; 50 import org.apache.cayenne.jpa.map.JpaEntityListener; 51 import org.apache.cayenne.jpa.map.JpaEntityListeners; 52 import org.apache.cayenne.jpa.map.JpaEntityMap; 53 import org.apache.cayenne.jpa.map.JpaIdClass; 54 import org.apache.cayenne.jpa.map.JpaInheritance; 55 import org.apache.cayenne.jpa.map.JpaMappedSuperclass; 56 import org.apache.cayenne.jpa.map.JpaNamedNativeQuery; 57 import org.apache.cayenne.jpa.map.JpaNamedQuery; 58 import org.apache.cayenne.jpa.map.JpaPrimaryKeyJoinColumn; 59 import org.apache.cayenne.jpa.map.JpaSecondaryTable; 60 import org.apache.cayenne.jpa.map.JpaSequenceGenerator; 61 import org.apache.cayenne.jpa.map.JpaSqlResultSetMapping; 62 import org.apache.cayenne.jpa.map.JpaTable; 63 import org.apache.cayenne.jpa.map.JpaTableGenerator; 64 import org.apache.cayenne.util.Util; 65 66 71 class ClassAnnotationProcessorFactory extends AnnotationProcessorFactory { 72 73 static final class EntityProcessor implements AnnotationProcessor { 74 75 public void onStartElement( 76 AnnotatedElement element, 77 AnnotationProcessorStack context) { 78 Entity entityAnnotation = element.getAnnotation(Entity.class); 79 80 JpaEntity entity = new JpaEntity(); 81 entity.setClassName(((Class ) element).getName()); 82 entity.setAttributes(new JpaAttributes()); 83 84 if (!Util.isEmptyString(entityAnnotation.name())) { 85 entity.setName(entityAnnotation.name()); 86 } 87 88 context.push(entity); 89 } 90 91 public void onFinishElement( 92 AnnotatedElement element, 93 AnnotationProcessorStack context) { 94 JpaEntity entity = (JpaEntity) context.pop(); 95 JpaEntityMap entityMap = (JpaEntityMap) context.peek(); 96 entityMap.getEntities().add(entity); 97 } 98 } 99 100 static final class EmbeddableProcessor implements AnnotationProcessor { 101 102 public void onStartElement( 103 AnnotatedElement element, 104 AnnotationProcessorStack context) { 105 106 JpaEmbeddable embeddable = new JpaEmbeddable(); 107 embeddable.setClassName(((Class ) element).getName()); 108 embeddable.setAttributes(new JpaAttributes()); 109 context.push(embeddable); 110 } 111 112 public void onFinishElement( 113 AnnotatedElement element, 114 AnnotationProcessorStack context) { 115 JpaEmbeddable embeddable = (JpaEmbeddable) context.pop(); 116 JpaEntityMap entityMap = (JpaEntityMap) context.peek(); 117 entityMap.getEmbeddables().add(embeddable); 118 } 119 } 120 121 static final class MappedSuperclassProcessor implements AnnotationProcessor { 122 123 public void onStartElement( 124 AnnotatedElement element, 125 AnnotationProcessorStack context) { 126 127 JpaMappedSuperclass superclass = new JpaMappedSuperclass(); 128 superclass.setClassName(((Class ) element).getName()); 129 superclass.setAttributes(new JpaAttributes()); 130 context.push(superclass); 131 } 132 133 public void onFinishElement( 134 AnnotatedElement element, 135 AnnotationProcessorStack context) { 136 JpaMappedSuperclass superclass = (JpaMappedSuperclass) context.pop(); 137 JpaEntityMap entityMap = (JpaEntityMap) context.peek(); 138 entityMap.getMappedSuperclasses().add(superclass); 139 } 140 } 141 142 146 abstract static class AbstractChildProcessor implements AnnotationProcessor { 147 148 public void onStartElement( 149 AnnotatedElement element, 150 AnnotationProcessorStack context) { 151 152 Object parent = context.peek(); 153 154 if (parent instanceof JpaEntity) { 155 onEntity((JpaEntity) parent, element, context); 156 } 157 else if (parent instanceof JpaMappedSuperclass) { 158 onMappedSuperclass((JpaMappedSuperclass) parent, element, context); 159 } 160 else if (parent instanceof JpaEntityMap) { 161 onEntityMap((JpaEntityMap) parent, element, context); 162 } 163 else if (parent instanceof JpaEmbeddable) { 164 onEmbeddable((JpaEmbeddable) parent, element, context); 165 } 166 else { 167 recordUnsupportedAnnotation(element, context); 168 } 169 } 170 171 public void onFinishElement( 172 AnnotatedElement element, 173 AnnotationProcessorStack context) { 174 } 176 177 void onEntity( 178 JpaEntity entity, 179 AnnotatedElement element, 180 AnnotationProcessorStack context) { 181 recordUnsupportedAnnotation(element, context); 182 } 183 184 void onMappedSuperclass( 185 JpaMappedSuperclass superclass, 186 AnnotatedElement element, 187 AnnotationProcessorStack context) { 188 recordUnsupportedAnnotation(element, context); 189 } 190 191 void onEmbeddable( 192 JpaEmbeddable embeddable, 193 AnnotatedElement element, 194 AnnotationProcessorStack context) { 195 recordUnsupportedAnnotation(element, context); 196 } 197 198 void onEntityMap( 201 JpaEntityMap entityMap, 202 AnnotatedElement element, 203 AnnotationProcessorStack context) { 204 recordUnsupportedAnnotation(element, context); 205 } 206 207 void recordUnsupportedAnnotation( 208 AnnotatedElement element, 209 AnnotationProcessorStack context) { 210 context.recordConflict(element, AnnotationProcessorFactory 211 .annotationClass(getClass()), "Unsupported in this place"); 212 } 213 } 214 215 static final class AttributeOverrideProcessor extends AbstractChildProcessor { 216 217 @Override 218 void onEntity( 219 JpaEntity entity, 220 AnnotatedElement element, 221 AnnotationProcessorStack context) { 222 223 AttributeOverride annotation = element.getAnnotation(AttributeOverride.class); 224 entity.getAttributeOverrides().add(new JpaAttributeOverride(annotation)); 225 } 226 } 227 228 static final class AttributeOverridesProcessor extends AbstractChildProcessor { 229 230 @Override 231 void onEntity( 232 JpaEntity entity, 233 AnnotatedElement element, 234 AnnotationProcessorStack context) { 235 236 AttributeOverrides annotation = element 237 .getAnnotation(AttributeOverrides.class); 238 for (int i = 0; i < annotation.value().length; i++) { 239 entity.getAttributeOverrides().add( 240 new JpaAttributeOverride(annotation.value()[i])); 241 } 242 } 243 } 244 245 static final class DiscriminatorColumnProcessor extends AbstractChildProcessor { 246 247 @Override 248 void onEntity( 249 JpaEntity entity, 250 AnnotatedElement element, 251 AnnotationProcessorStack context) { 252 253 DiscriminatorColumn annotation = element 254 .getAnnotation(DiscriminatorColumn.class); 255 entity.setDiscriminatorColumn(new JpaDiscriminatorColumn(annotation)); 256 } 257 } 258 259 static final class DiscriminatorValueProcessor extends AbstractChildProcessor { 260 261 @Override 262 void onEntity( 263 JpaEntity entity, 264 AnnotatedElement element, 265 AnnotationProcessorStack context) { 266 267 DiscriminatorValue annotation = element 268 .getAnnotation(DiscriminatorValue.class); 269 entity.setDiscriminatorValue(annotation.value()); 270 } 271 } 272 273 static final class EntityListenersProcessor extends AbstractChildProcessor { 274 275 private EntityListenerAnnotationLoader listenerLoader; 276 277 @Override 278 void onEntity( 279 JpaEntity entity, 280 AnnotatedElement element, 281 AnnotationProcessorStack context) { 282 283 EntityListeners annotation = element.getAnnotation(EntityListeners.class); 284 285 if (annotation.value().length > 0) { 286 if (listenerLoader == null) { 287 listenerLoader = new EntityListenerAnnotationLoader(); 288 } 289 290 JpaEntityListeners listenerHolder = entity.getEntityListeners(); 291 if (listenerHolder == null) { 292 listenerHolder = new JpaEntityListeners(); 293 entity.setEntityListeners(listenerHolder); 294 } 295 296 for (int i = 0; i < annotation.value().length; i++) { 297 JpaEntityListener listener = listenerLoader 298 .getEntityListener(annotation.value()[i]); 299 if (listener != null) { 300 listenerHolder.getEntityListeners().add(listener); 301 } 302 } 303 } 304 } 305 306 @Override 307 void onMappedSuperclass( 308 JpaMappedSuperclass superclass, 309 AnnotatedElement element, 310 AnnotationProcessorStack context) { 311 312 EntityListeners annotation = element.getAnnotation(EntityListeners.class); 313 314 if (annotation.value().length > 0) { 315 if (listenerLoader == null) { 316 listenerLoader = new EntityListenerAnnotationLoader(); 317 } 318 319 JpaEntityListeners listenerHolder = superclass.getEntityListeners(); 320 if(listenerHolder == null) { 321 listenerHolder = new JpaEntityListeners(); 322 superclass.setEntityListeners(listenerHolder); 323 } 324 325 for (int i = 0; i < annotation.value().length; i++) { 326 JpaEntityListener listener = listenerLoader 327 .getEntityListener(annotation.value()[i]); 328 if (listener != null) { 329 listenerHolder.getEntityListeners().add(listener); 330 } 331 } 332 } 333 } 334 } 335 336 static final class ExcludeDefaultListenersProcessor extends AbstractChildProcessor { 337 338 @Override 339 void onEntity( 340 JpaEntity entity, 341 AnnotatedElement element, 342 AnnotationProcessorStack context) { 343 344 entity.setExcludeDefaultListeners(true); 345 } 346 347 @Override 348 void onMappedSuperclass( 349 JpaMappedSuperclass superclass, 350 AnnotatedElement element, 351 AnnotationProcessorStack context) { 352 superclass.setExcludeDefaultListeners(true); 353 } 354 } 355 356 static final class ExcludeSuperclassListenersProcessor extends AbstractChildProcessor { 357 358 @Override 359 void onEntity( 360 JpaEntity entity, 361 AnnotatedElement element, 362 AnnotationProcessorStack context) { 363 364 entity.setExcludeSuperclassListeners(true); 365 } 366 367 @Override 368 void onMappedSuperclass( 369 JpaMappedSuperclass superclass, 370 AnnotatedElement element, 371 AnnotationProcessorStack context) { 372 superclass.setExcludeSuperclassListeners(true); 373 } 374 } 375 376 static final class IdClassProcessor extends AbstractChildProcessor { 377 378 @Override 379 void onEntity( 380 JpaEntity entity, 381 AnnotatedElement element, 382 AnnotationProcessorStack context) { 383 384 IdClass annotation = element.getAnnotation(IdClass.class); 385 JpaIdClass idClass = new JpaIdClass(); 386 idClass.setClassName(annotation.value().getName()); 387 entity.setIdClass(idClass); 388 } 389 390 @Override 391 void onMappedSuperclass( 392 JpaMappedSuperclass superclass, 393 AnnotatedElement element, 394 AnnotationProcessorStack context) { 395 IdClass annotation = element.getAnnotation(IdClass.class); 396 JpaIdClass idClass = new JpaIdClass(); 397 idClass.setClassName(annotation.value().getName()); 398 superclass.setIdClass(idClass); 399 } 400 } 401 402 static final class InheritanceProcessor extends AbstractChildProcessor { 403 404 @Override 405 void onEntity( 406 JpaEntity entity, 407 AnnotatedElement element, 408 AnnotationProcessorStack context) { 409 410 Inheritance annotation = element.getAnnotation(Inheritance.class); 411 entity.setInheritance(new JpaInheritance(annotation)); 412 } 413 } 414 415 static final class NamedNativeQueriesProcessor extends AbstractChildProcessor { 416 417 @Override 418 void onEntity( 419 JpaEntity entity, 420 AnnotatedElement element, 421 AnnotationProcessorStack context) { 422 423 NamedNativeQueries annotation = element 424 .getAnnotation(NamedNativeQueries.class); 425 for (int i = 0; i < annotation.value().length; i++) { 426 entity.getNamedNativeQueries().add( 427 new JpaNamedNativeQuery(annotation.value()[i])); 428 } 429 } 430 431 @Override 432 void onEntityMap( 433 JpaEntityMap entityMap, 434 AnnotatedElement element, 435 AnnotationProcessorStack context) { 436 437 NamedNativeQueries annotation = element 438 .getAnnotation(NamedNativeQueries.class); 439 for (int i = 0; i < annotation.value().length; i++) { 440 entityMap.getNamedNativeQueries().add( 441 new JpaNamedNativeQuery(annotation.value()[i])); 442 } 443 } 444 } 445 446 static final class NamedNativeQueryProcessor extends AbstractChildProcessor { 447 448 @Override 449 void onEntity( 450 JpaEntity entity, 451 AnnotatedElement element, 452 AnnotationProcessorStack context) { 453 454 NamedNativeQuery annotation = element.getAnnotation(NamedNativeQuery.class); 455 entity.getNamedNativeQueries().add(new JpaNamedNativeQuery(annotation)); 456 } 457 458 @Override 459 void onEntityMap( 460 JpaEntityMap entityMap, 461 AnnotatedElement element, 462 AnnotationProcessorStack context) { 463 464 NamedNativeQuery annotation = element.getAnnotation(NamedNativeQuery.class); 465 entityMap.getNamedNativeQueries().add(new JpaNamedNativeQuery(annotation)); 466 } 467 } 468 469 static final class NamedQueriesProcessor extends AbstractChildProcessor { 470 471 @Override 472 void onEntity( 473 JpaEntity entity, 474 AnnotatedElement element, 475 AnnotationProcessorStack context) { 476 477 NamedQueries annotation = element.getAnnotation(NamedQueries.class); 478 for (int i = 0; i < annotation.value().length; i++) { 479 entity.getNamedQueries().add(new JpaNamedQuery(annotation.value()[i])); 480 } 481 } 482 483 @Override 484 void onEntityMap( 485 JpaEntityMap entityMap, 486 AnnotatedElement element, 487 AnnotationProcessorStack context) { 488 489 NamedQueries annotation = element.getAnnotation(NamedQueries.class); 490 for (int i = 0; i < annotation.value().length; i++) { 491 entityMap.getNamedQueries().add(new JpaNamedQuery(annotation.value()[i])); 492 } 493 } 494 } 495 496 static final class NamedQueryProcessor extends AbstractChildProcessor { 497 498 @Override 499 void onEntity( 500 JpaEntity entity, 501 AnnotatedElement element, 502 AnnotationProcessorStack context) { 503 504 NamedQuery annotation = element.getAnnotation(NamedQuery.class); 505 entity.getNamedQueries().add(new JpaNamedQuery(annotation)); 506 } 507 508 @Override 509 void onEntityMap( 510 JpaEntityMap entityMap, 511 AnnotatedElement element, 512 AnnotationProcessorStack context) { 513 514 NamedQuery annotation = element.getAnnotation(NamedQuery.class); 515 entityMap.getNamedQueries().add(new JpaNamedQuery(annotation)); 516 } 517 } 518 519 static final class PrimaryKeyJoinColumnProcessor extends AbstractChildProcessor { 520 521 @Override 522 void onEntity( 523 JpaEntity entity, 524 AnnotatedElement element, 525 AnnotationProcessorStack context) { 526 527 PrimaryKeyJoinColumn annotation = element 528 .getAnnotation(PrimaryKeyJoinColumn.class); 529 entity 530 .getPrimaryKeyJoinColumns() 531 .add(new JpaPrimaryKeyJoinColumn(annotation)); 532 } 533 } 534 535 static final class PrimaryKeyJoinColumnsProcessor extends AbstractChildProcessor { 536 537 @Override 538 void onEntity( 539 JpaEntity entity, 540 AnnotatedElement element, 541 AnnotationProcessorStack context) { 542 543 PrimaryKeyJoinColumns annotation = element 544 .getAnnotation(PrimaryKeyJoinColumns.class); 545 for (int i = 0; i < annotation.value().length; i++) { 546 entity.getPrimaryKeyJoinColumns().add( 547 new JpaPrimaryKeyJoinColumn(annotation.value()[i])); 548 } 549 } 550 } 551 552 static final class SecondaryTableProcessor extends AbstractChildProcessor { 553 554 @Override 555 void onEntity( 556 JpaEntity entity, 557 AnnotatedElement element, 558 AnnotationProcessorStack context) { 559 560 SecondaryTable annotation = element.getAnnotation(SecondaryTable.class); 561 entity.getSecondaryTables().add(new JpaSecondaryTable(annotation)); 562 } 563 } 564 565 static final class SecondaryTablesProcessor extends AbstractChildProcessor { 566 567 @Override 568 void onEntity( 569 JpaEntity entity, 570 AnnotatedElement element, 571 AnnotationProcessorStack context) { 572 573 SecondaryTables annotation = element.getAnnotation(SecondaryTables.class); 574 for (int i = 0; i < annotation.value().length; i++) { 575 entity.getSecondaryTables().add( 576 new JpaSecondaryTable(annotation.value()[i])); 577 } 578 } 579 } 580 581 static final class SequenceGeneratorProcessor extends AbstractChildProcessor { 582 583 @Override 584 void onEntity( 585 JpaEntity entity, 586 AnnotatedElement element, 587 AnnotationProcessorStack context) { 588 589 SequenceGenerator annotation = element.getAnnotation(SequenceGenerator.class); 590 entity.setSequenceGenerator(new JpaSequenceGenerator(annotation)); 591 } 592 593 @Override 594 void onEntityMap( 595 JpaEntityMap entityMap, 596 AnnotatedElement element, 597 AnnotationProcessorStack context) { 598 599 SequenceGenerator annotation = element.getAnnotation(SequenceGenerator.class); 600 entityMap.getSequenceGenerators().add(new JpaSequenceGenerator(annotation)); 601 } 602 } 603 604 static final class SqlResultSetMappingProcessor extends AbstractChildProcessor { 605 606 @Override 607 void onEntity( 608 JpaEntity entity, 609 AnnotatedElement element, 610 AnnotationProcessorStack context) { 611 612 SqlResultSetMapping annotation = element 613 .getAnnotation(SqlResultSetMapping.class); 614 entity.setSqlResultSetMapping(new JpaSqlResultSetMapping(annotation)); 615 } 616 617 @Override 618 void onEntityMap( 619 JpaEntityMap entityMap, 620 AnnotatedElement element, 621 AnnotationProcessorStack context) { 622 623 SqlResultSetMapping annotation = element 624 .getAnnotation(SqlResultSetMapping.class); 625 entityMap.getSqlResultSetMappings().add( 626 new JpaSqlResultSetMapping(annotation)); 627 } 628 } 629 630 static final class TableGeneratorProcessor extends AbstractChildProcessor { 631 632 @Override 633 void onEntity( 634 JpaEntity entity, 635 AnnotatedElement element, 636 AnnotationProcessorStack context) { 637 638 TableGenerator annotation = element.getAnnotation(TableGenerator.class); 639 entity.setTableGenerator(new JpaTableGenerator(annotation)); 640 } 641 642 @Override 643 void onEntityMap( 644 JpaEntityMap entityMap, 645 AnnotatedElement element, 646 AnnotationProcessorStack context) { 647 648 TableGenerator annotation = element.getAnnotation(TableGenerator.class); 649 entityMap.getTableGenerators().add(new JpaTableGenerator(annotation)); 650 } 651 } 652 653 static final class TableProcessor extends AbstractChildProcessor { 654 655 @Override 656 void onEntity( 657 JpaEntity entity, 658 AnnotatedElement element, 659 AnnotationProcessorStack context) { 660 661 Table annotation = element.getAnnotation(Table.class); 662 entity.setTable(new JpaTable(annotation)); 663 } 664 } 665 666 } 667 | Popular Tags |