use of io.micronaut.inject.writer.GeneratedFile in project micronaut-core by micronaut-projects.
the class JsonConfigurationMetadataWriter method write.
@Override
public void write(ConfigurationMetadataBuilder<?> metadataBuilder, ClassWriterOutputVisitor classWriterOutputVisitor) throws IOException {
Optional<GeneratedFile> opt = classWriterOutputVisitor.visitMetaInfFile(getFileName(), metadataBuilder.getOriginatingElements());
if (opt.isPresent()) {
GeneratedFile file = opt.get();
List<ConfigurationMetadata> configurations = metadataBuilder.getConfigurations();
List<PropertyMetadata> properties = metadataBuilder.getProperties();
try (Writer writer = file.openWriter()) {
writer.write('{');
boolean hasGroups = !configurations.isEmpty();
boolean hasProps = !properties.isEmpty();
if (hasGroups) {
writeMetadata("groups", configurations, writer);
if (hasProps) {
writer.write(',');
}
}
if (hasProps) {
writeMetadata("properties", properties, writer);
}
writer.write('}');
}
}
}
use of io.micronaut.inject.writer.GeneratedFile in project micronaut-core by micronaut-projects.
the class GraalTypeElementVisitor method generateNativeImageProperties.
private void generateNativeImageProperties(VisitorContext visitorContext) {
List<Map> json;
Optional<Path> projectDir = visitorContext.getProjectDir();
File userReflectJsonFile = projectDir.map(projectPath -> Paths.get(projectPath.toString(), BASE_REFLECT_JSON).toFile()).orElse(null);
if (userReflectJsonFile != null && userReflectJsonFile.exists()) {
try {
json = MAPPER.readValue(userReflectJsonFile, new TypeReference<List<Map>>() {
});
} catch (Throwable e) {
visitorContext.fail("Error parsing base reflect.json: " + BASE_REFLECT_JSON, null);
return;
}
} else {
json = new ArrayList<>();
}
if (CollectionUtils.isEmpty(classes) && CollectionUtils.isEmpty(arrays) && CollectionUtils.isEmpty(json)) {
return;
}
try {
String path = buildNativeImagePath(visitorContext);
String reflectFile = path + REFLECTION_CONFIG_JSON;
final Optional<GeneratedFile> generatedFile = visitorContext.visitMetaInfFile(reflectFile, originatingElements.toArray(Element.EMPTY_ELEMENT_ARRAY));
generatedFile.ifPresent(gf -> {
for (Map<String, Object> value : classes.values()) {
json.add(value);
}
for (String array : arrays) {
json.add(CollectionUtils.mapOf(NAME, "[L" + array.substring(0, array.length() - 2) + ";", ALL_DECLARED_CONSTRUCTORS, true));
}
ObjectWriter writer = MAPPER.writer(new DefaultPrettyPrinter());
try (Writer w = gf.openWriter()) {
visitorContext.info("Writing " + REFLECTION_CONFIG_JSON + " file to destination: " + gf.getName());
writer.writeValue(w, json);
} catch (IOException e) {
visitorContext.fail("Error writing " + REFLECTION_CONFIG_JSON + ": " + e.getMessage(), null);
}
});
} finally {
packages.clear();
classes.clear();
arrays.clear();
originatingElements.clear();
}
}
use of io.micronaut.inject.writer.GeneratedFile in project micronaut-core by micronaut-projects.
the class VisitorContext method getProjectDir.
/**
* Obtain the project directory.
*
* @return An optional wrapping the project directory
*/
default Optional<Path> getProjectDir() {
Optional<Path> projectDir = get(MICRONAUT_PROCESSING_PROJECT_DIR, Path.class);
if (projectDir.isPresent()) {
return projectDir;
}
// let's find the projectDir
Optional<GeneratedFile> dummyFile = visitGeneratedFile("dummy");
if (dummyFile.isPresent()) {
URI uri = dummyFile.get().toURI();
// happens in tests 'mem:///CLASS_OUTPUT/dummy'
if (uri.getScheme() != null && !uri.getScheme().equals("mem")) {
// assume files are generated in 'build' or 'target' directories
Path dummy = Paths.get(uri).normalize();
while (dummy != null) {
Path dummyFileName = dummy.getFileName();
if (dummyFileName != null && ("build".equals(dummyFileName.toString()) || "target".equals(dummyFileName.toString()))) {
projectDir = Optional.ofNullable(dummy.getParent());
put(MICRONAUT_PROCESSING_PROJECT_DIR, dummy.getParent());
break;
}
dummy = dummy.getParent();
}
}
}
return projectDir;
}
Aggregations