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

com.browserstack.ClassExtractor Maven / Gradle / Ivy

The newest version!
package com.browserstack;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.PackageDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class ClassExtractor {
    private static final browserstack.shaded.org.slf4j.Logger logger = com.browserstack.logger.BrowserstackLoggerFactory.getLogger(ClassExtractor.class);

    public static ArrayList extract(String filePath) {
        try {
            FileInputStream inputStream = new FileInputStream(new File(filePath));
            CompilationUnit compilationUnit = new JavaParser().parse(inputStream).getResult().orElse(null);

            Optional packageDeclaration = compilationUnit.getPackageDeclaration();
            String packageName = packageDeclaration.map(pd -> pd.getNameAsString() + ".").orElse("");

            ArrayList classNames = new ArrayList<>();
            compilationUnit.accept(new ClassVisitor(packageName), classNames);
            return classNames;
        } catch (FileNotFoundException e) {
            logger.error("Unable to extract file names: " + com.browserstack.utils.UtilityMethods.getStackTraceAsString(e));
        }
        return new ArrayList();
    }

    private static class ClassVisitor extends VoidVisitorAdapter> {
        private final String packageName;
        public ClassVisitor(String packageName) {
            this.packageName = packageName;
        }

        @Override
        public void visit(ClassOrInterfaceDeclaration cid, List collector) {
            super.visit(cid, collector);
            collector.add(packageName + cid.getNameAsString());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy