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

br.com.andrewribeiro.ribrest.RibrestScanner Maven / Gradle / Ivy

Go to download

Ribrest Framework - A simple Java framework that truly improve your productivity when developing restful based webservices.

There is a newer version: 1.27.0
Show newest version
package br.com.andrewribeiro.ribrest;

import br.com.andrewribeiro.ribrest.annotations.RibrestModel;
import br.com.andrewribeiro.ribrest.logs.RibrestLog;
import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.ws.rs.Path;

/**
 *
 * @author Andrew Ribeiro
 */
public class RibrestScanner {

    List getNamesOfClassesWithAnnotation(Class annotation) {
        return new FastClasspathScanner().scan()
                .getNamesOfClassesWithAnnotation(annotation);
    }

    List getClassesInstances(List classesNames) {
        List classes = new ArrayList();
        classesNames.forEach((scannedClass) -> {
            try {
                Class clazz = Class.forName((String) scannedClass);
                classes.add(clazz);
            } catch (ClassNotFoundException ex) {
            }
        });

        return classes;
    }

    String[] getClassesPackageNames(List classes) {
        classes = classes != null ? classes : new ArrayList();
        String[] packages = new String[classes.size()];
        for (int i = 0; i < classes.size(); i++) {
            packages[i] = ((String) classes.get(i)).substring(0, ((String) classes.get(i)).lastIndexOf("."));
        }

        return packages;
    }
    
    String[] getResourcesPackagesNames() {
        RibrestLog.log("Trying to scan independent resources annoted with @javax.ws.rs.Path...");
        List classesNames = getNamesOfClassesWithAnnotation(Path.class);
        RibrestLog.log(new StringBuilder("Scanned ").append(classesNames.size()).append(" independent resource classes, they are :")
                .append(classesNames.toString()).toString());
        String[] packageNames = getClassesPackageNames(classesNames);
        RibrestLog.log(new StringBuilder("Identified the following packagenames for the scanned independent resources: ")
                .append(Arrays.toString(packageNames)).toString());
        return packageNames;
    }
    
    List getModelClassesInstances() {
        RibrestLog.log("Trying to scan @RibrestModel annotated classes...");
        List modelClassesInstances = getClassesInstances(getNamesOfClassesWithAnnotation(RibrestModel.class));
        RibrestLog.log(new StringBuilder("Scanned ").append(modelClassesInstances.size()).append(" models, they are: ")
                .append(modelClassesInstances).toString());
        return modelClassesInstances;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy