net.jbock.context.ContextUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jbock-compiler Show documentation
Show all versions of jbock-compiler Show documentation
jbock annotation processor
package net.jbock.context;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.ParameterSpec;
import net.jbock.util.ErrTokenType;
import net.jbock.util.ExToken;
import javax.inject.Inject;
import java.util.List;
@ContextScope
public class ContextUtil {
@Inject
ContextUtil() {
}
CodeBlock joinByNewline(List code) {
boolean indent = false;
CodeBlock.Builder result = CodeBlock.builder();
for (int i = 0; i < code.size(); i++) {
if (i == 0) {
result.add(code.get(i));
} else if (i == 1) {
result.add("\n").indent().add(code.get(i));
indent = true;
} else {
result.add("\n").add(code.get(i));
}
}
if (indent) {
result.unindent();
}
return result.build();
}
CodeBlock joinByComma(List code) {
CodeBlock.Builder args = CodeBlock.builder();
for (int i = 0; i < code.size(); i++) {
if (i != 0) {
args.add(",$W");
}
args.add(code.get(i));
}
return args.build();
}
CodeBlock throwRepetitionErrorStatement(ParameterSpec token) {
return CodeBlock.of("throw new $T($T.$L, $N)", ExToken.class, ErrTokenType.class,
ErrTokenType.OPTION_REPETITION, token);
}
}