RefactorInsight-master/java-impl/src/main/java/org/jetbrains/research/refactorinsight/data/variables/SplitVariableJavaHandler.java
package org.jetbrains.research.refactorinsight.data.variables;

import gr.uom.java.xmi.decomposition.VariableDeclaration;
import gr.uom.java.xmi.diff.SplitVariableRefactoring;
import org.jetbrains.research.refactorinsight.data.Group;
import org.jetbrains.research.refactorinsight.data.JavaRefactoringHandler;
import org.jetbrains.research.refactorinsight.data.RefactoringInfo;
import org.refactoringminer.api.Refactoring;

import java.util.stream.Collectors;

import static org.jetbrains.research.refactorinsight.data.util.JavaUtils.calculateSignatureForVariableDeclarationContainer;
import static org.jetbrains.research.refactorinsight.data.util.JavaUtils.createCodeRangeFromJava;

public class SplitVariableJavaHandler extends JavaRefactoringHandler {

    @Override
    public RefactoringInfo specify(Refactoring refactoring, RefactoringInfo info) {
        SplitVariableRefactoring ref = (SplitVariableRefactoring) refactoring;

        ref.getSplitVariables().forEach(var ->
                info.addMarking(createCodeRangeFromJava(ref.getOldVariable().codeRange()),
                        createCodeRangeFromJava(var.codeRange()), true));

        if (ref.getOldVariable().isParameter()) {
            info.setGroup(Group.METHOD)
                    .setDetailsBefore(ref.getOperationBefore().getClassName())
                    .setDetailsAfter(ref.getOperationAfter().getClassName());
        } else {
            info.setGroup(Group.VARIABLE);
        }

        return info
                .setNameBefore(calculateSignatureForVariableDeclarationContainer(ref.getOperationBefore()))
                .setNameAfter(calculateSignatureForVariableDeclarationContainer(ref.getOperationAfter()))
                .setElementBefore(ref.getOldVariable().getVariableDeclaration().toQualifiedString())
                .setElementAfter(ref.getSplitVariables().stream()
                        .map(VariableDeclaration::getVariableName)
                        .collect(Collectors.joining()));
    }

}
