All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.mictaege.jitter.plugin.ForkMethodProcessor Maven / Gradle / Ivy

Go to download

The jitter-plugin is a Gradle plugin to build and distribute different flavours of an application from a single source base.

There is a newer version: 2023.3
Show newest version
package com.github.mictaege.jitter.plugin;

import com.github.mictaege.jitter.api.Fork;
import spoon.processing.AbstractAnnotationProcessor;
import spoon.reflect.declaration.CtMethod;

import java.util.Optional;

import static com.github.mictaege.jitter.plugin.JitterUtil.active;
import static com.github.mictaege.jitter.plugin.JitterUtil.anyVariant;
import static com.github.mictaege.jitter.plugin.JitterUtil.log;

public class ForkMethodProcessor extends AbstractAnnotationProcessor> {

    @Override
    public void process(final Fork annotation, final CtMethod method) {
        final String flavour = annotation.ifActive();
        if (anyVariant() && active(flavour)) {
            final String altName = annotation.to();
            final Optional> altMethod = method.getDeclaringType().getMethodsByName(altName).stream().findFirst();
            if (altMethod.isPresent()) {
                log().info("Replace method " + method.getDeclaringType().getSimpleName() + "#" + method.getSimpleName() + " with #" + altName);
                method.setBody(altMethod.get().getBody());
                altMethod.get().delete();
            } else {
                log().error("The given alternative method " + altName + " could not be found");
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy