Warn when certain constructor is used (original) (raw)
Pietro Paolini Pietro.Paolini at alfasystems.com
Thu May 31 11:21:56 UTC 2018
- Previous message: Warn when certain constructor is used
- Next message: Warn when certain constructor is used
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi Maurizio,
I got stuck but I feel I am not that far from it, I have taken bits and pieces from here http://scg.unibe.ch/archive/projects/Erni08b.pdf - that is somewhat different from What I want to do, namely it wants to re-write part of the AST while I just want to scan it to print some diagnostic info.
I realize my usages of the term AST may be improper, is that the actual AST the parser generated ?
@SupportedAnnotationTypes("*") class LocalDateUtilDate extends TreeScanner {
@Override
public Object visitNewClass(NewClassTree newClassTree, Object o) {
processingEnvironment.getMessager().printMessage(Diagnostic.Kind.WARNING, "You should not be using ...");
return super.visitNewClass(newClassTree, o);
}
}
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (!roundEnv.processingOver()) {
Set<? extends Element> elements = roundEnv.getRootElements();
for (Element element : elements) {
if (element.getKind() == ElementKind.CLASS) {
JCTree tree = (JCTree) trees.getTree(element);
tree.accept(new LocalDateUtilDate());
}
System.out.println(element);
}
}
return false;
}
JCTree tree = (JCTree) trees.getTree(element); tree.accept(new LocalDateUtilDate());
This bit does not compile as I am probably using the API badly, what do I need to do to get my scanner "scanning" :-) ?
Thanks, P.
-----Original Message----- From: Maurizio Cimadamore [mailto:maurizio.cimadamore at oracle.com] Sent: 31 May 2018 11:32 To: Pietro Paolini; compiler-dev at openjdk.java.net Subject: Re: Warn when certain constructor is used
Hi Pietro, I believe the compiler tree API is what you want (I'm giving you JDK 7 API links, since your link seems to be referring to JDK 7, but these API points are available in later releases as well, of course): https://docs.oracle.com/javase/7/docs/jdk/api/javac/tree/index.html More specifically, if you are writing an annotation processor, the compiler tree API allows you to retrieve a Tree objects for a given Element: https://docs.oracle.com/javase/7/docs/jdk/api/javac/tree/com/sun/source/ut il/Trees.html After which, you can define a visitor which detects the constructor calls you are interested in. For the visitor look here: https://docs.oracle.com/javase/7/docs/jdk/api/javac/tree/com/sun/source/ut il/TreeScanner.html there's a scan method for every tree node, I think you want to override visitNewClass: https://docs.oracle.com/javase/7/docs/jdk/api/javac/tree/com/sun/source/ut il/TreeScanner.html#visitNewClass(com.sun.source.tree.NewClassTree,%20P) As for generating diagnostics, you can do that by using the Messager interface which is part of the annotation processing API: https://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/Mes sager.html you obtain one of these from the ProcessingEnvironment object you get at the start of anno processing - see https://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/Proc essor.html#init(javax.annotation.processing.ProcessingEnvironment)
I think these pointers should be enough to get started, feel free to reach out if you get stuck. Maurizio On 31/05/18 10:05, Pietro Paolini wrote: > Hi all, > > I am not sure the question is appropriate for compiler-dev but I would like to give it a try anyway. > > I would like to detect when a certain class's constructor is used and log its usage along with filename and line number somewhere, such as a log file or standard output, I can't use an IDE to do that as I am working on a fairly large codebase and I need to automate the process, moreover it seems like an interesting problem to solve. > > https://docs.oracle.com/javase/7/docs/api/javax/tools/JavaCompiler.html > > I was hoping that could help me but I haven't spot anything as yet, even though I must admit that my knowledge on the matter is limited. > > Thanks, > Pietro > > > Pietro Paolini > Consultant > > Alfa _> _________________ > e: pietro.paolini at alfasystems.com | w: alfasystems.com<https://www.alfasystems.com> > t: +44 (0) 20 7920-2643 | Moor Place, 1 Fore Street Avenue, London, EC2Y 9DT, GB _> _________________ > > The contents of this communication are not intended to be binding or constitute any form of offer or acceptance or give rise to any legal obligations on behalf of the sender or Alfa. The views or opinions expressed represent those of the author and not necessarily those of Alfa. This email and any attachments are strictly confidential and are intended solely for use by the individual or entity to whom it is addressed. If you are not the addressee (or responsible for delivery of the message to the addressee) you may not copy, forward, disclose or use any part of the message or its attachments. At present the integrity of email across the internet cannot be guaranteed and messages sent via this medium are potentially at risk. All liability is excluded to the extent permitted by law for any claims arising as a result of the use of this medium to transmit information by or to Alfa or its affiliates. > > Alfa Financial Software Ltd > Reg. in England No: 0248 2325
- Previous message: Warn when certain constructor is used
- Next message: Warn when certain constructor is used
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]