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

aQute.bnd.make.coverage.Coverage Maven / Gradle / Ivy

There is a newer version: 7.0.0
Show newest version
package aQute.bnd.make.coverage;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import aQute.bnd.osgi.ClassDataCollector;
import aQute.bnd.osgi.Clazz;
import aQute.bnd.osgi.Clazz.MethodDef;
import aQute.bnd.osgi.Descriptors.TypeRef;

/**
 * This class can create a coverage table between two classspaces. The
 * destination class space is used to create a table of methods. All source
 * methods that refer to a specific dest are then filled into the table.
 */
public class Coverage {

	/**
	 * Create a cross reference table from source to dest.
	 * 
	 * @param source The methods that refer to dest
	 * @param dest The methods that are being referred to
	 * @return A mapping of source methods to destination methods.
	 * @throws Exception
	 */
	public static Map> getCrossRef(Collection source, Collection dest)
			throws Exception {
		final Map> catalog = buildCatalog(dest);
		crossRef(source, catalog);
		return catalog;
	}

	private static void crossRef(Collection source, final Map> catalog)
			throws Exception {
		for (final Clazz clazz : source) {
			clazz.parseClassFileWithCollector(new ClassDataCollector() {
				// MethodDef source;

				@Override
				public void implementsInterfaces(TypeRef names[]) {
					MethodDef def = clazz.getMethodDef(0, "", "()V");
					// TODO
					for (TypeRef interfaceName : names) {
						for (Map.Entry> entry : catalog.entrySet()) {
							String catalogClass = entry.getKey().getContainingClass().getFQN();
							List references = entry.getValue();

							if (catalogClass.equals(interfaceName.getFQN())) {
								references.add(def);
							}
						}
					}
				}

				// Method definitions
				@Override
				public void method(MethodDef source) {
					// this.source = source;
				}

				// TODO need to use different reference method
				// public void reference(MethodDef reference) {
				// List references = catalog.get(reference);
				// if (references != null) {
				// references.add(source);
				// }
				// }
			});
		}
	}

	private static Map> buildCatalog(Collection sources) throws Exception {
		final Map> catalog = new TreeMap>(
				new Comparator() {
					public int compare(MethodDef a, MethodDef b) {
						return a.getName().compareTo(b.getName());
					}
				});
		for (final Clazz clazz : sources) {
			clazz.parseClassFileWithCollector(new ClassDataCollector() {

				@Override
				public boolean classStart(int access, TypeRef name) {
					return clazz.isPublic();
				}

				@Override
				public void method(MethodDef source) {
					if (source.isPublic() || source.isProtected())
						catalog.put(source, new ArrayList());
				}

			});
		}
		return catalog;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy