tool.MainProgram Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of staticanalyser Show documentation
Show all versions of staticanalyser Show documentation
A static design pattern verification plugin.
The newest version!
package tool;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import org.gradle.api.GradleException;
import tool.designpatterns.DesignPattern;
import tool.designpatterns.Pattern;
import tool.designpatterns.PatternGroup;
import tool.designpatterns.PatternUtils;
import tool.feedback.PatternGroupFeedback;
/**
* The main entry point for the analysis.
*/
@DesignPattern(pattern = {Pattern.IMMUTABLE})
public final class MainProgram {
private MainProgram() {
}
/**
* The main method.
*
* @param args commandline arguments.
*/
public static void main(String[] args) {
// Just use this project for now (src), will have to change
// to the target project with the gradle stuff
startAnalyse(new String[] {"src"});
}
/**
* Main entrance point to the program.
*
* @param paths an array of paths to analyse.
*/
@SuppressWarnings("PMD.SystemPrintln")
public static void startAnalyse(String[] paths) {
AnnotationExtractor extracter = new AnnotationExtractor();
for (String path : paths) {
List annotationHolders =
ProjectParser.findAllClassesAndInterfaces(path);
extracter.findAnnotations(annotationHolders);
}
Map> patternAnnotMap =
extracter.getPatternClassMap();
Map>> patternGroupMap =
mapToMap(patternAnnotMap);
List feedbacks = new ArrayList<>();
for (Map.Entry>> entry :
patternGroupMap
.entrySet()) {
PatternGroup group = entry.getKey();
Map> patternMap = entry.getValue();
PatternGroupFeedback verFeedback = group.getVerifier().verifyGroup(patternMap);
feedbacks.add(verFeedback);
}
List failingFeedbacks = new ArrayList<>();
feedbacks.forEach(feedback -> {
if (feedback.hasError()) {
failingFeedbacks.add(feedback.getFullMessage());
}
});
if (!failingFeedbacks.isEmpty()) {
failBuild(failingFeedbacks);
}
}
/**
* Fails the build and prints the failing feedbacks into a nice message.
*
* @param failingFeedbacks the feedbacks of what went wrong.
*/
private static void failBuild(List failingFeedbacks) {
StringBuilder msg = new StringBuilder(100);
msg.append("\n\nStaticAnalyser found the following errors: \n------------------\n");
failingFeedbacks.forEach(feedback -> {
msg.append(feedback);
msg.append("------------------\n");
});
throw new GradleException(msg.toString());
}
/**
* Groups a map from patterns to class or interface in their patternGroups, i.e. a map
* containing the keys "AdapterClient, AdapterInterface, Immutable" to lists of classes or
* interfaces will be converted to a map with keys "Adatpter, Immutable", with values same maps
* as earlier.
*
* @param map The map to convert.
*
* @return The converted map.
*/
private static Map>> mapToMap(
Map> map) {
Map>> newMap =
new ConcurrentHashMap<>();
map.forEach((pattern, list) -> {
PatternGroup group = PatternUtils.patternGroupFromPattern(pattern);
if (!newMap.containsKey(group)) {
// The group does not exist and we therefore want to create a new map.
newMap.put(group, new HashMap<>());
}
// The group exists so we have an initialized map.
newMap.get(group).put(pattern, list);
});
return newMap;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy