Skip to content

Commit 8e50444

Browse files
committed
smaller methods
1 parent b383d83 commit 8e50444

1 file changed

Lines changed: 54 additions & 55 deletions

File tree

‎core-java/src/main/java/com/baeldung/javac/SampleJavacPlugin.java‎

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ public class SampleJavacPlugin implements Plugin {
2626
public static final String NAME = "MyPlugin";
2727

2828
private static Set<String> TARGET_TYPES = new HashSet<>(Arrays.asList(
29-
// Use only primitive types for simplicity
30-
byte.class.getName(), short.class.getName(), char.class.getName(), int.class.getName(),
31-
long.class.getName(), float.class.getName(), double.class.getName()
32-
));
29+
// Use only primitive types for simplicity
30+
byte.class.getName(), short.class.getName(), char.class.getName(),
31+
int.class.getName(), long.class.getName(), float.class.getName(), double.class.getName()));
3332

3433
@Override
3534
public String getName() {
@@ -49,34 +48,35 @@ public void finished(TaskEvent e) {
4948
if (e.getKind() != TaskEvent.Kind.PARSE) {
5049
return;
5150
}
52-
e.getCompilationUnit().accept(new TreeScanner<Void, Void>() {
53-
@Override
54-
public Void visitMethod(MethodTree method, Void v) {
55-
List<VariableTree> parametersToInstrument = method.getParameters()
56-
.stream()
57-
.filter(SampleJavacPlugin.this::shouldInstrument)
58-
.collect(Collectors.toList());
59-
if (!parametersToInstrument.isEmpty()) {
60-
// There is a possible case that more than one argument is marked by @Positive,
61-
// as the checks are added to the method's body beginning, we process parameters RTL
62-
// to ensure correct order.
63-
Collections.reverse(parametersToInstrument);
64-
parametersToInstrument.forEach(p -> addCheck(method, p, context));
51+
e.getCompilationUnit()
52+
.accept(new TreeScanner<Void, Void>() {
53+
@Override
54+
public Void visitMethod(MethodTree method, Void v) {
55+
List<VariableTree> parametersToInstrument = method.getParameters()
56+
.stream()
57+
.filter(SampleJavacPlugin.this::shouldInstrument)
58+
.collect(Collectors.toList());
59+
if (!parametersToInstrument.isEmpty()) {
60+
// There is a possible case that more than one argument is marked by @Positive,
61+
// as the checks are added to the method's body beginning, we process parameters RTL
62+
// to ensure correct order.
63+
Collections.reverse(parametersToInstrument);
64+
parametersToInstrument.forEach(p -> addCheck(method, p, context));
65+
}
66+
// There is a possible case that there is a nested class declared in a method's body,
67+
// hence, we want to proceed with method body AST as well.
68+
return super.visitMethod(method, v);
6569
}
66-
// There is a possible case that there is a nested class declared in a method's body,
67-
// hence, we want to proceed with method body AST as well.
68-
return super.visitMethod(method, v);
69-
}
70-
}, null);
70+
}, null);
7171
}
7272
});
7373
}
7474

7575
private boolean shouldInstrument(VariableTree parameter) {
7676
return TARGET_TYPES.contains(parameter.getType().toString())
77-
&& parameter.getModifiers().getAnnotations()
78-
.stream()
79-
.anyMatch(a -> Positive.class.getSimpleName().equals(a.getAnnotationType().toString()));
77+
&& parameter.getModifiers().getAnnotations()
78+
.stream()
79+
.anyMatch(a -> Positive.class.getSimpleName().equals(a.getAnnotationType().toString()));
8080
}
8181

8282
private void addCheck(MethodTree method, VariableTree parameter, Context context) {
@@ -88,37 +88,36 @@ private void addCheck(MethodTree method, VariableTree parameter, Context context
8888
private static JCTree.JCIf createCheck(VariableTree parameter, Context context) {
8989
TreeMaker factory = TreeMaker.instance(context);
9090
Names symbolsTable = Names.instance(context);
91+
92+
return factory.at(((JCTree) parameter).pos)
93+
.If(factory.Parens(createIfCondition(factory, symbolsTable, parameter)),
94+
createIfBlock(factory, symbolsTable, parameter),
95+
null);
96+
}
97+
98+
private static JCTree.JCBinary createIfCondition(TreeMaker factory, Names symbolsTable, VariableTree parameter) {
99+
Name parameterId = symbolsTable.fromString(parameter.getName().toString());
100+
return factory.Binary(JCTree.Tag.LE,
101+
factory.Ident(parameterId),
102+
factory.Literal(TypeTag.INT, 0));
103+
}
104+
105+
private static JCTree.JCBlock createIfBlock(TreeMaker factory, Names symbolsTable, VariableTree parameter) {
91106
String parameterName = parameter.getName().toString();
92-
String errorMessagePrefix = String.format("Argument '%s' of type %s is marked by @%s but got '",
93-
parameterName, parameter.getType(), Positive.class.getSimpleName());
94-
String errorMessageSuffix = "' for it";
95107
Name parameterId = symbolsTable.fromString(parameterName);
96-
return factory.at(((JCTree) parameter).pos).If(
97-
factory.Parens(
98-
factory.Binary(
99-
JCTree.Tag.LE,
100-
factory.Ident(parameterId),
101-
factory.Literal(TypeTag.INT, 0))
102-
),
103-
factory.Block(0, com.sun.tools.javac.util.List.of(
104-
factory.Throw(
105-
factory.NewClass(
106-
null,
107-
nil(),
108-
factory.Ident(
109-
symbolsTable.fromString(IllegalArgumentException.class.getSimpleName())
110-
),
111-
com.sun.tools.javac.util.List.of(
112-
factory.Binary(JCTree.Tag.PLUS,
113-
factory.Binary(JCTree.Tag.PLUS,
114-
factory.Literal(TypeTag.CLASS, errorMessagePrefix),
115-
factory.Ident(parameterId)),
116-
factory.Literal(TypeTag.CLASS, errorMessageSuffix))),
117-
null
118-
)
119-
)
120-
)),
121-
null
122-
);
108+
109+
String errorMessagePrefix = String.format("Argument '%s' of type %s is marked by @%s but got '",
110+
parameterName, parameter.getType(), Positive.class.getSimpleName());
111+
String errorMessageSuffix = "' for it";
112+
113+
return factory.Block(0, com.sun.tools.javac.util.List.of(
114+
factory.Throw(
115+
factory.NewClass(null, nil(),
116+
factory.Ident(symbolsTable.fromString(IllegalArgumentException.class.getSimpleName())),
117+
com.sun.tools.javac.util.List.of(factory.Binary(JCTree.Tag.PLUS,
118+
factory.Binary(JCTree.Tag.PLUS, factory.Literal(TypeTag.CLASS, errorMessagePrefix),
119+
factory.Ident(parameterId)),
120+
factory.Literal(TypeTag.CLASS, errorMessageSuffix))), null))));
123121
}
122+
124123
}

0 commit comments

Comments
 (0)