All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.hdu.pvs.crashfinder.util.Utils Maven / Gradle / Ivy

The newest version!
package de.hdu.pvs.crashfinder.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collection;
import java.util.Collections;
//import java.util.Comparator;
//import java.util.Iterator;
//import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
//import java.util.Map;
import java.util.Random;
import java.util.Set;
//import java.util.Map.Entry;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Utils {

	public static void checkNotNull(Object o) {
		checkNotNull(o, null);
	}

	public static void checkNotNull(Object o, String msg) {
		if (o == null) {
			throw new RuntimeException(msg);
		}
	}

	public static void checkTrue(boolean cond) {
		checkTrue(cond, "");
	}

	public static void checkTrue(boolean cond, String msg) {
		if (!cond) {
			throw new RuntimeException(msg);
		}
	}

	public static String translateSlashToDot(String str) {
		assert str != null;
		return str.replace('/', '.');
	}

	public static String translateDotToSlash(String str) {
		assert str != null;
		return str.replace('.', '/');
	}

	public static boolean isPrimitiveType(String type) {
		try {
			getJVMDescriptorForPrimitiveType(type);
			return true;
		} catch (RuntimeException e) {
			return false;
		}
	}

	public static String getJVMDescriptorForPrimitiveType(String type) {
		if (type.equals("boolean")) {
			return "Z";
		} else if (type.equals("char")) {
			return "C";
		} else if (type.equals("byte")) {
			return "B";
		} else if (type.equals("short")) {
			return "S";
		} else if (type.equals("int")) {
			return "I";
		} else if (type.equals("float")) {
			return "F";
		} else if (type.equals("long")) {
			return "J";
		} else if (type.equals("double")) {
			return "D";
		} else {
			throw new RuntimeException("Unexpected primitive type: " + type);
		}
	}

	public static void checkDirExistence(String dir) {
		File f = new File(dir);
		if (!f.isDirectory()) {
			throw new RuntimeException("File: " + f + " is not a dir");
		}
		if (!f.exists()) {
			throw new RuntimeException("Dir: " + f + " does not exist");
		}
	}

	public static void checkFileExistence(String dir) {
		File f = new File(dir);
		if (f.isDirectory()) {
			throw new RuntimeException("File: " + f + " is  a dir");
		}
		if (!f.exists()) {
			throw new RuntimeException("File: " + f + " does not exist");
		}
	}

	public static  void checkNoNull(T[] ts) {
		for (int i = 0; i < ts.length; i++) {
			if (ts[i] == null) {
				throw new RuntimeException("The " + i + "-th element is null.");
			}
		}
	}

	public static void checkPathEntryExistence(String path) {
		String[] entries = path.split(Globals.pathSep);
		for (String entry : entries) {
			File f = new File(entry);
			if (!f.exists()) {
				throw new RuntimeException("The entry: " + entry
						+ " does not exist.");
			}
		}
	}

	// must wrap in a try - catch, since this will be used in a field
	// initializer
	public static List getClassesRecursive(String dir) {
		try {
			List fileNames = new LinkedList();
			for (File f : Files.getFileListing(new File(dir), ".class")) {
				fileNames.add(f.getAbsolutePath());
			}
			return fileNames;
		} catch (Throwable e) {
			throw new Error(e);
		}
	}

	// find all class files
	public static List getJars(String dir, boolean recursive)
			throws FileNotFoundException {
		if (!recursive) {
			return getJars(dir);
		} else {
			List fileNames = new LinkedList();
			for (File f : Files.getFileListing(new File(dir), ".jar")) {
				fileNames.add(f.getAbsolutePath());
			}
			return fileNames;
		}
	}

	// find all jar files, not this is not recursive
	public static List getJars(String dir) {
		List files = Files.findFilesInDir(dir, null, ".jar");
		List fullPaths = new LinkedList();
		for (String file : files) {
			fullPaths.add(dir + Globals.fileSep + file);
		}
		// System.out.println(fullPaths);
		return fullPaths;
	}

	public static Collection extractClassFromPlugXMLFiles(
			String... fileNames) {
		Collection classNames = new LinkedHashSet();

		for (String fileName : fileNames) {
			if (!fileName.endsWith(".xml")) {
				throw new RuntimeException("The file is not an XML file: "
						+ fileName);
			}
			String content = Files.readWholeAsString(fileName);
			Collection classes = extractClasses(content);
			classNames.addAll(classes);
		}

		return classNames;
	}

	public static Collection extractClassFromPluginXML(
			String pluginJarFile) throws IOException {
		if (!pluginJarFile.endsWith(".jar")) {
			throw new RuntimeException("The input file: " + pluginJarFile
					+ " is not a jar file.");
		}
		String content = getPluginXMLContent(pluginJarFile);
		if (content != null) {
			return extractClasses(content);
		} else {
			return Collections. emptySet();
		}
	}

	// be aware, this can return null
	public static String getPluginXMLContent(String jarFilePath)
			throws IOException {
		ZipFile jarFile = new ZipFile(jarFilePath);
		ZipEntry entry = jarFile.getEntry("plugin.xml");
		if (entry == null) {
			return null;
		}
		BufferedReader in = new BufferedReader(new InputStreamReader(
				jarFile.getInputStream(entry)));
		StringBuilder sb = new StringBuilder();
		String line = in.readLine();
		while (line != null) {
			sb.append(line);
			sb.append(Globals.lineSep);
			line = in.readLine();
		}
		return sb.toString();
	}

	public static Collection extractClasses(String xmlContent) {
		final Set classList = new LinkedHashSet();
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			DefaultHandler handler = new DefaultHandler() {
				public void startElement(String uri, String localName,
						String qName, Attributes attributes)
						throws SAXException {
					if (attributes != null) {
						for (int i = 0; i < attributes.getLength(); i++) {
							if (attributes.getQName(i).equals("class")) {
								classList.add(attributes.getValue(i));
							}
						}
					}
				}
			};
			byte[] bytes = xmlContent.getBytes("UTF8");
			InputStream inputStream = new ByteArrayInputStream(bytes);
			InputSource source = new InputSource(inputStream);
			saxParser.parse(source, handler);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return classList;
	}

	public static String concatenate(Iterable strs, String sep) {
		StringBuilder sb = new StringBuilder();
		int count = 0;
		for (String str : strs) {
			if (count != 0) {
				sb.append(sep);
			}
			sb.append(str);
			count++;
		}
		return sb.toString();
	}

	public static String concatenate(String[] strs, String sep) {
		StringBuilder sb = new StringBuilder();
		int count = 0;
		for (String str : strs) {
			if (count != 0) {
				sb.append(sep);
			}
			sb.append(str);
			count++;
		}
		return sb.toString();
	}

	public static String conToPath(List strs) {
		StringBuilder sb = new StringBuilder();
		int count = 0;
		for (String str : strs) {
			if (count != 0) {
				sb.append(Globals.pathSep);
			}
			sb.append(str);
			count++;
		}
		return sb.toString();
	}

	public static  boolean includedIn(T target, T[] array) {
		if (target == null) {
			throw new RuntimeException("target can not be null.");
		}
		for (T elem : array) {
			if (elem != null && elem.equals(target)) {
				return true;
			}
		}
		return false;
	}

	public static boolean startWith(String t, String[] prefix) {
		for (String p : prefix) {
			if (t.startsWith(p)) {
				return true;
			}
		}
		return false;
	}

	public static  Collection iterableToCollection(Iterable ts) {
		Collection collection = new LinkedList();
		for (T t : ts) {
			collection.add(t);
		}
		return collection;
	}

	public static  void removeRedundant(Collection coll) {
		Set set = new LinkedHashSet();
		set.addAll(coll);
		coll.clear();
		coll.addAll(set);
	}

	public static  Iterable returnUniqueIterable(Iterable coll) {
		Set set = new LinkedHashSet();
		for (T t : coll) {
			set.add(t);
		}
		return set;
	}

	// check if every element of its is included in all
	public static  boolean includedIn(Iterable its, Iterable all) {
		Collection collection_its = iterableToCollection(its);
		Collection collection_all = iterableToCollection(its);
		return collection_all.containsAll(collection_its);
	}

	/** This project-specific methods */
	public static  int countIterable(Iterable c) {
		int count = 0;
		for (@SuppressWarnings("unused")
		T t : c) {
			count++;
		}
		return count;
	}

	public static boolean debug = true;

	public static void debugPrintln(String str) {
		if (debug) {
			System.out.println(str);
		}
	}

	public static  void logCollection(Iterable c) {
		Log.logln(dumpCollection(c));
	}

	public static  void dumpCollection(Iterable c, PrintStream out) {
		out.println(dumpCollection(c));
	}

	public static  void dumpCollection(Iterable c, String fileName) {
		try {
			Files.writeToFile(dumpCollection(c), fileName);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static  String dumpCollection(Iterable c) {
		StringBuilder sb = new StringBuilder();
		int num = 0;
		for (T t : c) {
			sb.append(t);
			sb.append(Globals.lineSep);
			num++;
		}
		sb.append("Num in total: " + num);
		return sb.toString();
	}

	public static void flushToStd(String[] msgs) {
		for (String msg : msgs) {
			System.out.println(msg);
		}
	}

	static Random random = new Random();

	public static int nextRandomInt(int range) {
		return random.nextInt(range);
	}

	public static  Object[] randomSubArray(T[] array) {
		Utils.checkTrue(array.length > 0);
		int length = nextRandomInt(array.length) + 1;
		if (length == array.length) {
			return array;
		}
		Set indexSet = new LinkedHashSet();
		while (indexSet.size() != length) {
			indexSet.add(nextRandomInt(array.length));
		}
		List elements = new LinkedList();
		for (Integer index : indexSet) {
			elements.add(array[index]);
		}
		return elements.toArray();
	}

	public static  String dumpArray(T[] ts) {
		if (ts == null) {
			return "NULL ARRAY";
		}
		StringBuilder sb = new StringBuilder();
		int num = 0;
		for (T t : ts) {
			sb.append(t);
			if (num != ts.length - 1) {
				sb.append(", ");
			}
			num++;
		}
		return sb.toString();
	}

	public static Float average(Collection ts) {
		Utils.checkTrue(ts.size() > 0);
		Float sum = 0.0f;
		for (Integer t : ts) {
			sum += (float) t;
		}
		return sum / ts.size();
	}

	public static Integer sum(Collection ts) {
		Utils.checkTrue(ts.size() > 0);
		Integer sum = 0;
		for (Integer t : ts) {
			sum = sum + t;
		}
		return sum;
	}

	// public static  Map sortByKey(Map map, final boolean
	// increase) {
	// List> list = new LinkedList>(map.entrySet());
	// Collections.sort(list, new Comparator() {
	// public int compare(Object o1, Object o2) {
	// if(increase) {
	// return ((Comparable) ((Map.Entry) (o1)).getKey())
	// .compareTo(((Map.Entry) (o2)).getKey());
	// } else {
	// return ((Comparable) ((Map.Entry) (o2)).getKey())
	// .compareTo(((Map.Entry) (o1)).getKey());
	// }
	// }
	// });
	//
	// Map result = new LinkedHashMap();
	// for (Iterator> it = list.iterator(); it.hasNext();) {
	// Map.Entry entry = (Map.Entry)it.next();
	// result.put(entry.getKey(), entry.getValue());
	// }
	// return result;
	// }

	// public static  Map sortByValue(Map map, final boolean
	// increase) {
	// List> list = new LinkedList>(map.entrySet());
	// Collections.sort(list, new Comparator() {
	// public int compare(Object o1, Object o2) {
	// if(increase) {
	// return ((Comparable) ((Map.Entry) (o1)).getValue())
	// .compareTo(((Map.Entry) (o2)).getValue());
	// } else {
	// return ((Comparable) ((Map.Entry) (o2)).getValue())
	// .compareTo(((Map.Entry) (o1)).getValue());
	// }
	// }
	// });
	//
	// Map result = new LinkedHashMap();
	// for (Iterator> it = list.iterator(); it.hasNext();) {
	// Map.Entry entry = (Map.Entry)it.next();
	// result.put(entry.getKey(), entry.getValue());
	// }
	// return result;
	// }
	//
	// public static  List sortByValueAndReturnKeys(Map map,
	// final boolean increase) {
	// Map sorted = sortByValue(map, increase);
	// List list = new LinkedList();
	// list.addAll(sorted.keySet());
	// return list;
	// }

	// public static  Map

	public static String extractClassName(String fullElement) {
		return fullElement.substring(0, fullElement.lastIndexOf("."));
	}

	public static String extractElementName(String fullElement) {
		return fullElement.substring(fullElement.lastIndexOf(".") + 1);
	}

	public static Class lookupClass(String className) {
		try {
			return Class.forName(className);
		} catch (ClassNotFoundException e) {
			throw new Error(e);
		}
	}

	public static Field lookupField(String className, String fieldName) {
		Class clz = Utils.lookupClass(className);
		return lookupField(clz, fieldName);

	}

	public static Field lookupField(Class clz, String fieldName) {
		try {
			Field[] fields = clz.getDeclaredFields();
			for (Field f : fields) {
				// System.out.println(f);
				if (f.getName().equals(fieldName)) {
					return f;
				}
			}
			throw new Error("Can not find field: " + fieldName + " in "
					+ clz.toString());
		} catch (Throwable e) {
			throw new Error(e);
		}
	}

	@SuppressWarnings("deprecation")
	public static Class loadclass(String classPath, String className) {
		// Create a File object on the root of the directory containing the
		// class file
		String[] paths = classPath.split(Globals.pathSep);
		File[] files = new File[paths.length];
		for (int i = 0; i < paths.length; i++) {
			files[i] = new File(paths[i]);
		}

		try {
			// Convert File to a URL
			URL[] urls = new URL[files.length];
			for (int i = 0; i < files.length; i++) {
				urls[i] = files[i].toURL();
			}

			// Create a new class loader with the directory
			ClassLoader cl = new URLClassLoader(urls);

			// Load in the class; MyClass.class should be located in
			// the directory file:/c:/myclasses/com/mycompany
			Class cls = cl.loadClass(className);
			return cls;
		} catch (MalformedURLException e) {
		} catch (ClassNotFoundException e) {
		}
		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy