<commit_before>/* 
 * Copyright 2015 <NAME> (http://www.igormaznitsa.com).
 *
 * 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.igormaznitsa.jjjvm.impl.jse;

import com.igormaznitsa.jjjvm.model.JJJVMClass;
import com.igormaznitsa.jjjvm.model.JJJVMObject;
import com.igormaznitsa.jjjvm.model.JJJVMProvider;
import com.igormaznitsa.jjjvm.model.JJJVMInnerClassRecord;
import com.igormaznitsa.jjjvm.impl.JJJVMClassImpl;
import com.igormaznitsa.jjjvm.impl.JJJVMImplUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.reflect.*;
import java.util.*;

/**
 * Implementation of provider optimized for Java SE 1.5+.
 *
 * @see sun.misc.Unsafe
 */
public class JSEProviderImpl implements JJJVMProvider {

  /**
   * Class loader which loads and provides class byte-code
   */
  public interface ClassDataLoader {

    /**
     * Load class byte-code.
     *
     * @param jvmFormattedClassName the JVM formatted class name, must not be
     * null.
     * @return byte-code of the class, or null if class not found
     * @throws IOException it must be throws for transport error
     */
    byte[] loadClassBody(String jvmFormattedClassName) throws IOException;
  }

  protected static final sun.misc.Unsafe UNSAFE = getUnsafe();

  private static sun.misc.Unsafe getUnsafe() {
    try {
      final Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
      field.setAccessible(true);
      return (sun.misc.Unsafe) field.get(null);
    }
    catch (Exception ex) {
      throw new Error("Can't get access to sun.misc.Unsafe", ex);
    }
  }

  protected final Map<String, Object> classCache = new HashMap<String, Object>();
  protected final Map<String, Class[]> parsedArgsCache = new HashMap<String, Class[]>();
  protected final Map<String, Map<String, Boolean>> cachedCast = new HashMap<String, Map<String, Boolean>>();

  protected final ClassDataLoader classDataLoader;

  public JSEProviderImpl(final ClassDataLoader classLoader) {
    this.classDataLoader = classLoader;
  }

  protected JJJVMClass loadClassFromLoader(final String jvmFormattedClassName) throws Throwable {
    if (this.classDataLoader == null) {
      throw new NullPointerException("Class loader is not defined");
    }
    final byte[] classBody = this.classDataLoader.loadClassBody(jvmFormattedClassName);
    if (classBody == null) {
      throw new ClassNotFoundException("Can't find body for class '" + jvmFormattedClassName + '\'');
    }
    return new JJJVMClassImpl(new ByteArrayInputStream(classBody), this);
  }

  public JJJVMClass resolveInnerClass(final JJJVMClass caller, final JJJVMInnerClassRecord innerClassRecord) throws Throwable {
    final String outerClassName = innerClassRecord.getOuterClassInfo() == null ? null : innerClassRecord.getOuterClassInfo().getClassName();
    final String innerClassName = innerClassRecord.getInnerClassInfo().getClassName();

    JJJVMClass result;<commit_msg>added default constructor into JSEProviderImpl<commit_after>/* 
 * Copyright 2015 <NAME> (http://www.igormaznitsa.com).
 *
 * 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.igormaznitsa.jjjvm.impl.jse;

import com.igormaznitsa.jjjvm.model.JJJVMClass;
import com.igormaznitsa.jjjvm.model.JJJVMObject;
import com.igormaznitsa.jjjvm.model.JJJVMProvider;
import com.igormaznitsa.jjjvm.model.JJJVMInnerClassRecord;
import com.igormaznitsa.jjjvm.impl.JJJVMClassImpl;
import com.igormaznitsa.jjjvm.impl.JJJVMImplUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.reflect.*;
import java.util.*;

/**
 * Implementation of provider optimized for Java SE 1.5+.
 *
 * @see sun.misc.Unsafe
 */
public class JSEProviderImpl implements JJJVMProvider {

  /**
   * Class loader which loads and provides class byte-code
   */
  public interface ClassDataLoader {

    /**
     * Load class byte-code.
     *
     * @param jvmFormattedClassName the JVM formatted class name, must not be
     * null.
     * @return byte-code of the class, or null if class not found
     * @throws IOException it must be throws for transport error
     */
    byte[] loadClassBody(String jvmFormattedClassName) throws IOException;
  }

  protected static final sun.misc.Unsafe UNSAFE = getUnsafe();

  private static sun.misc.Unsafe getUnsafe() {
    try {
      final Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
      field.setAccessible(true);
      return (sun.misc.Unsafe) field.get(null);
    }
    catch (Exception ex) {
      throw new Error("Can't get access to sun.misc.Unsafe", ex);
    }
  }

  protected final Map<String, Object> classCache = new HashMap<String, Object>();
  protected final Map<String, Class[]> parsedArgsCache = new HashMap<String, Class[]>();
  protected final Map<String, Map<String, Boolean>> cachedCast = new HashMap<String, Map<String, Boolean>>();

  protected final ClassDataLoader classDataLoader;

  public JSEProviderImpl(){
    this.classDataLoader = new ClassDataLoader() {
      public byte[] loadClassBody(String jvmFormattedClassName) throws IOException {
        return null;
      }
    };
  }
  
  public JSEProviderImpl(final ClassDataLoader classLoader) {
    if (classLoader == null) throw new NullPointerException("Loader is null");
    this.classDataLoader = classLoader;
  }

  protected JJJVMClass loadClassFromLoader(final String jvmFormattedClassName) throws Throwable {
    if (this.classDataLoader == null) {
      throw new NullPointerException("Class loader is not defined");
    }
    final byte[] classBody = this.classDataLoader.loadClassBody(jvmFormattedClassName);
    if (classBody == null) {
      throw new ClassNotFoundException("Can't find body for class '" + jvmFormattedClassName + '\'');
    }
    return new JJJVMClassImpl(new ByteArrayInputStream(classBody), this);
  }

  public JJJVMClass resolveInnerClass(final JJJVMClass caller, final JJJVMInnerClassRecord innerClassRecord) throws Throwable {
    final String outerClassName = innerClassRecord.getOuterClassInfo() == null ? null : innerClassRecord.getOuterClassInfo().getClassName();
    final String innerClassName = innerClassRecord.getInnerClassInfo().getClassName();

    JJJVMClass result;
{'fraction_non_alphanumeric': 0.09417784538791316, 'fraction_numerical': 0.0027658691743880515, 'mean_word_length': 4.16940671908506, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '7153560', 'n_tokens_mistral': 2148, 'n_tokens_neox': 2062, 'n_words': 616}