<commit_before>package de.japkit.model

import de.japkit.activeannotations.FieldsFromInterface
import de.japkit.services.ExtensionRegistry
import de.japkit.services.TypesExtensions
import java.util.List
import javax.lang.model.element.Name
import javax.lang.model.element.Parameterizable
import javax.lang.model.element.TypeElement
import javax.lang.model.element.TypeParameterElement
import javax.lang.model.type.DeclaredType
import javax.lang.model.type.TypeMirror
import javax.lang.model.type.TypeVariable

@FieldsFromInterface
abstract class GenParameterizable extends GenElement implements Parameterizable {
	
	List<TypeParameterElement> typeParameters = newArrayList()

	def dispatch TypeMirror resolveTypeVariables(DeclaredType type){
		if(type.typeArguments.nullOrEmpty){
			type
		} else {
			getDeclaredType(type, type.typeArguments.map[resolveTypeVariables])
		}
	}
	
	def DeclaredType getDeclaredType(DeclaredType prototype, Iterable<TypeMirror> resolvedTypeArgs) {
		if(prototype instanceof GenDeclaredType){
			new GenDeclaredType(prototype.asElement as TypeElement)[
				typeArguments = resolvedTypeArgs.toList
			]
		} else {
			ExtensionRegistry.get(TypesExtensions).getDeclaredType(prototype.asElement as TypeElement, resolvedTypeArgs)		
		}
	}
	
	def dispatch TypeMirror resolveTypeVariables(TypeMirror type){
		type
	}
	
	def dispatch TypeMirror resolveTypeVariables(Void type){
		null
	}
	
	def dispatch TypeMirror resolveTypeVariables(TypeVariable tv){
		
		var resolvedTypeVar = resolveTypeVariable(tv)	
		if(resolvedTypeVar === null){		
			if(enclosingParameterizable !== null){
				//If there is an enclosing type, look there...
				resolvedTypeVar = enclosingParameterizable.resolveTypeVariables(tv)
			} else {
				//Otherwise, leave the type variable unresolved for now, since the element might be added to its enclosing type
				//at a later point in time. 
				resolvedTypeVar = tv
			}
		}
		
		//messager.printMessage(Kind.WARNING, '''Resolve type variable in context «this.simpleName»: «tv» - Resolved: «resolvedTypeVar» ''')
		
		resolvedTypeVar	
	}	
	
	/**
	 * resolve the type variable "locally" , for example by using type arguments of the method / class
	 * 
	 */
	def protected abstract TypeMirror resolveTypeVariable(TypeVariable tv);
	
	

	/**
	 * Gets or creates a type parameter with the same name and bounds as the given type parameter (typically from a superclass)
	 */
	def TypeParameterElement getOrCreateTypeParameter(TypeParameterElement otherTypeParam) {
		typeParameters.filter[simpleName.contentEquals(otherTypeParam.simpleName)].head ?: {
			new GenTypeParameter(otherTypeParam, this)
		}
	}
	
	//The following constructors shall be generated, but xtend has some bug and claims there are no super-constructors...
	new(String simpleName){
		super(simpleName)
	}
	
	new(Name  simpleName){
		super(simpleName)
	}
	
}<commit_msg>#24: Japkit-examples do not compile with Java9<commit_after>package de.japkit.model

import de.japkit.activeannotations.FieldsFromInterface
import de.japkit.services.ExtensionRegistry
import de.japkit.services.TypesExtensions
import java.util.List
import javax.lang.model.element.Name
import javax.lang.model.element.Parameterizable
import javax.lang.model.element.TypeElement
import javax.lang.model.element.TypeParameterElement
import javax.lang.model.type.DeclaredType
import javax.lang.model.type.TypeMirror
import javax.lang.model.type.TypeVariable
import javax.lang.model.util.SimpleTypeVisitor8
import javax.lang.model.type.ErrorType

@FieldsFromInterface
abstract class GenParameterizable extends GenElement implements Parameterizable {

	List<TypeParameterElement> typeParameters = newArrayList()

	def DeclaredType getDeclaredType(DeclaredType prototype, Iterable<TypeMirror> resolvedTypeArgs) {
		if (prototype instanceof GenDeclaredType) {
			new GenDeclaredType(prototype.asElement as TypeElement) [
				typeArguments = resolvedTypeArgs.toList
			]
		} else {
			ExtensionRegistry.get(TypesExtensions).getDeclaredType(prototype.asElement as TypeElement, resolvedTypeArgs)
		}
	}

	def TypeMirror resolveTypeVariables(TypeMirror type) {
		type?.accept(new SimpleTypeVisitor8<TypeMirror, Void>() {
			override defaultAction(TypeMirror type, Void p) {
				type
			}

			override TypeMirror visitDeclared(DeclaredType type, Void p) {
				if (type.typeArguments.nullOrEmpty) {
					type
				} else {
					getDeclaredType(type, type.typeArguments.map[resolveTypeVariables])
				}
			}
			
			override TypeMirror visitError(ErrorType type, Void p) {
				//Really? Is just 1:1 to the dispatch behavior before...
				visitDeclared(type, p);
			}

			override TypeMirror visitTypeVariable(TypeVariable tv, Void p) {

				var resolvedTypeVar = resolveTypeVariable(tv)
				if (resolvedTypeVar === null) {
					if (enclosingParameterizable !== null) {
						// If there is an enclosing type, look there...
						resolvedTypeVar = enclosingParameterizable.resolveTypeVariables(tv)
					} else {
						// Otherwise, leave the type variable unresolved for now, since the element might be added to its enclosing type
						// at a later point in time. 
						resolvedTypeVar = tv
					}
				}

				// messager.printMessage(Kind.WARNING, '''Resolve type variable in context «this.simpleName»: «tv» - Resolved: «resolvedTypeVar» ''')
				resolvedTypeVar
			}

		}, null);
	}

	/**
	 * resolve the type variable "locally" , for example by using type arguments of the method / class
	 * 
	 */
	def protected abstract TypeMirror resolveTypeVariable(TypeVariable tv);

	/**
	 * Gets or creates a type parameter with the same name and bounds as the given type parameter (typically from a superclass)
	 */
	def TypeParameterElement getOrCreateTypeParameter(TypeParameterElement otherTypeParam) {
		typeParameters.filter[simpleName.contentEquals(otherTypeParam.simpleName)].head ?: {
			new GenTypeParameter(otherTypeParam, this)
		}
	}

	// The following constructors shall be generated, but xtend has some bug and claims there are no super-constructors...
	new(String simpleName) {
		super(simpleName)
	}

	new(Name simpleName) {
		super(simpleName)
	}

}
{'fraction_non_alphanumeric': 0.08248938255472069, 'fraction_numerical': 0.0011434171839268213, 'mean_word_length': 4.541176470588235, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '5047367', 'n_tokens_mistral': 1940, 'n_tokens_neox': 1673, 'n_words': 467}