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

net.plsar.resources.ComponentAnnotationInspector Maven / Gradle / Ivy

Go to download

PLSA.R is an Open Source Server + Framework Environment for small to large scale requirements. There are no static references, no file reads, access to static fields per request. Everything is either cached and or instantiated on the fly. PLSA.R runs via one command so there are no .war files to deploy, no additional plugins to install it is a simple yet powerful alternative to container deployment environments.

There is a newer version: 3.0.1
Show newest version
package net.plsar.resources;

import net.plsar.annotations.Repository;
import net.plsar.annotations.Service;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ComponentAnnotationInspector {

    ClassLoader klassLoader;
    ComponentsHolder componentsHolder;

    public ComponentAnnotationInspector(ComponentsHolder componentsHolder){
        klassLoader = Thread.currentThread().getContextClassLoader();
        this.componentsHolder = componentsHolder;
    }

    public void inspect(){
        Path filePath = Paths.get("build");//requires build directory
        String completeFilePath = filePath.toAbsolutePath().toString();
        inspectFilePath(completeFilePath);
    }

    public void inspectFilePath(String filePath){
        File pathFile = new File(filePath);

        File[] files = pathFile.listFiles();
        for (File file : files) {

            if (file.isDirectory()) {
                inspectFilePath(file.getPath());
                continue;
            }

            try {

                if(!file.getPath().endsWith(".class"))continue;

                String separator = System.getProperty("file.separator");
                String regex = "classes" + "\\" + separator;//todo:fix
                String[] klassPathParts = file.getPath().split(regex);
                String klassPathSlashesRemoved =  klassPathParts[1].replace("\\", ".");
                String klassPathPeriod = klassPathSlashesRemoved.replace("/",".");
                String klassPathBefore = klassPathPeriod.replace("."+ "class", "");

                String klassPath = klassPathBefore.replaceFirst("java.", "").replaceFirst("main.", "");

                Class klass = klassLoader.loadClass(klassPath);

                if (klass.isAnnotation() || klass.isInterface()) continue;

                if(klass.isAnnotationPresent(Repository.class)){
                    AnnotationComponent annotationComponent = new AnnotationComponent();
                    annotationComponent.setKlass(klass);
                    String[] componentElements = klass.getName().split("\\.");
                    String componentKey = componentElements[componentElements.length -1].toLowerCase();
                    componentsHolder.getRepositories().put(componentKey, klass);
                }
                if(klass.isAnnotationPresent(Service.class)){
                    AnnotationComponent annotationComponent = new AnnotationComponent();
                    annotationComponent.setKlass(klass);
                    String[] componentElements = klass.getName().split("\\.");
                    String componentKey = componentElements[componentElements.length -1].toLowerCase();
                    componentsHolder.getServices().put(componentKey, klass);
                }

            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
    }

    public ComponentsHolder getComponentsHolder() {
        return this.componentsHolder;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy