10: Create LoggingPlugin
This commit is contained in:
@@ -7,6 +7,7 @@ import com.bartek.esa.error.EsaException;
|
||||
import com.bartek.esa.file.matcher.GlobMatcher;
|
||||
import com.github.javaparser.StaticJavaParser;
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.expr.Expression;
|
||||
import io.vavr.control.Try;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
@@ -61,5 +62,9 @@ public abstract class JavaPlugin extends BasePlugin {
|
||||
return globMatcher.fileMatchesGlobPattern(file, String.format("**/%s/**", path));
|
||||
}
|
||||
|
||||
protected Integer getLineNumberFromExpression(Expression expression) {
|
||||
return expression.getRange().map(r -> r.begin.line).orElse(null);
|
||||
}
|
||||
|
||||
public abstract void run(CompilationUnit compilationUnit);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.bartek.esa.core.di;
|
||||
|
||||
import com.bartek.esa.core.desc.provider.DescriptionProvider;
|
||||
import com.bartek.esa.core.executor.PluginExecutor;
|
||||
import com.bartek.esa.core.java.JavaSyntaxRegexProvider;
|
||||
import com.bartek.esa.core.xml.XmlHelper;
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
@@ -15,11 +14,6 @@ public class CoreModule {
|
||||
return new PluginExecutor(xmlHelper);
|
||||
}
|
||||
|
||||
@Provides
|
||||
public JavaSyntaxRegexProvider javaSyntaxRegexProvider() {
|
||||
return new JavaSyntaxRegexProvider();
|
||||
}
|
||||
|
||||
@Provides
|
||||
public DescriptionProvider descriptionProvider() {
|
||||
return new DescriptionProvider();
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package com.bartek.esa.core.di;
|
||||
|
||||
import com.bartek.esa.core.archetype.Plugin;
|
||||
import com.bartek.esa.core.plugin.LoggingPlugin;
|
||||
import com.bartek.esa.core.xml.XmlHelper;
|
||||
import com.bartek.esa.file.matcher.GlobMatcher;
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import dagger.multibindings.ElementsIntoSet;
|
||||
import dagger.multibindings.IntoSet;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
@@ -16,4 +20,10 @@ public class PluginModule {
|
||||
public Set<Plugin> plugins() {
|
||||
return new HashSet<>();
|
||||
}
|
||||
|
||||
@Provides
|
||||
@IntoSet
|
||||
public Plugin loggingPlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) {
|
||||
return new LoggingPlugin(globMatcher, xmlHelper);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.bartek.esa.core.java;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
public class JavaSyntaxRegexProvider {
|
||||
|
||||
@Inject
|
||||
public JavaSyntaxRegexProvider() {
|
||||
|
||||
}
|
||||
|
||||
public String methodInvocation(String methodName) {
|
||||
return format("^%s\\s*\\($", methodName);
|
||||
}
|
||||
}
|
||||
32
src/main/java/com/bartek/esa/core/plugin/LoggingPlugin.java
Normal file
32
src/main/java/com/bartek/esa/core/plugin/LoggingPlugin.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package com.bartek.esa.core.plugin;
|
||||
|
||||
import com.bartek.esa.core.archetype.JavaPlugin;
|
||||
import com.bartek.esa.core.model.enumeration.Severity;
|
||||
import com.bartek.esa.core.xml.XmlHelper;
|
||||
import com.bartek.esa.file.matcher.GlobMatcher;
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.expr.MethodCallExpr;
|
||||
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class LoggingPlugin extends JavaPlugin {
|
||||
|
||||
@Inject
|
||||
public LoggingPlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) {
|
||||
super(globMatcher, xmlHelper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(CompilationUnit compilationUnit) {
|
||||
compilationUnit.accept(new VoidVisitorAdapter<Void>() {
|
||||
@Override
|
||||
public void visit(MethodCallExpr methodCall, Void arg) {
|
||||
if (methodCall.getName().getIdentifier().matches("v|d|i|w|e|wtf")) {
|
||||
addIssue(Severity.INFO, getLineNumberFromExpression(methodCall), methodCall.toString());
|
||||
}
|
||||
super.visit(methodCall, arg);
|
||||
}
|
||||
}, null);
|
||||
}
|
||||
}
|
||||
@@ -2,3 +2,6 @@ com.bartek.esa.core.archetype.JavaPlugin.NO_PACKAGE=There is no package defined
|
||||
Package should be defined as attribute of <manifest> tag.\n\
|
||||
For example: <manifest package="com.bartek.esa.test">\n\
|
||||
Please fix it to use this tool.
|
||||
|
||||
com.bartek.esa.core.plugin.LoggingPlugin=Potential data leakage. \n\
|
||||
Logging method was detected. Please check if no sensitive data is logged there.
|
||||
Reference in New Issue
Block a user