<commit_before>/*
 * A High-Level Framework for Application Configuration
 *
 * Copyright 2007 <NAME> / Learning Objects, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * $Id$
 */
package com.haulmont.cuba.core.config.type;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import com.haulmont.cuba.core.config.ConfigUtil;
import com.haulmont.cuba.core.config.ConfigPersister;
import com.haulmont.cuba.core.config.SourceType;
import com.haulmont.cuba.core.entity.Entity;

/**
 * A class that sets a configuration type by converting the type to a
 * string and then setting the configuration property.
 *
 * @author <NAME>
 * @version 0.1, 2007/04/15
 */
public abstract class TypeStringify
{
    /**
     * Convert an instance of the configuration type into a string.
     *
     * @param value The configuration value.
     * @return A string representation.
     */
    public abstract String stringify(Object value);

    /**
     * Get a TypeStringify instance appropriate for the parameter type of the
     * specified configuration interface method.
     *
     * @param configInterface The configuration interface.
     * @param method          The method.
     * @return An appropriate TypeStringify.
     * @throws IllegalArgumentException If the type is not supported.
     */
    public static TypeStringify getInstance(Class<?> configInterface, Method method) {
        Class<?> methodType = ConfigUtil.getMethodType(method);
        try {
            String methodName;
            Stringify stringify = ConfigUtil.getAnnotation(configInterface, method, Stringify.class, true);
            if (stringify != null) {
                if ("".equals(stringify.method())) {
                    return stringify.stringify().newInstance();
                } else {
                    methodName = stringify.method();
                }
            } else {

                if (methodType.isPrimitive()) {
                    return new PrimitiveTypeStringify();
                } else if (methodType.isEnum()) { // enum
                    methodName = "name";
                } else if (Entity.class.isAssignableFrom(method.getParameterTypes()[0])) {
                    return new EntityStringify();
                } else if (Class.class.equals(methodType)) { // Class
                    methodName = "getName";
                } else { // all else
                    methodName = "toString";
                }
            }
            Method stringifyMethod = methodType.getMethod(methodName);
            if (Modifier.isStatic(stringifyMethod.getModifiers()) ||
                    !Modifier.isPublic(stringifyMethod.getModifiers()) ||
                    Void.TYPE.equals(stringifyMethod.getReturnType()) ||<commit_msg>Refs #578: Использование Config-интерфейсов для получения некоторых экземпляров сущностей<commit_after>/*
 * A High-Level Framework for Application Configuration
 *
 * Copyright 2007 <NAME> / Learning Objects, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * $Id$
 */
package com.haulmont.cuba.core.config.type;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import com.haulmont.cuba.core.config.ConfigUtil;
import com.haulmont.cuba.core.config.ConfigPersister;
import com.haulmont.cuba.core.config.SourceType;
import com.haulmont.cuba.core.entity.Entity;

/**
 * A class that sets a configuration type by converting the type to a
 * string and then setting the configuration property.
 *
 * @author <NAME>
 * @version 0.1, 2007/04/15
 */
public abstract class TypeStringify
{
    /**
     * Convert an instance of the configuration type into a string.
     *
     * @param value The configuration value.
     * @return A string representation.
     */
    public abstract String stringify(Object value);

    /**
     * Get a TypeStringify instance appropriate for the parameter type of the
     * specified configuration interface method.
     *
     * @param configInterface The configuration interface.
     * @param method          The method.
     * @return An appropriate TypeStringify.
     * @throws IllegalArgumentException If the type is not supported.
     */
    public static TypeStringify getInstance(Class<?> configInterface, Method method) {
        Class<?> methodType = ConfigUtil.getMethodType(method);
        try {
            String methodName;
            Stringify stringify = ConfigUtil.getAnnotation(configInterface, method, Stringify.class, true);
            if (stringify != null) {
                if ("".equals(stringify.method())) {
                    return stringify.stringify().newInstance();
                } else {
                    methodName = stringify.method();
                }
            } else {

                if (methodType.isPrimitive()) {
                    return new PrimitiveTypeStringify();
                } else if (methodType.isEnum()) { // enum
                    methodName = "name";
                } else if ((method.getParameterTypes().length > 0)) {
                    if (Entity.class.isAssignableFrom(method.getParameterTypes()[0]))
                        return new EntityStringify();
                    else throw new Exception("Invalid stringify method :" + method);
                } else if (Class.class.equals(methodType)) { // Class
                    methodName = "getName";
                } else { // all else
                    methodName = "toString";
                }
            }
            Method stringifyMethod = methodType.getMethod(methodName);
            if (Modifier.isStatic(stringifyMethod.getModifiers()) ||
                    !Modifier.isPublic(stringifyMethod.getModifiers()) ||
                    Void.TYPE.equals(stringifyMethod.getReturnType()) ||
{'fraction_non_alphanumeric': 0.09280023605783418, 'fraction_numerical': 0.006196518146946002, 'mean_word_length': 2.350963914977756, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2771204', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1657, 'n_words': 538}