5: Create basic plugin archetypes
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -3,4 +3,5 @@ ext {
|
||||
lombokVersion = '1.18.6'
|
||||
commonCliVersion = '1.4'
|
||||
vavrVersion = '1.0.0-alpha-2'
|
||||
javaParserVersion = '3.13.4'
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.bartek.esa.core.archetype;
|
||||
|
||||
public abstract class AndroidManifestPlugin extends XmlPlugin {
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
19
src/main/java/com/bartek/esa/core/archetype/JavaPlugin.java
Normal file
19
src/main/java/com/bartek/esa/core/archetype/JavaPlugin.java
Normal 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);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.bartek.esa.core.archetype;
|
||||
|
||||
public abstract class ResourceLayoutPlugin extends XmlPlugin {
|
||||
}
|
||||
29
src/main/java/com/bartek/esa/core/archetype/XmlPlugin.java
Normal file
29
src/main/java/com/bartek/esa/core/archetype/XmlPlugin.java
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user