<commit_before>package soot.jimple.infoflow.android;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import soot.PackManager;
import soot.Scene;
import soot.SootClass;
import soot.SootMethod;
import soot.jimple.infoflow.util.ArgBuilder;
import soot.jimple.infoflow.util.EntryPointCreator;
import soot.options.Options;
import soot.util.Chain;

public class AnalyzeJimpleClass {

	private List<String> methodList = new ArrayList<String>();
//	private Set<Class> classes = new HashSet<Class>();
	private Map<String, HashSet<String>> classes = new HashMap<String, HashSet<String>>();
	private Set<String> extendsImplementsClasses = new HashSet<String>();
	private String jimpleFilesLocation;
	private String androidJar;
	private List<SootMethod> methodListApp;
	private SootClass c;

	public AnalyzeJimpleClass(String jimpleFilesLocation, String androidJar) {
		this.jimpleFilesLocation = jimpleFilesLocation;
		this.androidJar = androidJar;
	}

	public void collectAndroidMethods() throws IOException {
		//search directory and process every file
		File f = new File(jimpleFilesLocation);
		File[] files = f.listFiles();
		if (files != null) {
			for (int i = 0; i < files.length; i++) {
				if (!files[i].isDirectory()) {
//					analyzeClass(files[i].getAbsolutePath());
					analyzeJimple(files[i].getName().substring(0, files[i].getName().lastIndexOf(".")));
				} else {
					System.err.println("unexpected directory in output folder");
				}

			}
		}

	}
	
	@Deprecated
	private void analyzeClass(String filename) throws IOException {

		if (checkFilename(filename)) {
			FileReader fReader = new FileReader(filename);
			BufferedReader bReader = new BufferedReader(fReader);
			// System.out.println(filename);
			String zeile;
			String extendsClass;
			String className;
			List<String> implementsClass = new ArrayList<String>();

			//only in the first line there could be class name, extends and implements
			if ((zeile = bReader.readLine()) != null) {
				// find class name
				if (zeile.matches(".*(\\s|.{0})(class|interface)\\s.*")) {
					String[] splitClassname = zeile.split("(\\s|.{0})(class|interface)\\s");

					splitClassname[1] = splitClassname[1].trim();
					int spaceIndex = splitClassname[1].indexOf(" ");
					if (spaceIndex != -1) {
						className = splitClassname[1].substring(0, spaceIndex);
					} else {
						className = splitClassname[1];
					}


					
				}
				else{
					throw new IOException("unexpected file" + zeile);
				}
				// find extend class
				if (zeile.matches(".*\\sextends\\s.*")) {
					String[] splitExtends = zeile.split("\\sextends\\s");
					splitExtends[1] = splitExtends[1].trim();
					
					int spaceIndex = splitExtends[1].indexOf(" ");
					if (spaceIndex != -1) {
						extendsClass = splitExtends[1].substring(0, spaceIndex);

					} else {
						extendsClass = splitExtends[1];
					}
					if (!extendsClass.matches("java.lang.Object")){
						extendsImplementsClasses.add(extendsClass);
					}

				}

				// find implement class
				if (zeile.matches(".*\\simplements\\s.*")) {
					String[] splitImplements = zeile.split("\\simplements\\s");
					splitImplements[1] = splitImplements[1].trim();
					
					for (String item : splitImplements[1].split(",")) {
						implementsClass.add(item.trim());
						
					}
					extendsImplementsClasses.addAll(implementsClass);

				}
				
				boolean looksLikeMethod = false;
				String savedLine = null;
				//analyze rest of the class
				while ((zeile = bReader.readLine()) != null) {
					if(looksLikeMethod){
						//it looks for {
						if(zeile.matches("\\s*\\{.*")){
		
							newEntry(className, handleLine(savedLine.trim()));
//							Class toBeAddedClass = readClass(className, savedLine.trim());
//							classes.add(toBeAddedClass);

						}
						else{
							looksLikeMethod = false;
						}
					}

					//every method that starts with on
					if(zeile.matches(".*\\son.*(.*)")){
						looksLikeMethod = true;
						savedLine = zeile;
					}


				}

			}

			bReader.close();
			fReader.close();
		}
	}
	
	
	private void analyzeJimple(String className) {

		String path = jimpleFilesLocation + ";" + androidJar;
		// prepare soot arguments:
		ArgBuilder builder = new ArgBuilder();
		String[] args = builder.buildArgs(path, className);

		Options.v().set_allow_phantom_refs(true);
		Options.v().set_no_bodies_for_excluded(true);
		Options.v().set_output_format(Options.output_format_none);
		Options.v().parse(args);

		c = Scene.v().tryLoadClass(className, SootClass.BODIES);
		
		if(c.isConcrete()){	
			c.setApplicationClass();
			
			//TODO is it recursive?
			List<SootClass> listSootClass = Scene.v().getActiveHierarchy()
					.getSuperclassesOf(c);
			methodListApp = c.getMethods();
			
			for (SootClass sc : listSootClass) {
				if (!sc.getName().equals("java.lang.Object")) {
					SootClass superClass = Scene.v().tryLoadClass(sc.getName(),
							SootClass.BODIES);
					for (SootMethod m : methodListApp) {
	
						if (superClass.declaresMethod(m.getSubSignature())
								&& !m.getSubSignature().equals("void <init>()")
								&& !m.getSubSignature().equals("void <clinit>()")) {<commit_msg>soot is called only once for jimple analysis in android<commit_after>package soot.jimple.infoflow.android;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import soot.Scene;
import soot.SootClass;
import soot.SootMethod;
import soot.jimple.infoflow.util.ArgBuilder;
import soot.options.Options;
import soot.util.Chain;

public class AnalyzeJimpleClass {

	private List<String> methodList = new ArrayList<String>();
//	private Set<Class> classes = new HashSet<Class>();
	private Map<String, HashSet<String>> classes = new HashMap<String, HashSet<String>>();
	private Set<String> extendsImplementsClasses = new HashSet<String>();
	private String jimpleFilesLocation;
	private String androidJar;
	private List<SootMethod> methodListApp;
	private SootClass c;

	public AnalyzeJimpleClass(String jimpleFilesLocation, String androidJar) {
		this.jimpleFilesLocation = jimpleFilesLocation;
		this.androidJar = androidJar;
	}

	public void collectAndroidMethods() throws IOException {
		//search directory and process every file
		File f = new File(jimpleFilesLocation);
		File[] files = f.listFiles();
		if (files != null) {
			String path = jimpleFilesLocation + ";" + androidJar;
			// prepare soot arguments:
			ArgBuilder builder = new ArgBuilder();
			String[] args = builder.buildArgs(path);

			Options.v().set_allow_phantom_refs(true);
			Options.v().set_no_bodies_for_excluded(true);
			Options.v().set_output_format(Options.output_format_none);
			Options.v().parse(args);

			
			for (int i = 0; i < files.length; i++) {
				if (!files[i].isDirectory()) {
//					analyzeClass(files[i].getAbsolutePath());
					analyzeJimple(files[i].getName().substring(0, files[i].getName().lastIndexOf(".")));
				} else {
					System.err.println("unexpected directory in output folder");
				}

			}
		}

	}
	
	@Deprecated
	private void analyzeClass(String filename) throws IOException {

		if (checkFilename(filename)) {
			FileReader fReader = new FileReader(filename);
			BufferedReader bReader = new BufferedReader(fReader);
			// System.out.println(filename);
			String zeile;
			String extendsClass;
			String className;
			List<String> implementsClass = new ArrayList<String>();

			//only in the first line there could be class name, extends and implements
			if ((zeile = bReader.readLine()) != null) {
				// find class name
				if (zeile.matches(".*(\\s|.{0})(class|interface)\\s.*")) {
					String[] splitClassname = zeile.split("(\\s|.{0})(class|interface)\\s");

					splitClassname[1] = splitClassname[1].trim();
					int spaceIndex = splitClassname[1].indexOf(" ");
					if (spaceIndex != -1) {
						className = splitClassname[1].substring(0, spaceIndex);
					} else {
						className = splitClassname[1];
					}


					
				}
				else{
					throw new IOException("unexpected file" + zeile);
				}
				// find extend class
				if (zeile.matches(".*\\sextends\\s.*")) {
					String[] splitExtends = zeile.split("\\sextends\\s");
					splitExtends[1] = splitExtends[1].trim();
					
					int spaceIndex = splitExtends[1].indexOf(" ");
					if (spaceIndex != -1) {
						extendsClass = splitExtends[1].substring(0, spaceIndex);

					} else {
						extendsClass = splitExtends[1];
					}
					if (!extendsClass.matches("java.lang.Object")){
						extendsImplementsClasses.add(extendsClass);
					}

				}

				// find implement class
				if (zeile.matches(".*\\simplements\\s.*")) {
					String[] splitImplements = zeile.split("\\simplements\\s");
					splitImplements[1] = splitImplements[1].trim();
					
					for (String item : splitImplements[1].split(",")) {
						implementsClass.add(item.trim());
						
					}
					extendsImplementsClasses.addAll(implementsClass);

				}
				
				boolean looksLikeMethod = false;
				String savedLine = null;
				//analyze rest of the class
				while ((zeile = bReader.readLine()) != null) {
					if(looksLikeMethod){
						//it looks for {
						if(zeile.matches("\\s*\\{.*")){
		
							newEntry(className, handleLine(savedLine.trim()));
//							Class toBeAddedClass = readClass(className, savedLine.trim());
//							classes.add(toBeAddedClass);

						}
						else{
							looksLikeMethod = false;
						}
					}

					//every method that starts with on
					if(zeile.matches(".*\\son.*(.*)")){
						looksLikeMethod = true;
						savedLine = zeile;
					}


				}

			}

			bReader.close();
			fReader.close();
		}
	}
	
	
	private void analyzeJimple(String className) {
		c = Scene.v().tryLoadClass(className, SootClass.BODIES);
		if(c.isConcrete()){	
			c.setApplicationClass();
			
			//TODO is it recursive?
			List<SootClass> listSootClass = Scene.v().getActiveHierarchy()
					.getSuperclassesOf(c);
			methodListApp = c.getMethods();
			
			for (SootClass sc : listSootClass) {
				if (!sc.getName().equals("java.lang.Object")) {
					SootClass superClass = Scene.v().tryLoadClass(sc.getName(),
							SootClass.BODIES);
					for (SootMethod m : methodListApp) {
	
						if (superClass.declaresMethod(m.getSubSignature())
								&& !m.getSubSignature().equals("void <init>()")
								&& !m.getSubSignature().equals("void <clinit>()")) {
{'fraction_non_alphanumeric': 0.1452169855342977, 'fraction_numerical': 0.003919738684087727, 'mean_word_length': 3.442786069651741, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7484324', 'n_tokens_mistral': 4237, 'n_tokens_neox': 3340, 'n_words': 684}