Warn when certain constructor is used

Eddie Aftandilian eaftan at google.com
Thu May 31 16:59:03 UTC 2018


Forgive the self promotion, but you could use Error Prone (
github.com/google/error-prone).  Just write a BugChecker that implements
NewClassTreeMatcher, match on the constructor you want, and emit a warning
when it matches.  Here's an example similar to what you want to do:
https://github.com/google/error-prone/blob/d6574253e5cff136662c4fd2ace6ba1c5ae831fa/core/src/main/java/com/google/errorprone/bugpatterns/StringBuilderInitWithChar.java

On Thu, May 31, 2018 at 7:45 AM Maurizio Cimadamore <
maurizio.cimadamore at oracle.com> wrote:

> Try to call
>
>
> https://docs.oracle.com/javase/7/docs/jdk/api/javac/tree/com/sun/source/util/Trees.html#getElement(com.sun.source.util.TreePath)
>
> This should trigger attribution of the tree and give you the 'symbol'
> associated with the constructor. From there you can access the symbol
> signature using
>
>
> https://docs.oracle.com/javase/7/docs/api/javax/lang/model/element/Element.html#asType()
>
> And downcasting to this:
>
>
> https://docs.oracle.com/javase/7/docs/api/javax/lang/model/type/ExecutableType.html
>
> If this works, good, otherwise the solution would be more cumbersome -
> and it will involve setting up a task listener. But try this first.
>
> Maurizio
>
>
> On 31/05/18 13:19, Pietro Paolini wrote:
> > Hi Maurizio,
> >
> > Thanks a lot. I would have an additional, and hopefully last, question.
> >
> > I am getting the NewClassTree which seems to provide me access to the -
> forgive me the coarse wording of it -  the "token", namely the textual
> representation
> > of it, while I am mainly interested in the type.
> >
> > I am getting around it by comparing strings but I wonder if there is any
> way to get to the type of the expression :
> >
> > := new identifier ( arguments )
> >
> > What I have in my mind is to detect all instances in which the
> identifier has *type* A and the argument's  list is composed by a single
> *type* , let say B. That allows me to
> > print something:
> >
> > "Hey, constructor new A(B) has been detected"
> >
> > Thanks a lot for your help so far, really.
> >
> > Thanks,
> > P.
> >
> >
> >
> >
> >
> >> -----Original Message-----
> >> From: Maurizio Cimadamore [mailto:maurizio.cimadamore at oracle.com]
> >> Sent: 31 May 2018 12:45
> >> To: Pietro Paolini
> >> Cc: compiler-dev at openjdk.java.net
> >> Subject: Re: Warn when certain constructor is used
> >>
> >>
> >>
> >> On 31/05/18 12:21, Pietro Paolini wrote:
> >>> JCTree tree = (JCTree) trees.getTree(element);
> >>> tree.accept(new LocalDateUtilDate());
> >> There re two visitor methods in JCTree, one internal (used by javac),
> >> one external (used by the API). I don't think you need to cast down to
> >> JCTree (yet, at least). Just keep it as a com.sun.source.tree.Tree, and
> >> you will see that the only 'accept' method there takes two arguments: a
> >> visitor (which you have) and a visitor parameter; since you don't seem
> >> to need a visitor parameter, you can just pass 'null' as second
> parameter.
> >>
> >> Maurizio
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20180531/062d418c/attachment.html>


More information about the compiler-dev mailing list