Search in sources :

Example 1 with FileCodeWriter

use of com.sun.codemodel.writer.FileCodeWriter in project rest.li by linkedin.

the class PegasusDataTemplateGenerator method run.

public static GeneratorResult run(String resolverPath, String defaultPackage, final boolean generateImported, String targetDirectoryPath, String[] sources) throws IOException {
    final DataSchemaParser schemaParser = new DataSchemaParser(resolverPath);
    final TemplateSpecGenerator specGenerator = new TemplateSpecGenerator(schemaParser.getSchemaResolver());
    final JavaDataTemplateGenerator dataTemplateGenerator = new JavaDataTemplateGenerator(defaultPackage);
    for (DataSchema predefinedSchema : JavaDataTemplateGenerator.PredefinedJavaClasses.keySet()) {
        specGenerator.registerDefinedSchema(predefinedSchema);
    }
    final DataSchemaParser.ParseResult parseResult = schemaParser.parseSources(sources);
    for (Map.Entry<DataSchema, DataSchemaLocation> entry : parseResult.getSchemaAndLocations().entrySet()) {
        specGenerator.generate(entry.getKey(), entry.getValue());
    }
    for (ClassTemplateSpec spec : specGenerator.getGeneratedSpecs()) {
        dataTemplateGenerator.generate(spec);
    }
    final JavaCodeUtil.PersistentClassChecker checker = new DataTemplatePersistentClassChecker(generateImported, specGenerator, dataTemplateGenerator, parseResult.getSourceFiles());
    final File targetDirectory = new File(targetDirectoryPath);
    final List<File> targetFiles = JavaCodeUtil.targetFiles(targetDirectory, dataTemplateGenerator.getCodeModel(), JavaCodeUtil.classLoaderFromResolverPath(schemaParser.getResolverPath()), checker);
    final List<File> modifiedFiles;
    if (FileUtil.upToDate(parseResult.getSourceFiles(), targetFiles)) {
        modifiedFiles = Collections.emptyList();
        _log.info("Target files are up-to-date: " + targetFiles);
    } else {
        modifiedFiles = targetFiles;
        _log.info("Generating " + targetFiles.size() + " files: " + targetFiles);
        validateDefinedClassRegistration(dataTemplateGenerator.getCodeModel(), dataTemplateGenerator.getGeneratedClasses().keySet());
        dataTemplateGenerator.getCodeModel().build(new FileCodeWriter(targetDirectory, true));
    }
    return new DefaultGeneratorResult(parseResult.getSourceFiles(), targetFiles, modifiedFiles);
}
Also used : DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) FileCodeWriter(com.sun.codemodel.writer.FileCodeWriter) ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) Map(java.util.Map) File(java.io.File)

Example 2 with FileCodeWriter

use of com.sun.codemodel.writer.FileCodeWriter in project rest.li by linkedin.

the class RestRequestBuilderGenerator method run.

public static GeneratorResult run(String resolverPath, String defaultPackage, final boolean generateImported, final boolean generateDataTemplates, RestliVersion version, RestliVersion deprecatedByVersion, String targetDirectoryPath, String[] sources) throws IOException {
    final RestSpecParser parser = new RestSpecParser();
    final JavaRequestBuilderGenerator generator = new JavaRequestBuilderGenerator(resolverPath, defaultPackage, generateDataTemplates, version, deprecatedByVersion);
    final ClassLoader classLoader = JavaCodeUtil.classLoaderFromResolverPath(resolverPath);
    final RestSpecParser.ParseResult parseResult = parser.parseSources(sources);
    final StringBuilder message = new StringBuilder();
    for (CodeUtil.Pair<ResourceSchema, File> pair : parseResult.getSchemaAndFiles()) {
        try {
            final JDefinedClass clazz = generator.generate(pair.first, pair.second);
        } catch (Exception e) {
            message.append(e.getMessage() + "\n");
        }
    }
    if (message.length() > 0) {
        throw new IOException(message.toString());
    }
    final PegasusDataTemplateGenerator.DataTemplatePersistentClassChecker dataTemplateChecker = new PegasusDataTemplateGenerator.DataTemplatePersistentClassChecker(generateImported, generator.getSpecGenerator(), generator.getJavaDataTemplateGenerator(), Collections.<File>emptySet());
    final JavaCodeUtil.PersistentClassChecker checker = new JavaCodeUtil.PersistentClassChecker() {

        @Override
        public boolean isPersistent(JDefinedClass clazz) {
            if (generateDataTemplates || generator.isGeneratedArrayClass(clazz)) {
                try {
                    Class.forName(clazz.fullName(), false, classLoader);
                } catch (ClassNotFoundException e) {
                    return true;
                }
            }
            return dataTemplateChecker.isPersistent(clazz);
        }
    };
    final JCodeModel requestBuilderCodeModel = generator.getCodeModel();
    final JCodeModel dataTemplateCodeModel = generator.getJavaDataTemplateGenerator().getCodeModel();
    final File targetDirectory = new File(targetDirectoryPath);
    final List<File> targetFiles = JavaCodeUtil.targetFiles(targetDirectory, requestBuilderCodeModel, classLoader, checker);
    targetFiles.addAll(JavaCodeUtil.targetFiles(targetDirectory, dataTemplateCodeModel, classLoader, checker));
    final List<File> modifiedFiles;
    if (FileUtil.upToDate(parseResult.getSourceFiles(), targetFiles)) {
        modifiedFiles = Collections.emptyList();
        _log.info("Target files are up-to-date: " + targetFiles);
    } else {
        modifiedFiles = targetFiles;
        _log.info("Generating " + targetFiles.size() + " files: " + targetFiles);
        requestBuilderCodeModel.build(new FileCodeWriter(targetDirectory, true));
        dataTemplateCodeModel.build(new FileCodeWriter(targetDirectory, true));
    }
    return new DefaultGeneratorResult(parseResult.getSourceFiles(), targetFiles, modifiedFiles);
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) JDefinedClass(com.sun.codemodel.JDefinedClass) IOException(java.io.IOException) PegasusDataTemplateGenerator(com.linkedin.pegasus.generator.PegasusDataTemplateGenerator) JCodeModel(com.sun.codemodel.JCodeModel) IOException(java.io.IOException) FileCodeWriter(com.sun.codemodel.writer.FileCodeWriter) JavaCodeUtil(com.linkedin.pegasus.generator.JavaCodeUtil) CodeUtil(com.linkedin.pegasus.generator.CodeUtil) DefaultGeneratorResult(com.linkedin.pegasus.generator.DefaultGeneratorResult) JavaCodeUtil(com.linkedin.pegasus.generator.JavaCodeUtil) File(java.io.File)

Example 3 with FileCodeWriter

use of com.sun.codemodel.writer.FileCodeWriter in project groovy-cps by cloudbees.

the class Driver method run.

public void run(File dir) throws Exception {
    JavaCompiler javac = JavacTool.create();
    DiagnosticListener<JavaFileObject> errorListener = createErrorListener();
    try (StandardJavaFileManager fileManager = javac.getStandardFileManager(errorListener, Locale.getDefault(), Charset.defaultCharset())) {
        fileManager.setLocation(StandardLocation.CLASS_PATH, Collections.singleton(Which.jarFile(GroovyShell.class)));
        File groovySrcJar = Which.jarFile(Driver.class.getClassLoader().getResource("groovy/lang/GroovyShell.java"));
        // classes to translate
        // TODO include other classes mentioned in DgmConverter if they have any applicable methods; and certainly StringGroovyMethods which has some Closure-based methods
        List<String> fileNames = asList("DefaultGroovyMethods", "DefaultGroovyStaticMethods");
        List<JavaFileObject> src = new ArrayList<>();
        ZipArchive a = new ZipArchive((JavacFileManager) fileManager, new ZipFile(groovySrcJar));
        for (String name : fileNames) {
            src.add(a.getFileObject(new RelativeDirectory("org/codehaus/groovy/runtime"), name + ".java"));
        }
        // annotation processing appears to cause the source files to be reparsed
        // (even though I couldn't find exactly where it's done), which causes
        // Tree symbols created by the original JavacTask.parse() call to be thrown away,
        // which breaks later processing.
        // So for now, don't perform annotation processing
        List<String> options = asList("-proc:none");
        Translator t = new Translator(javac.getTask(null, fileManager, errorListener, options, null, src));
        for (String name : fileNames) {
            t.translate("org.codehaus.groovy.runtime." + name, "com.cloudbees.groovy.cps.Cps" + name, groovySrcJar.getName());
        }
        dir.mkdirs();
        t.generateTo(new FileCodeWriter(dir));
    }
}
Also used : RelativeDirectory(com.sun.tools.javac.file.RelativePath.RelativeDirectory) ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) ZipArchive(com.sun.tools.javac.file.ZipArchive) FileCodeWriter(com.sun.codemodel.writer.FileCodeWriter) JavaFileObject(javax.tools.JavaFileObject) ZipFile(java.util.zip.ZipFile) StandardJavaFileManager(javax.tools.StandardJavaFileManager) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Aggregations

FileCodeWriter (com.sun.codemodel.writer.FileCodeWriter)3 File (java.io.File)3 DataSchema (com.linkedin.data.schema.DataSchema)1 DataSchemaLocation (com.linkedin.data.schema.DataSchemaLocation)1 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)1 CodeUtil (com.linkedin.pegasus.generator.CodeUtil)1 DefaultGeneratorResult (com.linkedin.pegasus.generator.DefaultGeneratorResult)1 JavaCodeUtil (com.linkedin.pegasus.generator.JavaCodeUtil)1 PegasusDataTemplateGenerator (com.linkedin.pegasus.generator.PegasusDataTemplateGenerator)1 ClassTemplateSpec (com.linkedin.pegasus.generator.spec.ClassTemplateSpec)1 ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)1 JCodeModel (com.sun.codemodel.JCodeModel)1 JDefinedClass (com.sun.codemodel.JDefinedClass)1 RelativeDirectory (com.sun.tools.javac.file.RelativePath.RelativeDirectory)1 ZipArchive (com.sun.tools.javac.file.ZipArchive)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ZipFile (java.util.zip.ZipFile)1 JavaCompiler (javax.tools.JavaCompiler)1