<commit_before>package ru.noties.parcelable.creator;

import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.Name;

import java.lang.reflect.Modifier;

import javax.lang.model.element.Element;
import javax.lang.model.type.TypeKind;

import ru.noties.parcelable.ASTHelper;

/**
 * Created by <NAME> on 17.10.2015.
 */
class StatementCreatorCharSequence implements StatementCreator {

    @Override
    public List<JCTree.JCStatement> createReadFromParcel(ASTHelper astHelper, Element rootElement, JCTree.JCExpression parcel, String varName, boolean isArray) {

        final TreeMaker treeMaker = astHelper.getTreeMaker();

        final JCTree.JCExpression readCharSequence = treeMaker.Apply(
                List.<JCTree.JCExpression>nil(),
                treeMaker.Select(
                        treeMaker.Select(astHelper.getType("android.text", "TextUtils"), astHelper.getName("CHAR_SEQUENCE_CREATOR")),
                        astHelper.getName("createFromParcel")
                ),
                List.of(parcel)
        );

        final JCTree.JCExpression itemExpression = treeMaker.Ident(astHelper.getName(varName));

        if (!isArray) {
            final JCTree.JCStatement readCharSequenceStatement = treeMaker.Exec(
                    treeMaker.Assign(
                            itemExpression,
                            readCharSequence
                    )
            );
            return List.of(readCharSequenceStatement);
        }

        // final int charSequenceArrayLength = source.readInt();
        // if (csa == -1) { csa = null; }
        // else {
        //    this.csa = new CharSequence[csal];
        //    for (int i = 0; i < csal; i++) {
        //        this.csa[i] = TextUtils.CHAR.createFromParcel();
        //    }
        //}

        final Name lengthName = astHelper.getName("_" + varName + "length");

        final JCTree.JCVariableDecl length = treeMaker.VarDef(
                astHelper.getModifiers(Modifier.FINAL),
                lengthName,
                astHelper.getPrimitiveType(TypeKind.INT),
                treeMaker.Apply(
                        List.<JCTree.JCExpression>nil(),
                        treeMaker.Select(parcel, astHelper.getName("readInt")),
                        List.<JCTree.JCExpression>nil()
                )
        );

        final JCTree.JCStatement minusBody = treeMaker.Exec(
                treeMaker.Assign(
                        itemExpression,
                        astHelper.getNull()
                )
        );

        final JCTree.JCStatement newCharSequenceArray = treeMaker.Exec(
                treeMaker.Assign(
                        itemExpression,
                        treeMaker.NewArray(
                                astHelper.getType("java.lang", "CharSequence"),
                                List.of((JCTree.JCExpression) treeMaker.Ident(lengthName)),
                                null
                        )
                )
        );

        final Name indexVarName = astHelper.getName("i");

        final JCTree.JCStatement indexVar = treeMaker.VarDef(
                astHelper.getModifiers(),
                indexVarName,
                astHelper.getPrimitiveType(TypeKind.INT),
                treeMaker.Literal(0)
        );

        final JCTree.JCExpressionStatement indexIncrement = treeMaker.Exec(
                treeMaker.Assign(
                        treeMaker.Ident(indexVarName),
                        astHelper.getPlus(
                                treeMaker.Ident(indexVarName),
                                treeMaker.Literal(1)
                        )
                )
        );

        final JCTree.JCStatement forLoopBody = treeMaker.Exec(
                treeMaker.Assign(
                        treeMaker.Indexed(itemExpression, treeMaker.Ident(indexVarName)),
                        readCharSequence
                )
        );

        final JCTree.JCStatement arrayForLoop = treeMaker.ForLoop(
                List.of(indexVar),
                astHelper.getLess(
                        treeMaker.Ident(astHelper.getName("i")),
                        treeMaker.Ident(lengthName)
                ),
                List.of(indexIncrement),
                forLoopBody
        );

        final JCTree.JCStatement notMinusBody = treeMaker.Block(
                0,
                List.of(newCharSequenceArray, arrayForLoop)
        );

        final JCTree.JCStatement ifMinus = treeMaker.If(
                astHelper.getEquals(treeMaker.Ident(lengthName), treeMaker.Literal(-1)),
                minusBody,
                notMinusBody
        );

        return List.of(length, ifMinus);
    }

    @Override
    public List<JCTree.JCStatement> createWriteToParcel(ASTHelper astHelper, Element rootElement, JCTree.JCExpression parcel, JCTree.JCExpression flags, String varName, boolean isArray) {
        return List.nil();
    }
}
<commit_msg>Added support for CharSequence (with array support)<commit_after>package ru.noties.parcelable.creator;

import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.Name;

import java.lang.reflect.Modifier;

import javax.lang.model.element.Element;
import javax.lang.model.type.TypeKind;

import ru.noties.parcelable.ASTHelper;

/**
 * Created by <NAME> on 17.10.2015.
 */
class StatementCreatorCharSequence implements StatementCreator {

    @Override
    public List<JCTree.JCStatement> createReadFromParcel(ASTHelper astHelper, Element rootElement, JCTree.JCExpression parcel, String varName, boolean isArray) {

        final TreeMaker treeMaker = astHelper.getTreeMaker();

        final JCTree.JCExpression readCharSequence = treeMaker.Apply(
                List.<JCTree.JCExpression>nil(),
                treeMaker.Select(
                        treeMaker.Select(astHelper.getType("android.text", "TextUtils"), astHelper.getName("CHAR_SEQUENCE_CREATOR")),
                        astHelper.getName("createFromParcel")
                ),
                List.of(parcel)
        );

        final JCTree.JCExpression itemExpression = treeMaker.Ident(astHelper.getName(varName));

        if (!isArray) {
            final JCTree.JCStatement readCharSequenceStatement = treeMaker.Exec(
                    treeMaker.Assign(
                            itemExpression,
                            readCharSequence
                    )
            );
            return List.of(readCharSequenceStatement);
        }

        // final int charSequenceArrayLength = source.readInt();
        // if (csa == -1) { csa = null; }
        // else {
        //    this.csa = new CharSequence[csal];
        //    for (int i = 0; i < csal; i++) {
        //        this.csa[i] = TextUtils.CHAR.createFromParcel();
        //    }
        //}

        final Name lengthName = astHelper.getName("_" + varName + "length");

        final JCTree.JCVariableDecl length = treeMaker.VarDef(
                astHelper.getModifiers(Modifier.FINAL),
                lengthName,
                astHelper.getPrimitiveType(TypeKind.INT),
                treeMaker.Apply(
                        List.<JCTree.JCExpression>nil(),
                        treeMaker.Select(parcel, astHelper.getName("readInt")),
                        List.<JCTree.JCExpression>nil()
                )
        );

        final JCTree.JCStatement minusBody = treeMaker.Exec(
                treeMaker.Assign(
                        itemExpression,
                        astHelper.getNull()
                )
        );

        final JCTree.JCStatement newCharSequenceArray = treeMaker.Exec(
                treeMaker.Assign(
                        itemExpression,
                        treeMaker.NewArray(
                                astHelper.getType("java.lang", "CharSequence"),
                                List.of((JCTree.JCExpression) treeMaker.Ident(lengthName)),
                                null
                        )
                )
        );

        final Name indexVarName = astHelper.getName("i");

        final JCTree.JCStatement indexVar = treeMaker.VarDef(
                astHelper.getModifiers(),
                indexVarName,
                astHelper.getPrimitiveType(TypeKind.INT),
                treeMaker.Literal(0)
        );

        final JCTree.JCExpressionStatement indexIncrement = treeMaker.Exec(
                treeMaker.Assign(
                        treeMaker.Ident(indexVarName),
                        astHelper.getPlus(
                                treeMaker.Ident(indexVarName),
                                treeMaker.Literal(1)
                        )
                )
        );

        final JCTree.JCStatement forLoopBody = treeMaker.Exec(
                treeMaker.Assign(
                        treeMaker.Indexed(itemExpression, treeMaker.Ident(indexVarName)),
                        readCharSequence
                )
        );

        final JCTree.JCStatement arrayForLoop = treeMaker.ForLoop(
                List.of(indexVar),
                astHelper.getLess(
                        treeMaker.Ident(astHelper.getName("i")),
                        treeMaker.Ident(lengthName)
                ),
                List.of(indexIncrement),
                forLoopBody
        );

        final JCTree.JCStatement notMinusBody = treeMaker.Block(
                0,
                List.of(newCharSequenceArray, arrayForLoop)
        );

        final JCTree.JCStatement ifMinus = treeMaker.If(
                astHelper.getEquals(treeMaker.Ident(lengthName), treeMaker.Literal(-1)),
                minusBody,
                notMinusBody
        );

        return List.of(length, ifMinus);
    }

    @Override
    public List<JCTree.JCStatement> createWriteToParcel(ASTHelper astHelper, Element rootElement, JCTree.JCExpression parcel, JCTree.JCExpression flags, String varName, boolean isArray) {

        final TreeMaker treeMaker = astHelper.getTreeMaker();

        final Name itemName = astHelper.getName(varName);
        final JCTree.JCExpression item = treeMaker.Ident(itemName);

        final JCTree.JCExpression textUtils = astHelper.getType("android.text", "TextUtils");

        if (!isArray) {
            final JCTree.JCStatement writeToParcel = treeMaker.Exec(
                    treeMaker.Apply(
                            List.<JCTree.JCExpression>nil(),
                            treeMaker.Select(textUtils, astHelper.getName("writeToParcel")),
                            List.of(
                                    item, parcel, flags
                            )
                    )
            );
            return List.of(writeToParcel);
        }

        // if csa == null -> write -1
        // else iterate and write

        final JCTree.JCStatement ifNull = treeMaker.Exec(
                treeMaker.Apply(
                        List.<JCTree.JCExpression>nil(),
                        treeMaker.Select(parcel, astHelper.getName("writeInt")),
                        List.of((JCTree.JCExpression) treeMaker.Literal(-1))
                )
        );

        final Name loopVariableName = astHelper.getName("_cs");

        final JCTree.JCVariableDecl loopVar = treeMaker.VarDef(
                astHelper.getModifiers(),
                loopVariableName,
                astHelper.getType("java.lang", "CharSequence"),
                null
        );

        final JCTree.JCStatement forLoop = treeMaker.ForeachLoop(
                loopVar,
                item,
                treeMaker.Exec(
                        treeMaker.Apply(
                                List.<JCTree.JCExpression>nil(),
                                treeMaker.Select(textUtils, astHelper.getName("writeToParcel")),
                                List.of(
                                        treeMaker.Ident(loopVariableName), parcel, flags
                                )
                        )
                )
        );

        final JCTree.JCStatement ifNotNull = treeMaker.Block(
                0,
                List.of(forLoop)
        );

        final JCTree.JCStatement ifBlock = treeMaker.If(
                astHelper.getEquals(item, astHelper.getNull()),
                ifNull,
                ifNotNull
        );

        return List.of(ifBlock);
    }
}

{'fraction_non_alphanumeric': 0.08981126039667306, 'fraction_numerical': 0.002479206653870761, 'mean_word_length': 1.4752573238321456, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6820313', 'n_tokens_mistral': 3427, 'n_tokens_neox': 3180, 'n_words': 506}