org.apache.avro.compiler.specific.SpecificCompiler Java Examples

The following examples show how to use org.apache.avro.compiler.specific.SpecificCompiler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GenerateAvroJavaTask.java    From gradle-avro-plugin with Apache License 2.0 6 votes vote down vote up
private void compile(SpecificCompiler compiler, File sourceFile) throws IOException {
    compiler.setOutputCharacterEncoding(getOutputCharacterEncoding().getOrNull());
    compiler.setStringType(stringTypeProvider.get());
    compiler.setFieldVisibility(fieldVisibilityProvider.get());
    if (getTemplateDirectory().isPresent()) {
        compiler.setTemplateDir(getTemplateDirectory().get());
    }
    compiler.setCreateOptionalGetters(createOptionalGetters.get());
    compiler.setGettersReturnOptional(gettersReturnOptional.get());
    compiler.setOptionalGettersForNullableFieldsOnly(optionalGettersForNullableFieldsOnly.get());
    compiler.setCreateSetters(isCreateSetters().get());
    compiler.setEnableDecimalLogicalType(isEnableDecimalLogicalType().get());
    registerCustomConversions(compiler);

    compiler.compileToDestination(sourceFile, getOutputDir().get().getAsFile());
}
 
Example #2
Source File: CodeTransformationsAvro19Test.java    From avro-util with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private String runNativeCodegen(Schema schema) throws Exception {
  File outputRoot = Files.createTempDirectory(null).toFile();
  SpecificCompiler compiler = new SpecificCompiler(schema);
  compiler.compileToDestination(null, outputRoot);
  File javaFile = new File(outputRoot, schema.getNamespace().replaceAll("\\.",File.separator) + File.separator + schema.getName() + ".java");
  Assert.assertTrue(javaFile.exists());

  String sourceCode;
  try (FileInputStream fis = new FileInputStream(javaFile)) {
    sourceCode = IOUtils.toString(fis, StandardCharsets.UTF_8);
  }

  return sourceCode;
}
 
Example #3
Source File: CodeTransformationsAvro18Test.java    From avro-util with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private String runNativeCodegen(Schema schema) throws Exception {
  File outputRoot = Files.createTempDirectory(null).toFile();
  SpecificCompiler compiler = new SpecificCompiler(schema);
  compiler.compileToDestination(null, outputRoot);
  File javaFile = new File(outputRoot, schema.getNamespace().replaceAll("\\.",File.separator) + File.separator + schema.getName() + ".java");
  Assert.assertTrue(javaFile.exists());

  String sourceCode;
  try (FileInputStream fis = new FileInputStream(javaFile)) {
    sourceCode = IOUtils.toString(fis, StandardCharsets.UTF_8);
  }

  return sourceCode;
}
 
Example #4
Source File: CodeTransformationsAvro15Test.java    From avro-util with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private String runNativeCodegen(Schema schema) throws Exception {
  File outputRoot = Files.createTempDirectory(null).toFile();
  SpecificCompiler compiler = new SpecificCompiler(schema);
  compiler.compileToDestination(null, outputRoot);
  File javaFile = new File(outputRoot, schema.getNamespace().replaceAll("\\.",File.separator) + File.separator + schema.getName() + ".java");
  Assert.assertTrue(javaFile.exists());

  String sourceCode;
  try (FileInputStream fis = new FileInputStream(javaFile)) {
    sourceCode = IOUtils.toString(fis, StandardCharsets.UTF_8);
  }

  return sourceCode;
}
 
Example #5
Source File: CodeTransformationsAvro16Test.java    From avro-util with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private String runNativeCodegen(Schema schema) throws Exception {
  File outputRoot = Files.createTempDirectory(null).toFile();
  SpecificCompiler compiler = new SpecificCompiler(schema);
  compiler.compileToDestination(null, outputRoot);
  File javaFile = new File(outputRoot, schema.getNamespace().replaceAll("\\.",File.separator) + File.separator + schema.getName() + ".java");
  Assert.assertTrue(javaFile.exists());

  String sourceCode;
  try (FileInputStream fis = new FileInputStream(javaFile)) {
    sourceCode = IOUtils.toString(fis, StandardCharsets.UTF_8);
  }

  return sourceCode;
}
 
Example #6
Source File: CodeTransformationsAvro17Test.java    From avro-util with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private String runNativeCodegen(Schema schema) throws Exception {
  File outputRoot = Files.createTempDirectory(null).toFile();
  SpecificCompiler compiler = new SpecificCompiler(schema);
  compiler.compileToDestination(null, outputRoot);
  File javaFile = new File(outputRoot, schema.getNamespace().replaceAll("\\.",File.separator) + File.separator + schema.getName() + ".java");
  Assert.assertTrue(javaFile.exists());

  String sourceCode;
  try (FileInputStream fis = new FileInputStream(javaFile)) {
    sourceCode = IOUtils.toString(fis, StandardCharsets.UTF_8);
  }

  return sourceCode;
}
 
Example #7
Source File: GenerateAvroJavaTask.java    From gradle-avro-plugin with Apache License 2.0 5 votes vote down vote up
private void processProtoFile(File sourceFile) {
    getLogger().info("Processing {}", sourceFile);
    try {
        compile(new SpecificCompiler(Protocol.parse(sourceFile)), sourceFile);
    } catch (IOException ex) {
        throw new GradleException(String.format("Failed to compile protocol definition file %s", sourceFile), ex);
    }
}
 
Example #8
Source File: GenerateAvroJavaTask.java    From gradle-avro-plugin with Apache License 2.0 5 votes vote down vote up
private int processSchemaFiles() {
    Set<File> files = filterSources(new FileExtensionSpec(SCHEMA_EXTENSION)).getFiles();
    ProcessingState processingState = resolver.resolve(files);
    for (File file : files) {
        String path = getProject().relativePath(file);
        for (Schema schema : processingState.getSchemasForLocation(path)) {
            try {
                compile(new SpecificCompiler(schema), file);
            } catch (IOException ex) {
                throw new GradleException(String.format("Failed to compile schema definition file %s", path), ex);
            }
        }
    }
    return processingState.getProcessedTotal();
}
 
Example #9
Source File: AvroCompiler.java    From Avro-Schema-Generator with Apache License 2.0 5 votes vote down vote up
/**
 * Calls the avro compiler to compile Java bindings from avro schema files.
 * The files MUST be in a sorted order which accounts for their dependencies!
 *
 * @param sources JSON schema files to compile
 * @param outputDirectory directory to write packages and files
 */
public static void compileSchemas(File[] sources, File outputDirectory) {
	try {
		SpecificCompiler.compileSchema(sources, outputDirectory);
	} catch (Exception ex) {
		throw new SchemagenException("An error occurred while compiling.", ex);
	}
}
 
Example #10
Source File: AvroConverterTest.java    From bunsen with Apache License 2.0 4 votes vote down vote up
@Test
public void testCompile() throws IOException {

  List<Schema> schemas = AvroConverter.generateSchemas(FhirContexts.forStu3(),
      ImmutableMap.of(TestData.US_CORE_PATIENT, Collections.emptyList(),
          TestData.VALUE_SET, Collections.emptyList(),
          TestData.US_CORE_MEDICATION_REQUEST, ImmutableList.of(TestData.US_CORE_MEDICATION)));

  // Wrap the schemas in a protocol to simplify the invocation of the compiler.
  Protocol protocol = new Protocol("fhir-test",
      "FHIR Resources for Testing",
      null);

  protocol.setTypes(schemas);

  SpecificCompiler compiler = new SpecificCompiler(protocol);

  Path generatedCodePath = Files.createTempDirectory("generated_code");

  generatedCodePath.toFile().deleteOnExit();

  compiler.compileToDestination(null, generatedCodePath.toFile());

  // Check that java files were created as expected.
  Set<String> javaFiles = Files.find(generatedCodePath,
      10,
      (path, basicFileAttributes) -> true)
      .map(path -> generatedCodePath.relativize(path))
      .map(Object::toString)
      .collect(Collectors.toSet());

  // Ensure common types were generated
  Assert.assertTrue(javaFiles.contains("com/cerner/bunsen/stu3/avro/Period.java"));
  Assert.assertTrue(javaFiles.contains("com/cerner/bunsen/stu3/avro/Coding.java"));
  Assert.assertTrue(javaFiles.contains("com/cerner/bunsen/stu3/avro/ValueSet.java"));

  // The specific profile should be created in the expected sub-package.
  Assert.assertTrue(javaFiles.contains("com/cerner/bunsen/stu3/avro/us/core/Patient.java"));

  // Check extension types.
  Assert.assertTrue(javaFiles.contains("com/cerner/bunsen/stu3/avro/us/core/UsCoreRace.java"));

  // Choice types include each choice that could be used.
  Assert.assertTrue(javaFiles.contains("com/cerner/bunsen/stu3/avro/ChoiceBooleanInteger.java"));

  // Contained types created.
  Assert.assertTrue(javaFiles.contains(
      "com/cerner/bunsen/stu3/avro/us/core/MedicationRequestContained.java"));
}
 
Example #11
Source File: GenerateAvroJavaTask.java    From gradle-avro-plugin with Apache License 2.0 4 votes vote down vote up
public void setFieldVisibility(SpecificCompiler.FieldVisibility fieldVisibility) {
    setFieldVisibility(fieldVisibility.name());
}
 
Example #12
Source File: GenerateAvroJavaTask.java    From gradle-avro-plugin with Apache License 2.0 4 votes vote down vote up
private void registerCustomConversions(SpecificCompiler compiler) {
    customConversions.get().forEach(compiler::addCustomConversion);
}
 
Example #13
Source File: DefaultAvroExtension.java    From gradle-avro-plugin with Apache License 2.0 4 votes vote down vote up
public void setFieldVisibility(SpecificCompiler.FieldVisibility fieldVisibility) {
    setFieldVisibility(fieldVisibility.name());
}
 
Example #14
Source File: AvroClassGenerator.java    From tutorials with MIT License 4 votes vote down vote up
public void generateAvroClasses() throws IOException {
    SpecificCompiler compiler = new SpecificCompiler(new Schema.Parser().parse(new File("src/main/resources/avroHttpRequest-schema.avsc")));
    compiler.compileToDestination(new File("src/main/resources"), new File("src/main/java")); 
}