- 
                Notifications
    You must be signed in to change notification settings 
- Fork 6.1k
8174840: Elements.overrides does not check the return type of the methods #22244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Closed
      
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            52 changes: 52 additions & 0 deletions
          
          52 
        
  test/langtools/tools/javac/processing/model/util/elements/overrides/S.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
|  | ||
| class S { | ||
|  | ||
| public void m() { } | ||
| } | ||
|  | ||
| class T1 extends S { | ||
|  | ||
| protected void m() { } | ||
| } | ||
|  | ||
| class T2 extends S { | ||
|  | ||
| void m() { } | ||
| } | ||
|  | ||
| class T3 extends S { | ||
|  | ||
| private void m() { } | ||
| } | ||
|  | ||
| class T4 extends S { | ||
|  | ||
| public int m() { return 0; } | ||
| } | ||
|  | ||
| class T5 extends S { | ||
|  | ||
| public void m() throws Exception { } | ||
| } | 
        
          
          
            68 changes: 68 additions & 0 deletions
          
          68 
        
  test/langtools/tools/javac/processing/model/util/elements/overrides/TestOverrides.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
|  | ||
| /* | ||
| * @test | ||
| * @bug 8174840 | ||
| * @library /tools/javac/lib | ||
| * @build JavacTestingAbstractProcessor TestOverrides | ||
| * @compile -processor TestOverrides -proc:only S.java | ||
| */ | ||
|  | ||
| import java.util.Set; | ||
| import javax.annotation.processing.RoundEnvironment; | ||
| import javax.lang.model.element.ExecutableElement; | ||
| import javax.lang.model.element.TypeElement; | ||
|  | ||
| import static javax.lang.model.element.ElementKind.METHOD; | ||
|  | ||
| /* | ||
| * This test models a few cases where Elements.overrides produces a false | ||
| * positive which warrants @apiNote. | ||
| */ | ||
| public class TestOverrides extends JavacTestingAbstractProcessor { | ||
|  | ||
| @Override | ||
| public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment round) { | ||
| if (!round.processingOver()) { | ||
| var sm = mIn(elements.getTypeElement("S")); | ||
| for (var subtypeName : new String[]{"T1", "T2", "T3", "T4", "T5"}) { | ||
| var t = elements.getTypeElement(subtypeName); | ||
| var tm = mIn(t); | ||
| if (!elements.overrides(tm, sm, t)) | ||
| messager.printError(String.format( | ||
| "%s does not override %s from %s", tm, sm, t.getQualifiedName())); | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|  | ||
| private ExecutableElement mIn(TypeElement t) { | ||
| return t.getEnclosedElements().stream() | ||
| .filter(e -> e.getKind() == METHOD) | ||
| .filter(e -> e.getSimpleName().toString().equals("m")) | ||
| .map(e -> (ExecutableElement) e) | ||
| .findAny() | ||
| .get(); | ||
| } | ||
| } | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The gist of this paragraph is good; some refinement is needed to not imply any particular requirements on the implementation, something like :"An implementation [of annotation processing] may choose to not check the additional requirements [under some limited conditions]".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please correct me if I am wrong, but:
m_Coverridesm_A(when viewed fromC)", and condition is met a compile-time error occurs. I.e. the fact that there e.g. an incorrect throws clause does not mean thatm_Cdoes not overridem_A(when viewed fromC), it only means a compile-time error occurs.Elements.overridesmethod strives to implement the relation, not the additional checks.I agree it is reasonable to include a warning that only the relation is checked, not the additional constraints. But I find the wording "has not been sufficiently compiled" fairly confusing. I would try to point out this method only implements the "overrides" relation, not the additional constraints that are not part of the relation as such, as defined in JLS 8.4.8.1 and JLS 8.4.8.3. And hence, the two methods may satisfy the "overrides" relation, but still be part of erroneous code.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to rephrase it the way you seem to have proposed; is that better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My idea was something along these lines:
As I understand now, there may be implementations that also do the additional checks, so if we that should be permitted, the it could say "may not", instead of "does not".
Not sure if this makes sense.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hear you: the Java Language Specification separates "overrides" from those additional checks.
So, if
Elements.overridesmodels "overrides", it is under no obligation to perform those checks. Moreover, I think, it might not be always possible to perform them. Still, I believe we should NOT say that this method always skips them. So your "may not" is better.Here's why. Whether any additional checks are performed seems to be implementation-dependant. For example, here's how ECJ (Eclipse) runs that same test (after some necessary modifications to decouple the test from our test infrastructure):