<commit_before>package tools.vitruv.dsls.commonalities.generator

import edu.kit.ipd.sdq.activextendannotations.Utility
import java.util.Collection
import org.eclipse.emf.ecore.EAttribute
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.EReference
import org.eclipse.emf.ecore.EStructuralFeature
import org.eclipse.xtext.common.types.TypesFactory
import org.eclipse.xtext.xbase.XExpression
import org.eclipse.xtext.xbase.XbaseFactory
import tools.vitruv.dsls.reactions.builder.TypeProvider
import tools.vitruv.extensions.dslruntime.commonalities.EmfAccess

import static tools.vitruv.dsls.commonalities.generator.XbaseCollectionHelper.*

import static extension tools.vitruv.dsls.commonalities.generator.JvmTypeProviderHelper.*
import static extension tools.vitruv.dsls.commonalities.generator.ReactionsHelper.*
import static extension tools.vitruv.dsls.commonalities.generator.XbaseHelper.*

@Utility
package class EmfAccessExpressions {

	def private static eGetFeatureValue(extension TypeProvider typeProvider, XExpression object, String featureName) {
		XbaseFactory.eINSTANCE.createXMemberFeatureCall => [
			memberCallTarget = object
			feature = typeProvider.findMethod(EObject, 'eGet', 1, EStructuralFeature)
			explicitOperationCall = true
			memberCallArguments += getEFeature(typeProvider, object.copy, featureName)
		]
	}

	def private static eSetFeatureValue(extension TypeProvider typeProvider,  XExpression object, String featureName,
		XExpression newValue) {
		XbaseFactory.eINSTANCE.createXMemberFeatureCall => [
			memberCallTarget = object
			feature = typeProvider.findMethod(EObject, 'eSet')
			explicitOperationCall = true
			memberCallArguments += getEFeature(typeProvider, object.copy, featureName)
			memberCallArguments += newValue
		]
	}

	def private static eGetListFeatureValue(extension TypeProvider typeProvider, XExpression object,
		String featureName) {
		XbaseFactory.eINSTANCE.createXCastedExpression => [
			type = TypesFactory.eINSTANCE.createJvmParameterizedTypeReference => [
				type = typeProvider.findType(Collection)
				arguments += TypesFactory.eINSTANCE.createJvmParameterizedTypeReference => [
					type = typeProvider.findType(Object)
				]
			]
			target = eGetFeatureValue(typeProvider, object, featureName)
		]
	}

	def private static eAddListFeatureValue(extension TypeProvider typeProvider, XExpression object,
		String featureName, XExpression newValue) {
		val getList = eGetListFeatureValue(typeProvider, object, featureName)
		addToCollection(typeProvider, getList, newValue)
	}

	def private static eRemoveListFeatureValue(extension TypeProvider typeProvider, XExpression object,
		String featureName, XExpression newValue) {
		val getList = eGetListFeatureValue(typeProvider, object, featureName)
		removeFromCollection(typeProvider, getList, newValue)
	}

	def private static eSetListFeatureValue(extension TypeProvider typeProvider, XExpression object,
		String featureName, XExpression newValues) {
		val getList = eGetListFeatureValue(typeProvider, object, featureName)
		XbaseFactory.eINSTANCE.createXBlockExpression => [
			expressions += expressions(
				clearCollection(typeProvider, getList),
				addAllToCollection(typeProvider, getList.copy, newValues)
			)
		]
	}

	def package static getFeatureValue(extension TypeProvider typeProvider, XExpression object,
		EStructuralFeature eFeature) {
		try {
			// try to guess the accessor:
			return getEFeatureValue(typeProvider, object, eFeature)
		} catch (NoSuchJvmElementException e) {
			// if that fails, use EMF's reflection:
			return eGetFeatureValue(typeProvider, object, eFeature.name)
		}
	}

	def package static setFeatureValue(extension TypeProvider typeProvider, XExpression object,
		EStructuralFeature eFeature, XExpression newValue) {
		try {
			// try to guess the accessor:
			val containingInstanceClassName = eFeature.EContainingClass.javaClassName
			if (containingInstanceClassName === null) {
				throw new RuntimeException('''Containing instance class name is null!''')
			}
			return XbaseFactory.eINSTANCE.createXAssignment => [
				assignable = object
				feature = typeProvider.findMethod(containingInstanceClassName, 'set' + eFeature.name.toFirstUpper)
				value = newValue
			]
		} catch (NoSuchJvmElementException e) {
			// if that fails, use EMF's reflection:
			return eSetFeatureValue(typeProvider, object, eFeature.name, newValue)
		}
	}

	def package static getListFeatureValue(extension TypeProvider typeProvider, XExpression object,
		EStructuralFeature eFeature) {
		try {
			// try to guess the accessor:
			return getEFeatureValue(typeProvider, object, eFeature)
		} catch (NoSuchJvmElementException e) {
			// if that fails, use EMF's reflection:
			return eGetListFeatureValue(typeProvider, object, eFeature.name)
		}
	}

	def package static addListFeatureValue(extension TypeProvider typeProvider, XExpression object,
		EStructuralFeature eFeature, XExpression newValue) {
		try {
			// try to guess the accessor:
			val getList = getEFeatureValue(typeProvider, object, eFeature)
			return addToCollection(typeProvider, getList, newValue)
		} catch (NoSuchJvmElementException e) {
			// if that fails, use EMF's reflection:
			return eAddListFeatureValue(typeProvider, object, eFeature.name, newValue)
		}
	}

	def package static removeListFeatureValue(extension TypeProvider typeProvider, XExpression object,
		EStructuralFeature eFeature, XExpression newValue) {
		try {
			// try to guess the accessor:
			val getList = getEFeatureValue(typeProvider, object, eFeature)
			return removeFromCollection(typeProvider, getList, newValue)
		} catch (NoSuchJvmElementException e) {
			// if that fails, use EMF's reflection:
			return eRemoveListFeatureValue(typeProvider, object, eFeature.name, newValue)
		}
	}

	def package static setListFeatureValue(extension TypeProvider typeProvider, XExpression object,
		EStructuralFeature eFeature, XExpression newValues) {
		try {
			// try to guess the accessor:
			val getList = getEFeatureValue(typeProvider, object, eFeature)
			XbaseFactory.eINSTANCE.createXBlockExpression => [
				expressions += expressions(
					clearCollection(typeProvider, getList),
					addAllToCollection(typeProvider, getList.copy, newValues)
				)
			]
		} catch (NoSuchJvmElementException e) {
			// if that fails, use EMF's reflection:
			return eSetListFeatureValue(typeProvider, object, eFeature.name, newValues)
		}
	}

	def package static insertFeatureValue(extension TypeProvider typeProvider, XExpression object,
		EStructuralFeature eFeature, XExpression newValue) {
		if (eFeature.many) {
			addListFeatureValue(typeProvider, object, eFeature, newValue)
		} else {
			setFeatureValue(typeProvider, object, eFeature, newValue)
		}
	}

	// throws NoSuchJvmElementException on failure
	def private static getEFeatureValue(extension TypeProvider typeProvider, XExpression object,
		EStructuralFeature eFeature) {
		val containingInstanceClassName = eFeature.EContainingClass.javaClassName
		if (containingInstanceClassName === null) {
			throw new RuntimeException('''Containing instance class name is null!''')
		}
		XbaseFactory.eINSTANCE.createXMemberFeatureCall => [
			memberCallTarget = object
			feature = typeProvider.findMethod(containingInstanceClassName, 'get' + eFeature.name.toFirstUpper, 0)
		]
	}

	def package static getEFeature(extension TypeProvider typeProvider, XExpression object, String featureName) {
		// TODO try to guess feature literal or package accessor?
		XbaseFactory.eINSTANCE.createXMemberFeatureCall => [
			memberCallTarget = XbaseFactory.eINSTANCE.createXMemberFeatureCall => [
				memberCallTarget = object
				feature = typeProvider.findMethod(EObject, 'eClass')
				concreteSyntaxFeatureName
			]
			feature = typeProvider.findMethod(EClass, 'getEStructuralFeature', String)
			explicitOperationCall = true
			memberCallArguments += XbaseFactory.eINSTANCE.createXStringLiteral => [
				value = featureName
			]
		]
	}

	def static getEPackage(extension TypeProvider typeProvider, EPackage ePackage) {
		val emfAccessType = typeProvider.findDeclaredType(EmfAccess)
		return emfAccessType.memberFeatureCall => [
			staticWithDeclaringType = true
			feature = emfAccessType.findMethod("getEPackage", String)
			memberCallArguments += stringLiteral(ePackage.nsURI)
		]
	}<commit_msg>Minor: Removed some code line that serves no purpose.<commit_after>package tools.vitruv.dsls.commonalities.generator

import edu.kit.ipd.sdq.activextendannotations.Utility
import java.util.Collection
import org.eclipse.emf.ecore.EAttribute
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.EReference
import org.eclipse.emf.ecore.EStructuralFeature
import org.eclipse.xtext.common.types.TypesFactory
import org.eclipse.xtext.xbase.XExpression
import org.eclipse.xtext.xbase.XbaseFactory
import tools.vitruv.dsls.reactions.builder.TypeProvider
import tools.vitruv.extensions.dslruntime.commonalities.EmfAccess

import static tools.vitruv.dsls.commonalities.generator.XbaseCollectionHelper.*

import static extension tools.vitruv.dsls.commonalities.generator.JvmTypeProviderHelper.*
import static extension tools.vitruv.dsls.commonalities.generator.ReactionsHelper.*
import static extension tools.vitruv.dsls.commonalities.generator.XbaseHelper.*

@Utility
package class EmfAccessExpressions {

	def private static eGetFeatureValue(extension TypeProvider typeProvider, XExpression object, String featureName) {
		XbaseFactory.eINSTANCE.createXMemberFeatureCall => [
			memberCallTarget = object
			feature = typeProvider.findMethod(EObject, 'eGet', 1, EStructuralFeature)
			explicitOperationCall = true
			memberCallArguments += getEFeature(typeProvider, object.copy, featureName)
		]
	}

	def private static eSetFeatureValue(extension TypeProvider typeProvider,  XExpression object, String featureName,
		XExpression newValue) {
		XbaseFactory.eINSTANCE.createXMemberFeatureCall => [
			memberCallTarget = object
			feature = typeProvider.findMethod(EObject, 'eSet')
			explicitOperationCall = true
			memberCallArguments += getEFeature(typeProvider, object.copy, featureName)
			memberCallArguments += newValue
		]
	}

	def private static eGetListFeatureValue(extension TypeProvider typeProvider, XExpression object,
		String featureName) {
		XbaseFactory.eINSTANCE.createXCastedExpression => [
			type = TypesFactory.eINSTANCE.createJvmParameterizedTypeReference => [
				type = typeProvider.findType(Collection)
				arguments += TypesFactory.eINSTANCE.createJvmParameterizedTypeReference => [
					type = typeProvider.findType(Object)
				]
			]
			target = eGetFeatureValue(typeProvider, object, featureName)
		]
	}

	def private static eAddListFeatureValue(extension TypeProvider typeProvider, XExpression object,
		String featureName, XExpression newValue) {
		val getList = eGetListFeatureValue(typeProvider, object, featureName)
		addToCollection(typeProvider, getList, newValue)
	}

	def private static eRemoveListFeatureValue(extension TypeProvider typeProvider, XExpression object,
		String featureName, XExpression newValue) {
		val getList = eGetListFeatureValue(typeProvider, object, featureName)
		removeFromCollection(typeProvider, getList, newValue)
	}

	def private static eSetListFeatureValue(extension TypeProvider typeProvider, XExpression object,
		String featureName, XExpression newValues) {
		val getList = eGetListFeatureValue(typeProvider, object, featureName)
		XbaseFactory.eINSTANCE.createXBlockExpression => [
			expressions += expressions(
				clearCollection(typeProvider, getList),
				addAllToCollection(typeProvider, getList.copy, newValues)
			)
		]
	}

	def package static getFeatureValue(extension TypeProvider typeProvider, XExpression object,
		EStructuralFeature eFeature) {
		try {
			// try to guess the accessor:
			return getEFeatureValue(typeProvider, object, eFeature)
		} catch (NoSuchJvmElementException e) {
			// if that fails, use EMF's reflection:
			return eGetFeatureValue(typeProvider, object, eFeature.name)
		}
	}

	def package static setFeatureValue(extension TypeProvider typeProvider, XExpression object,
		EStructuralFeature eFeature, XExpression newValue) {
		try {
			// try to guess the accessor:
			val containingInstanceClassName = eFeature.EContainingClass.javaClassName
			if (containingInstanceClassName === null) {
				throw new RuntimeException('''Containing instance class name is null!''')
			}
			return XbaseFactory.eINSTANCE.createXAssignment => [
				assignable = object
				feature = typeProvider.findMethod(containingInstanceClassName, 'set' + eFeature.name.toFirstUpper)
				value = newValue
			]
		} catch (NoSuchJvmElementException e) {
			// if that fails, use EMF's reflection:
			return eSetFeatureValue(typeProvider, object, eFeature.name, newValue)
		}
	}

	def package static getListFeatureValue(extension TypeProvider typeProvider, XExpression object,
		EStructuralFeature eFeature) {
		try {
			// try to guess the accessor:
			return getEFeatureValue(typeProvider, object, eFeature)
		} catch (NoSuchJvmElementException e) {
			// if that fails, use EMF's reflection:
			return eGetListFeatureValue(typeProvider, object, eFeature.name)
		}
	}

	def package static addListFeatureValue(extension TypeProvider typeProvider, XExpression object,
		EStructuralFeature eFeature, XExpression newValue) {
		try {
			// try to guess the accessor:
			val getList = getEFeatureValue(typeProvider, object, eFeature)
			return addToCollection(typeProvider, getList, newValue)
		} catch (NoSuchJvmElementException e) {
			// if that fails, use EMF's reflection:
			return eAddListFeatureValue(typeProvider, object, eFeature.name, newValue)
		}
	}

	def package static removeListFeatureValue(extension TypeProvider typeProvider, XExpression object,
		EStructuralFeature eFeature, XExpression newValue) {
		try {
			// try to guess the accessor:
			val getList = getEFeatureValue(typeProvider, object, eFeature)
			return removeFromCollection(typeProvider, getList, newValue)
		} catch (NoSuchJvmElementException e) {
			// if that fails, use EMF's reflection:
			return eRemoveListFeatureValue(typeProvider, object, eFeature.name, newValue)
		}
	}

	def package static setListFeatureValue(extension TypeProvider typeProvider, XExpression object,
		EStructuralFeature eFeature, XExpression newValues) {
		try {
			// try to guess the accessor:
			val getList = getEFeatureValue(typeProvider, object, eFeature)
			XbaseFactory.eINSTANCE.createXBlockExpression => [
				expressions += expressions(
					clearCollection(typeProvider, getList),
					addAllToCollection(typeProvider, getList.copy, newValues)
				)
			]
		} catch (NoSuchJvmElementException e) {
			// if that fails, use EMF's reflection:
			return eSetListFeatureValue(typeProvider, object, eFeature.name, newValues)
		}
	}

	def package static insertFeatureValue(extension TypeProvider typeProvider, XExpression object,
		EStructuralFeature eFeature, XExpression newValue) {
		if (eFeature.many) {
			addListFeatureValue(typeProvider, object, eFeature, newValue)
		} else {
			setFeatureValue(typeProvider, object, eFeature, newValue)
		}
	}

	// throws NoSuchJvmElementException on failure
	def private static getEFeatureValue(extension TypeProvider typeProvider, XExpression object,
		EStructuralFeature eFeature) {
		val containingInstanceClassName = eFeature.EContainingClass.javaClassName
		if (containingInstanceClassName === null) {
			throw new RuntimeException('''Containing instance class name is null!''')
		}
		XbaseFactory.eINSTANCE.createXMemberFeatureCall => [
			memberCallTarget = object
			feature = typeProvider.findMethod(containingInstanceClassName, 'get' + eFeature.name.toFirstUpper, 0)
		]
	}

	def package static getEFeature(extension TypeProvider typeProvider, XExpression object, String featureName) {
		// TODO try to guess feature literal or package accessor?
		XbaseFactory.eINSTANCE.createXMemberFeatureCall => [
			memberCallTarget = XbaseFactory.eINSTANCE.createXMemberFeatureCall => [
				memberCallTarget = object
				feature = typeProvider.findMethod(EObject, 'eClass')
			]
			feature = typeProvider.findMethod(EClass, 'getEStructuralFeature', String)
			explicitOperationCall = true
			memberCallArguments += XbaseFactory.eINSTANCE.createXStringLiteral => [
				value = featureName
			]
		]
	}

	def static getEPackage(extension TypeProvider typeProvider, EPackage ePackage) {
		val emfAccessType = typeProvider.findDeclaredType(EmfAccess)
		return emfAccessType.memberFeatureCall => [
			staticWithDeclaringType = true
			feature = emfAccessType.findMethod("getEPackage", String)
			memberCallArguments += stringLiteral(ePackage.nsURI)
		]
	}
{'fraction_non_alphanumeric': 0.07674003569303986, 'fraction_numerical': 0.0002379535990481856, 'mean_word_length': 5.438529299119112, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '1470537', 'n_tokens_mistral': 5254, 'n_tokens_neox': 4701, 'n_words': 1107}