Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 274349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:35:47+00:00 2026-05-12T00:35:47+00:00

I am writing some tools for our build system to enforce some strict calling

  • 0

I am writing some tools for our build system to enforce some strict calling conventions on methods belonging to classes containing certain annotations.

I’m using the Compiler Tree API…

What i’m wondering is when traversing the ‘tree’, how can you tell the type of class/interface for a MethodInvocation.

I’m subclassing TreePathScanner with :

@Override
public Object visitMethodInvocation(MethodInvocationTree node, Trees trees) {

}

I’m hoping theres a way to tell the type of the class(or interface) that you’re trying to invoke the method on. Am I going about this the wrong way? Thanks for any ideas…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:35:47+00:00Added an answer on May 12, 2026 at 12:35 am

    There are a couple of issues here. You can either be interested in
    knowing the Java type of the method invocation receiver or just
    knowing the class on the method is invoked. Java information is more
    informative as it gives you generic types as well, e.g. List<String>
    while Elements would only provide you with the class, e.g. List<E>.

    Getting the Element

    To get the Element of the class the method is invoked on, you can do
    the following:

    
      MethodInvocationTree node = ...;
      Element method =
            TreeInfo.symbol((JCTree)node.getMethodSelect());
      TypeElement invokedClass = (TypeElement)method.getEnclosingElement();
    

    Corner cases:

    1.
    invokedClass might be a superclass of the receiver type. So running
    the snippet on new ArrayList<String>.equals(null) would return
    AbstractList rather than ArrayList, since equals() is implemented
    in AbstractList not ArrayList.

    2.
    When handling array invocations, e.g. new int[].clone(), you would
    get TypeElement of class Array.

    Getting the actual type

    To get the type, there is no direct way for determining it what the
    receiver type is. There is some complexity in handling method invocations
    within inner classes where the receiver is not given explicitly
    (e.g. unlike OuterClass.this.toString()). Here is a sample implementation:

    
      MethodInvocationTree node = ...;
      TypeMirror receiver;
      if (methodSel.getKind() == Tree.Kind.MEMBER_SELECT) {
        ExpressionTree receiver = ((MemberSelectTree)methodSel).getExpression();
        receiverType = ((JCTree)receiver).type;
      } else if (methodSel.getKind() == Tree.Kind.IDENTIFIER) {
        // need to resolve implicit this, which is described in
        //  JLS3 15.12.1 and 15.9.2
    
        // A bit too much work that I don't want to work on now
        // Look at source code of
        //   Attr.visitApply(JCMethodInvocation)
        //   resolveImplicitThis(DiagnosticPosition, Env, Type)
      } else
        throw new AssertionError("Unexpected type: " + methodSel.getKind());
    

    Note:

    The receiver type needs to be TypeMirror not DeclaredType
    unfortunately. When calling new int[5].clone(), receiver would be
    an ArrayType of int[], which is more informative than the previous
    method.

    Getting it to run

    Both of the previous methods require the compiler to resolve the type
    information for the classes. In usual circumstances, the compiler
    only resolve the types for method declarations but not the bodies.
    Hence, the methods described earlier would return null instead.

    To have the compiler resolve the type information, you can do one of
    the following ways:

    1.
    Use AbstractTypeProcessor class that just got added to the
    compiler repository for JDK 7. Check out the work on JSR
    308
    and their compiler.
    While the work is mainly on annotated types, it might be useful for.
    The compiler allows you to use the provided class in a backward
    compatible manner with Java 5.

    This approach allows you to write processors that get invoked just
    like your current processors.

    2.
    Use JavacTask instead and call JavacTask.analyze(). Look at
    the main method of this javac test to see how
    to invoke your visitor on the classes.

    This approach makes your processor look more like an analysis tool
    rather than a plug-in to the compiler, as you would need to invoke it
    directly rather than have it be a regular process.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 249k
  • Answers 249k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use this method on your MKMapView object: - (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(UIView… May 13, 2026 at 9:07 am
  • Editorial Team
    Editorial Team added an answer This is quite easy in Solr.. All you need to… May 13, 2026 at 9:07 am
  • Editorial Team
    Editorial Team added an answer The version of Indy 10 that shipped with D2010 is… May 13, 2026 at 9:07 am

Related Questions

First a bit about the environment: We use a program called Clearview to manage
I am not sure how to phrase a good question, so I will just
We maintain a large number of .NET Project and Solution files by hand; it's
I currently work with an Oracle database and we use stored procedures for all

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.