<commit_before>package com.github.ruediste1.i18n.message;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;

import javax.inject.Inject;

import com.github.ruediste1.i18n.lString.PatternString;
import com.github.ruediste1.i18n.lString.PatternStringResolver;
import com.github.ruediste1.i18n.lString.StringUtil;
import com.github.ruediste1.i18n.lString.TranslatedStringResolver;
import com.github.ruediste1.i18n.lString.TranslatedString;
import com.google.common.base.CaseFormat;

public class TMessageUtil {

    @Inject
    PatternStringResolver pStringResolver;

    @Inject
    TranslatedStringResolver tStringResovler;

    @SuppressWarnings("unchecked")
    public <T> T getMessageInterfaceInstance(Class<T> clazz) {
        return (T) Proxy.newProxyInstance(clazz.getClassLoader(),
                new Class<?>[] { clazz }, this::invoke);
    }

    private Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {

        // calculate fallback
        String fallback;
        TMessage tMessage = method.getAnnotation(TMessage.class);
        if (tMessage != null) {
            fallback = tMessage.value();
        } else {
            fallback = StringUtil
                    .insertSpacesIntoCamelCaseString(CaseFormat.LOWER_CAMEL.to(
                            CaseFormat.UPPER_CAMEL, method.getName()))
                    + ".";
        }

        // build string
        TranslatedString tString = new TranslatedString(tStringResovler, method
                .getDeclaringClass().getName() + "." + method.getName(),
                fallback);

        // check return type
        if (TranslatedString.class.equals(method.getReturnType())) {
            if (args != null && args.length > 0) {
                throw new RuntimeException(
                        "The return type of "
                                + method
                                + " is TranslatedString but there are parameters. Change the return type to PatternString instead");
            }
            return tString;
        }

        if (PatternString.class.equals(method.getReturnType())) {
            // build parameter map
            HashMap<String, Object> parameters = new HashMap<>();
            for (int i = 0; i < method.getParameters().length; i++) {
                parameters.put(method.getParameters()[i].getName(), args[i]);
            }

            return new PatternString(pStringResolver, tString, parameters);
        }

        throw new RuntimeException("The return type of " + method
                + " must be TString or PString");
    }

}<commit_msg>better Message return type logic<commit_after>package com.github.ruediste1.i18n.message;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collections;
import java.util.HashMap;

import javax.inject.Inject;
import javax.inject.Singleton;

import com.github.ruediste1.i18n.lString.PatternString;
import com.github.ruediste1.i18n.lString.PatternStringResolver;
import com.github.ruediste1.i18n.lString.StringUtil;
import com.github.ruediste1.i18n.lString.TranslatedString;
import com.github.ruediste1.i18n.lString.TranslatedStringResolver;
import com.google.common.base.CaseFormat;

@Singleton
public class TMessageUtil {

    @Inject
    PatternStringResolver pStringResolver;

    @Inject
    TranslatedStringResolver tStringResovler;

    @SuppressWarnings("unchecked")
    public <T> T getMessageInterfaceInstance(Class<T> clazz) {
        return (T) Proxy.newProxyInstance(clazz.getClassLoader(),
                new Class<?>[] { clazz }, this::invoke);
    }

    private Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {

        // calculate fallback
        String fallback;
        TMessage tMessage = method.getAnnotation(TMessage.class);
        if (tMessage != null) {
            fallback = tMessage.value();
        } else {
            fallback = StringUtil
                    .insertSpacesIntoCamelCaseString(CaseFormat.LOWER_CAMEL.to(
                            CaseFormat.UPPER_CAMEL, method.getName()))
                    + ".";
        }

        // build string
        TranslatedString tString = new TranslatedString(tStringResovler, method
                .getDeclaringClass().getName() + "." + method.getName(),
                fallback);

        if (args == null || args.length == 0) {
            // no arguments
            if (method.getReturnType().isAssignableFrom(TranslatedString.class))
                return tString;
            if (method.getReturnType().isAssignableFrom(PatternString.class))
                return new PatternString(pStringResolver, tString,
                        Collections.emptyMap());

            throw new RuntimeException(
                    "The return type of "
                            + method
                            + " must be assigneable from LString, TranslatedString or PatternString");
        } else {
            // there are arguments
            if (method.getReturnType().isAssignableFrom(PatternString.class)) {
                // build parameter map
                HashMap<String, Object> parameters = new HashMap<>();
                for (int i = 0; i < method.getParameters().length; i++) {
                    parameters
                            .put(method.getParameters()[i].getName(), args[i]);
                }
                return new PatternString(pStringResolver, tString, parameters);
            }
            throw new RuntimeException("The return type of " + method
                    + " must be assigneable from LString or PatternString");
        }
    }

}
{'fraction_non_alphanumeric': 0.09770215751622523, 'fraction_numerical': 0.007016312927556569, 'mean_word_length': 2.1502762430939226, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5564553', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1482, 'n_words': 319}