<commit_before>/**
 * 
 */
package com.github.aro_tech.interface_it_ant;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.interfaceit.ClassCodeGenerator;
import org.interfaceit.DelegateMethodGenerator;
import org.interfaceit.meta.arguments.ArgumentNameSource;
import org.interfaceit.meta.arguments.LookupArgumentNameSource;
import org.interfaceit.meta.arguments.SourceLineReadingArgumentNameLoader;
import org.interfaceit.util.FileUtils;

import com.github.aro_tech.interface_it_ant.io.Writer;

/**
 * Custom Ant task to generate a wrapper interface delegating to static methods
 * 
 * @author aro_tech
 *
 */
public class InterfaceItTask extends Task {
	public static final int DEFAULT_INDENTATION_SPACES = 4;

	public String echo;
	private boolean debug;

	private String sourceArchivePath;
	private String delegateClass;
	private String sourceTextFilePath;
	private String outputSourceRootDirectory;
	private String targetInterfaceName;
	private String targetPackageName;
	private int indentationSpaces = DEFAULT_INDENTATION_SPACES;
	private Writer out = new Writer() {

		@Override
		public void emitThrowable(Throwable t) {
			out.emitThrowable(t);
		}

		@Override
		public void emitText(String text) {
			System.out.println(text);
		}
	};

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.tools.ant.Task#execute()
	 */
	@Override
	public void execute() throws BuildException {
		if (null != this.echo) {
			out.emitText(echo);
		}
		validateAttributes();

		ClassCodeGenerator generator = makeInterfaceItGenerator();

		try {
			if (debug) {
				out.emitText(this.toString());
			}
			File wroteFile = generator.generateClassToFile(getOutputDirectory(), getTargetInterfaceName(), getDelegateClassObject(),
					getTargetPackageName(), makeArgumentNameSource(), getIndentationSpaces());
			
			out.emitText("Wrote file: " + wroteFile.getAbsolutePath());
		} catch (IOException e) {
			if (debug) {
				out.emitThrowable(e);
			}
			throw new BuildException(e);
		} catch (ClassNotFoundException e) {
			if (debug) {
				out.emitThrowable(e);
			}
			throw new BuildException(makeMessageForDelegateClasssNotFound(e.getMessage()), e);
		} catch (Throwable t) {
			out.emitThrowable(t);
			throw new BuildException("Unepected error.  Please verify the attributes provided for this task.");
		}
	}

	private String makeMessageForDelegateClasssNotFound(String msg) {
		return "Execution of interface-it was interrupted by a ClassNotFoundException. " + "Class not found: " + msg
				+ System.lineSeparator() + "Please verify the "
				+ "classpath provided to this ant task and the value of its 'delegateClass' attribute."
				+ makePackageReminderIfNecessary(msg);
	}

	private String makePackageReminderIfNecessary(String msg) {
		String packageReminder = "";
		if (msg.indexOf('.') < 0) {
			packageReminder = "\nReminder, the 'delegateClass' requires a fully qualified class name, including the package name (for example 'com.example.ExampleDelegateClass').";
		}
		return packageReminder;
	}

	/**
	 * @return the indentationSpaces
	 */
	public int getIndentationSpaces() {
		return indentationSpaces;
	}

	/**
	 * @param indentationSpaces
	 *            the indentationSpaces to set
	 */
	public void setIndentationSpaces(int indentationSpaces) {
		this.indentationSpaces = indentationSpaces;
	}

	protected ArgumentNameSource makeArgumentNameSource() throws IOException, ClassNotFoundException {
		LookupArgumentNameSource nameSource = new LookupArgumentNameSource();
		File archiveFile = getArchiveFile();
		List<String> sourceLines = new ArrayList<>();
		if (null != archiveFile && archiveFile.exists()) {
			Class<?> delegate = this.getDelegateClassObject();
			List<String> args = new ArrayList<>();
			while(null != delegate && delegate != Object.class) {
				String canonicalClassName = delegate.getName();
				args.add(canonicalClassNameToJavaFilePath(canonicalClassName));	
				delegate = delegate.getSuperclass();
			}
			sourceLines = new FileUtils().readFilesInZipArchive(archiveFile,
					args.toArray(new String[0]));

		} else {
			File textFile = getTextFile();
			if (null != textFile && textFile.exists()) {
				sourceLines = new FileUtils().readTrimmedLinesFromFilePath(textFile.toPath());
				new SourceLineReadingArgumentNameLoader().parseAndLoad(sourceLines, nameSource);
			}
		}
		if (!sourceLines.isEmpty()) {
			new SourceLineReadingArgumentNameLoader().parseAndLoad(sourceLines, nameSource);
		}
		return nameSource;
	}

	private String canonicalClassNameToJavaFilePath(String canonicalClassName) {
		return canonicalClassName.replace('.', '/') + ".java";
	}

	private File getTextFile() {
		return getFile(this.getSourceTextFilePath());
	}

	private File getArchiveFile() {
		return getFile(this.getSourceArchivePath());
	}

	private File getFile(String pathStr) {
		if (null == pathStr) {
			return null;
		}
		return new File(pathStr);
	}

	private Class<?> getDelegateClassObject() throws ClassNotFoundException {
		return Class.forName(this.getDelegateClass());
	}

	/**
	 * @return the targetInterfaceName
	 */
	public String getTargetInterfaceName() {<commit_msg>Fix issue #1 - load source for delegate superclass - code cleanup<commit_after>/**
 * 
 */
package com.github.aro_tech.interface_it_ant;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.interfaceit.ClassCodeGenerator;
import org.interfaceit.DelegateMethodGenerator;
import org.interfaceit.meta.arguments.ArgumentNameSource;
import org.interfaceit.meta.arguments.LookupArgumentNameSource;
import org.interfaceit.meta.arguments.SourceLineReadingArgumentNameLoader;
import org.interfaceit.util.FileUtils;

import com.github.aro_tech.interface_it_ant.io.Writer;

/**
 * Custom Ant task to generate a wrapper interface delegating to static methods
 * 
 * @author aro_tech
 *
 */
public class InterfaceItTask extends Task {
	public static final int DEFAULT_INDENTATION_SPACES = 4;

	public String echo;
	private boolean debug;

	private String sourceArchivePath;
	private String delegateClass;
	private String sourceTextFilePath;
	private String outputSourceRootDirectory;
	private String targetInterfaceName;
	private String targetPackageName;
	private int indentationSpaces = DEFAULT_INDENTATION_SPACES;
	private Writer out = new Writer() {

		@Override
		public void emitThrowable(Throwable t) {
			out.emitThrowable(t);
		}

		@Override
		public void emitText(String text) {
			System.out.println(text);
		}
	};

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.tools.ant.Task#execute()
	 */
	@Override
	public void execute() throws BuildException {
		if (null != this.echo) {
			out.emitText(echo);
		}
		validateAttributes();

		ClassCodeGenerator generator = makeInterfaceItGenerator();

		try {
			if (debug) {
				out.emitText(this.toString());
			}
			File wroteFile = generator.generateClassToFile(getOutputDirectory(), getTargetInterfaceName(), getDelegateClassObject(),
					getTargetPackageName(), makeArgumentNameSource(), getIndentationSpaces());
			
			out.emitText("Wrote file: " + wroteFile.getAbsolutePath());
		} catch (IOException e) {
			if (debug) {
				out.emitThrowable(e);
			}
			throw new BuildException(e);
		} catch (ClassNotFoundException e) {
			if (debug) {
				out.emitThrowable(e);
			}
			throw new BuildException(makeMessageForDelegateClasssNotFound(e.getMessage()), e);
		} catch (Throwable t) {
			out.emitThrowable(t);
			throw new BuildException("Unepected error.  Please verify the attributes provided for this task.");
		}
	}

	private String makeMessageForDelegateClasssNotFound(String msg) {
		return "Execution of interface-it was interrupted by a ClassNotFoundException. " + "Class not found: " + msg
				+ System.lineSeparator() + "Please verify the "
				+ "classpath provided to this ant task and the value of its 'delegateClass' attribute."
				+ makePackageReminderIfNecessary(msg);
	}

	private String makePackageReminderIfNecessary(String msg) {
		String packageReminder = "";
		if (msg.indexOf('.') < 0) {
			packageReminder = "\nReminder, the 'delegateClass' requires a fully qualified class name, including the package name (for example 'com.example.ExampleDelegateClass').";
		}
		return packageReminder;
	}

	/**
	 * @return the indentationSpaces
	 */
	public int getIndentationSpaces() {
		return indentationSpaces;
	}

	/**
	 * @param indentationSpaces
	 *            the indentationSpaces to set
	 */
	public void setIndentationSpaces(int indentationSpaces) {
		this.indentationSpaces = indentationSpaces;
	}

	protected ArgumentNameSource makeArgumentNameSource() throws IOException, ClassNotFoundException {
		LookupArgumentNameSource nameSource = new LookupArgumentNameSource();
		File archiveFile = getArchiveFile();
		List<String> sourceLines = new ArrayList<>();
		if (null != archiveFile && archiveFile.exists()) {
			sourceLines = new FileUtils().readFilesInZipArchive(archiveFile,
					makeDelegateSourcePaths());

		} else {
			File textFile = getTextFile();
			if (null != textFile && textFile.exists()) {
				sourceLines = new FileUtils().readTrimmedLinesFromFilePath(textFile.toPath());
				new SourceLineReadingArgumentNameLoader().parseAndLoad(sourceLines, nameSource);
			}
		}
		if (!sourceLines.isEmpty()) {
			new SourceLineReadingArgumentNameLoader().parseAndLoad(sourceLines, nameSource);
		}
		return nameSource;
	}

	private String[] makeDelegateSourcePaths() throws ClassNotFoundException {
		Class<?> delegate = this.getDelegateClassObject();
		List<String> args = new ArrayList<>();
		while(null != delegate && delegate != Object.class) {
			String canonicalClassName = delegate.getName();
			args.add(canonicalClassNameToJavaFilePath(canonicalClassName));	
			delegate = delegate.getSuperclass();
		}
		String[] sourcePaths = args.toArray(new String[0]);
		return sourcePaths;
	}

	private String canonicalClassNameToJavaFilePath(String canonicalClassName) {
		return canonicalClassName.replace('.', '/') + ".java";
	}

	private File getTextFile() {
		return getFile(this.getSourceTextFilePath());
	}

	private File getArchiveFile() {
		return getFile(this.getSourceArchivePath());
	}

	private File getFile(String pathStr) {
		if (null == pathStr) {
			return null;
		}
		return new File(pathStr);
	}

	private Class<?> getDelegateClassObject() throws ClassNotFoundException {
		return Class.forName(this.getDelegateClass());
	}

	/**
	 * @return the targetInterfaceName
	 */
	public String getTargetInterfaceName() {
{'fraction_non_alphanumeric': 0.10631726719700514, 'fraction_numerical': 0.0006551240056153487, 'mean_word_length': 4.7981551817688555, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '534702', 'n_tokens_mistral': 3370, 'n_tokens_neox': 3015, 'n_words': 729}