5: Create basic plugin archetypes

This commit is contained in:
Bartłomiej Pluta
2019-03-31 17:16:25 +02:00
parent 0c8a94903a
commit 87ea622309
9 changed files with 104 additions and 2 deletions

View File

@@ -20,6 +20,7 @@ dependencies {
annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
compile "commons-cli:commons-cli:${commonCliVersion}"
compile "io.vavr:vavr:${vavrVersion}"
compile "com.github.javaparser:javaparser-core:${javaParserVersion}"
}
jar {

View File

@@ -3,4 +3,5 @@ ext {
lombokVersion = '1.18.6'
commonCliVersion = '1.4'
vavrVersion = '1.0.0-alpha-2'
javaParserVersion = '3.13.4'
}

View File

@@ -0,0 +1,4 @@
package com.bartek.esa.core.archetype;
public abstract class AndroidManifestPlugin extends XmlPlugin {
}

View File

@@ -12,6 +12,7 @@ public abstract class BasePlugin {
public void update(File file) {
this.file = file;
this.issues.clear();
}
public List<Issue> runForIssues() {
@@ -21,7 +22,7 @@ public abstract class BasePlugin {
protected abstract void run(File file);
protected void addIssue(int lineNumber, String line) {
protected void addIssue(Integer lineNumber, String line) {
Issue issue = Issue.builder()
.issuer(this.getClass())
.file(file)
@@ -31,4 +32,8 @@ public abstract class BasePlugin {
issues.add(issue);
}
protected File getOriginalFile() {
return file;
}
}

View File

@@ -0,0 +1,39 @@
package com.bartek.esa.core.archetype;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.BodyDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.TypeDeclaration;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.Statement;
import java.util.Collection;
import java.util.Optional;
import java.util.stream.Stream;
public abstract class JavaMethodBodyStatementPlugin extends JavaPlugin {
@Override
public void run(CompilationUnit compilationUnit) {
Stream<Statement> statements = compilationUnit.getTypes().stream()
.map(TypeDeclaration::getMembers)
.flatMap(Collection::stream)
.filter(BodyDeclaration::isMethodDeclaration)
.map(b -> (MethodDeclaration) b)
.map(MethodDeclaration::getBody)
.flatMap(Optional::stream)
.map(BlockStmt::getStatements)
.flatMap(Collection::stream);
checkStatements(statements);
}
protected abstract void checkStatements(Stream<Statement> statements);
protected Integer getLineNumberOfStatement(Statement statement) {
return statement.getRange()
.map(range -> range.begin)
.map(begin -> begin.line)
.orElse(null);
}
}

View File

@@ -0,0 +1,19 @@
package com.bartek.esa.core.archetype;
import com.bartek.esa.error.EsaException;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import io.vavr.control.Try;
import java.io.File;
public abstract class JavaPlugin extends BasePlugin {
@Override
protected void run(File file) {
CompilationUnit compilationUnit = Try.of(() -> StaticJavaParser.parse(file)).getOrElseThrow(EsaException::new);
run(compilationUnit);
}
public abstract void run(CompilationUnit compilationUnit);
}

View File

@@ -0,0 +1,4 @@
package com.bartek.esa.core.archetype;
public abstract class ResourceLayoutPlugin extends XmlPlugin {
}

View File

@@ -0,0 +1,29 @@
package com.bartek.esa.core.archetype;
import com.bartek.esa.error.EsaException;
import io.vavr.control.Try;
import org.w3c.dom.Document;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPathFactory;
import java.io.File;
public abstract class XmlPlugin extends BasePlugin {
@Override
protected void run(File file) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newDefaultInstance();
DocumentBuilder builder = Try.of(factory::newDocumentBuilder).getOrElseThrow(EsaException::new);
Document document = Try.of(() -> builder.parse(file)).getOrElseThrow(EsaException::new);
run(document);
}
protected abstract void run(Document xml);
protected Object xPath(Document xml, String expression, QName returnType) {
return Try.of(() -> XPathFactory.newDefaultInstance().newXPath().evaluate(expression, xml, returnType))
.getOrElseThrow(EsaException::new);
}
}

View File

@@ -10,6 +10,6 @@ import java.io.File;
public class Issue {
private final Class<?> issuer;
private final File file;
private final int lineNumber;
private final Integer lineNumber;
private final String line;
}