@Test public void shouldRegisterClasses() throws Exception { runInADifferentClassLoader(new Callable<Void>() { public Void call() throws Exception { URL url = File.createTempFile("vraptor", "test").getParentFile().toURI().toURL(); when(resolver.findWebInfClassesLocation()).thenReturn(url); Collection<String> components = asList(ResourceA.class.getName(), ResourceB.class.getName()); Class<WebAppBootstrap> generated = generator.generate(components, resolver); WebAppBootstrap instance = generated.newInstance(); instance.configure(registry); verify(registry).deepRegister(ResourceA.class); verify(registry).deepRegister(ResourceB.class); return null; } }); }
public Class<WebAppBootstrap> generate(Collection<String> components, ClasspathResolver resolver) { final String fullName = WebAppBootstrap.STATIC_BOOTSTRAP_NAME; final String simpleName = fullName.substring(WebAppBootstrap.STATIC_BOOTSTRAP_NAME.lastIndexOf('.') + 1); final String packageName = fullName.substring(0, fullName.lastIndexOf('.')); final String webInfClasses = resolver.findWebInfClassesLocation().getPath(); final String path = webInfClasses + '/' + packageName.replace('.', '/') + '/'; final String filename = path + simpleName + ".class"; new File(path).mkdirs(); String methodDef = createMethodDef(components); try { ClassPool pool = ClassPool.getDefault(); CtClass clazz = pool.makeClass(fullName); CtConstructor constructor = new CtConstructor(null, clazz); constructor.setBody("{super();}"); clazz.addConstructor(constructor); CtMethod m = CtNewMethod.make(methodDef, clazz); clazz.addMethod(m); ClassFile cf = clazz.getClassFile(); cf.setVersionToJava5(); cf.setInterfaces(new String[]{WebAppBootstrap.class.getName()}); ConstPool cp = cf.getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag); attr.setAnnotation(new Annotation(Generated.class.getName(), cp)); cf.addAttribute(attr); cf.write(new DataOutputStream(new FileOutputStream(filename))); return clazz.toClass(); } catch (Exception e) { throw new ScannerException("Error while generating the class file", e); } }
JavassistBootstrapGenerator implements BootstrapGenerator { public Class<WebAppBootstrap> generate(Collection<String> components, ClasspathResolver resolver) { final String fullName = WebAppBootstrap.STATIC_BOOTSTRAP_NAME; final String simpleName = fullName.substring(WebAppBootstrap.STATIC_BOOTSTRAP_NAME.lastIndexOf('.') + 1); final String packageName = fullName.substring(0, fullName.lastIndexOf('.')); final String webInfClasses = resolver.findWebInfClassesLocation().getPath(); final String path = webInfClasses + '/' + packageName.replace('.', '/') + '/'; final String filename = path + simpleName + ".class"; new File(path).mkdirs(); String methodDef = createMethodDef(components); try { ClassPool pool = ClassPool.getDefault(); CtClass clazz = pool.makeClass(fullName); CtConstructor constructor = new CtConstructor(null, clazz); constructor.setBody("{super();}"); clazz.addConstructor(constructor); CtMethod m = CtNewMethod.make(methodDef, clazz); clazz.addMethod(m); ClassFile cf = clazz.getClassFile(); cf.setVersionToJava5(); cf.setInterfaces(new String[]{WebAppBootstrap.class.getName()}); ConstPool cp = cf.getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag); attr.setAnnotation(new Annotation(Generated.class.getName(), cp)); cf.addAttribute(attr); cf.write(new DataOutputStream(new FileOutputStream(filename))); return clazz.toClass(); } catch (Exception e) { throw new ScannerException("Error while generating the class file", e); } } }
JavassistBootstrapGenerator implements BootstrapGenerator { public Class<WebAppBootstrap> generate(Collection<String> components, ClasspathResolver resolver) { final String fullName = WebAppBootstrap.STATIC_BOOTSTRAP_NAME; final String simpleName = fullName.substring(WebAppBootstrap.STATIC_BOOTSTRAP_NAME.lastIndexOf('.') + 1); final String packageName = fullName.substring(0, fullName.lastIndexOf('.')); final String webInfClasses = resolver.findWebInfClassesLocation().getPath(); final String path = webInfClasses + '/' + packageName.replace('.', '/') + '/'; final String filename = path + simpleName + ".class"; new File(path).mkdirs(); String methodDef = createMethodDef(components); try { ClassPool pool = ClassPool.getDefault(); CtClass clazz = pool.makeClass(fullName); CtConstructor constructor = new CtConstructor(null, clazz); constructor.setBody("{super();}"); clazz.addConstructor(constructor); CtMethod m = CtNewMethod.make(methodDef, clazz); clazz.addMethod(m); ClassFile cf = clazz.getClassFile(); cf.setVersionToJava5(); cf.setInterfaces(new String[]{WebAppBootstrap.class.getName()}); ConstPool cp = cf.getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag); attr.setAnnotation(new Annotation(Generated.class.getName(), cp)); cf.addAttribute(attr); cf.write(new DataOutputStream(new FileOutputStream(filename))); return clazz.toClass(); } catch (Exception e) { throw new ScannerException("Error while generating the class file", e); } }  }
JavassistBootstrapGenerator implements BootstrapGenerator { public Class<WebAppBootstrap> generate(Collection<String> components, ClasspathResolver resolver) { final String fullName = WebAppBootstrap.STATIC_BOOTSTRAP_NAME; final String simpleName = fullName.substring(WebAppBootstrap.STATIC_BOOTSTRAP_NAME.lastIndexOf('.') + 1); final String packageName = fullName.substring(0, fullName.lastIndexOf('.')); final String webInfClasses = resolver.findWebInfClassesLocation().getPath(); final String path = webInfClasses + '/' + packageName.replace('.', '/') + '/'; final String filename = path + simpleName + ".class"; new File(path).mkdirs(); String methodDef = createMethodDef(components); try { ClassPool pool = ClassPool.getDefault(); CtClass clazz = pool.makeClass(fullName); CtConstructor constructor = new CtConstructor(null, clazz); constructor.setBody("{super();}"); clazz.addConstructor(constructor); CtMethod m = CtNewMethod.make(methodDef, clazz); clazz.addMethod(m); ClassFile cf = clazz.getClassFile(); cf.setVersionToJava5(); cf.setInterfaces(new String[]{WebAppBootstrap.class.getName()}); ConstPool cp = cf.getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag); attr.setAnnotation(new Annotation(Generated.class.getName(), cp)); cf.addAttribute(attr); cf.write(new DataOutputStream(new FileOutputStream(filename))); return clazz.toClass(); } catch (Exception e) { throw new ScannerException("Error while generating the class file", e); } }  Class<WebAppBootstrap> generate(Collection<String> components, ClasspathResolver resolver); }
JavassistBootstrapGenerator implements BootstrapGenerator { public Class<WebAppBootstrap> generate(Collection<String> components, ClasspathResolver resolver) { final String fullName = WebAppBootstrap.STATIC_BOOTSTRAP_NAME; final String simpleName = fullName.substring(WebAppBootstrap.STATIC_BOOTSTRAP_NAME.lastIndexOf('.') + 1); final String packageName = fullName.substring(0, fullName.lastIndexOf('.')); final String webInfClasses = resolver.findWebInfClassesLocation().getPath(); final String path = webInfClasses + '/' + packageName.replace('.', '/') + '/'; final String filename = path + simpleName + ".class"; new File(path).mkdirs(); String methodDef = createMethodDef(components); try { ClassPool pool = ClassPool.getDefault(); CtClass clazz = pool.makeClass(fullName); CtConstructor constructor = new CtConstructor(null, clazz); constructor.setBody("{super();}"); clazz.addConstructor(constructor); CtMethod m = CtNewMethod.make(methodDef, clazz); clazz.addMethod(m); ClassFile cf = clazz.getClassFile(); cf.setVersionToJava5(); cf.setInterfaces(new String[]{WebAppBootstrap.class.getName()}); ConstPool cp = cf.getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag); attr.setAnnotation(new Annotation(Generated.class.getName(), cp)); cf.addAttribute(attr); cf.write(new DataOutputStream(new FileOutputStream(filename))); return clazz.toClass(); } catch (Exception e) { throw new ScannerException("Error while generating the class file", e); } }  Class<WebAppBootstrap> generate(Collection<String> components, ClasspathResolver resolver);  }