<commit_before>package org.xtendroid.annotations

import org.eclipse.xtend.lib.macro.AbstractFieldProcessor
import org.eclipse.xtend.lib.macro.Active
import org.eclipse.xtend.lib.macro.TransformationContext
import org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration
import org.eclipse.xtend.lib.macro.declaration.TypeReference
import org.eclipse.xtend.lib.macro.declaration.Visibility
import org.xtendroid.utils.Utils

import static extension org.xtendroid.utils.Utils.*

@Active(typeof(AndroidPreferenceProcessor))
annotation AndroidPreference {
}

class AndroidPreferenceProcessor extends AbstractFieldProcessor {

   override doTransform(MutableFieldDeclaration field, extension TransformationContext context) {
      if (field.initializer == null)
         field.addError("A Preference field must have an initializer.")

      // add synthetic init-method
      var getter = if(field.type.simpleName.equalsIgnoreCase("Boolean")) "is" else "get"
      field.declaringType.addMethod(getter + field.simpleName.upperCaseFirst) [
         visibility = Visibility::PUBLIC
         returnType = field.type
         val methodName = "get" + returnType.prefMethodName
         // reassign the initializer expression to be the init method’s body
         // this automatically removes the expression as the field’s initializer
         body = [
            '''
               return pref.«methodName»("«Utils.toResourceName(field.simpleName)»", «field.simpleName»);
            '''
         ]
      ]

      // add a getter method which lazily initializes the field
      field.declaringType.addMethod("set" + field.simpleName.upperCaseFirst) [
         visibility = Visibility::PUBLIC
         returnType = context.primitiveBoolean
         addParameter("value", field.type)
         val methodName = "put" + field.type.prefMethodName<commit_msg>Added code to determine if BasePreference is extended.<commit_after>package org.xtendroid.annotations

import org.eclipse.xtend.lib.macro.AbstractFieldProcessor
import org.eclipse.xtend.lib.macro.Active
import org.eclipse.xtend.lib.macro.TransformationContext
import org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration
import org.eclipse.xtend.lib.macro.declaration.TypeReference
import org.eclipse.xtend.lib.macro.declaration.Visibility
import org.xtendroid.utils.BasePreferences
import org.xtendroid.utils.Utils

import static extension org.xtendroid.utils.Utils.*

@Active(typeof(AndroidPreferenceProcessor))
annotation AndroidPreference {
}

class AndroidPreferenceProcessor extends AbstractFieldProcessor {

   override doTransform(MutableFieldDeclaration field, extension TransformationContext context) {
      if (field.initializer == null)
      {
         field.addError("A Preference field must have an initializer.")      	
      }
      
      val clazz = field.declaringType
      val isTypeOfBasePreferences = BasePreferences.newTypeReference.isAssignableFrom(clazz.newTypeReference)
		if (!isTypeOfBasePreferences)
		{
			clazz.addError('This class must extend org.xtendroid.utils.BasePreferences')
		}

      // add synthetic init-method
      var getter = if(field.type.simpleName.equalsIgnoreCase("Boolean")) "is" else "get"
      field.declaringType.addMethod(getter + field.simpleName.upperCaseFirst) [
         visibility = Visibility::PUBLIC
         returnType = field.type
         val methodName = "get" + returnType.prefMethodName
         // reassign the initializer expression to be the init method’s body
         // this automatically removes the expression as the field’s initializer
         body = [
            '''
               return pref.«methodName»("«Utils.toResourceName(field.simpleName)»", «field.simpleName»);
            '''
         ]
      ]

      // add a getter method which lazily initializes the field
      field.declaringType.addMethod("set" + field.simpleName.upperCaseFirst) [
         visibility = Visibility::PUBLIC
         returnType = context.primitiveBoolean
         addParameter("value", field.type)
         val methodName = "put" + field.type.prefMethodName
{'fraction_non_alphanumeric': 0.08830724070450098, 'fraction_numerical': 0.0, 'mean_word_length': 3.533259423503326, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6986570', 'n_tokens_mistral': 1105, 'n_tokens_neox': 1073, 'n_words': 249}