@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); } }
private Index buildIndex(JarFile jar) throws IOException { Indexer indexer = new Indexer(); for (JarEntry entry : asIterable(jar.entries())) { if (entry.getName().endsWith(".class")) { try (InputStream inputStream = jar.getInputStream(entry)) { indexer.index(inputStream); } } } return indexer.complete(); }
/** * Adds the class w/ the specified name to the jandex index. * * @param indexer The jandex indexer * @param className the fully qualified class name to be indexed */ private void indexClass(Indexer indexer, String className) { InputStream stream = classLoaderService().locateResourceStream(className); try { indexer.index(stream); } catch (IOException e) { throw new HibernateException("Unable to open input stream for class " + className, e); } }
/** Creates an annotation index for the given entity type */ public static synchronized Index createIndex(Class<?> type) { Index index = indices.get(type); if (index == null) { try { Indexer indexer = new Indexer(); Class<?> currentType = type; while (currentType != null) { String className = currentType.getName().replace(".", "/") + ".class"; InputStream stream = type.getClassLoader().getResourceAsStream(className); indexer.index(stream); currentType = currentType.getSuperclass(); } index = indexer.complete(); indices.put(type, index); } catch (IOException e) { throw new RuntimeException("Failed to initialize Indexer", e); } } return index; }
private static Index readModuleIndex(final File jarFile, boolean everything) { try { try (JarFile jar = new JarFile(jarFile)) { Enumeration<JarEntry> entries = jar.entries(); Indexer indexer = new Indexer(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); String name = entry.getName().toLowerCase(); if (everything && name.endsWith(".class") || name.endsWith("/module_.class") || name.endsWith("/$module_.class")) { try (InputStream stream = jar.getInputStream(entry)) { indexer.index(stream); } } } return indexer.complete(); } } catch (IOException e) { throw new RuntimeException("Failed to read index for module " + jarFile.getPath(), e); } }
/** {@inheritDoc} */ @Override public AnnotationRepository scan(URL[] urls, ClassLoader cl) { Indexer indexer = new Indexer(); if (urls != null && urls.length > 0) { for (URL url : urls) { String externalForm = url.toExternalForm(); if (externalForm.endsWith(".class")) { InputStream is = null; try { is = new FileInputStream(new File(url.toURI())); indexer.index(is); } catch (Throwable t) { log.error("Unable to process: " + externalForm, t); } finally { if (is != null) { try { is.close(); } catch (IOException ioe) { // Nothing } } } } else if (externalForm.endsWith(".jar")) { JarFile jarFile = null; try { jarFile = new JarFile(new File(url.toURI())); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry jarEntry = entries.nextElement(); if (jarEntry.getName().endsWith(".class")) { InputStream is = null; try { is = jarFile.getInputStream(jarEntry); indexer.index(is); } catch (Throwable t) { log.error("Unable to process: " + jarEntry.getName(), t); } finally { if (is != null) { try { is.close(); } catch (IOException ioe) { // Nothing } } } } } } catch (Throwable t) { log.error("Unable to process: " + externalForm, t); } finally { if (jarFile != null) { try { jarFile.close(); } catch (IOException ioe) { // Nothing } } } } } } return new AnnotationRepositoryImpl(indexer.complete(), cl); }