how to tell 2 methods are override-equivalent

Jonathan Gibbons jonathan.gibbons at oracle.com
Wed Mar 22 00:50:12 UTC 2017


Hamlin,

Note that while it is relative easy to convert a string containing a 
type name (such as "java.net.URL") to an Element and/or TypeMirror, in 
general it will not be possible to convert a string like 
"getContent(java.lang.Class[])" to a Element or TypeMirror, because the 
string as written does not contain sufficient information.

Even if you did provide more info, you would still have to be careful to 
evaluate the string in the same compilation context as that to which you 
are comparing it, for the comparison to have any chance of succeeding. 
Given your other message mentioning a doclet, you are effectively asking 
to be able to compile fragments of code (i.e. strings) in the context of 
your doclet, and that is not possible with the existing public API.

-- Jon

On 03/21/2017 02:28 PM, Jonathan Gibbons wrote:
>
> Hamlin,
>
> Use Element.asType() to move from the Element world to the TypeMirror 
> world.
> http://download.java.net/java/jdk9/docs/api/javax/lang/model/element/Element.html#asType--
>
> Use Types.asElement(TypeMirror) to go in the reverse direction, from a 
> TypeMirror to an Element.
> http://download.java.net/java/jdk9/docs/api/javax/lang/model/util/Types.html#asElement-javax.lang.model.type.TypeMirror-
> but note the reverse direction is lossy and may throw exceptions:
>     e.g. for type "int", there is no direct element equivalent; for 
> type List<String>, the element will be the declaration List<T>
>
> -- Jon
>
> On 3/16/17 8:15 PM, Hamlin Li wrote:
>>
>> Hi,
>>
>> ( I'm not sure if this is the right alias to ask the question, please 
>> help forward if necessary. )
>>
>> I have a question about telling whether one method is subsignature of 
>> another one. My situation is,
>>
>> 1. there is a TypeElement *t1 *which represents java.net.URL, and a 
>> ExecutableElement *m1* which represents 
>> getContent(java.lang.Class*<?>*[]),
>>
>> 2. there is a String *t2* which is "java.net.URL", and another String 
>> *m2 *which is "getContent(java.lang.Class[])".
>>
>> The difference between m1 and m2 is that, m1 is an instance of 
>> ExecutableElement, m2 is an instance of String; there is a wildcard 
>> type argument in m1.
>>
>>
>> Is there a way to tell whether m1 and m2 are override-equivalent, m2 
>> is subsignature of m1?
>>
>> I suppose I should use 
>> javax.lang.model.util.Types.isSubsignature(ExecutableType m1, 
>> ExecutableType m2), but I don't know how can I convert 
>> ExecutableElement *m1* and String *m2* to**ExecutableType.
>>
>> Or I'm in wrong direction?
>>
>>
>> Thank you
>>
>> -Hamlin
>>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20170321/0976b12f/attachment-0001.html>


More information about the compiler-dev mailing list