/** * Generate service stubs. * * @throws IOException */ private void generateServiceStubs() throws IOException { final ServiceStubGenerator stubGenerator = new ServiceStubGenerator(new RestInterfaceAnalyzer()); final File generatedSourceDirectory = outputDir; final Set<Class<?>> classes = getServiceClasses(); @Cleanup final FileCodeWriter codeWriter = new FileCodeWriter(generatedSourceDirectory); log("Generating service stubs", Project.MSG_ERR); for (final Class<?> klass : classes) { final String stubClassName = getStubClassName(klass); try { stubGenerator.generateStubInterface(klass, stubClassName, destinationPackage, codeWriter); log("Generated " + stubClassName, Project.MSG_INFO); } catch (final Exception e) { log("Stub generation failed for " + klass.getCanonicalName(), Project.MSG_ERR); throw new RuntimeException(e); } } }
/** * Parses data schema files and generates stubs for them. * @param sources provides the paths to schema files and/or fully qualified schema names. * @param targetDirectoryPath path to target root java source directory * @return a result that includes collection of files accessed, would have generated and actually modified. * @throws IOException if there are problems opening or deleting files. */ private GeneratorResult generate(String targetDirectoryPath, String[] sources) throws IOException { initializeDefaultPackage(); initSchemaResolver(); List<File> sourceFiles = parseSources(sources); File targetDirectory = new File(targetDirectoryPath); List<File> targetFiles = targetFiles(targetDirectory); List<File> modifiedFiles; if (upToDate(sourceFiles, targetFiles)) { modifiedFiles = Collections.emptyList(); log.info("Target files are up-to-date: " + targetFiles); } else { modifiedFiles = targetFiles; log.info("Generating " + targetFiles.size() + " files: " + targetFiles); validateDefinedClassRegistration(); getCodeModel().build(new FileCodeWriter(targetDirectory, true)); } return new Result(sourceFiles, targetFiles, modifiedFiles); }
public void generate(final File targetFolder, final String comment) throws IOException { if (!targetFolder.exists()) { targetFolder.mkdirs(); } final CodeWriter sourceCodeWriter = new SourceCodeWriter(targetFolder, comment); final CodeWriter fileCodeWriter = new FileCodeWriter(targetFolder); this.codeModel.build(sourceCodeWriter, fileCodeWriter); }
private void writeCodeModel() { try { File dir = config.getTargetDir(); FileHelper.createDirectoryIfNeeded(dir); context.getCodeModel().build(new FileCodeWriter(dir)); } catch (IOException exc) { throw Exceptions.unchecked(exc); } }
/** * Parses REST IDL files and generates Request builders from them. * @param sources path to IDL files or directories * @param targetDirectoryPath path to target root java source directory * @return a result that includes collection of files accessed, would have generated and actually modified. * @throws IOException if there are problems opening or deleting files. */ private GeneratorResult generate(String targetDirectoryPath, String[] sources) throws IOException { List<File> sourceFiles = parseSources(sources); if(getMessage().length() > 0) { throw new IOException(getMessage().toString()); } File targetDirectory = new File(targetDirectoryPath); List<File> targetFiles = targetFiles(targetDirectory); List<File> modifiedFiles; if (upToDate(sourceFiles, targetFiles)) { modifiedFiles = Collections.emptyList(); log.info("Target files are up-to-date: " + targetFiles); } else { modifiedFiles = targetFiles; log.info("Generating " + targetFiles.size() + " files: " + targetFiles); getCodeModel().build(new FileCodeWriter(targetDirectory, true)); } return new Result(sourceFiles, targetFiles, modifiedFiles); }
/** * @param outputFolder * @param basePackage * @param appInfos * @param encoding * encoding of output files - e.g. "UTF-8" * @throws JClassAlreadyExistsException * @throws IOException */ static void generateSources(File outputFolder, String basePackage, List<Application> appInfos, String encoding) throws JClassAlreadyExistsException, IOException { // generate code: CodeGenerator codeGen = new CodeGenerator(basePackage); JCodeModel jCodeModel = codeGen.generateCode(appInfos); FileCodeWriter writer = new FileCodeWriter(outputFolder, encoding); jCodeModel.build(writer); }
public CodeWriter createCodeWriter() throws IOException { return new FileCodeWriter(targetDirectory, false); }
public void write(File outputdir) throws java.io.IOException { this.model.build(new FileCodeWriter(outputdir)); }
public static void main(String[] args) throws JClassAlreadyExistsException, IOException, XPathExpressionException, SAXException, ParserConfigurationException { JCodeModel cm = new JCodeModel(); JPackage pkg = cm._package("org.saintandreas.serket.scpd"); File scpdDir = new File("src/main/resources/scpd"); File[] dirs = scpdDir.listFiles(new FileFilter() { public boolean accept(File pathname) { return pathname.getName().endsWith(".xml"); } }); for (File f : dirs) { String serviceName = f.getName();//"ContentDirectory3"; serviceName = serviceName.substring(0, serviceName.length() - 4); Document scpdDoc = XmlUtil.parseXmlFile(f); JDefinedClass cls = pkg._class(JMod.PUBLIC | JMod.ABSTRACT, serviceName, ClassType.CLASS); { JClass type = (JClass) cm._ref(BaseService.class); cls._extends(type); JMethod method = cls.constructor(JMod.PUBLIC); method.param(String.class, "id"); method.param(String.class, "controlURL"); method.param(String.class, "eventURL"); method.body().directStatement("super(id, controlURL, eventURL);") ; } // public static final String URI = "urn:schemas-upnp-org:service:ContentDirectory:1"; { String uriServiceName = serviceName; String uriVersionNumber = "1"; if (Character.isDigit(serviceName.charAt(serviceName.length() - 1))) { uriVersionNumber = serviceName.substring(serviceName.length() - 1); uriServiceName = serviceName.substring(0, serviceName.length() - 1); } JVar uriDecl = cls.field(JMod.STATIC | JMod.PUBLIC | JMod.FINAL, String.class, "URI").init(JExpr.lit(URI_PREFIX + uriServiceName + ":" + uriVersionNumber)); cls.method(JMod.PUBLIC, String.class, "getURI").body()._return(uriDecl); } for (Element e : XPathUtil.getElements(scpdDoc, "/scpd/actionList/action")) { createAction(cm, cls, e); } // for (Element e : XPathUtil.getElements(scpdDoc, "/scpd/serviceStateTable/stateVariable")) { // createField(cls, e); // } } File dest = new File("src/main/java/"); dest.mkdirs(); cm.build(new FileCodeWriter(dest)); }
/** * Generates Java source code. * A convenience method that calls {@link #build(CodeWriter,CodeWriter)}. * * @param srcDir * Java source files are generated into this directory. * @param resourceDir * Other resource files are generated into this directory. * @param status * if non-null, progress indication will be sent to this stream. */ public void build( File srcDir, File resourceDir, PrintStream status ) throws IOException { CodeWriter src = new FileCodeWriter(srcDir); CodeWriter res = new FileCodeWriter(resourceDir); if(status!=null) { src = new ProgressCodeWriter(src, status ); res = new ProgressCodeWriter(res, status ); } build(src,res); }