<commit_before>package org.jvnet.jaxb2_commons.plugin.simplehashcode;

import java.util.Arrays;
import java.util.Collection;
import java.util.Set;

import javax.xml.namespace.QName;

import org.jvnet.jaxb2_commons.plugin.AbstractParameterizablePlugin;
import org.jvnet.jaxb2_commons.plugin.Customizations;
import org.jvnet.jaxb2_commons.plugin.CustomizedIgnoring;
import org.jvnet.jaxb2_commons.plugin.Ignoring;
import org.jvnet.jaxb2_commons.plugin.simplehashcode.generator.HashCodeCodeGenerator;
import org.jvnet.jaxb2_commons.plugin.simplehashcode.generator.HashCodeCodeGeneratorFactory;
import org.jvnet.jaxb2_commons.plugin.simplehashcode.generator.TypedHashCodeCodeGeneratorFactory;
import org.jvnet.jaxb2_commons.plugin.util.FieldOutlineUtils;
import org.jvnet.jaxb2_commons.plugin.util.StrategyClassUtils;
import org.jvnet.jaxb2_commons.util.FieldAccessorFactory;
import org.jvnet.jaxb2_commons.util.FieldUtils;
import org.jvnet.jaxb2_commons.util.PropertyFieldAccessorFactory;
import org.jvnet.jaxb2_commons.xjc.outline.FieldAccessorEx;
import org.xml.sax.ErrorHandler;

import com.sun.codemodel.JBlock;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JType;
import com.sun.codemodel.JVar;
import com.sun.tools.xjc.Options;
import com.sun.tools.xjc.outline.Aspect;
import com.sun.tools.xjc.outline.ClassOutline;
import com.sun.tools.xjc.outline.FieldOutline;
import com.sun.tools.xjc.outline.Outline;

public class SimpleHashCodePlugin extends AbstractParameterizablePlugin {

	@Override
	public String getOptionName() {
		return "XsimpleHashCode";
	}

	@Override
	public String getUsage() {
		// TODO
		return "TBD";
	}

	private FieldAccessorFactory fieldAccessorFactory = PropertyFieldAccessorFactory.INSTANCE;

	public FieldAccessorFactory getFieldAccessorFactory() {
		return fieldAccessorFactory;
	}

	public void setFieldAccessorFactory(
			FieldAccessorFactory fieldAccessorFactory) {
		this.fieldAccessorFactory = fieldAccessorFactory;
	}

	/*
	 * private String hashCodeStrategyClass =
	 * JAXBHashCodeStrategy.class.getName();
	 * 
	 * public void setHashCodeStrategyClass(String hashCodeStrategy) {
	 * this.hashCodeStrategyClass = hashCodeStrategy; }
	 * 
	 * public String getHashCodeStrategyClass() { return hashCodeStrategyClass;
	 * }
	 * 
	 * public JExpression createHashCodeStrategy(JCodeModel codeModel) { return
	 * StrategyClassUtils.createStrategyInstanceExpression(codeModel,
	 * HashCodeStrategy.class, getHashCodeStrategyClass()); }
	 */

	private Ignoring ignoring = new CustomizedIgnoring(
			org.jvnet.jaxb2_commons.plugin.hashcode.Customizations.IGNORED_ELEMENT_NAME,
			Customizations.IGNORED_ELEMENT_NAME,
			Customizations.GENERATED_ELEMENT_NAME);

	public Ignoring getIgnoring() {
		return ignoring;
	}

	public void setIgnoring(Ignoring ignoring) {
		this.ignoring = ignoring;
	}

	@Override
	public Collection<QName> getCustomizationElementNames() {
		return Arrays
				.asList(org.jvnet.jaxb2_commons.plugin.hashcode.Customizations.IGNORED_ELEMENT_NAME,
						Customizations.IGNORED_ELEMENT_NAME,
						Customizations.GENERATED_ELEMENT_NAME);
	}

	private TypedHashCodeCodeGeneratorFactory codeGeneratorFactory;

	private TypedHashCodeCodeGeneratorFactory getCodeGeneratorFactory() {
		if (codeGeneratorFactory == null) {
			throw new IllegalStateException(
					"Code generator factory was not set yet.");
		}
		return codeGeneratorFactory;
	}

	@Override
	public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) {
		this.codeGeneratorFactory = new HashCodeCodeGeneratorFactory(
				outline.getCodeModel());

		for (final ClassOutline classOutline : outline.getClasses()) {
			if (!getIgnoring().isIgnored(classOutline)) {
				processClassOutline(classOutline);
			}
		}
		return true;
	}

	protected void processClassOutline(ClassOutline classOutline) {
		final JDefinedClass theClass = classOutline.implClass;
		@SuppressWarnings("unused")
		final JMethod object$hashCode = generateObject$hashCode(classOutline,
				theClass);
	}

	protected JMethod generateObject$hashCode(final ClassOutline classOutline,
			final JDefinedClass theClass) {

		final JCodeModel codeModel = theClass.owner();
		final JMethod object$hashCode = theClass.method(JMod.PUBLIC,
				codeModel.INT, "hashCode");
		{
			final JBlock body = object$hashCode.body();

			final JExpression currentHashCodeExpression = JExpr
					.lit(getCodeGeneratorFactory().getInitial());

			final JVar currentHashCode = body.decl(codeModel.INT,
					"currentHashCode", currentHashCodeExpression);

			final Boolean superClassImplementsHashCode = StrategyClassUtils
					.superClassNotIgnored(classOutline, ignoring);

			if (superClassImplementsHashCode != null) {
				body.assign(
						currentHashCode,
						currentHashCode.mul(
								JExpr.lit(getCodeGeneratorFactory()
										.getMultiplier())).plus(
								JExpr._super().invoke("hashCode")));
			}

			final FieldOutline[] declaredFields = FieldOutlineUtils.filter(
					classOutline.getDeclaredFields(), getIgnoring());

			if (declaredFields.length > 0) {

				for (final FieldOutline fieldOutline : declaredFields) {
					final FieldAccessorEx fieldAccessor = getFieldAccessorFactory()
							.createFieldAccessor(fieldOutline, JExpr._this());
					if (fieldAccessor.isConstant()) {
						continue;
					}
					final JBlock block = body.block();

					String propertyName = fieldOutline.getPropertyInfo().getName(
							true);
					final JVar value = block.decl(
							fieldAccessor.getType(),
							"the"
									+ propertyName);
					if ("CeOrCf".equalsIgnoreCase(propertyName))
					{
						System.out.println(fieldOutline);
					}

					fieldAccessor.toRawValue(block, value);
					final JType exposedType = fieldAccessor.getType();
					
					final Collection<JType> possibleTypes = FieldUtils.getPossibleTypes(fieldOutline, Aspect.EXPOSED);
					System.out.println(propertyName);
					System.out.println(possibleTypes);
					final boolean isAlwaysSet = fieldAccessor.isAlwaysSet();
					final JExpression hasSetValue = fieldAccessor.hasSetValue();
					final HashCodeCodeGenerator codeGenerator = getCodeGeneratorFactory()
							.getCodeGenerator(exposedType);
					codeGenerator.generate(block, currentHashCode, exposedType, possibleTypes,
							value, hasSetValue, isAlwaysSet);
				}
			}
			body._return(currentHashCode);
		}
		return object$hashCode;
	}
}<commit_msg>Issue #11. Removed debug statements.<commit_after>package org.jvnet.jaxb2_commons.plugin.simplehashcode;

import java.util.Arrays;
import java.util.Collection;
import java.util.Set;

import javax.xml.namespace.QName;

import org.jvnet.jaxb2_commons.plugin.AbstractParameterizablePlugin;
import org.jvnet.jaxb2_commons.plugin.Customizations;
import org.jvnet.jaxb2_commons.plugin.CustomizedIgnoring;
import org.jvnet.jaxb2_commons.plugin.Ignoring;
import org.jvnet.jaxb2_commons.plugin.simplehashcode.generator.HashCodeCodeGenerator;
import org.jvnet.jaxb2_commons.plugin.simplehashcode.generator.HashCodeCodeGeneratorFactory;
import org.jvnet.jaxb2_commons.plugin.simplehashcode.generator.TypedHashCodeCodeGeneratorFactory;
import org.jvnet.jaxb2_commons.plugin.util.FieldOutlineUtils;
import org.jvnet.jaxb2_commons.plugin.util.StrategyClassUtils;
import org.jvnet.jaxb2_commons.util.FieldAccessorFactory;
import org.jvnet.jaxb2_commons.util.FieldUtils;
import org.jvnet.jaxb2_commons.util.PropertyFieldAccessorFactory;
import org.jvnet.jaxb2_commons.xjc.outline.FieldAccessorEx;
import org.xml.sax.ErrorHandler;

import com.sun.codemodel.JBlock;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JType;
import com.sun.codemodel.JVar;
import com.sun.tools.xjc.Options;
import com.sun.tools.xjc.outline.Aspect;
import com.sun.tools.xjc.outline.ClassOutline;
import com.sun.tools.xjc.outline.FieldOutline;
import com.sun.tools.xjc.outline.Outline;

public class SimpleHashCodePlugin extends AbstractParameterizablePlugin {

	@Override
	public String getOptionName() {
		return "XsimpleHashCode";
	}

	@Override
	public String getUsage() {
		// TODO
		return "TBD";
	}

	private FieldAccessorFactory fieldAccessorFactory = PropertyFieldAccessorFactory.INSTANCE;

	public FieldAccessorFactory getFieldAccessorFactory() {
		return fieldAccessorFactory;
	}

	public void setFieldAccessorFactory(
			FieldAccessorFactory fieldAccessorFactory) {
		this.fieldAccessorFactory = fieldAccessorFactory;
	}

	/*
	 * private String hashCodeStrategyClass =
	 * JAXBHashCodeStrategy.class.getName();
	 * 
	 * public void setHashCodeStrategyClass(String hashCodeStrategy) {
	 * this.hashCodeStrategyClass = hashCodeStrategy; }
	 * 
	 * public String getHashCodeStrategyClass() { return hashCodeStrategyClass;
	 * }
	 * 
	 * public JExpression createHashCodeStrategy(JCodeModel codeModel) { return
	 * StrategyClassUtils.createStrategyInstanceExpression(codeModel,
	 * HashCodeStrategy.class, getHashCodeStrategyClass()); }
	 */

	private Ignoring ignoring = new CustomizedIgnoring(
			org.jvnet.jaxb2_commons.plugin.hashcode.Customizations.IGNORED_ELEMENT_NAME,
			Customizations.IGNORED_ELEMENT_NAME,
			Customizations.GENERATED_ELEMENT_NAME);

	public Ignoring getIgnoring() {
		return ignoring;
	}

	public void setIgnoring(Ignoring ignoring) {
		this.ignoring = ignoring;
	}

	@Override
	public Collection<QName> getCustomizationElementNames() {
		return Arrays
				.asList(org.jvnet.jaxb2_commons.plugin.hashcode.Customizations.IGNORED_ELEMENT_NAME,
						Customizations.IGNORED_ELEMENT_NAME,
						Customizations.GENERATED_ELEMENT_NAME);
	}

	private TypedHashCodeCodeGeneratorFactory codeGeneratorFactory;

	private TypedHashCodeCodeGeneratorFactory getCodeGeneratorFactory() {
		if (codeGeneratorFactory == null) {
			throw new IllegalStateException(
					"Code generator factory was not set yet.");
		}
		return codeGeneratorFactory;
	}

	@Override
	public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) {
		this.codeGeneratorFactory = new HashCodeCodeGeneratorFactory(
				outline.getCodeModel());

		for (final ClassOutline classOutline : outline.getClasses()) {
			if (!getIgnoring().isIgnored(classOutline)) {
				processClassOutline(classOutline);
			}
		}
		return true;
	}

	protected void processClassOutline(ClassOutline classOutline) {
		final JDefinedClass theClass = classOutline.implClass;
		@SuppressWarnings("unused")
		final JMethod object$hashCode = generateObject$hashCode(classOutline,
				theClass);
	}

	protected JMethod generateObject$hashCode(final ClassOutline classOutline,
			final JDefinedClass theClass) {

		final JCodeModel codeModel = theClass.owner();
		final JMethod object$hashCode = theClass.method(JMod.PUBLIC,
				codeModel.INT, "hashCode");
		{
			final JBlock body = object$hashCode.body();

			final JExpression currentHashCodeExpression = JExpr
					.lit(getCodeGeneratorFactory().getInitial());

			final JVar currentHashCode = body.decl(codeModel.INT,
					"currentHashCode", currentHashCodeExpression);

			final Boolean superClassImplementsHashCode = StrategyClassUtils
					.superClassNotIgnored(classOutline, ignoring);

			if (superClassImplementsHashCode != null) {
				body.assign(
						currentHashCode,
						currentHashCode.mul(
								JExpr.lit(getCodeGeneratorFactory()
										.getMultiplier())).plus(
								JExpr._super().invoke("hashCode")));
			}

			final FieldOutline[] declaredFields = FieldOutlineUtils.filter(
					classOutline.getDeclaredFields(), getIgnoring());

			if (declaredFields.length > 0) {

				for (final FieldOutline fieldOutline : declaredFields) {
					final FieldAccessorEx fieldAccessor = getFieldAccessorFactory()
							.createFieldAccessor(fieldOutline, JExpr._this());
					if (fieldAccessor.isConstant()) {
						continue;
					}
					final JBlock block = body.block();

					String propertyName = fieldOutline.getPropertyInfo().getName(
							true);
					final JVar value = block.decl(
							fieldAccessor.getType(),
							"the"
									+ propertyName);
					if ("CeOrCf".equalsIgnoreCase(propertyName))
					{
						System.out.println(fieldOutline);
					}

					fieldAccessor.toRawValue(block, value);
					final JType exposedType = fieldAccessor.getType();
					
					final Collection<JType> possibleTypes = FieldUtils.getPossibleTypes(fieldOutline, Aspect.EXPOSED);
					final boolean isAlwaysSet = fieldAccessor.isAlwaysSet();
					final JExpression hasSetValue = fieldAccessor.hasSetValue();
					final HashCodeCodeGenerator codeGenerator = getCodeGeneratorFactory()
							.getCodeGenerator(exposedType);
					codeGenerator.generate(block, currentHashCode, exposedType, possibleTypes,
							value, hasSetValue, isAlwaysSet);
				}
			}
			body._return(currentHashCode);
		}
		return object$hashCode;
	}
}
{'fraction_non_alphanumeric': 0.09137482795534485, 'fraction_numerical': 0.0027527144823367485, 'mean_word_length': 5.024412713035468, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '3114682', 'n_tokens_mistral': 4554, 'n_tokens_neox': 3892, 'n_words': 670}