<commit_before>/*  Copyright 2007 <NAME>.
 *
 * 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 org.qi4j.runtime;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.qi4j.api.Composite;
import org.qi4j.api.CompositeBuilder;
import org.qi4j.api.CompositeInstantiationException;
import org.qi4j.api.PropertyValue;
import org.qi4j.api.model.Binding;
import org.qi4j.api.model.CompositeContext;
import org.qi4j.api.model.CompositeModel;
import org.qi4j.api.model.CompositeState;
import org.qi4j.api.model.InjectionKey;
import org.qi4j.api.persistence.Lifecycle;
import org.qi4j.runtime.resolution.MixinResolution;
import org.qi4j.spi.dependency.MixinDependencyInjectionContext;

/**
 *
 */
public class CompositeBuilderImpl<T extends Composite>
    implements CompositeBuilder<T>
{
    private static final Method CREATE_METHOD;

    private Class<T> compositeInterface;
    private CompositeContextImpl<T> context;
    private InstanceFactory fragmentFactory;

    private Map<InjectionKey, Object> adaptContext;
    private Map<InjectionKey, Object> decorateContext;
    private Map<MixinResolution, Map<InjectionKey, Object>> propertyContext;

    static
    {
        try
        {
            CREATE_METHOD = Lifecycle.class.getMethod( "create" );
        }
        catch( NoSuchMethodException e )
        {
            throw new InternalError( "Lifecycle class is corrupt." );
        }
    }

    CompositeBuilderImpl( CompositeContextImpl<T> context, InstanceFactory fragmentFactory )
    {
        this.fragmentFactory = fragmentFactory;
        this.context = context;
        this.compositeInterface = context.getCompositeModel().getCompositeClass();
    }

    public CompositeContext<T> getContext()
    {
        return context;
    }

    public void adapt( Object adaptedObject )
    {
        if( adaptedObject instanceof Binding )
        {
            Binding binding = (Binding) adaptedObject;
            getAdaptContext().put( binding.getKey(), binding.getValue() );
        }
        else
        {
            InjectionKey key = new InjectionKey( adaptedObject.getClass(), null, null );
            getAdaptContext().put( key, adaptedObject );
        }
    }

    public void decorate( Object decoratedObject )
    {
        if( decoratedObject instanceof Binding )
        {
            Binding binding = (Binding) decoratedObject;
            getDecorateContext().put( binding.getKey(), binding.getValue() );
        }
        else
        {
            InjectionKey key = new InjectionKey( decoratedObject.getClass(), null, null );
            getDecorateContext().put( key, decoratedObject );
        }
    }

    public <K, T extends K> void properties( Class<K> mixinType, PropertyValue... properties )
    {
        MixinResolution resolution = context.getCompositeResolution().getMixinForInterface( mixinType );

        Map<MixinResolution, Map<InjectionKey, Object>> context = getPropertyContext();
        Map<InjectionKey, Object> mixinContext = new LinkedHashMap<InjectionKey, Object>();
        for( PropertyValue property : properties )
        {
            String name = property.getName();
            Object value = property.getValue();
            InjectionKey key = new InjectionKey( value.getClass(), name, resolution.getMixinModel().getModelClass() );
            mixinContext.put( key, property );
        }
        context.put( resolution, mixinContext );
    }

    public T properties()
    {
        // Instantiate proxy for given composite interface
        try
        {
            PropertiesInvocationHandler handler = new PropertiesInvocationHandler();
            ClassLoader proxyClassloader = compositeInterface.getClassLoader();
            Class[] interfaces = new Class[]{ compositeInterface };
            return compositeInterface.cast( Proxy.newProxyInstance( proxyClassloader, interfaces, handler ) );
        }
        catch( Exception e )<commit_msg>Fixed bug.<commit_after>/*  Copyright 2007 <NAME>.
 *
 * 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 org.qi4j.runtime;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.qi4j.api.Composite;
import org.qi4j.api.CompositeBuilder;
import org.qi4j.api.CompositeInstantiationException;
import org.qi4j.api.PropertyValue;
import org.qi4j.api.model.Binding;
import org.qi4j.api.model.CompositeContext;
import org.qi4j.api.model.CompositeModel;
import org.qi4j.api.model.CompositeState;
import org.qi4j.api.model.InjectionKey;
import org.qi4j.api.persistence.Lifecycle;
import org.qi4j.runtime.resolution.MixinResolution;
import org.qi4j.spi.dependency.MixinDependencyInjectionContext;

/**
 *
 */
public class CompositeBuilderImpl<T extends Composite>
    implements CompositeBuilder<T>
{
    private static final Method CREATE_METHOD;

    private Class<T> compositeInterface;
    private CompositeContextImpl<T> context;
    private InstanceFactory fragmentFactory;

    private Map<InjectionKey, Object> adaptContext;
    private Map<InjectionKey, Object> decorateContext;
    private Map<MixinResolution, Map<InjectionKey, Object>> propertyContext;

    static
    {
        try
        {
            CREATE_METHOD = Lifecycle.class.getMethod( "create" );
        }
        catch( NoSuchMethodException e )
        {
            throw new InternalError( "Lifecycle class is corrupt." );
        }
    }

    CompositeBuilderImpl( CompositeContextImpl<T> context, InstanceFactory fragmentFactory )
    {
        this.fragmentFactory = fragmentFactory;
        this.context = context;
        this.compositeInterface = context.getCompositeModel().getCompositeClass();
    }

    public CompositeContext<T> getContext()
    {
        return context;
    }

    public void adapt( Object adaptedObject )
    {
        if( adaptedObject instanceof Binding )
        {
            Binding binding = (Binding) adaptedObject;
            getAdaptContext().put( binding.getKey(), binding.getValue() );
        }
        else
        {
            InjectionKey key = new InjectionKey( adaptedObject.getClass(), null, null );
            getAdaptContext().put( key, adaptedObject );
        }
    }

    public void decorate( Object decoratedObject )
    {
        if( decoratedObject instanceof Binding )
        {
            Binding binding = (Binding) decoratedObject;
            getDecorateContext().put( binding.getKey(), binding.getValue() );
        }
        else
        {
            InjectionKey key = new InjectionKey( decoratedObject.getClass(), null, null );
            getDecorateContext().put( key, decoratedObject );
        }
    }

    public <K, T extends K> void properties( Class<K> mixinType, PropertyValue... properties )
    {
        MixinResolution resolution = context.getCompositeResolution().getMixinForInterface( mixinType );

        Map<MixinResolution, Map<InjectionKey, Object>> context = getPropertyContext();
        Map<InjectionKey, Object> mixinContext = new LinkedHashMap<InjectionKey, Object>();
        for( PropertyValue property : properties )
        {
            String name = property.getName();
            Object value = property.getValue();
            InjectionKey key = new InjectionKey( value.getClass(), name, resolution.getMixinModel().getModelClass() );
            mixinContext.put( key, value );
        }
        context.put( resolution, mixinContext );
    }

    public T properties()
    {
        // Instantiate proxy for given composite interface
        try
        {
            PropertiesInvocationHandler handler = new PropertiesInvocationHandler();
            ClassLoader proxyClassloader = compositeInterface.getClassLoader();
            Class[] interfaces = new Class[]{ compositeInterface };
            return compositeInterface.cast( Proxy.newProxyInstance( proxyClassloader, interfaces, handler ) );
        }
        catch( Exception e )
{'fraction_non_alphanumeric': 0.09056175134242049, 'fraction_numerical': 0.004337050805452293, 'mean_word_length': 3.0778947368421052, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '5526938', 'n_tokens_mistral': 2608, 'n_tokens_neox': 2539, 'n_words': 667}