|
| 1 | +package org.jboss.jandex.gizmo2; |
| 2 | + |
| 3 | +import io.quarkus.gizmo2.Const; |
| 4 | +import io.quarkus.gizmo2.Expr; |
| 5 | +import io.quarkus.gizmo2.Var; |
| 6 | +import io.quarkus.gizmo2.creator.BlockCreator; |
| 7 | +import io.quarkus.gizmo2.creator.ops.ComparableOps; |
| 8 | +import io.quarkus.gizmo2.creator.ops.ObjectOps; |
| 9 | + |
| 10 | +/** |
| 11 | + * Generator of {@link StringBuilder} call chains. The expected usage pattern is: |
| 12 | + * <ol> |
| 13 | + * <li>Create an instance using {@link #ofNew(BlockCreator)}</li> |
| 14 | + * <li>Append to it using {@link #append(Expr)}</li> |
| 15 | + * <li>Create the final string using {@link #toString_()}</li> |
| 16 | + * </ol> |
| 17 | + * If you need to perform other operations on the {@code StringBuilder} |
| 18 | + * that this class doesn't provide, you should create an instance |
| 19 | + * using {@link #of(Var, BlockCreator)}, which allows passing an already |
| 20 | + * created {@code StringBuilder}. This class itself doesn't provide access |
| 21 | + * to the underlying object. |
| 22 | + */ |
| 23 | +public final class StringBuilderGen extends ObjectOps implements ComparableOps { |
| 24 | + /** |
| 25 | + * Allocates a local variable in the given block as if by {@code new StringBuilder()} |
| 26 | + * and passes it to {@link #of(Var, BlockCreator)}. |
| 27 | + * |
| 28 | + * @param bc the block in which the new {@code StringBuilder} should be created |
| 29 | + * @return the result of {@link #of(Var, BlockCreator)} called on the new {@code StringBuilder} |
| 30 | + */ |
| 31 | + public static StringBuilderGen ofNew(BlockCreator bc) { |
| 32 | + return new StringBuilderGen(bc, bc.localVar("$$stringBuilder", bc.new_(StringBuilder.class))); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Allocates a local variable in the given block as if by {@code new StringBuilder(capacity)} |
| 37 | + * and passes it to {@link #of(Var, BlockCreator)}. |
| 38 | + * |
| 39 | + * @param bc the block in which the new {@code StringBuilder} should be created |
| 40 | + * @return the result of {@link #of(Var, BlockCreator)} called on the new {@code StringBuilder} |
| 41 | + */ |
| 42 | + public static StringBuilderGen ofNew(int capacity, BlockCreator bc) { |
| 43 | + return new StringBuilderGen(bc, bc.localVar("$$stringBuilder", bc.new_(StringBuilder.class, Const.of(capacity)))); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates a {@code StringBuilder} generator that helps to generate a chain of |
| 48 | + * {@code append} calls and a final {@code toString} call. |
| 49 | + * |
| 50 | + * <pre> |
| 51 | + * StringBuilderGen str = StringBuilderGen.of(theStringBuilder, bc); |
| 52 | + * str.append("constant"); |
| 53 | + * str.append(someExpr); |
| 54 | + * Expr result = str.toString_(); |
| 55 | + * </pre> |
| 56 | + * |
| 57 | + * The {@code append} method mimics the regular {@code StringBuilder.append}, so |
| 58 | + * it accepts {@code Expr}s of all types for which {@code StringBuilder} |
| 59 | + * has an overload: |
| 60 | + * <ul> |
| 61 | + * <li>primitive types</li> |
| 62 | + * <li>{@code char[]}</li> |
| 63 | + * <li>{@code java.lang.String}</li> |
| 64 | + * <li>{@code java.lang.CharSequence}</li> |
| 65 | + * <li>{@code java.lang.Object}</li> |
| 66 | + * </ul> |
| 67 | + * |
| 68 | + * Notably, arrays except of {@code char[]} are appended using {@code Object.toString} |
| 69 | + * and if {@code Arrays.toString} should be used, it must be generated manually |
| 70 | + * (see {@link BlockCreator#arrayToString(Expr)}). |
| 71 | + * <p> |
| 72 | + * Methods for appending only a part of {@code char[]} or {@code CharSequence} are not |
| 73 | + * provided. |
| 74 | + * <p> |
| 75 | + * Note that the returned instance <em>may be reused</em> to append to the same {@code StringBuilder} |
| 76 | + * in the same {@code BlockCreator} multiple times. This allows using {@code StringBuilderGen} |
| 77 | + * in the same manner a {@code StringBuilder} would normally be used. |
| 78 | + * |
| 79 | + * @param stringBuilder the {@link StringBuilder} |
| 80 | + * @param bc the {@link BlockCreator} |
| 81 | + * @return a convenience wrapper for accessing instance methods of the given {@link StringBuilder} |
| 82 | + */ |
| 83 | + public static StringBuilderGen of(Var stringBuilder, BlockCreator bc) { |
| 84 | + return new StringBuilderGen(bc, stringBuilder); |
| 85 | + } |
| 86 | + |
| 87 | + private StringBuilderGen(final BlockCreator bc, final Var obj) { |
| 88 | + super(StringBuilder.class, bc, obj); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Appends the string value of given {@code expr} to this {@code StringBuilder}. |
| 93 | + * |
| 94 | + * @param expr the value to append |
| 95 | + * @return this instance |
| 96 | + */ |
| 97 | + public StringBuilderGen append(final Expr expr) { |
| 98 | + switch (expr.type().descriptorString()) { |
| 99 | + case "Z" -> invokeInstance(StringBuilder.class, "append", boolean.class, expr); |
| 100 | + case "B", "S", "I" -> invokeInstance(StringBuilder.class, "append", int.class, expr); |
| 101 | + case "J" -> invokeInstance(StringBuilder.class, "append", long.class, expr); |
| 102 | + case "F" -> invokeInstance(StringBuilder.class, "append", float.class, expr); |
| 103 | + case "D" -> invokeInstance(StringBuilder.class, "append", double.class, expr); |
| 104 | + case "C" -> invokeInstance(StringBuilder.class, "append", char.class, expr); |
| 105 | + case "[C" -> invokeInstance(StringBuilder.class, "append", char[].class, expr); |
| 106 | + case "Ljava/lang/String;" -> invokeInstance(StringBuilder.class, "append", String.class, expr); |
| 107 | + case "Ljava/lang/CharSequence;" -> invokeInstance(StringBuilder.class, "append", CharSequence.class, expr); |
| 108 | + default -> invokeInstance(StringBuilder.class, "append", Object.class, expr); |
| 109 | + } |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Appends the given {@code char} constant to this {@code StringBuilder}. |
| 115 | + * |
| 116 | + * @param constant the value to append |
| 117 | + * @return this instance |
| 118 | + */ |
| 119 | + public StringBuilderGen append(final char constant) { |
| 120 | + return append(Const.of(constant)); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Appends the given {@code String} constant to this {@code StringBuilder}. |
| 125 | + * |
| 126 | + * @param constant the value to append |
| 127 | + * @return this instance |
| 128 | + */ |
| 129 | + public StringBuilderGen append(final String constant) { |
| 130 | + return append(Const.of(constant)); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Appends the given code point to this {@code StringBuilder}. |
| 135 | + * |
| 136 | + * @param codePoint the value to append (must not be {@code null}) |
| 137 | + * @return this instance |
| 138 | + */ |
| 139 | + public StringBuilderGen appendCodePoint(final Expr codePoint) { |
| 140 | + invokeInstance(StringBuilder.class, "appendCodePoint", int.class, codePoint); |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Appends the given code point to this {@code StringBuilder}. |
| 146 | + * |
| 147 | + * @param codePoint the value to append |
| 148 | + * @return this instance |
| 149 | + */ |
| 150 | + public StringBuilderGen appendCodePoint(final int codePoint) { |
| 151 | + return appendCodePoint(Const.of(codePoint)); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Set the length of this {@code StringBuilder}. |
| 156 | + * |
| 157 | + * @param length the length expression (must not be {@code null}) |
| 158 | + */ |
| 159 | + public void setLength(Expr length) { |
| 160 | + invokeInstance(void.class, "setLength", int.class, length); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Set the length of this {@code StringBuilder}. |
| 165 | + * |
| 166 | + * @param length the constant length |
| 167 | + */ |
| 168 | + public void setLength(int length) { |
| 169 | + setLength(Const.of(length)); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public Expr compareTo(Expr other) { |
| 174 | + return invokeInstance(int.class, "compareTo", StringBuilder.class, other); |
| 175 | + } |
| 176 | +} |
0 commit comments