<commit_before>/*
 * Copyright (c) 2006-2014 <NAME>
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.internal.mockups;

import java.lang.reflect.*;
import java.lang.reflect.Type;
import java.util.*;

import mockit.*;
import mockit.external.asm4.*;
import mockit.internal.*;
import mockit.internal.startup.*;
import mockit.internal.state.*;
import mockit.internal.util.*;
import static mockit.external.asm4.ClassReader.*;

import org.jetbrains.annotations.*;

public final class MockClassSetup
{
   @NotNull final Class<?> realClass;
   @Nullable private ClassReader rcReader;
   @NotNull private final MockMethods mockMethods;
   @NotNull final MockUp<?> mockUp;
   private final boolean forStartupMock;

   public MockClassSetup(
      @NotNull Class<?> realClass, @Nullable Type mockedType, @NotNull MockUp<?> mockUp, @Nullable byte[] realClassCode)
   {
      this.realClass = realClass;
      mockMethods = new MockMethods(realClass, mockedType);
      this.mockUp = mockUp;
      forStartupMock = Startup.initializing;
      rcReader = realClassCode == null ? null : new ClassReader(realClassCode);

      Class<?> mockUpClass = mockUp.getClass();
      new MockMethodCollector(mockMethods).collectMockMethods(mockUpClass);

      mockMethods.registerMockStates(mockUp, forStartupMock);

      if (forStartupMock) {
         TestRun.getMockClasses().addMock(mockMethods.getMockClassInternalName(), mockUp);
      }
      else {
         TestRun.getMockClasses().addMock(mockUp);
      }
   }

   public void redefineMethodsInGeneratedClass()
   {
      byte[] modifiedClassFile = modifyRealClass(realClass);
      validateThatAllMockMethodsWereApplied();

      if (modifiedClassFile != null) {
         applyClassModifications(realClass, modifiedClassFile);
      }
   }

   @NotNull
   public Set<Class<?>> redefineMethods()
   {
      Set<Class<?>> redefinedClasses = redefineMethodsInClassHierarchy();
      validateThatAllMockMethodsWereApplied();
      return redefinedClasses;
   }

   @NotNull
   private Set<Class<?>> redefineMethodsInClassHierarchy()
   {
      Set<Class<?>> redefinedClasses = new HashSet<Class<?>>();
      @Nullable Class<?> classToModify = realClass;

      while (classToModify != null && mockMethods.hasUnusedMocks()) {
         byte[] modifiedClassFile = modifyRealClass(classToModify);

         if (modifiedClassFile != null) {
            applyClassModifications(classToModify, modifiedClassFile);
            redefinedClasses.add(classToModify);
         }

         Class<?> superClass = classToModify.getSuperclass();
         classToModify = superClass == Object.class || superClass == Proxy.class ? null : superClass;
         rcReader = null;
      }

      return redefinedClasses;
   }

   @Nullable
   private byte[] modifyRealClass(@NotNull Class<?> classToModify)
   {
      if (rcReader == null) {
         rcReader = createClassReaderForRealClass(classToModify);
      }

      MockupsModifier modifier = new MockupsModifier(rcReader, classToModify, mockUp, mockMethods);
      rcReader.accept(modifier, SKIP_FRAMES);

      return modifier.wasModified() ? modifier.toByteArray() : null;
   }

   @NotNull
   BaseClassModifier createClassModifier(@NotNull ClassReader cr)
   {
      return new MockupsModifier(cr, realClass, mockUp, mockMethods);
   }

   @NotNull
   private static ClassReader createClassReaderForRealClass(@NotNull Class<?> classToModify)
   {
      if (classToModify.isInterface() || classToModify.isArray()) {
         throw new IllegalArgumentException("Not a modifiable class: " + classToModify.getName());
      }
<commit_msg>Excluded System class from mocking when running on J9 JRE, since it somehow gets the main thread stuck.<commit_after>/*
 * Copyright (c) 2006-2014 <NAME>
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.internal.mockups;

import java.lang.reflect.*;
import java.lang.reflect.Type;
import java.util.*;

import mockit.*;
import mockit.external.asm4.*;
import mockit.internal.*;
import mockit.internal.startup.*;
import mockit.internal.state.*;
import mockit.internal.util.*;
import static mockit.external.asm4.ClassReader.*;
import static mockit.internal.util.Utilities.*;

import org.jetbrains.annotations.*;

public final class MockClassSetup
{
   @NotNull final Class<?> realClass;
   @Nullable private ClassReader rcReader;
   @NotNull private final MockMethods mockMethods;
   @NotNull final MockUp<?> mockUp;
   private final boolean forStartupMock;

   public MockClassSetup(
      @NotNull Class<?> realClass, @Nullable Type mockedType, @NotNull MockUp<?> mockUp, @Nullable byte[] realClassCode)
   {
      this.realClass = realClass;
      mockMethods = new MockMethods(realClass, mockedType);
      this.mockUp = mockUp;
      forStartupMock = Startup.initializing;
      rcReader = realClassCode == null ? null : new ClassReader(realClassCode);

      Class<?> mockUpClass = mockUp.getClass();
      new MockMethodCollector(mockMethods).collectMockMethods(mockUpClass);

      mockMethods.registerMockStates(mockUp, forStartupMock);

      if (forStartupMock) {
         TestRun.getMockClasses().addMock(mockMethods.getMockClassInternalName(), mockUp);
      }
      else {
         TestRun.getMockClasses().addMock(mockUp);
      }
   }

   public void redefineMethodsInGeneratedClass()
   {
      byte[] modifiedClassFile = modifyRealClass(realClass);
      validateThatAllMockMethodsWereApplied();

      if (modifiedClassFile != null) {
         applyClassModifications(realClass, modifiedClassFile);
      }
   }

   @NotNull
   public Set<Class<?>> redefineMethods()
   {
      Set<Class<?>> redefinedClasses = redefineMethodsInClassHierarchy();
      validateThatAllMockMethodsWereApplied();
      return redefinedClasses;
   }

   @NotNull
   private Set<Class<?>> redefineMethodsInClassHierarchy()
   {
      Set<Class<?>> redefinedClasses = new HashSet<Class<?>>();
      @Nullable Class<?> classToModify = realClass;

      while (classToModify != null && mockMethods.hasUnusedMocks()) {
         byte[] modifiedClassFile = modifyRealClass(classToModify);

         if (modifiedClassFile != null) {
            applyClassModifications(classToModify, modifiedClassFile);
            redefinedClasses.add(classToModify);
         }

         Class<?> superClass = classToModify.getSuperclass();
         classToModify = superClass == Object.class || superClass == Proxy.class ? null : superClass;
         rcReader = null;
      }

      return redefinedClasses;
   }

   @Nullable
   private byte[] modifyRealClass(@NotNull Class<?> classToModify)
   {
      if (rcReader == null) {
         if (!HOTSPOT_VM && classToModify == System.class) {
            return null;
         }

         rcReader = createClassReaderForRealClass(classToModify);
      }

      MockupsModifier modifier = new MockupsModifier(rcReader, classToModify, mockUp, mockMethods);
      rcReader.accept(modifier, SKIP_FRAMES);

      return modifier.wasModified() ? modifier.toByteArray() : null;
   }

   @NotNull
   BaseClassModifier createClassModifier(@NotNull ClassReader cr)
   {
      return new MockupsModifier(cr, realClass, mockUp, mockMethods);
   }

   @NotNull
   private static ClassReader createClassReaderForRealClass(@NotNull Class<?> classToModify)
   {
      if (classToModify.isInterface() || classToModify.isArray()) {
         throw new IllegalArgumentException("Not a modifiable class: " + classToModify.getName());
      }

{'fraction_non_alphanumeric': 0.10496284501061572, 'fraction_numerical': 0.002786624203821656, 'mean_word_length': 3.6126070991432067, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1365922', 'n_tokens_mistral': 2133, 'n_tokens_neox': 2099, 'n_words': 474}