Skip to content

Commit

Permalink
fix: java class parsing failed (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
heowc authored Nov 17, 2024
1 parent f9de5c0 commit 3878389
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 45 deletions.
10 changes: 7 additions & 3 deletions heo-cli/src/main/java/dev/heowc/heo/cli/HeoCliService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import java.util.List;

import dev.heowc.heo.core.HeoException;

import org.springframework.stereotype.Service;

import dev.heowc.heo.core.HeoException;
import dev.heowc.heo.core.Module;
import dev.heowc.heo.core.analysis.application.DependencyAnalysisService;
import dev.heowc.heo.core.analysis.domain.DependencyAnalysisResult;
import dev.heowc.heo.core.loader.ModuleLoaderConfig;
import dev.heowc.heo.core.loader.application.ModuleLoaderService;
import dev.heowc.heo.core.reporting.AnalysisReportService;
import dev.heowc.heo.core.visualization.ReportVisualizationService;
Expand All @@ -32,7 +32,7 @@ public HeoCliService(ModuleLoaderService moduleLoaderService,
}

public void command(String directory, String rootPackage, String destination, HeoConfig heoConfig) {
final List<Module> modules = moduleLoaderService.loads(directory, rootPackage);
final List<Module> modules = moduleLoaderService.loads(createConfig(directory, rootPackage));
final DependencyAnalysisResult result =
dependencyAnalysisService.analyzeProjectDependencies(modules, rootPackage);
final String report = analysisReportService.createReport(result);
Expand All @@ -41,4 +41,8 @@ public void command(String directory, String rootPackage, String destination, He
throw new HeoException("Cycles occurred");
}
}

private static ModuleLoaderConfig createConfig(String directory, String rootPackage) {
return new ModuleLoaderConfig(directory, rootPackage);
}
}
30 changes: 30 additions & 0 deletions heo-core/src/main/java/dev/heowc/heo/core/ParserConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dev.heowc.heo.core;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ParserConfiguration.LanguageLevel;
import com.github.javaparser.utils.ParserCollectionStrategy;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
class ParserConfig {

@Bean
ParserConfiguration parserConfiguration() {
final ParserConfiguration configuration = new ParserConfiguration();
configuration.setLanguageLevel(LanguageLevel.JAVA_17);
return configuration;
}

@Bean
ParserCollectionStrategy parserCollectionStrategy(ParserConfiguration parserConfiguration) {
return new ParserCollectionStrategy(parserConfiguration);
}

@Bean
JavaParser javaParser(ParserConfiguration parserConfiguration) {
return new JavaParser(parserConfiguration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
Expand All @@ -28,18 +30,11 @@
@DomainService
public class DependencyMapper {

private final JavaParser javaParser = new JavaParser();
private final Logger logger = LoggerFactory.getLogger(DependencyMapper.class);
private final JavaParser javaParser;

private static String toGroupId(GroupIdProvider groupIdProvider, String rootPackage, Module module) {
return groupIdProvider.groupId(rootPackage, module);
}

private static BinaryOperator<Set<String>> merge() {
return (deps, deps2) -> {
final Set<String> merged = new HashSet<>(deps);
merged.addAll(deps2);
return merged;
};
public DependencyMapper(JavaParser javaParser) {
this.javaParser = javaParser;
}

public DomainGraph mapDependencies(List<Module> modules, String rootPackage) {
Expand All @@ -48,16 +43,29 @@ public DomainGraph mapDependencies(List<Module> modules, String rootPackage) {
final Map<String, Module> moduleGroup = modules.stream()
.collect(Collectors.toUnmodifiableMap(Module::getIdentity,
Function.identity()));
final Map<String, Set<String>> result = modules.stream()
.map(it -> toDependentModule(rootPackage, it, moduleGroup,
groupIdProvider))
.collect(Collectors.toUnmodifiableMap(Pair::getKey,
Pair::getValue, merge()));
final Map<String, Set<String>> result =
modules.stream()
.map(it -> toDependentModule(rootPackage, it, moduleGroup, groupIdProvider))
.peek(it -> logger.debug("module={}, dependent={}",
it.getKey(), it.getValue()))
.collect(Collectors.toUnmodifiableMap(Pair::getKey, Pair::getValue, merge()));
graph.addVertex(result);
graph.addEdge(result);
return graph;
}

private static String toGroupId(GroupIdProvider groupIdProvider, String rootPackage, Module module) {
return groupIdProvider.groupId(rootPackage, module);
}

private static BinaryOperator<Set<String>> merge() {
return (deps, deps2) -> {
final Set<String> merged = new HashSet<>(deps);
merged.addAll(deps2);
return merged;
};
}

private Pair<String, Set<String>> toDependentModule(String rootPackage,
Module module,
Map<String, Module> moduleGroup,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dev.heowc.heo.core.loader;

public record ModuleLoaderConfig(String projectDirectory, String rootPackage) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,32 @@
import java.io.UncheckedIOException;
import java.util.List;

import dev.heowc.heo.core.Module;
import dev.heowc.heo.core.loader.domain.ModuleLoader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.github.javaparser.utils.ParserCollectionStrategy;

import dev.heowc.heo.core.Module;
import dev.heowc.heo.core.loader.ModuleLoaderConfig;
import dev.heowc.heo.core.loader.domain.ModuleLoader;

@Service
public class ModuleLoaderService {

private final Logger logger = LoggerFactory.getLogger(ModuleLoaderService.class);
private final ParserCollectionStrategy parserCollectionStrategy;

public ModuleLoaderService(ParserCollectionStrategy parserCollectionStrategy) {
this.parserCollectionStrategy = parserCollectionStrategy;
}

public List<Module> loads(String projectDirectory, String rootPackage) {
public List<Module> loads(ModuleLoaderConfig config) {
try {
logger.info("Loading " + rootPackage + " from " + projectDirectory);
return new ModuleLoader(projectDirectory, rootPackage).loadModules();
logger.info("Loading " + config.rootPackage() + " from " + config.projectDirectory());
return new ModuleLoader(config, parserCollectionStrategy).loadModules();
} catch (IOException e) {
logger.error("Error while loading " + rootPackage + " from " + projectDirectory, e);
logger.error("Error while loading " + config.rootPackage() + " from " + config.projectDirectory(), e);
throw new UncheckedIOException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,47 @@
import java.util.List;
import java.util.stream.Stream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.javaparser.ParseResult;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.utils.ParserCollectionStrategy;
import com.github.javaparser.utils.ProjectRoot;
import com.github.javaparser.utils.SourceRoot;

import dev.heowc.heo.core.Module;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import dev.heowc.heo.core.loader.ModuleLoaderConfig;

public class ModuleLoader {

private static final Logger logger = LoggerFactory.getLogger(ModuleLoader.class);

private final Path projectPath;
private final String rootPackage;
private final ParserCollectionStrategy parserCollectionStrategy;

public ModuleLoader(ModuleLoaderConfig config, ParserCollectionStrategy parserCollectionStrategy) {
this.projectPath = Path.of(config.projectDirectory()).toAbsolutePath();
this.rootPackage = config.rootPackage();
this.parserCollectionStrategy = parserCollectionStrategy;
}

public ModuleLoader(String projectDirectory, String rootPackage) {
this.projectPath = Path.of(projectDirectory).toAbsolutePath();
this.rootPackage = rootPackage;
this(new ModuleLoaderConfig(projectDirectory, rootPackage),
new ParserCollectionStrategy());
}

public List<Module> loadModules() throws IOException {
final ProjectRoot projectRoot = new ParserCollectionStrategy().collect(projectPath);
final ProjectRoot projectRoot = parserCollectionStrategy.collect(projectPath);
return projectRoot.getSourceRoots()
.stream()
.filter(ModuleLoader::ignoreNonMainSourceRoot)
.peek(it -> logger.info("Detected module (file://{})", it.getRoot()))
.peek(it -> logger.info("Detected source (file://{})", it.getRoot()))
.flatMap(this::tryParseSources)
.filter(it -> ignoreFile(it.getStorage().orElseThrow().getPath()))
.map(ModuleLoader::extractModule)
.peek(it -> logger.debug("Extract module ({})", it))
.toList();
}

Expand Down
36 changes: 25 additions & 11 deletions heo-gradle-plugin/src/main/java/dev/heowc/heo/gradle/HeoPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
import java.nio.file.StandardCopyOption;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.Nullable;
Expand All @@ -21,6 +25,8 @@
public class HeoPlugin implements Plugin<Project> {

private static final String REPORT_PATH = "build/reports/heo";
private static final Pattern DOT_PATTERN = Pattern.compile("\\.");
private static final Pattern EQ_PATTERN = Pattern.compile("=");

@Override
public void apply(Project project) {
Expand All @@ -43,18 +49,16 @@ public void apply(Project project) {
task.setMain("-jar");
task.args(tempJar.getAbsolutePath());
task.args(arguments(project, config));

task.environment(environments(project, config));
tempJar.deleteOnExit();
});
}

private List<? extends Serializable> arguments(Project project, HeoPluginConfig config) {
return Stream.concat(Stream.of("-d", determineDirectory(project, config.getDirectoryPath()),
"-p", determinePrefixPackage(project, config.getPrefixPackage()),
"-o", determineDestination(project, config.getDestination()),
"--failure-on-cycles", String.valueOf(config.isFailureOnCycles())),
logging(project, config.getLogging()))
.toList();
private static List<? extends Serializable> arguments(Project project, HeoPluginConfig config) {
return List.of("-d", determineDirectory(project, config.getDirectoryPath()),
"-p", determinePrefixPackage(project, config.getPrefixPackage()),
"-o", determineDestination(project, config.getDestination()),
"--failure-on-cycles", String.valueOf(config.isFailureOnCycles()));
}

private static String determineDirectory(Project project, @Nullable String directoryPath) {
Expand All @@ -75,15 +79,25 @@ private static String determineDestination(Project project, @Nullable String des
: destination;
}

private Stream<String> logging(Project project, @Nullable List<String> logging) {
private static Map<String, String> environments(Project project, HeoPluginConfig config) {
return logging(project, config.getLogging())
.map(String::toUpperCase)
.map(it -> DOT_PATTERN.matcher(it).replaceAll("_"))
.map(it -> {
final String[] keyValue = EQ_PATTERN.split(it);
return Map.entry(keyValue[0], keyValue[1]);
}).collect(Collectors.toUnmodifiableMap(Entry::getKey, Entry::getValue));
}

private static Stream<String> logging(Project project, @Nullable List<String> logging) {
if (project.getGradle().getStartParameter().getLogLevel() == LogLevel.DEBUG) {
return Stream.of("-Dlogging.level.root=DEBUG");
return Stream.of("logging.level.root=DEBUG");
}
return Stream.ofNullable(logging)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.filter(Objects::nonNull)
.map(it -> "-Dlogging.level." + it);
.map(it -> "logging.level." + it);
}

}
3 changes: 2 additions & 1 deletion it/cycled-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ if (project.hasProperty('localPlugin')) {
var failure = false
try {
tasks.heoReport.exec()
} catch (Exception e) {
} catch (Exception ignored) {
println("heoReport task failed")
failure = true
}
if (!failure) {
Expand Down
1 change: 1 addition & 0 deletions it/gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ if (project.hasProperty('localPlugin')) {
doLast {
try {
tasks.heoReport.exec()
println("heoReport task successful")
} catch (Exception e) {
throw new IllegalStateException("The task did not succeed", e)
}
Expand Down

0 comments on commit 3878389

Please sign in to comment.