All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.otto.jsonhome.resource.scanner.AnnotationScanner Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 Guido Steinacker
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.otto.jsonhome.resource.scanner;

import de.otto.jsonhome.JsonHomeProperties;
import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;

import javax.ws.rs.Path;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import static org.reflections.util.ClasspathHelper.forClass;
import static org.reflections.util.ClasspathHelper.forPackage;

/**
 * @author Sebastian Schroeder
 * @since 15.12.2012
 */
public final class AnnotationScanner {

    private final Set packages;

    public AnnotationScanner() {
        final String packages = JsonHomeProperties.getProperties().getProperty("resource.packages");
        if (packages == null) {
            throw new IllegalStateException("resource.packages property not set in jsonhome.properties");
        }
        this.packages = new HashSet(Arrays.asList(packages.split("\\s*,\\s*")));
    }

    public AnnotationScanner(Set packages) {
        this.packages = packages;
    }

    public Set> scanClasses() {
        final Set> classes = new HashSet>();
        for (String pkg : packages) {
            final Reflections reflections = new Reflections(new ConfigurationBuilder()
                    .addUrls(forClass(Path.class))
                    .addUrls(forPackage(pkg))
                    .setScanners(new SubTypesScanner(), new TypeAnnotationsScanner())
                    .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(pkg))));
            classes.addAll(reflections.getTypesAnnotatedWith(Path.class));
        }
        return classes;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy