Search in sources :

Example 1 with AnnotationDB

use of org.scannotation.AnnotationDB in project simplejpa by appoxy.

the class EntityManagerFactoryImpl method scanClasses.

private void scanClasses(Set<String> libsToScan) {
    try {
        logger.info("Scanning for entity classes...");
        URL[] urls;
        try {
            urls = ClasspathUrlFinder.findClassPaths();
        } catch (Exception e) {
            System.err.println("CAUGHT");
            e.printStackTrace();
            urls = new URL[0];
        }
        if (libsToScan != null) {
            URL[] urls2 = new URL[urls.length + libsToScan.size()];
            System.arraycopy(urls, 0, urls2, 0, urls.length);
            // urls = new URL[libsToScan.size()];
            int count = 0;
            for (String s : libsToScan) {
                logger.fine("libinset=" + s);
                urls2[count + urls.length] = new File(s).toURL();
                count++;
            }
            urls = urls2;
        }
        logger.info("classpath=" + System.getProperty("java.class.path"));
        for (URL url : urls) {
            logger.info("Scanning: " + url.toString());
        }
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("classpath urls:");
            for (URL url : urls) {
                logger.fine(url.toString());
            }
        }
        AnnotationDB annotationDB = new AnnotationDB();
        annotationDB.scanArchives(urls);
        entities = annotationDB.getAnnotationIndex().get(Entity.class.getName());
        if (entities != null) {
            for (String entity : entities) {
                initEntity(entity);
            }
        }
        logger.info("Finished scanning for entity classes.");
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : AnnotationDB(org.scannotation.AnnotationDB) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PersistenceException(javax.persistence.PersistenceException) AmazonClientException(com.amazonaws.AmazonClientException)

Example 2 with AnnotationDB

use of org.scannotation.AnnotationDB in project simplejpa by appoxy.

the class AnnotationTests method findAnnotations.

@Test
public void findAnnotations() throws IOException {
    // scan java.class.path
    URL[] urls = ClasspathUrlFinder.findClassPaths();
    AnnotationDB db = new AnnotationDB();
    db.scanArchives(urls);
    Set<String> entities = db.getAnnotationIndex().get(Entity.class.getName());
    for (String entity : entities) {
        System.out.println("entity=" + entity);
    }
}
Also used : AnnotationDB(org.scannotation.AnnotationDB) Entity(javax.persistence.Entity) URL(java.net.URL) Test(org.junit.Test)

Example 3 with AnnotationDB

use of org.scannotation.AnnotationDB in project sling by apache.

the class GenerateAdapterMetadataMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        final Map<String, Object> descriptor = new HashMap<>();
        final AnnotationDB annotationDb = new AnnotationDB();
        annotationDb.scanArchives(buildOutputDirectory.toURI().toURL());
        final Set<String> annotatedClassNames = new HashSet<String>();
        addAnnotatedClasses(annotationDb, annotatedClassNames, Adaptable.class);
        addAnnotatedClasses(annotationDb, annotatedClassNames, Adaptables.class);
        for (final String annotatedClassName : annotatedClassNames) {
            getLog().info(String.format("found adaptable annotation on %s", annotatedClassName));
            final String pathToClassFile = annotatedClassName.replace('.', '/') + ".class";
            final File classFile = new File(buildOutputDirectory, pathToClassFile);
            final FileInputStream input = new FileInputStream(classFile);
            final ClassReader classReader;
            try {
                classReader = new ClassReader(input);
            } finally {
                input.close();
            }
            final ClassNode classNode = new ClassNode();
            classReader.accept(classNode, SKIP_CODE | SKIP_DEBUG | SKIP_FRAMES);
            @SuppressWarnings("unchecked") final List<AnnotationNode> annotations = classNode.invisibleAnnotations;
            for (final AnnotationNode annotation : annotations) {
                if (ADAPTABLE_DESC.equals(annotation.desc)) {
                    parseAdaptableAnnotation(annotation, classNode, descriptor);
                } else if (ADAPTABLES_DESC.equals(annotation.desc)) {
                    parseAdaptablesAnnotation(annotation, classNode, descriptor);
                }
            }
        }
        final File outputFile = new File(outputDirectory, fileName);
        outputFile.getParentFile().mkdirs();
        try (FileWriter writer = new FileWriter(outputFile);
            JsonWriter jsonWriter = Json.createWriter(writer)) {
            jsonWriter.writeObject(JsonSupport.toJson(descriptor));
        }
        addResource();
    } catch (IOException e) {
        throw new MojoExecutionException("Unable to generate metadata", e);
    } catch (JsonException e) {
        throw new MojoExecutionException("Unable to generate metadata", e);
    }
}
Also used : AnnotationDB(org.scannotation.AnnotationDB) JsonException(javax.json.JsonException) ClassNode(org.objectweb.asm.tree.ClassNode) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) IOException(java.io.IOException) JsonWriter(javax.json.JsonWriter) FileInputStream(java.io.FileInputStream) AnnotationNode(org.objectweb.asm.tree.AnnotationNode) ClassReader(org.objectweb.asm.ClassReader) File(java.io.File) HashSet(java.util.HashSet)

Example 4 with AnnotationDB

use of org.scannotation.AnnotationDB in project compiler by boalang.

the class SymbolTable method importLibs.

private static void importLibs(final List<URL> urls) throws IOException {
    // load built-in functions
    final Class<?>[] builtinFuncs = { boa.functions.BoaAstIntrinsics.class, boa.functions.BoaGraphIntrinsics.class, boa.functions.BoaIntrinsics.class, boa.functions.BoaMetricIntrinsics.class, boa.functions.BoaModifierIntrinsics.class, boa.functions.BoaCasts.class, boa.functions.BoaMathIntrinsics.class, boa.functions.BoaSortIntrinsics.class, boa.functions.BoaSpecialIntrinsics.class, boa.functions.BoaStringIntrinsics.class, boa.functions.BoaTimeIntrinsics.class };
    for (final Class<?> c : builtinFuncs) importFunctions(c);
    // load built-in aggregators
    final Class<?>[] builtinAggs = { boa.aggregators.BottomAggregator.class, boa.aggregators.CollectionAggregator.class, boa.aggregators.ConfidenceIntervalAggregator.class, boa.aggregators.DistinctAggregator.class, boa.aggregators.FloatHistogramAggregator.class, boa.aggregators.FloatMeanAggregator.class, boa.aggregators.FloatQuantileAggregator.class, boa.aggregators.FloatSumAggregator.class, boa.aggregators.GraphAggregator.class, boa.aggregators.GraphvizAggregator.class, boa.aggregators.IntHistogramAggregator.class, boa.aggregators.IntMeanAggregator.class, boa.aggregators.IntQuantileAggregator.class, boa.aggregators.IntSumAggregator.class, boa.aggregators.KurtosisAggregator.class, boa.aggregators.LogAggregator.class, boa.aggregators.MaximumAggregator.class, boa.aggregators.MedianAggregator.class, boa.aggregators.MinimumAggregator.class, boa.aggregators.SetAggregator.class, boa.aggregators.SkewnessAggregator.class, boa.aggregators.StatisticsAggregator.class, boa.aggregators.StDevAggregator.class, boa.aggregators.TopAggregator.class, boa.aggregators.UniqueAggregator.class, boa.aggregators.VarianceAggregator.class };
    for (final Class<?> c : builtinAggs) importAggregator(c);
    // also check any libs passed into the compiler
    if (urls.size() > 0) {
        final AnnotationDB db = new AnnotationDB();
        db.setScanMethodAnnotations(true);
        db.setScanClassAnnotations(true);
        for (final URL url : urls) db.scanArchives(url);
        final Map<String, Set<String>> annotationIndex = db.getAnnotationIndex();
        for (final String s : annotationIndex.get(AggregatorSpec.class.getCanonicalName())) importAggregator(s);
        for (final String s : annotationIndex.get(FunctionSpec.class.getCanonicalName())) importFunctions(s);
    }
}
Also used : AnnotationDB(org.scannotation.AnnotationDB) AggregatorSpec(boa.aggregators.AggregatorSpec) FunctionSpec(boa.functions.FunctionSpec) URL(java.net.URL)

Aggregations

AnnotationDB (org.scannotation.AnnotationDB)4 URL (java.net.URL)3 File (java.io.File)2 IOException (java.io.IOException)2 AggregatorSpec (boa.aggregators.AggregatorSpec)1 FunctionSpec (boa.functions.FunctionSpec)1 AmazonClientException (com.amazonaws.AmazonClientException)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileWriter (java.io.FileWriter)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 JsonException (javax.json.JsonException)1 JsonWriter (javax.json.JsonWriter)1 Entity (javax.persistence.Entity)1 PersistenceException (javax.persistence.PersistenceException)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 Test (org.junit.Test)1 ClassReader (org.objectweb.asm.ClassReader)1 AnnotationNode (org.objectweb.asm.tree.AnnotationNode)1