Java 类org.robolectric.bytecode.AsmInstrumentingClassLoader 实例源码

项目:injectview    文件:InjectViewTestRunner.java   
@Override protected ClassLoader createRobolectricClassLoader(Setup setup, SdkConfig sdkConfig) {
  URL[] urls = getJarResolver().getLocalArtifactUrls(sdkConfig.getSdkClasspathDependencies());
  return new AsmInstrumentingClassLoader(setup, urls) {
    @Override protected byte[] getByteCode(String className) throws ClassNotFoundException {
      if (className.startsWith("com.github.stephanenicolas.injectview")) {
        try {
          CtClass dummyClass = ClassPool.getDefault().get(className);
          if (processor.shouldTransform(dummyClass)) {
            log.debug("Intercepting via InjectView " + className);
            processor.applyTransformations(dummyClass);
            byte[] bytes = dummyClass.toBytecode();
            System.out.println("Size of weaved byte code :" + bytes.length);
            FileOutputStream fileOutputStream = new FileOutputStream("/tmp/" + className + ".class");
            fileOutputStream.write(bytes);
            fileOutputStream.close();
            return bytes;
          }
        } catch (Exception e) {
          throw new RuntimeException("Impossible to transform class " + className, e);
        }
      }
      return super.getByteCode(className);
    }
  };
}
项目:easy-a    文件:LxgLifeCycleTestRunner.java   
@Override
protected ClassLoader createRobolectricClassLoader(Setup setup, SdkConfig sdkConfig) {
  URL[] urls = getJarResolver().getLocalArtifactUrls(sdkConfig.getSdkClasspathDependencies());
  return new AsmInstrumentingClassLoader(setup, urls) {
    @Override
    protected byte[] getByteCode(String className) throws ClassNotFoundException {
      if (className.startsWith("com.github.stephanenicolas.lxglifecycle")) {
        try {
          CtClass dummyClass = ClassPool.getDefault().get(className);
          if (processor.shouldTransform(dummyClass)) {
            log.debug("Intercepting via InjectExtra " + className);
            processor.applyTransformations(dummyClass);
            byte[] bytes = dummyClass.toBytecode();
            System.out.println("Size of weaved byte code :" + bytes.length);
            FileOutputStream fileOutputStream =
                new FileOutputStream("/tmp/" + className + ".class");
            fileOutputStream.write(bytes);
            fileOutputStream.close();
            return bytes;
          }
        } catch (Exception e) {
          throw new RuntimeException("Impossible to transform class " + className, e);
        }
      }
      return super.getByteCode(className);
    }
  };
}
项目:injectresource    文件:InjectResourceTestRunner.java   
@Override
protected ClassLoader createRobolectricClassLoader(Setup setup, SdkConfig sdkConfig) {
  URL[] urls = getJarResolver().getLocalArtifactUrls(sdkConfig.getSdkClasspathDependencies());
  return new AsmInstrumentingClassLoader(setup, urls) {
    @Override
    protected byte[] getByteCode(String className) throws ClassNotFoundException {
      if (className.startsWith("com.github.stephanenicolas.injectresource")) {
        try {
          CtClass dummyClass = ClassPool.getDefault().get(className);
          if (processor.shouldTransform(dummyClass)) {
            log.debug("Intercepting via InjectResource " + className);
            processor.applyTransformations(dummyClass);
            byte[] bytes = dummyClass.toBytecode();
            System.out.println("Size of weaved byte code :" + bytes.length);
            FileOutputStream fileOutputStream =
                new FileOutputStream("/tmp/" + className + ".class");
            fileOutputStream.write(bytes);
            fileOutputStream.close();
            return bytes;
          }
        } catch (Exception e) {
          throw new RuntimeException("Impossible to transform class " + className, e);
        }
      }
      return super.getByteCode(className);
    }
  };
}
项目:injectextra    文件:InjectExtraTestRunner.java   
@Override
protected ClassLoader createRobolectricClassLoader(Setup setup, SdkConfig sdkConfig) {
  URL[] urls = getJarResolver().getLocalArtifactUrls(sdkConfig.getSdkClasspathDependencies());
  return new AsmInstrumentingClassLoader(setup, urls) {
    @Override
    protected byte[] getByteCode(String className) throws ClassNotFoundException {
      if (className.startsWith("com.github.stephanenicolas.injectextra")) {
        try {
          CtClass dummyClass = ClassPool.getDefault().get(className);
          if (processor.shouldTransform(dummyClass)) {
            log.debug("Intercepting via InjectExtra " + className);
            processor.applyTransformations(dummyClass);
            byte[] bytes = dummyClass.toBytecode();
            System.out.println("Size of weaved byte code :" + bytes.length);
            FileOutputStream fileOutputStream =
                new FileOutputStream("/tmp/" + className + ".class");
            fileOutputStream.write(bytes);
            fileOutputStream.close();
            return bytes;
          }
        } catch (Exception e) {
          throw new RuntimeException("Impossible to transform class " + className, e);
        }
      }
      return super.getByteCode(className);
    }
  };
}
项目:loglifecycle    文件:LogLifeCycleTestRunner.java   
@Override
protected ClassLoader createRobolectricClassLoader(Setup setup, SdkConfig sdkConfig) {
  URL[] urls = getJarResolver().getLocalArtifactUrls(sdkConfig.getSdkClasspathDependencies());
  return new AsmInstrumentingClassLoader(setup, urls) {
    @Override
    protected byte[] getByteCode(String className) throws ClassNotFoundException {
      if (className.startsWith("com.github.stephanenicolas.loglifecycle")) {
        try {
          CtClass dummyClass = ClassPool.getDefault().get(className);
          if (processor.shouldTransform(dummyClass)) {
            log.debug("Intercepting via InjectExtra " + className);
            processor.applyTransformations(dummyClass);
            byte[] bytes = dummyClass.toBytecode();
            System.out.println("Size of weaved byte code :" + bytes.length);
            FileOutputStream fileOutputStream =
                new FileOutputStream("/tmp/" + className + ".class");
            fileOutputStream.write(bytes);
            fileOutputStream.close();
            return bytes;
          }
        } catch (Exception e) {
          throw new RuntimeException("Impossible to transform class " + className, e);
        }
      }
      return super.getByteCode(className);
    }
  };
}
项目:FullRobolectricTestSample    文件:RobolectricTestRunner.java   
protected ClassLoader createRobolectricClassLoader(Setup setup, SdkConfig sdkConfig) {
  URL[] urls = MAVEN_CENTRAL.getLocalArtifactUrls(this, sdkConfig.getSdkClasspathDependencies());
  return new AsmInstrumentingClassLoader(setup, urls);
}
项目:FullRobolectricTestSample    文件:RobolectricTestRunnerClassLoaderSetupTest.java   
@Test
public void testUsingClassLoader() throws ClassNotFoundException {
  ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  Assert.assertEquals(classLoader.getClass().getName(), AsmInstrumentingClassLoader.class.getName());
}
项目:FullRobolectricTestSample    文件:RobolectricTestRunnerClassLoaderSetupTest.java   
@Test
public void testGetPackage() {
  assertThat(DummyClass.class.getClassLoader()).isInstanceOf(AsmInstrumentingClassLoader.class);
  assertThat(DummyClass.class.getPackage()).isNotNull();
  assertThat(DummyClass.class.getName()).startsWith(DummyClass.class.getPackage().getName());
}