Skip to content

Revisit current transformers for cachebility #778

Closed
@musketyr

Description

@musketyr

Shadow Version

7.1.2

Gradle Version

7.4.1

Expected Behavior

Every transformer which is cacheable is marked with @CacheableTransformer.

Actual Behavior

Some of the transformers are missing the annotation but it's not really clear to me if it's really because they are not cacheable or because their authors have forgotten to add the annotation.

It would be also nice to add more example when the transformer is cacheable and when it is not.

I'm especially interested into Log4j2PluginsCacheFileTransformer and my own transformer to join application.yml files.

public class ApplicationYamlTransformer implements Transformer {

    private static final String FILE_NAME = "application.yml";
    private static final String DOCUMENT_SEPARATOR = "---";

    private final List<String> parts = new ArrayList<>();

    @Override
    public boolean canTransformResource(FileTreeElement element) {
        return element.getName().equals(FILE_NAME);
    }

    @Override
    public void transform(TransformerContext context) {
        parts.add(toString(context.getIs()));
    }

    @Override
    public boolean hasTransformedResource() {
        return !parts.isEmpty();
    }

    @Override
    public void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
        try {
            os.putNextEntry(new ZipEntry(FILE_NAME));
            IOUtil.copy(new ByteArrayInputStream(getContent().getBytes(StandardCharsets.UTF_8)), os);
            os.closeEntry();
        } catch (IOException e) {
            throw new IllegalStateException("Cannot write application.yml", e);
        }
    }

    @Override
    public String getName() {
        return getClass().getSimpleName();
    }

    private String toString(InputStream is) {
        String text = new BufferedReader(
            new InputStreamReader(is, StandardCharsets.UTF_8))
            .lines()
            .collect(Collectors.joining("\n"));

        try {
            is.close();
        } catch (IOException e) {
            throw new IllegalStateException("Cannot close input stream", e);
        }

        return text;
    }

    private String getContent() {
        return parts.stream().map(part -> {
           if (!part.startsWith(DOCUMENT_SEPARATOR)) {
               return String.format("%s\n%s", DOCUMENT_SEPARATOR, part);
           }
           return part;
        }).collect(Collectors.joining(String.format("%n")));
    }
}

Myself, I'm not sure if the transformer can be annotated with @CacheableTransformer or not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions