6: Improve Analysers
This commit is contained in:
43
src/main/java/com/bartek/esa/analyser/apk/ApkAnalyser.java
Normal file
43
src/main/java/com/bartek/esa/analyser/apk/ApkAnalyser.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package com.bartek.esa.analyser.apk;
|
||||
|
||||
import com.bartek.esa.analyser.core.Analyser;
|
||||
import com.bartek.esa.core.archetype.Plugin;
|
||||
import com.bartek.esa.core.executor.PluginExecutor;
|
||||
import com.bartek.esa.decompiler.decompiler.Decompiler;
|
||||
import com.bartek.esa.file.provider.FileProvider;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Set;
|
||||
|
||||
public class ApkAnalyser extends Analyser {
|
||||
private static final String ANDROID_MANIFEST_GLOB = "**/" + Decompiler.XML_FILES_DIR + "/AndroidManifest.xml";
|
||||
private static final String JAVA_GLOB = "**/" + Decompiler.JAVA_FILES_DIR + "/**/*.java";
|
||||
private static final String LAYOUT_GLOB = "**/" + Decompiler.XML_FILES_DIR + "/**/layout*/*.xml";
|
||||
|
||||
private final Decompiler decompiler;
|
||||
|
||||
public ApkAnalyser(PluginExecutor pluginExecutor, Set<Plugin> plugins, FileProvider fileProvider, Decompiler decompiler) {
|
||||
super(pluginExecutor, plugins, fileProvider);
|
||||
this.decompiler = decompiler;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String prepareSources(String source) {
|
||||
return decompiler.decompile(new File(source)).getAbsolutePath();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getAndroidManifestGlob() {
|
||||
return ANDROID_MANIFEST_GLOB;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getJavaGlob() {
|
||||
return JAVA_GLOB;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getLayoutGlob() {
|
||||
return LAYOUT_GLOB;
|
||||
}
|
||||
}
|
||||
@@ -3,33 +3,68 @@ package com.bartek.esa.analyser.core;
|
||||
import com.bartek.esa.core.archetype.Plugin;
|
||||
import com.bartek.esa.core.executor.PluginExecutor;
|
||||
import com.bartek.esa.core.model.object.Issue;
|
||||
import com.bartek.esa.error.EsaException;
|
||||
import com.bartek.esa.file.provider.FileProvider;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public abstract class Analyser {
|
||||
private final PluginExecutor pluginExecutor;
|
||||
private final Set<Plugin> plugins;
|
||||
private final FileProvider fileProvider;
|
||||
|
||||
public Analyser(PluginExecutor pluginExecutor, Set<Plugin> plugins) {
|
||||
public Analyser(PluginExecutor pluginExecutor, Set<Plugin> plugins, FileProvider fileProvider) {
|
||||
|
||||
this.pluginExecutor = pluginExecutor;
|
||||
this.plugins = plugins;
|
||||
this.fileProvider = fileProvider;
|
||||
}
|
||||
|
||||
public List<Issue> analyse(String source, Set<String> pluginCodes, Set<String> excludeCodes) {
|
||||
File manifest = getManifest(source);
|
||||
Set<File> files = getFiles(source);
|
||||
String newSource = prepareSources(source);
|
||||
File manifest = getManifest(newSource);
|
||||
Set<File> files = getFiles(newSource);
|
||||
Set<Plugin> selectedPlugins = getPlugins(pluginCodes, excludeCodes);
|
||||
|
||||
return pluginExecutor.executeForFiles(manifest, files, selectedPlugins);
|
||||
}
|
||||
|
||||
protected abstract File getManifest(String source);
|
||||
protected abstract String prepareSources(String source);
|
||||
|
||||
protected abstract Set<File> getFiles(String source);
|
||||
protected abstract String getAndroidManifestGlob();
|
||||
|
||||
protected abstract String getJavaGlob();
|
||||
|
||||
protected abstract String getLayoutGlob();
|
||||
|
||||
|
||||
private File getManifest(String source) {
|
||||
Set<File> manifests = fileProvider.getGlobMatchedFiles(source, getAndroidManifestGlob());
|
||||
if (manifests.isEmpty()) {
|
||||
throw new EsaException("No AndroidManifest.xml file found. Interrupting...");
|
||||
}
|
||||
|
||||
if (manifests.size() > 1) {
|
||||
throw new EsaException("Found multiple AndroidManifest.xml files. Interrupting...");
|
||||
}
|
||||
|
||||
return (File) (manifests.toArray())[0];
|
||||
}
|
||||
|
||||
private Set<File> getFiles(String source) {
|
||||
Set<File> javaFiles = fileProvider.getGlobMatchedFiles(source, getJavaGlob());
|
||||
Set<File> layoutFiles = fileProvider.getGlobMatchedFiles(source, getLayoutGlob());
|
||||
Set<File> androidManifest = Collections.singleton(getManifest(source));
|
||||
|
||||
return Stream.of(javaFiles, androidManifest, layoutFiles)
|
||||
.flatMap(Set::stream)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private Set<Plugin> getPlugins(Set<String> pluginCodes, Set<String> excludeCodes) {
|
||||
Set<Plugin> outputPlugins = plugins;
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.bartek.esa.analyser.di;
|
||||
|
||||
import com.bartek.esa.analyser.apk.ApkAnalyser;
|
||||
import com.bartek.esa.analyser.source.SourceAnalyser;
|
||||
import com.bartek.esa.core.archetype.Plugin;
|
||||
import com.bartek.esa.core.executor.PluginExecutor;
|
||||
import com.bartek.esa.decompiler.decompiler.Decompiler;
|
||||
import com.bartek.esa.file.provider.FileProvider;
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
@@ -16,4 +18,9 @@ public class AnalyserModule {
|
||||
public SourceAnalyser sourceAnalyser(PluginExecutor pluginExecutor, Set<Plugin> plugins, FileProvider fileProvider) {
|
||||
return new SourceAnalyser(pluginExecutor, plugins, fileProvider);
|
||||
}
|
||||
|
||||
@Provides
|
||||
public ApkAnalyser apkAnalyser(PluginExecutor pluginExecutor, Set<Plugin> plugins, FileProvider fileProvider, Decompiler decompiler) {
|
||||
return new ApkAnalyser(pluginExecutor, plugins, fileProvider, decompiler);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,50 +3,36 @@ package com.bartek.esa.analyser.source;
|
||||
import com.bartek.esa.analyser.core.Analyser;
|
||||
import com.bartek.esa.core.archetype.Plugin;
|
||||
import com.bartek.esa.core.executor.PluginExecutor;
|
||||
import com.bartek.esa.error.EsaException;
|
||||
import com.bartek.esa.file.provider.FileProvider;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class SourceAnalyser extends Analyser {
|
||||
private final FileProvider fileProvider;
|
||||
private static final String ANDROID_MANIFEST_GLOB = "**/AndroidManifest.xml";
|
||||
private static final String JAVA_GLOB = "**/*.java";
|
||||
private static final String LAYOUT_GLOB = "**/res/layout*/*.xml";
|
||||
|
||||
@Inject
|
||||
public SourceAnalyser(PluginExecutor pluginExecutor, Set<Plugin> plugins, FileProvider fileProvider) {
|
||||
super(pluginExecutor, plugins);
|
||||
this.fileProvider = fileProvider;
|
||||
super(pluginExecutor, plugins, fileProvider);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected File getManifest(String source) {
|
||||
Set<File> manifests = fileProvider.getGlobMatchedFilesRecursively(source, "**/AndroidManifest.xml");
|
||||
if (manifests.isEmpty()) {
|
||||
throw new EsaException("No AndroidManifest.xml file found. Interrupting...");
|
||||
}
|
||||
|
||||
if (manifests.size() > 1) {
|
||||
throw new EsaException("Found multiple AndroidManifest.xml files. Interrupting...");
|
||||
}
|
||||
|
||||
return (File) (manifests.toArray())[0];
|
||||
protected String prepareSources(String source) {
|
||||
return source;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<File> getFiles(String source) {
|
||||
Set<File> javaFiles = fileProvider.getGlobMatchedFilesRecursively(source, "**/*.java");
|
||||
System.out.println(javaFiles);
|
||||
Set<File> layoutFiles = fileProvider.getGlobMatchedFilesRecursively(source, "**/res/layout*/**.xml");
|
||||
System.out.println(layoutFiles);
|
||||
Set<File> androidManifest = Collections.singleton(getManifest(source));
|
||||
System.out.println(androidManifest);
|
||||
protected String getAndroidManifestGlob() {
|
||||
return ANDROID_MANIFEST_GLOB;
|
||||
}
|
||||
|
||||
return Stream.of(javaFiles, androidManifest, layoutFiles)
|
||||
.flatMap(Set::stream)
|
||||
.collect(Collectors.toSet());
|
||||
@Override
|
||||
protected String getJavaGlob() {
|
||||
return JAVA_GLOB;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getLayoutGlob() {
|
||||
return LAYOUT_GLOB;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public class FileProvider {
|
||||
.toFile();
|
||||
}
|
||||
|
||||
public Set<File> getGlobMatchedFilesRecursively(String path, String globPattern) {
|
||||
public Set<File> getGlobMatchedFiles(String path, String globPattern) {
|
||||
return Try.of(() -> Files.walk(Paths.get(path))
|
||||
.filter(p -> globMatcher.pathMatchesGlobPattern(p, globPattern))
|
||||
.map(Path::toFile)
|
||||
|
||||
Reference in New Issue
Block a user