<commit_before>/*
 * Copyright © 2016 Schlichtherle IT Services
 *
 * 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
 *
 *     https://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 global.namespace.neuron.di.internal;

import global.namespace.neuron.di.java.BreedingException;
import global.namespace.neuron.di.java.DependencyProvider;
import org.objectweb.asm.*;

import java.lang.reflect.Method;
import java.util.List;

import static global.namespace.neuron.di.internal.Reflection.boxed;
import static java.lang.Math.max;
import static org.objectweb.asm.Opcodes.*;
import static org.objectweb.asm.Type.*;

final class ProxyClassVisitor extends ClassVisitor {

    private static final int ACC_ABSTRACT_INTERFACE = ACC_ABSTRACT | ACC_INTERFACE;
    private static final int ACC_ABSTRACT_NATIVE = ACC_ABSTRACT | ACC_NATIVE;
    private static final int ACC_PRIVATE_SYNTHETIC = ACC_PRIVATE | ACC_SYNTHETIC;
    private static final int ACC_FINAL_SUPER_SYNTHETIC = ACC_FINAL | ACC_SUPER | ACC_SYNTHETIC;

    private static final String CONSTRUCTOR_NAME = "<init>";
    private static final String ACCEPTS_NOTHING_AND_RETURNS_VOID_DESC = "()V";
    private static final String OBJECT_DESC = "Ljava/lang/Object;";
    private static final String ACCEPTS_NOTHING_AND_RETURNS_OBJECT_DESC = "()" + OBJECT_DESC;

    static final String PROVIDER = "$provider";

    private static final Type acceptsNothingAndReturnsObjectType = getType(ACCEPTS_NOTHING_AND_RETURNS_OBJECT_DESC);
    private static final String dependencyProviderName = getInternalName(DependencyProvider.class);
    private static final String dependencyProviderDesc = getDescriptor(DependencyProvider.class);
    private static final Handle metaFactoryHandle = new Handle(H_INVOKESTATIC,
            "java/lang/invoke/LambdaMetafactory",
            "metafactory",
            "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;",
            false);

    private final String proxyName, proxyDesc, superName;
    private final String[] interfaces;
    private final List<Method> bindableMethods;

    ProxyClassVisitor(final ClassVisitor cv,
                      final String proxyName,
                      final Class<?> superclass,
                      final Class<?>[] interfaces,
                      final List<Method> bindableMethods) {
        super(ASM5, cv);
        this.proxyName = proxyName;
        this.proxyDesc = "L" + proxyName + ";";
        this.superName = getInternalName(superclass);
        int i = interfaces.length;
        this.interfaces = new String[i];
        while (0 <= --i) {
            this.interfaces[i] = getInternalName(interfaces[i]);
        }
        this.bindableMethods = bindableMethods;
    }

    @Override
    public void visit(final int version,
                      final int access,
                      final String name,
                      final String signature,
                      String superName,
                      String[] interfaces) {
        cv.visit(max(version, V1_8),
                access & ~ACC_ABSTRACT_INTERFACE | ACC_FINAL_SUPER_SYNTHETIC,
                this.proxyName,
                null,
                this.superName,
                this.interfaces);
    }

    @Override
    public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
        return null;
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
        return null;
    }

    @Override
    public void visitEnd() {
        insertConstructor();
        insertMethods();
        cv.visitEnd();
    }

    private void insertConstructor() {
        final MethodVisitor mv = cv.visitMethod(ACC_PRIVATE_SYNTHETIC,
                CONSTRUCTOR_NAME,
                ACCEPTS_NOTHING_AND_RETURNS_VOID_DESC,<commit_msg>Improves proxy class generation.<commit_after>/*
 * Copyright © 2016 Schlichtherle IT Services
 *
 * 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
 *
 *     https://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 global.namespace.neuron.di.internal;

import global.namespace.neuron.di.java.BreedingException;
import global.namespace.neuron.di.java.DependencyProvider;
import org.objectweb.asm.*;

import java.lang.reflect.Method;
import java.util.List;

import static global.namespace.neuron.di.internal.Reflection.boxed;
import static java.lang.Math.max;
import static org.objectweb.asm.Opcodes.*;
import static org.objectweb.asm.Type.*;

final class ProxyClassVisitor extends ClassVisitor {

    private static final int ACC_ABSTRACT_INTERFACE = ACC_ABSTRACT | ACC_INTERFACE;
    private static final int ACC_ABSTRACT_NATIVE = ACC_ABSTRACT | ACC_NATIVE;
    private static final int ACC_PRIVATE_SYNTHETIC = ACC_PRIVATE | ACC_SYNTHETIC;
    private static final int ACC_FINAL_SUPER_SYNTHETIC = ACC_FINAL | ACC_SUPER | ACC_SYNTHETIC;

    private static final String CONSTRUCTOR_NAME = "<init>";
    private static final String ACCEPTS_NOTHING_AND_RETURNS_VOID_DESC = "()V";
    private static final String OBJECT_DESC = "Ljava/lang/Object;";
    private static final String ACCEPTS_NOTHING_AND_RETURNS_OBJECT_DESC = "()" + OBJECT_DESC;

    static final String PROVIDER = "$provider";

    private static final Type acceptsNothingAndReturnsObjectType = getType(ACCEPTS_NOTHING_AND_RETURNS_OBJECT_DESC);
    private static final String dependencyProviderName = getInternalName(DependencyProvider.class);
    private static final String dependencyProviderDesc = getDescriptor(DependencyProvider.class);
    private static final Handle metaFactoryHandle = new Handle(H_INVOKESTATIC,
            "java/lang/invoke/LambdaMetafactory",
            "metafactory",
            "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;",
            false);

    private final String proxyName, proxyDesc, superName;
    private final String[] interfaces;
    private final List<Method> bindableMethods;

    ProxyClassVisitor(final ClassVisitor cv,
                      final String proxyName,
                      final Class<?> superclass,
                      final Class<?>[] interfaces,
                      final List<Method> bindableMethods) {
        super(ASM7, cv);
        this.proxyName = proxyName;
        this.proxyDesc = "L" + proxyName + ";";
        this.superName = getInternalName(superclass);
        int i = interfaces.length;
        this.interfaces = new String[i];
        while (0 <= --i) {
            this.interfaces[i] = getInternalName(interfaces[i]);
        }
        this.bindableMethods = bindableMethods;
    }

    @Override
    public void visit(final int version,
                      final int access,
                      final String name,
                      final String signature,
                      String superName,
                      String[] interfaces) {
        cv.visit(max(version, V1_8),
                access & ~ACC_ABSTRACT_INTERFACE | ACC_FINAL_SUPER_SYNTHETIC,
                this.proxyName,
                null,
                this.superName,
                this.interfaces);
    }

    @Override
    public void visitSource(String source, String debug) {
    }

    @Override
    public ModuleVisitor visitModule(String name, int access, String version) {
        return null;
    }

    @Override
    public void visitNestHost(String nestHost) {
    }

    @Override
    public void visitOuterClass(String owner, String name, String descriptor) {
    }

    @Override
    public void visitAttribute(Attribute attribute) {
    }

    @Override
    public void visitNestMember(String nestMember) {
    }

    @Override
    public void visitInnerClass(String name, String outerName, String innerName, int access) {
    }

    @Override
    public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
        return null;
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
        return null;
    }

    @Override
    public void visitEnd() {
        insertConstructor();
        insertMethods();
        cv.visitEnd();
    }

    private void insertConstructor() {
        final MethodVisitor mv = cv.visitMethod(ACC_PRIVATE_SYNTHETIC,
                CONSTRUCTOR_NAME,
                ACCEPTS_NOTHING_AND_RETURNS_VOID_DESC,
{'fraction_non_alphanumeric': 0.0789281618183714, 'fraction_numerical': 0.0025023459493274947, 'mean_word_length': 2.923108384458078, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '6891984', 'n_tokens_mistral': 2678, 'n_tokens_neox': 2566, 'n_words': 707}