@Test public void testSchemaInPersistenceMetadata() { Index index = getMockedIndex("default-schema.xml"); index.printAnnotations(); // Global Configuration should be accessed like this, not from ClassInfo List<AnnotationInstance> annotationInstanceList = index.getAnnotations(JPADotNames.TABLE_GENERATOR); assertNotNull(annotationInstanceList); assertEquals(1, annotationInstanceList.size()); AnnotationInstance generator = annotationInstanceList.get(0); assertEquals("TABLE_GEN", generator.value("name").asString()); assertEquals("ANNOTATION_CATALOG", generator.value("catalog").asString()); assertEquals("ANNOTATION_SCHEMA", generator.value("schema").asString()); annotationInstanceList = index.getAnnotations(JPADotNames.SEQUENCE_GENERATOR); assertNotNull(annotationInstanceList); assertEquals(1, annotationInstanceList.size()); generator = annotationInstanceList.get(0); assertEquals("SEQ_GEN", generator.value("name").asString()); assertEquals("XML_CATALOG", generator.value("catalog").asString()); assertEquals("XML_SCHEMA", generator.value("schema").asString()); assertEquals(123, generator.value("initialValue").asInt()); // Book and Author and Topic are all not defined @Table // but orm xml defines default schema and catalog in persistence-unit-metadata // so, we have to mock @Table for entities, Book and Author but not Topic which is a Embeddable annotationInstanceList = index.getAnnotations(JPADotNames.TABLE); assertNotNull(annotationInstanceList); assertEquals(2, annotationInstanceList.size()); for (AnnotationInstance table : annotationInstanceList) { assertEquals("XML_CATALOG", table.value("catalog").asString()); assertEquals("XML_SCHEMA", table.value("schema").asString()); } }
@Test public void testSchemaInEntityMapping() { Index index = getMockedIndex("default-schema2.xml"); index.printAnnotations(); // Global Configuration should be accessed like this, not from ClassInfo List<AnnotationInstance> annotationInstanceList = index.getAnnotations(JPADotNames.TABLE_GENERATOR); assertNotNull(annotationInstanceList); assertEquals(1, annotationInstanceList.size()); AnnotationInstance generator = annotationInstanceList.get(0); assertEquals("TABLE_GEN", generator.value("name").asString()); assertEquals("ANNOTATION_CATALOG", generator.value("catalog").asString()); assertEquals("ANNOTATION_SCHEMA", generator.value("schema").asString()); annotationInstanceList = index.getAnnotations(JPADotNames.SEQUENCE_GENERATOR); assertNotNull(annotationInstanceList); assertEquals(1, annotationInstanceList.size()); generator = annotationInstanceList.get(0); assertEquals("SEQ_GEN", generator.value("name").asString()); assertNull(generator.value("catalog")); assertNull(generator.value("schema")); assertEquals(123, generator.value("initialValue").asInt()); annotationInstanceList = index.getAnnotations(JPADotNames.TABLE); assertNotNull(annotationInstanceList); assertEquals(0, annotationInstanceList.size()); }
// TODO verify that association exists. See former VerifyFetchProfileReferenceSecondPass public static void bind(MetadataImpl metadata, Index jandex) { for (AnnotationInstance fetchProfile : jandex.getAnnotations(HibernateDotNames.FETCH_PROFILE)) { bind(metadata, fetchProfile); } for (AnnotationInstance fetchProfiles : jandex.getAnnotations(HibernateDotNames.FETCH_PROFILES)) { for (AnnotationInstance fetchProfile : JandexHelper.getValueAsArray(fetchProfiles, "value")) { bind(metadata, fetchProfile); } } }
/** * Binds all {@link org.hibernate.annotations.TypeDef} and {@link TypeDefs} annotations to the * supplied metadata. * * @param metadata the global metadata * @param jandex the jandex jandex */ public static void bind(MetadataImplementor metadata, Index jandex) { for (AnnotationInstance typeDef : jandex.getAnnotations(HibernateDotNames.TYPE_DEF)) { bind(metadata, typeDef); } for (AnnotationInstance typeDefs : jandex.getAnnotations(HibernateDotNames.TYPE_DEFS)) { AnnotationInstance[] typeDefAnnotations = JandexHelper.getValue(typeDefs, "value", AnnotationInstance[].class); for (AnnotationInstance typeDef : typeDefAnnotations) { bind(metadata, typeDef); } } }
@Override @SuppressWarnings({"unchecked"}) public void prepare(MetadataSources sources) { // create a jandex index from the annotated classes Indexer indexer = new Indexer(); for (Class<?> clazz : sources.getAnnotatedClasses()) { indexClass(indexer, clazz.getName().replace('.', '/') + ".class"); } // add package-info from the configured packages for (String packageName : sources.getAnnotatedPackages()) { indexClass(indexer, packageName.replace('.', '/') + "/package-info.class"); } index = indexer.complete(); List<JaxbRoot<XMLEntityMappings>> mappings = new ArrayList<JaxbRoot<XMLEntityMappings>>(); for (JaxbRoot<?> root : sources.getJaxbRootList()) { if (root.getRoot() instanceof XMLEntityMappings) { mappings.add((JaxbRoot<XMLEntityMappings>) root); } } if (!mappings.isEmpty()) { // process the xml configuration final OrmXmlParser ormParser = new OrmXmlParser(metadata); index = ormParser.parseAndUpdateIndex(mappings, index); } if (index.getAnnotations(PseudoJpaDotNames.DEFAULT_DELIMITED_IDENTIFIERS) != null) { metadata.setGloballyQuotedIdentifiers(true); } }
void collectGlobalConfigurationFromIndex(GlobalAnnotations globalAnnotations) { for (DotName annName : DefaultConfigurationHelper.GLOBAL_ANNOTATIONS) { List<AnnotationInstance> annotationInstanceList = index.getAnnotations(annName); if (MockHelper.isNotEmpty(annotationInstanceList)) { globalAnnotations.addIndexedAnnotationInstance(annotationInstanceList); } } globalAnnotations.filterIndexedAnnotations(); }
/** @see {@link Index#getAnnotations(org.jboss.jandex.DotName)} */ public List<AnnotationInstance> getAnnotations(final DotName annotationName) { final List<AnnotationInstance> allInstances = new ArrayList<AnnotationInstance>(); for (Index index : indexes) { final List<AnnotationInstance> list = index.getAnnotations(annotationName); if (list != null) { allInstances.addAll(list); } } return Collections.unmodifiableList(allInstances); }
@Override public Collection<Annotation> getAnnotation(Class<?> annotationClass) { List<AnnotationInstance> instances = backingRepository.getAnnotations(DotName.createSimple(annotationClass.getName())); ArrayList<Annotation> annotations = new ArrayList<Annotation>(instances.size()); for (AnnotationInstance instance : instances) { AnnotationTarget target = instance.target(); Annotation annotation = null; if (target instanceof MethodInfo) { MethodInfo m = (MethodInfo) target; List<String> parameterTypes = new ArrayList<String>(m.args().length); for (Type type : m.args()) { parameterTypes.add(type.toString()); } String declaringClass = m.declaringClass().name().toString(); annotation = new AnnotationImpl( declaringClass, cl, parameterTypes, m.name(), true, false, annotationClass); } if (target instanceof FieldInfo) { FieldInfo f = (FieldInfo) target; String declaringClass = f.declaringClass().name().toString(); annotation = new AnnotationImpl(declaringClass, cl, null, f.name(), false, true, annotationClass); } if (target instanceof ClassInfo) { ClassInfo c = (ClassInfo) target; annotation = new AnnotationImpl(c.name().toString(), cl, null, null, false, false, annotationClass); } if (annotation != null) { annotations.add(annotation); } } annotations.trimToSize(); if (annotations.size() == 0) { return null; } else { return Collections.unmodifiableList(annotations); } }