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

net.kemitix.dependency.digraph.maven.plugin.DefaultSourceFileAnalyser Maven / Gradle / Ivy

There is a newer version: 0.9.1
Show newest version
package net.kemitix.dependency.digraph.maven.plugin;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseException;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;

import javax.inject.Inject;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Analyses a Java source file for package and import statements.
 *
 * @author pcampbell
 */
class DefaultSourceFileAnalyser extends AbstractMojoService
        implements SourceFileAnalyser {

    @Inject
    private DependencyData dependencyData;

    private static final Pattern METHOD_IMPORT
            = Pattern.compile("^(?.+)\\.(?.+)\\.(?.+)");
    private static final Pattern CLASS_IMPORT
            = Pattern.compile("^(?.+)\\.(?.+)");

    @Override
    public void analyse(final File file) {
        try {
            CompilationUnit cu = JavaParser.parse(file);
            String packageName = cu.getPackage().getName().toString();
            cu.getImports().forEach((ImportDeclaration id) -> {
                final String name = id.getName().toString();
                Matcher m;
                if (id.isStatic() && !id.isAsterisk()) {
                    m = METHOD_IMPORT.matcher(name);
                } else {
                    m = CLASS_IMPORT.matcher(name);
                }
                if (m.find()) {
                    dependencyData.addDependency(
                            packageName, m.group("package"));
                }
            });
        } catch (ParseException ex) {
            getLog().error("Error parsing file " + file, ex);
        } catch (IOException ex) {
            getLog().error("Error reading file " + file, ex);
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy