<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 org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Type;

import java.util.ArrayList;
import java.util.List;

import static org.objectweb.asm.Opcodes.*;

public class ModuleVisitor extends ClassVisitor {
    private final List<String> providerMethods = new ArrayList<>();

    public ModuleVisitor(final ClassVisitor classVisitor) {
        super(ASM5, classVisitor);
    }

    @Override
    public void visit(final int version, final int access, final String name, final String signature,
            final String superName, final String[] interfaces) {
        final String[] newInterfaces = new String[interfaces.length + 1];
        System.arraycopy(interfaces, 0, newInterfaces, 0, interfaces.length);
        newInterfaces[interfaces.length] = InternalNames.CLASS_INTERNAL_MODULE;
        super.visit(version, access, name, signature, superName, newInterfaces);
    }

    @Override
    public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature,
            final String[] exceptions) {
        final String methodName = name;
        final MethodVisitor methodVisitor = super.visitMethod(access, name, desc, signature, exceptions);
        return new MethodVisitor(ASM5, methodVisitor) {
            @Override
            public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) {
                if (Descriptors.CLASS_PROVIDES.equals(desc)) {
                    providerMethods.add(methodName);
                }

                return super.visitAnnotation(desc, visible);
            }
        };
    }

    @Override
    public void visitEnd() {
        generateConfigureInjectorMethod();
        super.visitEnd();
    }

    private void generateConfigureInjectorMethod() {
        System.out.println("Generating configureInjector");
        final MethodVisitor methodVisitor = cv.visitMethod(ACC_PUBLIC | ACC_SYNTHETIC,
                InternalNames.METHOD_CONFIGURE_INJECTOR,
                Descriptors.METHOD_CONFIGURE_INJECTOR,
                null,
                null);
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(ALOAD, 1);
        methodVisitor.visitLdcInsn(Type.getType("Lcom/michaelrocks/lightsaber/sample/Wookiee;"));
        methodVisitor.visitTypeInsn(NEW, "com/michaelrocks/lightsaber/sample/LightsaberModule$1");
        methodVisitor.visitInsn(DUP);
        methodVisitor.visitVarInsn(ALOAD, 0);
        methodVisitor.visitMethodInsn(INVOKESPECIAL,
                "com/michaelrocks/lightsaber/sample/LightsaberModule$1",
                "<init>",
                "(Lcom/michaelrocks/lightsaber/sample/LightsaberModule;)V",
                false);
        methodVisitor.visitMethodInsn(INVOKEVIRTUAL,
                InternalNames.CLASS_LIGHTSABER_INJECTOR,
                InternalNames.METHOD_REGISTER_PROVIDER,
                Descriptors.METHOD_REGISTER_PROVIDER,
                false);
        methodVisitor.visitInsn(RETURN);
        methodVisitor.visitMaxs(0, 0);
        methodVisitor.visitEnd();
    }
}<commit_msg>Generate more realistic configureInjector method for modules<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 org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Type;

import java.util.ArrayList;
import java.util.List;

import static org.objectweb.asm.Opcodes.*;

public class ModuleVisitor extends ClassVisitor {
    private String className;
    private final List<ProviderMethodDescriptor> providerMethods = new ArrayList<>();

    public ModuleVisitor(final ClassVisitor classVisitor) {
        super(ASM5, classVisitor);
    }

    @Override
    public void visit(final int version, final int access, final String name, final String signature,
            final String superName, final String[] interfaces) {
        className = name;
        final String[] newInterfaces = new String[interfaces.length + 1];
        System.arraycopy(interfaces, 0, newInterfaces, 0, interfaces.length);
        newInterfaces[interfaces.length] = InternalNames.CLASS_INTERNAL_MODULE;
        super.visit(version, access, name, signature, superName, newInterfaces);
    }

    @Override
    public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature,
            final String[] exceptions) {
        final String methodName = name;
        final String methodDesc = desc;
        final MethodVisitor methodVisitor = super.visitMethod(access, name, desc, signature, exceptions);
        return new MethodVisitor(ASM5, methodVisitor) {
            @Override
            public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) {
                if (Descriptors.CLASS_PROVIDES.equals(desc)) {
                    final Type methodType = Type.getMethodType(methodDesc);
                    final ProviderMethodDescriptor descriptor = new ProviderMethodDescriptor(methodName, methodType);
                    providerMethods.add(descriptor);
                }

                return super.visitAnnotation(desc, visible);
            }
        };
    }

    @Override
    public void visitEnd() {
        generateConfigureInjectorMethod();
        super.visitEnd();
    }

    private void generateConfigureInjectorMethod() {
        System.out.println("Generating configureInjector");
        final MethodVisitor methodVisitor = cv.visitMethod(ACC_PUBLIC | ACC_SYNTHETIC,
                InternalNames.METHOD_CONFIGURE_INJECTOR,
                Descriptors.METHOD_CONFIGURE_INJECTOR,
                null,
                null);
        methodVisitor.visitCode();
        for (int i = 0; i < providerMethods.size(); ++i) {
            final ProviderMethodDescriptor descriptor = providerMethods.get(i);
            generateRegisterProviderInvocation(methodVisitor, descriptor, i);
        }
        methodVisitor.visitInsn(RETURN);
        methodVisitor.visitMaxs(0, 0);
        methodVisitor.visitEnd();
    }

    private void generateRegisterProviderInvocation(final MethodVisitor methodVisitor,
            final ProviderMethodDescriptor descriptor, final int invocationIndex) {
        System.out.println("Generating invocation for method " + descriptor.name);
        final String providerClass = className + "$$Provider$$" + (invocationIndex + 1);
        methodVisitor.visitVarInsn(ALOAD, 1);
        methodVisitor.visitLdcInsn(descriptor.type.getReturnType());
        methodVisitor.visitTypeInsn(NEW, providerClass);
        methodVisitor.visitInsn(DUP);
        methodVisitor.visitVarInsn(ALOAD, 0);
        methodVisitor.visitMethodInsn(INVOKESPECIAL,
                providerClass,
                "<init>",
                Type.getMethodDescriptor(Type.VOID_TYPE, Type.getObjectType(className)),
                false);
        methodVisitor.visitMethodInsn(INVOKEVIRTUAL,
                InternalNames.CLASS_LIGHTSABER_INJECTOR,
                InternalNames.METHOD_REGISTER_PROVIDER,
                Descriptors.METHOD_REGISTER_PROVIDER,
                false);
    }

    private class ProviderMethodDescriptor {
        public final String name;
        public final Type type;

        public ProviderMethodDescriptor(final String name, final Type type) {
            this.name = name;
            this.type = type;
        }
    }
}
{'fraction_non_alphanumeric': 0.0806507045480582, 'fraction_numerical': 0.0043533050750372325, 'mean_word_length': 2.8869100623330364, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 11, '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': '3143998', 'n_tokens_mistral': 2282, 'n_tokens_neox': 2192, 'n_words': 581}