|
| 1 | +package com.baeldung.javac; |
| 2 | + |
| 3 | +import com.sun.source.tree.MethodTree; |
| 4 | +import com.sun.source.tree.VariableTree; |
| 5 | +import com.sun.source.util.*; |
| 6 | +import com.sun.tools.javac.api.BasicJavacTask; |
| 7 | +import com.sun.tools.javac.code.TypeTag; |
| 8 | +import com.sun.tools.javac.tree.JCTree; |
| 9 | +import com.sun.tools.javac.tree.TreeMaker; |
| 10 | +import com.sun.tools.javac.util.Context; |
| 11 | +import com.sun.tools.javac.util.Name; |
| 12 | +import com.sun.tools.javac.util.Names; |
| 13 | + |
| 14 | +import javax.tools.JavaCompiler; |
| 15 | +import java.util.*; |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | +import static com.sun.tools.javac.util.List.nil; |
| 19 | + |
| 20 | +/** |
| 21 | + * A {@link JavaCompiler javac} plugin which inserts {@code >= 0} checks into resulting {@code *.class} files |
| 22 | + * for numeric method parameters marked by {@link Positive} |
| 23 | + */ |
| 24 | +public class SampleJavacPlugin implements Plugin { |
| 25 | + |
| 26 | + public static final String NAME = "MyPlugin"; |
| 27 | + |
| 28 | + 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 | + )); |
| 33 | + |
| 34 | + @Override |
| 35 | + public String getName() { |
| 36 | + return NAME; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void init(JavacTask task, String... args) { |
| 41 | + Context context = ((BasicJavacTask) task).getContext(); |
| 42 | + task.addTaskListener(new TaskListener() { |
| 43 | + @Override |
| 44 | + public void started(TaskEvent e) { |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void finished(TaskEvent e) { |
| 49 | + if (e.getKind() != TaskEvent.Kind.PARSE) { |
| 50 | + return; |
| 51 | + } |
| 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)); |
| 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); |
| 69 | + } |
| 70 | + }, null); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + private boolean shouldInstrument(VariableTree parameter) { |
| 76 | + return TARGET_TYPES.contains(parameter.getType().toString()) |
| 77 | + && parameter.getModifiers().getAnnotations() |
| 78 | + .stream() |
| 79 | + .anyMatch(a -> Positive.class.getSimpleName().equals(a.getAnnotationType().toString())); |
| 80 | + } |
| 81 | + |
| 82 | + private void addCheck(MethodTree method, VariableTree parameter, Context context) { |
| 83 | + JCTree.JCIf check = createCheck(parameter, context); |
| 84 | + JCTree.JCBlock body = (JCTree.JCBlock) method.getBody(); |
| 85 | + body.stats = body.stats.prepend(check); |
| 86 | + } |
| 87 | + |
| 88 | + private static JCTree.JCIf createCheck(VariableTree parameter, Context context) { |
| 89 | + TreeMaker factory = TreeMaker.instance(context); |
| 90 | + Names symbolsTable = Names.instance(context); |
| 91 | + 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"; |
| 95 | + 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 | + ); |
| 123 | + } |
| 124 | +} |
0 commit comments