<commit_before>/*
 * Copyright 2015 <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 com.michaelrocks.lightsaber.processor;

import com.michaelrocks.lightsaber.processor.analysis.AnalysisClassFileVisitor;
import com.michaelrocks.lightsaber.processor.descriptors.InjectionTargetDescriptor;
import com.michaelrocks.lightsaber.processor.descriptors.InjectorDescriptor;
import com.michaelrocks.lightsaber.processor.descriptors.MethodDescriptor;
import com.michaelrocks.lightsaber.processor.descriptors.ModuleDescriptor;
import com.michaelrocks.lightsaber.processor.generation.ClassProducer;
import com.michaelrocks.lightsaber.processor.generation.GlobalModuleGenerator;
import com.michaelrocks.lightsaber.processor.generation.InjectorFactoryClassGenerator;
import com.michaelrocks.lightsaber.processor.generation.InjectorsGenerator;
import com.michaelrocks.lightsaber.processor.generation.ProcessorClassProducer;
import com.michaelrocks.lightsaber.processor.generation.ProvidersGenerator;
import com.michaelrocks.lightsaber.processor.graph.CycleSearcher;
import com.michaelrocks.lightsaber.processor.graph.DependencyGraph;
import com.michaelrocks.lightsaber.processor.graph.UnresolvedDependenciesSearcher;
import com.michaelrocks.lightsaber.processor.injection.InjectionClassFileVisitor;
import com.michaelrocks.lightsaber.processor.io.ClassFileReader;
import com.michaelrocks.lightsaber.processor.io.ClassFileWriter;
import org.objectweb.asm.Type;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class ClassProcessor {
    private final ClassFileReader classFileReader;
    private final ClassFileWriter classFileWriter;

    private final ProcessorContext processorContext = new ProcessorContext();

    public ClassProcessor(final ClassFileReader classFileReader, final ClassFileWriter classFileWriter) {
        this.classFileWriter = classFileWriter;
        this.classFileReader = classFileReader;
    }

    public void processClasses() throws IOException {
        performAnalysis();
        composeGlobalModule();
        composeInjectors();
        processorContext.dump();
        validateDependencyGraph();
        generateGlobalModule();
        generateProviders();
        generateInjectorFactory();
        generateInjectors();
        copyAndPatchClasses();
    }

    private void performAnalysis() throws IOException {
        final AnalysisClassFileVisitor analysisVisitor = new AnalysisClassFileVisitor(processorContext);
        classFileReader.accept(analysisVisitor);
        checkErrors();
    }

    private void composeGlobalModule() {
        final ModuleDescriptor.Builder globalModuleBuilder =
                new ModuleDescriptor.Builder(processorContext.getGlobalModuleType());
        for (final InjectionTargetDescriptor providableTarget : processorContext.getProvidableTargets()) {
            final Type providableTargetType = providableTarget.getTargetType();
            final MethodDescriptor providableTargetConstructor = providableTarget.getInjectableConstructor();

            final String providerMethodName = "provide$" + providableTargetType.getInternalName().replace('/', '$');
            final Type[] providerMethodArgumentTypes = providableTargetConstructor.getType().getArgumentTypes();
            final MethodDescriptor providerMethod =
                    MethodDescriptor.forMethod(providerMethodName, providableTargetType, providerMethodArgumentTypes);

            globalModuleBuilder.addProviderMethod(providerMethod, providableTarget.getScope());
        }
        processorContext.setGlobalModule(globalModuleBuilder.build());
    }

    private void composeInjectors() {
        for (final InjectionTargetDescriptor injectableTarget : processorContext.getInjectableTargets()) {
            final Type injectorType =
                    Type.getObjectType(injectableTarget.getTargetType().getInternalName() + "$$Injector");
            final InjectorDescriptor injector = new InjectorDescriptor(injectorType, injectableTarget);
            processorContext.addInjector(injector);
        }
    }

    private void validateDependencyGraph() throws ProcessingException {
        final DependencyGraph dependencyGraph = new DependencyGraph(processorContext);<commit_msg>Generate provider for providable target in target's package<commit_after>/*
 * Copyright 2015 <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 com.michaelrocks.lightsaber.processor;

import com.michaelrocks.lightsaber.processor.analysis.AnalysisClassFileVisitor;
import com.michaelrocks.lightsaber.processor.descriptors.InjectionTargetDescriptor;
import com.michaelrocks.lightsaber.processor.descriptors.InjectorDescriptor;
import com.michaelrocks.lightsaber.processor.descriptors.MethodDescriptor;
import com.michaelrocks.lightsaber.processor.descriptors.ModuleDescriptor;
import com.michaelrocks.lightsaber.processor.descriptors.ProviderDescriptor;
import com.michaelrocks.lightsaber.processor.descriptors.ScopeDescriptor;
import com.michaelrocks.lightsaber.processor.generation.ClassProducer;
import com.michaelrocks.lightsaber.processor.generation.GlobalModuleGenerator;
import com.michaelrocks.lightsaber.processor.generation.InjectorFactoryClassGenerator;
import com.michaelrocks.lightsaber.processor.generation.InjectorsGenerator;
import com.michaelrocks.lightsaber.processor.generation.ProcessorClassProducer;
import com.michaelrocks.lightsaber.processor.generation.ProvidersGenerator;
import com.michaelrocks.lightsaber.processor.graph.CycleSearcher;
import com.michaelrocks.lightsaber.processor.graph.DependencyGraph;
import com.michaelrocks.lightsaber.processor.graph.UnresolvedDependenciesSearcher;
import com.michaelrocks.lightsaber.processor.injection.InjectionClassFileVisitor;
import com.michaelrocks.lightsaber.processor.io.ClassFileReader;
import com.michaelrocks.lightsaber.processor.io.ClassFileWriter;
import org.objectweb.asm.Type;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class ClassProcessor {
    private final ClassFileReader classFileReader;
    private final ClassFileWriter classFileWriter;

    private final ProcessorContext processorContext = new ProcessorContext();

    public ClassProcessor(final ClassFileReader classFileReader, final ClassFileWriter classFileWriter) {
        this.classFileWriter = classFileWriter;
        this.classFileReader = classFileReader;
    }

    public void processClasses() throws IOException {
        performAnalysis();
        composeGlobalModule();
        composeInjectors();
        processorContext.dump();
        validateDependencyGraph();
        generateGlobalModule();
        generateProviders();
        generateInjectorFactory();
        generateInjectors();
        copyAndPatchClasses();
    }

    private void performAnalysis() throws IOException {
        final AnalysisClassFileVisitor analysisVisitor = new AnalysisClassFileVisitor(processorContext);
        classFileReader.accept(analysisVisitor);
        checkErrors();
    }

    private void composeGlobalModule() {
        final ModuleDescriptor.Builder globalModuleBuilder =
                new ModuleDescriptor.Builder(processorContext.getGlobalModuleType());
        for (final InjectionTargetDescriptor providableTarget : processorContext.getProvidableTargets()) {
            final Type providableTargetType = providableTarget.getTargetType();
            final MethodDescriptor providableTargetConstructor = providableTarget.getInjectableConstructor();

            final String providerMethodName = "provide$" + providableTargetType.getInternalName().replace('/', '$');
            final Type[] providerMethodArgumentTypes = providableTargetConstructor.getType().getArgumentTypes();
            final MethodDescriptor providerMethod =
                    MethodDescriptor.forMethod(providerMethodName, providableTargetType, providerMethodArgumentTypes);

            final Type providerType = Type.getObjectType(providableTargetType.getInternalName() + "$$Provider");
            final ScopeDescriptor scope = providableTarget.getScope();
            final Type delegatorType = scope != null ? scope.getProviderType() : null;
            final ProviderDescriptor provider =
                    new ProviderDescriptor(providerType, providerMethod.getReturnType(), providerMethod,
                            processorContext.getGlobalModuleType(), delegatorType);

            globalModuleBuilder.addProvider(provider);
        }
        processorContext.setGlobalModule(globalModuleBuilder.build());
    }

    private void composeInjectors() {
        for (final InjectionTargetDescriptor injectableTarget : processorContext.getInjectableTargets()) {
            final Type injectorType =
                    Type.getObjectType(injectableTarget.getTargetType().getInternalName() + "$$Injector");
            final InjectorDescriptor injector = new InjectorDescriptor(injectorType, injectableTarget);
            processorContext.addInjector(injector);
        }
    }

    private void validateDependencyGraph() throws ProcessingException {
        final DependencyGraph dependencyGraph = new DependencyGraph(processorContext);
{'fraction_non_alphanumeric': 0.07518212724623603, 'fraction_numerical': 0.0015541525012141815, 'mean_word_length': 4.464968152866242, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4267553', 'n_tokens_mistral': 2532, 'n_tokens_neox': 2442, 'n_words': 538}