smaller methods
This commit is contained in:
parent
b383d83bf4
commit
8e50444ba6
|
@ -27,9 +27,8 @@ public class SampleJavacPlugin implements Plugin {
|
||||||
|
|
||||||
private static Set<String> TARGET_TYPES = new HashSet<>(Arrays.asList(
|
private static Set<String> TARGET_TYPES = new HashSet<>(Arrays.asList(
|
||||||
// Use only primitive types for simplicity
|
// Use only primitive types for simplicity
|
||||||
byte.class.getName(), short.class.getName(), char.class.getName(), int.class.getName(),
|
byte.class.getName(), short.class.getName(), char.class.getName(),
|
||||||
long.class.getName(), float.class.getName(), double.class.getName()
|
int.class.getName(), long.class.getName(), float.class.getName(), double.class.getName()));
|
||||||
));
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
@ -49,7 +48,8 @@ public class SampleJavacPlugin implements Plugin {
|
||||||
if (e.getKind() != TaskEvent.Kind.PARSE) {
|
if (e.getKind() != TaskEvent.Kind.PARSE) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
e.getCompilationUnit().accept(new TreeScanner<Void, Void>() {
|
e.getCompilationUnit()
|
||||||
|
.accept(new TreeScanner<Void, Void>() {
|
||||||
@Override
|
@Override
|
||||||
public Void visitMethod(MethodTree method, Void v) {
|
public Void visitMethod(MethodTree method, Void v) {
|
||||||
List<VariableTree> parametersToInstrument = method.getParameters()
|
List<VariableTree> parametersToInstrument = method.getParameters()
|
||||||
|
@ -88,37 +88,36 @@ public class SampleJavacPlugin implements Plugin {
|
||||||
private static JCTree.JCIf createCheck(VariableTree parameter, Context context) {
|
private static JCTree.JCIf createCheck(VariableTree parameter, Context context) {
|
||||||
TreeMaker factory = TreeMaker.instance(context);
|
TreeMaker factory = TreeMaker.instance(context);
|
||||||
Names symbolsTable = Names.instance(context);
|
Names symbolsTable = Names.instance(context);
|
||||||
|
|
||||||
|
return factory.at(((JCTree) parameter).pos)
|
||||||
|
.If(factory.Parens(createIfCondition(factory, symbolsTable, parameter)),
|
||||||
|
createIfBlock(factory, symbolsTable, parameter),
|
||||||
|
null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static JCTree.JCBinary createIfCondition(TreeMaker factory, Names symbolsTable, VariableTree parameter) {
|
||||||
|
Name parameterId = symbolsTable.fromString(parameter.getName().toString());
|
||||||
|
return factory.Binary(JCTree.Tag.LE,
|
||||||
|
factory.Ident(parameterId),
|
||||||
|
factory.Literal(TypeTag.INT, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static JCTree.JCBlock createIfBlock(TreeMaker factory, Names symbolsTable, VariableTree parameter) {
|
||||||
String parameterName = parameter.getName().toString();
|
String parameterName = parameter.getName().toString();
|
||||||
|
Name parameterId = symbolsTable.fromString(parameterName);
|
||||||
|
|
||||||
String errorMessagePrefix = String.format("Argument '%s' of type %s is marked by @%s but got '",
|
String errorMessagePrefix = String.format("Argument '%s' of type %s is marked by @%s but got '",
|
||||||
parameterName, parameter.getType(), Positive.class.getSimpleName());
|
parameterName, parameter.getType(), Positive.class.getSimpleName());
|
||||||
String errorMessageSuffix = "' for it";
|
String errorMessageSuffix = "' for it";
|
||||||
Name parameterId = symbolsTable.fromString(parameterName);
|
|
||||||
return factory.at(((JCTree) parameter).pos).If(
|
return factory.Block(0, com.sun.tools.javac.util.List.of(
|
||||||
factory.Parens(
|
|
||||||
factory.Binary(
|
|
||||||
JCTree.Tag.LE,
|
|
||||||
factory.Ident(parameterId),
|
|
||||||
factory.Literal(TypeTag.INT, 0))
|
|
||||||
),
|
|
||||||
factory.Block(0, com.sun.tools.javac.util.List.of(
|
|
||||||
factory.Throw(
|
factory.Throw(
|
||||||
factory.NewClass(
|
factory.NewClass(null, nil(),
|
||||||
null,
|
factory.Ident(symbolsTable.fromString(IllegalArgumentException.class.getSimpleName())),
|
||||||
nil(),
|
com.sun.tools.javac.util.List.of(factory.Binary(JCTree.Tag.PLUS,
|
||||||
factory.Ident(
|
factory.Binary(JCTree.Tag.PLUS, factory.Literal(TypeTag.CLASS, errorMessagePrefix),
|
||||||
symbolsTable.fromString(IllegalArgumentException.class.getSimpleName())
|
|
||||||
),
|
|
||||||
com.sun.tools.javac.util.List.of(
|
|
||||||
factory.Binary(JCTree.Tag.PLUS,
|
|
||||||
factory.Binary(JCTree.Tag.PLUS,
|
|
||||||
factory.Literal(TypeTag.CLASS, errorMessagePrefix),
|
|
||||||
factory.Ident(parameterId)),
|
factory.Ident(parameterId)),
|
||||||
factory.Literal(TypeTag.CLASS, errorMessageSuffix))),
|
factory.Literal(TypeTag.CLASS, errorMessageSuffix))), null))));
|
||||||
null
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue