package com.github.wreulicke.bean.validation;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.atomic.AtomicBoolean;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;

public class Instruments {
  public static String forInstruments(String className) {
    return className.replaceAll("\\,", "/");
  }

  public static String forInstruments(Class<?> clazz) {
    return forInstruments(clazz.getName());
  }

  public static ClassPool mock(Class<?> clazz) {
    String clazzName = clazz.getName();
    AtomicBoolean isCreated = new AtomicBoolean(false);
    return new ClassPool(ClassPool.getDefault()) {
      @Override
      public CtClass makeClass(InputStream classfile, boolean ifNotFrozen) throws IOException, RuntimeException {
        isCreated.set(true);
        return super.makeClass(classfile, false);
      }


      @Override
      protected synchronized CtClass get0(String classname, boolean useCache) throws NotFoundException {
        if (!isCreated.get() && clazzName.equals(classname))
          return null;
        return super.get0(classname, useCache);
      }
    };
  }
}

@@ -44,206 +44,8 @@
 n;%0A%0A
-import java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.util.concurrent.atomic.AtomicBoolean;%0A%0Aimport javassist.ClassPool;%0Aimport javassist.CtClass;%0Aimport javassist.NotFoundException;%0A%0A
 publ
@@ -282,695 +282,7 @@
 %0A  %7D
-%0A%0A  public static ClassPool mock(Class%3C?%3E clazz) %7B%0A    String clazzName = clazz.getName();%0A    AtomicBoolean isCreated = new AtomicBoolean(false);%0A    return new ClassPool(ClassPool.getDefault()) %7B%0A      @Override%0A      public CtClass makeClass(InputStream classfile, boolean ifNotFrozen) throws IOException, RuntimeException %7B%0A        isCreated.set(true);%0A        return super.makeClass(classfile, false);%0A      %7D%0A%0A%0A      @Override%0A      protected synchronized CtClass get0(String classname, boolean useCache) throws NotFoundException %7B%0A        if (!isCreated.get() && clazzName.equals(classname))%0A          return null;%0A        return super.get0(classname, useCache);%0A      %7D%0A    %7D;%0A  %7D
 %0A%7D%0A

remove mock pool