alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (Classpath.java)

This example Play Framework source code file (Classpath.java) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

class, classpath, configurationbuilder, lib, library, module, play, play framework, reflections, reflectionscache, set, string, typeannotationsscanner, typesscanner

The Classpath.java Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.libs;

import play.*;

import org.reflections.*;
import org.reflections.util.*;
import org.reflections.scanners.*;

import java.util.*;

public class Classpath {

	/**
     * Scans the application classloader to retrieve all types within a specific package.
     * <p>
     * This method is useful for some plug-ins, for example the EBean plugin will automatically detect all types
     * within the models package.
     * <p>
     * Note that it is better to specify a very specific package to avoid expensive searches.
     *
     * @param packageName the root package to scan
     * @return a set of types names satisfying the condition
     */
    public static Set<String> getTypes(Application app, String packageName) {
        return getReflections(app, packageName).getStore().get(TypesScanner.class).keySet();
    }

    /**
     * Scans the application classloader to retrieve all types annotated with a specific annotation.
     * <p>
     * This method is useful for some plug-ins, for example the EBean plugin will automatically detect all types
     * annotated with <code>@javax.persistance.Entity</code>.
     * <p>
     * Note that it is better to specify a very specific package to avoid expensive searches.
     *
     * @param packageName the root package to scan
     * @param annotation annotation class
     * @return a set of types names statifying the condition
     */
    public static Set<String> getTypesAnnotatedWith(Application app, String packageName, Class<? extends java.lang.annotation.Annotation> annotation) {
        return getReflections(app, packageName).getStore().getTypesAnnotatedWith(annotation.getName());
    }

    private static Reflections getReflections(Application app, String packageName) {
        if (app.isTest()) {
            return ReflectionsCache$.MODULE$.getReflections(app.classloader(), packageName);
        } else {
            return new Reflections(
                new ConfigurationBuilder()
                    .addUrls(ClasspathHelper.forPackage(packageName, app.classloader()))
                    .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(packageName + ".")))
                    .setScanners(new TypesScanner(), new TypeAnnotationsScanner()));
        }
    }

}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework Classpath.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.