diff --git a/src/main/java/com/bartek/esa/analyser/apk/ApkAnalyser.java b/src/main/java/com/bartek/esa/analyser/apk/ApkAnalyser.java new file mode 100644 index 0000000..2dae5f3 --- /dev/null +++ b/src/main/java/com/bartek/esa/analyser/apk/ApkAnalyser.java @@ -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 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; + } +} diff --git a/src/main/java/com/bartek/esa/analyser/core/Analyser.java b/src/main/java/com/bartek/esa/analyser/core/Analyser.java index 14de815..0f3eb4c 100644 --- a/src/main/java/com/bartek/esa/analyser/core/Analyser.java +++ b/src/main/java/com/bartek/esa/analyser/core/Analyser.java @@ -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 plugins; + private final FileProvider fileProvider; - public Analyser(PluginExecutor pluginExecutor, Set plugins) { + public Analyser(PluginExecutor pluginExecutor, Set plugins, FileProvider fileProvider) { this.pluginExecutor = pluginExecutor; this.plugins = plugins; + this.fileProvider = fileProvider; } public List analyse(String source, Set pluginCodes, Set excludeCodes) { - File manifest = getManifest(source); - Set files = getFiles(source); + String newSource = prepareSources(source); + File manifest = getManifest(newSource); + Set files = getFiles(newSource); Set 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 getFiles(String source); + protected abstract String getAndroidManifestGlob(); + + protected abstract String getJavaGlob(); + + protected abstract String getLayoutGlob(); + + + private File getManifest(String source) { + Set 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 getFiles(String source) { + Set javaFiles = fileProvider.getGlobMatchedFiles(source, getJavaGlob()); + Set layoutFiles = fileProvider.getGlobMatchedFiles(source, getLayoutGlob()); + Set androidManifest = Collections.singleton(getManifest(source)); + + return Stream.of(javaFiles, androidManifest, layoutFiles) + .flatMap(Set::stream) + .collect(Collectors.toSet()); + } private Set getPlugins(Set pluginCodes, Set excludeCodes) { Set outputPlugins = plugins; diff --git a/src/main/java/com/bartek/esa/analyser/di/AnalyserModule.java b/src/main/java/com/bartek/esa/analyser/di/AnalyserModule.java index 1dac532..3087dd5 100644 --- a/src/main/java/com/bartek/esa/analyser/di/AnalyserModule.java +++ b/src/main/java/com/bartek/esa/analyser/di/AnalyserModule.java @@ -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 plugins, FileProvider fileProvider) { return new SourceAnalyser(pluginExecutor, plugins, fileProvider); } + + @Provides + public ApkAnalyser apkAnalyser(PluginExecutor pluginExecutor, Set plugins, FileProvider fileProvider, Decompiler decompiler) { + return new ApkAnalyser(pluginExecutor, plugins, fileProvider, decompiler); + } } diff --git a/src/main/java/com/bartek/esa/analyser/source/SourceAnalyser.java b/src/main/java/com/bartek/esa/analyser/source/SourceAnalyser.java index b1093d7..6374ee9 100644 --- a/src/main/java/com/bartek/esa/analyser/source/SourceAnalyser.java +++ b/src/main/java/com/bartek/esa/analyser/source/SourceAnalyser.java @@ -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 plugins, FileProvider fileProvider) { - super(pluginExecutor, plugins); - this.fileProvider = fileProvider; + super(pluginExecutor, plugins, fileProvider); } @Override - protected File getManifest(String source) { - Set 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 getFiles(String source) { - Set javaFiles = fileProvider.getGlobMatchedFilesRecursively(source, "**/*.java"); - System.out.println(javaFiles); - Set layoutFiles = fileProvider.getGlobMatchedFilesRecursively(source, "**/res/layout*/**.xml"); - System.out.println(layoutFiles); - Set 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; } } diff --git a/src/main/java/com/bartek/esa/file/provider/FileProvider.java b/src/main/java/com/bartek/esa/file/provider/FileProvider.java index 411dc05..b058d37 100644 --- a/src/main/java/com/bartek/esa/file/provider/FileProvider.java +++ b/src/main/java/com/bartek/esa/file/provider/FileProvider.java @@ -27,7 +27,7 @@ public class FileProvider { .toFile(); } - public Set getGlobMatchedFilesRecursively(String path, String globPattern) { + public Set getGlobMatchedFiles(String path, String globPattern) { return Try.of(() -> Files.walk(Paths.get(path)) .filter(p -> globMatcher.pathMatchesGlobPattern(p, globPattern)) .map(Path::toFile)