@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); } }; }
@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); } }; }
@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); } }; }
@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); } }; }
@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); } }; }
protected ClassLoader createRobolectricClassLoader(Setup setup, SdkConfig sdkConfig) { URL[] urls = MAVEN_CENTRAL.getLocalArtifactUrls(this, sdkConfig.getSdkClasspathDependencies()); return new AsmInstrumentingClassLoader(setup, urls); }
@Test public void testUsingClassLoader() throws ClassNotFoundException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); Assert.assertEquals(classLoader.getClass().getName(), AsmInstrumentingClassLoader.class.getName()); }
@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()); }