Search in sources :

Example 1 with GeneratedFile

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('}');
        }
    }
}
Also used : GeneratedFile(io.micronaut.inject.writer.GeneratedFile) Writer(java.io.Writer)

Example 2 with GeneratedFile

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();
    }
}
Also used : Path(java.nio.file.Path) java.util(java.util) Creator(io.micronaut.core.annotation.Creator) Function(java.util.function.Function) BeanImportVisitor(io.micronaut.context.visitor.BeanImportVisitor) TypeElementVisitor(io.micronaut.inject.visitor.TypeElementVisitor) Import(io.micronaut.context.annotation.Import) DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) ReflectiveAccess(io.micronaut.core.annotation.ReflectiveAccess) TypeReference(com.fasterxml.jackson.core.type.TypeReference) NameUtils(io.micronaut.core.naming.NameUtils) TypeHint(io.micronaut.core.annotation.TypeHint) Path(java.nio.file.Path) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) Bean(io.micronaut.context.annotation.Bean) NonNull(io.micronaut.core.annotation.NonNull) StringUtils(io.micronaut.core.util.StringUtils) io.micronaut.inject.ast(io.micronaut.inject.ast) VisitorContext(io.micronaut.inject.visitor.VisitorContext) GeneratedFile(io.micronaut.inject.writer.GeneratedFile) AnnotationUtil(io.micronaut.core.annotation.AnnotationUtil) Paths(java.nio.file.Paths) CollectionUtils(io.micronaut.core.util.CollectionUtils) Writer(java.io.Writer) Inject(jakarta.inject.Inject) DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) IOException(java.io.IOException) GeneratedFile(io.micronaut.inject.writer.GeneratedFile) TypeReference(com.fasterxml.jackson.core.type.TypeReference) File(java.io.File) GeneratedFile(io.micronaut.inject.writer.GeneratedFile) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Writer(java.io.Writer)

Example 3 with GeneratedFile

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;
}
Also used : Path(java.nio.file.Path) URI(java.net.URI) GeneratedFile(io.micronaut.inject.writer.GeneratedFile)

Aggregations

GeneratedFile (io.micronaut.inject.writer.GeneratedFile)3 Writer (java.io.Writer)2 Path (java.nio.file.Path)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 Bean (io.micronaut.context.annotation.Bean)1 Import (io.micronaut.context.annotation.Import)1 BeanImportVisitor (io.micronaut.context.visitor.BeanImportVisitor)1 AnnotationUtil (io.micronaut.core.annotation.AnnotationUtil)1 Creator (io.micronaut.core.annotation.Creator)1 NonNull (io.micronaut.core.annotation.NonNull)1 ReflectiveAccess (io.micronaut.core.annotation.ReflectiveAccess)1 TypeHint (io.micronaut.core.annotation.TypeHint)1 NameUtils (io.micronaut.core.naming.NameUtils)1 CollectionUtils (io.micronaut.core.util.CollectionUtils)1 StringUtils (io.micronaut.core.util.StringUtils)1 io.micronaut.inject.ast (io.micronaut.inject.ast)1 TypeElementVisitor (io.micronaut.inject.visitor.TypeElementVisitor)1