Bug 369848 - Add possibility to browse synthetic fields and parameters with Java DOM
Summary: Add possibility to browse synthetic fields and parameters with Java DOM
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.8   Edit
Hardware: PC All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Jay Arthanareeswaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-01-26 13:23 EST by Jacek Sieka CLA
Modified: 2012-01-30 00:02 EST (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jacek Sieka CLA 2012-01-26 13:23:28 EST
Build Identifier: M20110909-1335

I'm writing a plugin that uses the AST parser from org.eclipse.jdt.core.dom, resolving my types to ITypeBinding. When using ITypeBinding.getDeclaredMethods and friends, there's no way to specify that I also want the synthetic ones (for example those generated when inheriting and implementing a generic interface).

I looked at IMethodBinding which explicitly skips synthetics and always returns false so either I have to use the full internal compiler (which the docs advice against) or guess the synthetics myself (which seems silly since they are actually there, but hidden by TypeBinding.

I also noticed that the IType returned by an ITypeBinding includes synthetic methods (but using it would be awkward at best in my case).

Reproducible: Always

Steps to Reproduce:
1. Parse a class with ASTParser
2. Find a TypeDeclaration node
3. Browse methods with getDeclaredMethods()
Comment 1 Ayushman Jain CLA 2012-01-27 00:12:22 EST
Deepak, isn't there already a way which jdt/ui uses to get the synthetic methods (viz. for outline views, etc.)?
Comment 2 Ayushman Jain CLA 2012-01-27 00:27:31 EST
Hmm, looks like we don't show synthetic methods anywhere.
Jay, can you check if returning the synthetic methods has side effects or is it safe?

Code snippet:
public interface LockEx<T> extends java.util.concurrent.locks.Lock {
  //extended lock method
  void lockEx(T t);
}

final class LockExImpl<T> extends java.util.concurrent.locks.ReentrantLock
            implements LockEx<LockExImpl>  {
  public void lockEx(LockExImpl mutex) {
    throw new UnsupportedOperationException("Not supported yet.");
  }
}

Synthetic method should be void LockExImpl#lock(Object o) {}
Comment 3 Srikanth Sankaran CLA 2012-01-27 00:30:48 EST
Olivier, do you have an opinion/recommendation for us or
for Jacek on this topic ?
Comment 4 Jacek Sieka CLA 2012-01-27 02:41:50 EST
One place where synthetic methods can be seen is when looking at class without source code. If I open java.lang.String.class, it's compareTo(Object) synthetic method is seen in the outline and the bytecode.

This request is not limited to synthetic methods though, but also other compiler trickery, such as that for inner classes (fake this$0) and local classes (closures capturing method local final variables).

The docs unhelpfully state that "Synthetic fields may or may not be included." and the same for methods etc. The IBinding class is prepared for it though with an isSynthetic method, which in the case of TypeBinding always returns false.

In my particular case, I need every method and field to be able to analyse which exact method is called and which method overrides which, and without the synthetics that becomes impossible (short of generating them myself).

I would be perfectly fine with an option to ASTParser much like you have to ask for bindings to be resolved, but it would arguably be more clean to include them from the start, specially since IBinding already supports the notion (why have it there at all otherwise - it's only confusing, I lost a lot of time trying to find a way to get the synthetics because the presence of the method suggested there would be some way of making use of it).
Comment 5 Jay Arthanareeswaran CLA 2012-01-30 00:02:42 EST
Looking at the documentation (the "may or may not be included" part), I would like to believe that the clients of these APIs should be prepared for the synthetic methods/fields in the list. And IBinding#isSynthetic() can be used and in fact _is_ used by some of the JDT/UI clients I looked at.

So, I can't imagine this having any side-effects and even in case of any such effects, we should address them, given the documentation does say that the client should expect the synthetic methods.