<commit_before>/**
 * Copyright (C) 2010 hprange <<EMAIL>>
 *
 * 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.
 */
package com.webobjects.foundation;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.AbstractModule;
import com.google.inject.Binder;
import com.google.inject.Binding;
import com.google.inject.BindingAnnotation;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.Scopes;
import com.google.inject.util.Providers;
import com.webobjects.appserver.WOComponent;
import com.webobjects.appserver.WODirectAction;
import com.webobjects.appserver.WOSession;
import com.webobjects.eocontrol.EOEnterpriseObject;
import com.woinject.InjectableApplication;
import com.woinject.WOInjectException;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @since 1.0
 */
class InstantiationInterceptor {
    /**
     * @author <a href="mailto:<EMAIL>"><NAME></a>
     * @since 1.0
     */
    @BindingAnnotation
    @Target({ FIELD, PARAMETER, METHOD })
    @Retention(RUNTIME)
    private @interface WOInjectBinding {
    }

    private static final Logger LOGGER = LoggerFactory.getLogger(InstantiationInterceptor.class);

    private static <T> Constructor<T> findConstructor(final Class<T> type, final Class<?>[] parameterTypes, boolean shouldThrow, boolean shouldLog) {
	try {
	    return type.getConstructor(parameterTypes);
	} catch (Exception exception) {
	    return handleException(type, exception, shouldThrow, shouldLog);
	}
    }

    private static <T, N> N handleException(Class<T> type, Exception exception, boolean shouldThrow, boolean shouldLog) {
	if (shouldLog) {
	    LOGGER.error("The instantiation of " + type.getName() + " class has failed.", exception);
	}

	if (!shouldThrow) {
	    return null;
	}

	if (exception instanceof WOInjectException) {
	    throw (WOInjectException) exception;
	}

	throw new WOInjectException("The instantiation of " + type.getName() + " class has failed.", exception);
    }

    private static Injector injector() {
	return InjectableApplication.application().injector();
    }

    public static <T> T instantiateObject(final Class<T> type, final Class<?>[] parameterTypes, final Object[] parameters, boolean shouldThrow, boolean shouldLog) {
	Constructor<T> constructor = findConstructor(type, parameterTypes, shouldThrow, shouldLog);

	if (constructor == null) {
	    return null;
	}

	return instantiateObject(constructor, type, parameters, shouldThrow, shouldLog);
    }

    public static <T> T instantiateObject(final Constructor<T> constructor, final Class<T> type, final Object[] parameters, boolean shouldThrow, boolean shouldLog) {
	Injector injector = injector();

	if (injector == null) {
	    return instantiateObjectByReflection(type, constructor, parameters, shouldThrow, shouldLog);
	}
<commit_msg>Remove unnecessary code<commit_after>/**
 * Copyright (C) 2010 hprange <<EMAIL>>
 *
 * 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.
 */
package com.webobjects.foundation;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.AbstractModule;
import com.google.inject.Binder;
import com.google.inject.Binding;
import com.google.inject.BindingAnnotation;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.Scopes;
import com.google.inject.util.Providers;
import com.webobjects.appserver.WOComponent;
import com.webobjects.appserver.WODirectAction;
import com.webobjects.appserver.WOSession;
import com.webobjects.eocontrol.EOEnterpriseObject;
import com.woinject.InjectableApplication;
import com.woinject.WOInjectException;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @since 1.0
 */
class InstantiationInterceptor {
    /**
     * @author <a href="mailto:<EMAIL>"><NAME></a>
     * @since 1.0
     */
    @BindingAnnotation
    @Target({ FIELD, PARAMETER, METHOD })
    @Retention(RUNTIME)
    private @interface WOInjectBinding {
    }

    private static final Logger LOGGER = LoggerFactory.getLogger(InstantiationInterceptor.class);

    private static <T> Constructor<T> findConstructor(final Class<T> type, final Class<?>[] parameterTypes, boolean shouldThrow, boolean shouldLog) {
	try {
	    return type.getConstructor(parameterTypes);
	} catch (Exception exception) {
	    return handleException(type, exception, shouldThrow, shouldLog);
	}
    }

    private static <T, N> N handleException(Class<T> type, Exception exception, boolean shouldThrow, boolean shouldLog) {
	if (shouldLog) {
	    LOGGER.error("The instantiation of " + type.getName() + " class has failed.", exception);
	}

	if (!shouldThrow) {
	    return null;
	}

	throw new WOInjectException("The instantiation of " + type.getName() + " class has failed.", exception);
    }

    private static Injector injector() {
	return InjectableApplication.application().injector();
    }

    public static <T> T instantiateObject(final Class<T> type, final Class<?>[] parameterTypes, final Object[] parameters, boolean shouldThrow, boolean shouldLog) {
	Constructor<T> constructor = findConstructor(type, parameterTypes, shouldThrow, shouldLog);

	if (constructor == null) {
	    return null;
	}

	return instantiateObject(constructor, type, parameters, shouldThrow, shouldLog);
    }

    public static <T> T instantiateObject(final Constructor<T> constructor, final Class<T> type, final Object[] parameters, boolean shouldThrow, boolean shouldLog) {
	Injector injector = injector();

	if (injector == null) {
	    return instantiateObjectByReflection(type, constructor, parameters, shouldThrow, shouldLog);
	}

{'fraction_non_alphanumeric': 0.10656068974808029, 'fraction_numerical': 0.0037720598140913377, 'mean_word_length': 4.836477987421383, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1116917', 'n_tokens_mistral': 2128, 'n_tokens_neox': 2088, 'n_words': 605}