<commit_before>/**
 * 
 */
package com.evolveum.midpoint.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;

/**
 * @author <NAME>
 * 
 */
public class ClassPathUtil {

	public static Trace LOGGER = TraceManager.getTrace(ClassPathUtil.class);

	public static Set<Class> listClasses(String packageName) {

		Set<Class> classes = new HashSet<Class>();

		String path = packageName.replace('.', '/');
		Enumeration<URL> resources = null;
		try {//HACK this is not available use LOGGER
			resources = LOGGER.getClass().getClassLoader().getResources(path);
		} catch (IOException e) {
			LOGGER.error("Classloader scaning error for " + path, e);
		}

		while (resources.hasMoreElements()) {
			URL candidateUrl = resources.nextElement();
			LOGGER.debug("Candidates from: " + candidateUrl.getPath());

			// test if it is a directory or JAR
			if ("file".contentEquals(candidateUrl.getProtocol())) {
				classes.addAll(getFromDirectory(candidateUrl, packageName));

			} else if ("jar".contentEquals(candidateUrl.getProtocol())) {
				classes.addAll(getFromJar(candidateUrl, packageName));

			}
		}

		return classes;
	}

	/**
	 * Get clasess from JAR
	 * 
	 * @param candidateUrl
	 * @param packageName
	 * @return
	 */
	private static Collection<? extends Class> getFromJar(URL srcUrl, String packageName) {
		Set<Class> classes = new HashSet<Class>();
		// sample:
		// file:/C:/.m2/repository/test-util/1.9-SNAPSHOT/test-util-1.9-SNAPSHOT.jar!/test-data/opendj.template
		// output:
		// /C:/.m2/repository/test-util/1.9-SNAPSHOT/test-util-1.9-SNAPSHOT.jar
		String srcName = srcUrl.getPath().substring(5).split("!/")[0];

		JarFile jar = null;
		try {
			jar = new JarFile(srcName);
		} catch (IOException ex) {
			LOGGER.error("Error during open JAR {} " + srcName, ex);
			return classes;
		}
		String path = packageName.replace('.', '/');
		Enumeration<JarEntry> entries = jar.entries();
		LOGGER.debug("PATH:" + path);
		
		JarEntry e;
		while (entries.hasMoreElements()) {
			e = entries.nextElement();
			//get name and replace inner class
			String name = e.getName().replace('$', '/');
			//NOTICE: inner class are seperated and anonymous are part of the listing !! 
			
			// skip other files in other packas and skip non class files
			if (! name.contains(path) || ! name.contains(".class")) {
				continue;
			}
			//Skip all that are not in package
			if ( name.matches(path +"/.+/.*.class")) {
				continue;
			}
			
			LOGGER.info("JAR Candidate: {}", name);
			try {// to create class
				
				//Convert name back to package
				classes.add(Class.forName(name.replace('/', '.').replace(".class", "")));
			} catch (ClassNotFoundException ex) {<commit_msg>Fix which may help in windows<commit_after>/**
 * 
 */
package com.evolveum.midpoint.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;

/**
 * @author <NAME>
 * 
 */
public class ClassPathUtil {

	public static Trace LOGGER = TraceManager.getTrace(ClassPathUtil.class);

	public static Set<Class> listClasses(String packageName) {

		Set<Class> classes = new HashSet<Class>();

		String path = packageName.replace('.', '/');
		Enumeration<URL> resources = null;
		try {//HACK this is not available use LOGGER
			resources = LOGGER.getClass().getClassLoader().getResources(path);
		} catch (IOException e) {
			LOGGER.error("Classloader scaning error for " + path, e);
		}

		while (resources.hasMoreElements()) {
			URL candidateUrl = resources.nextElement();
			LOGGER.debug("Candidates from: " + candidateUrl.getPath());

			// test if it is a directory or JAR
			if ("file".contentEquals(candidateUrl.getProtocol())) {
				classes.addAll(getFromDirectory(candidateUrl, packageName));

			} else if ("jar".contentEquals(candidateUrl.getProtocol())) {
				classes.addAll(getFromJar(candidateUrl, packageName));

			}
		}

		return classes;
	}

	/**
	 * Get clasess from JAR
	 * 
	 * @param candidateUrl
	 * @param packageName
	 * @return
	 */
	private static Collection<? extends Class> getFromJar(URL srcUrl, String packageName) {
		Set<Class> classes = new HashSet<Class>();
		// sample:
		// file:/C:/.m2/repository/test-util/1.9-SNAPSHOT/test-util-1.9-SNAPSHOT.jar!/test-data/opendj.template
		// output:
		// /C:/.m2/repository/test-util/1.9-SNAPSHOT/test-util-1.9-SNAPSHOT.jar
		String srcName = srcUrl.getPath().split("!/")[0];

		//Probaly hepls fix error in windows with URI
		File jarTmp = null;
		try {
			 jarTmp = new File(new URI(srcName));
		} catch (URISyntaxException ex) {
			LOGGER.error("Error converting jar " + srcName + " name to URI:",ex);
			return classes;
		}
		
		if (! jarTmp.isFile()) {
			LOGGER.error("Is {} not a file." , srcName);
		}
		
		JarFile jar = null;
		try {
			jar = new JarFile(jarTmp);
		} catch (IOException ex) {
			LOGGER.error("Error during open JAR " + srcName, ex);
			return classes;
		}
		String path = packageName.replace('.', '/');
		Enumeration<JarEntry> entries = jar.entries();
		LOGGER.debug("PATH:" + path);
		
		JarEntry e;
		while (entries.hasMoreElements()) {
			e = entries.nextElement();
			//get name and replace inner class
			String name = e.getName().replace('$', '/');
			//NOTICE: inner class are seperated and anonymous are part of the listing !! 
			
			// skip other files in other packas and skip non class files
			if (! name.contains(path) || ! name.contains(".class")) {
				continue;
			}
			//Skip all that are not in package
			if ( name.matches(path +"/.+/.*.class")) {
				continue;
			}
			
			LOGGER.info("JAR Candidate: {}", name);
			try {// to create class
				
				//Convert name back to package
				classes.add(Class.forName(name.replace('/', '.').replace(".class", "")));
			} catch (ClassNotFoundException ex) {
{'fraction_non_alphanumeric': 0.1484631453297523, 'fraction_numerical': 0.0034318113995822144, 'mean_word_length': 3.6516308119361556, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5045267', 'n_tokens_mistral': 2426, 'n_tokens_neox': 2185, 'n_words': 512}