<commit_before>/*
 * Copyright 2018 <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 io.michaelrocks.lightsaber.processor.commons

import io.michaelrocks.grip.mirrors.Type
import io.michaelrocks.grip.mirrors.isArray
import io.michaelrocks.grip.mirrors.toAsmType
import io.michaelrocks.lightsaber.processor.descriptors.FieldDescriptor
import io.michaelrocks.lightsaber.processor.descriptors.MethodDescriptor
import io.michaelrocks.lightsaber.processor.descriptors.descriptor
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.Label
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes.ACONST_NULL
import org.objectweb.asm.Opcodes.ASM5
import org.objectweb.asm.Opcodes.DOUBLE
import org.objectweb.asm.Opcodes.FLOAT
import org.objectweb.asm.Opcodes.INTEGER
import org.objectweb.asm.Opcodes.INVOKEINTERFACE
import org.objectweb.asm.Opcodes.INVOKESPECIAL
import org.objectweb.asm.Opcodes.INVOKESTATIC
import org.objectweb.asm.Opcodes.INVOKEVIRTUAL
import org.objectweb.asm.Opcodes.LONG
import org.objectweb.asm.Opcodes.TOP
import java.util.ArrayList

class GeneratorAdapter(
    methodVisitor: MethodVisitor,
    access: Int,
    name: String,
    desc: String
) : org.objectweb.asm.commons.GeneratorAdapter(ASM5, methodVisitor, access, name, desc) {

  companion object {
    private fun visitMethod(
        classVisitor: ClassVisitor,
        access: Int,
        method: MethodDescriptor,
        signature: String?,
        exceptions: Array<Type.Object>?
    ): MethodVisitor =
        classVisitor.visitMethod(access, method.name, method.descriptor, signature, exceptions?.toInternalNames())

    private fun Array<Type.Object>.toInternalNames(): Array<String>? =
        this.mapToArray { it.internalName }
  }

  constructor(
      methodVisitor: MethodVisitor,
      access: Int,
      method: MethodDescriptor
  ) : this(methodVisitor, access, method.name, method.descriptor)

  constructor(
      classVisitor: ClassVisitor,
      access: Int,
      method: MethodDescriptor
  ) : this(visitMethod(classVisitor, access, method, null, null), access, method)

  constructor(
      classVisitor: ClassVisitor,
      access: Int,
      method: MethodDescriptor,
      signature: String?,
      exceptions: Array<Type.Object>?
  ) : this(visitMethod(classVisitor, access, method, signature, exceptions), access, method)

  fun newArray(type: Type, size: Int) {
    push(size)
    super.newArray(type.toAsmType())
  }

  fun newInstance(type: Type) {
    newInstance(type.toAsmType())
  }

  fun invokeVirtual(owner: Type, method: MethodDescriptor) {
    invoke(INVOKEVIRTUAL, owner, method, false)
  }

  fun invokeConstructor(type: Type, method: MethodDescriptor) {
    invoke(INVOKESPECIAL, type, method, false)
  }

  fun invokeStatic(owner: Type, method: MethodDescriptor) {
    invoke(INVOKESTATIC, owner, method, false)
  }

  fun invokeInterface(owner: Type, method: MethodDescriptor) {
    invoke(INVOKEINTERFACE, owner, method, true)
  }

  fun invokePrivate(owner: Type, method: MethodDescriptor) {
    invoke(INVOKESPECIAL, owner, method, false)
  }

  private fun invoke(opcode: Int, type: Type, method: MethodDescriptor, ownerIsInterface: Boolean) {
    val owner = if (type.isArray) type.descriptor else type.internalName<commit_msg>Support super method invocation in GeneratorAdapter<commit_after>/*
 * Copyright 2018 <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 io.michaelrocks.lightsaber.processor.commons

import io.michaelrocks.grip.mirrors.Type
import io.michaelrocks.grip.mirrors.isArray
import io.michaelrocks.grip.mirrors.toAsmType
import io.michaelrocks.lightsaber.processor.descriptors.FieldDescriptor
import io.michaelrocks.lightsaber.processor.descriptors.MethodDescriptor
import io.michaelrocks.lightsaber.processor.descriptors.descriptor
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.Label
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes.ACONST_NULL
import org.objectweb.asm.Opcodes.ASM5
import org.objectweb.asm.Opcodes.DOUBLE
import org.objectweb.asm.Opcodes.FLOAT
import org.objectweb.asm.Opcodes.INTEGER
import org.objectweb.asm.Opcodes.INVOKEINTERFACE
import org.objectweb.asm.Opcodes.INVOKESPECIAL
import org.objectweb.asm.Opcodes.INVOKESTATIC
import org.objectweb.asm.Opcodes.INVOKEVIRTUAL
import org.objectweb.asm.Opcodes.LONG
import org.objectweb.asm.Opcodes.TOP
import java.util.ArrayList

class GeneratorAdapter(
    methodVisitor: MethodVisitor,
    access: Int,
    name: String,
    desc: String
) : org.objectweb.asm.commons.GeneratorAdapter(ASM5, methodVisitor, access, name, desc) {

  companion object {
    private fun visitMethod(
        classVisitor: ClassVisitor,
        access: Int,
        method: MethodDescriptor,
        signature: String?,
        exceptions: Array<Type.Object>?
    ): MethodVisitor =
        classVisitor.visitMethod(access, method.name, method.descriptor, signature, exceptions?.toInternalNames())

    private fun Array<Type.Object>.toInternalNames(): Array<String>? =
        this.mapToArray { it.internalName }
  }

  constructor(
      methodVisitor: MethodVisitor,
      access: Int,
      method: MethodDescriptor
  ) : this(methodVisitor, access, method.name, method.descriptor)

  constructor(
      classVisitor: ClassVisitor,
      access: Int,
      method: MethodDescriptor
  ) : this(visitMethod(classVisitor, access, method, null, null), access, method)

  constructor(
      classVisitor: ClassVisitor,
      access: Int,
      method: MethodDescriptor,
      signature: String?,
      exceptions: Array<Type.Object>?
  ) : this(visitMethod(classVisitor, access, method, signature, exceptions), access, method)

  fun newArray(type: Type, size: Int) {
    push(size)
    super.newArray(type.toAsmType())
  }

  fun newInstance(type: Type) {
    newInstance(type.toAsmType())
  }

  fun invokeVirtual(owner: Type, method: MethodDescriptor) {
    invoke(INVOKEVIRTUAL, owner, method, false)
  }

  fun invokeConstructor(type: Type, method: MethodDescriptor) {
    invoke(INVOKESPECIAL, type, method, false)
  }

  fun invokeStatic(owner: Type, method: MethodDescriptor) {
    invoke(INVOKESTATIC, owner, method, false)
  }

  fun invokeInterface(owner: Type, method: MethodDescriptor) {
    invoke(INVOKEINTERFACE, owner, method, true)
  }

  fun invokePrivate(owner: Type, method: MethodDescriptor) {
    invoke(INVOKESPECIAL, owner, method, false)
  }

  fun invokeSuper(owner: Type, method: MethodDescriptor) {
    invoke(INVOKESPECIAL, owner, method, false)
  }

  private fun invoke(opcode: Int, type: Type, method: MethodDescriptor, ownerIsInterface: Boolean) {
    val owner = if (type.isArray) type.descriptor else type.internalName
{'fraction_non_alphanumeric': 0.09474768280123584, 'fraction_numerical': 0.0025746652935118436, 'mean_word_length': 4.321232876712329, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3877534', 'n_tokens_mistral': 2316, 'n_tokens_neox': 2283, 'n_words': 581}