United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 5081782 type arguments to non-generic methods
5081782 : type arguments to non-generic methods

Details
Type:
Bug
Submit Date:
2004-08-03
Status:
Closed
Updated Date:
2010-07-09
Project Name:
JDK
Resolved Date:
2011-03-08
Component:
tools
OS:
linux,generic,windows_7
Sub-Component:
javac
CPU:
x86,generic
Priority:
P5
Resolution:
Fixed
Affected Versions:
5.0,6,7
Fixed Versions:
7

Related Reports
Duplicate:
Duplicate:
Relates:
Relates:

Sub Tasks

Description
Giving Type argument to a method which does not take one compiles

Tried with Beta3 buiold 59 on solaris

Test Case:-
/**Not a valid program, since one cannot pass a type argument to a method
  *that does not take one
  */
  
class A {
  
   public <T> void callA() {
       System.out.println("Called callA method in class A");
   }

}

class B<T> {
   public <T> void callB() {
      System.out.println("Called callB method in class B");
   }
}


public class Test17 {
  
  public void check1() {
      A a_ref = new A();
      a_ref.<String>callA();
  }
  
  public void check2() {
       B<String> b_ref = new B<String>();
       b_ref.<String>callB();
  }
  
  public static void main(String args[]) {
      Test17 test17_ref = new Test17();
      test17_ref.check1();
      test17_ref.check2();
  }

}

                                    

Comments
EVALUATION

The exact specification is found in
JLS3 15.12.2.1 Identify Potentially Applicable Methods

"If the method invocation includes explicit type parameters, and
the member is a generic method, then the number of actual type
parameters is equal to the number of formal type parameters."

This says nothing about explicit parameters to non-generic methods
implying that these are ignored.

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.2.1

However, there are various degrees of ignoring.  Should this program
compile:

class Test {
    static void m() {}
    public static void main(String... args) {
        Test.<Can,I,write,a,little,story,here>m();
    }
}

Currently, it doesn't and that is reasonable if we assume that the
explicit type arguments are categorized as type names, according
to JLS3 6.5.5.1 Simple Type Names:

"If a type name consists of a single Identifier, then the identifier
must occur in the scope of exactly one visible declaration of a type
with this name, or a compile-time error occurs. The meaning of the
type name is that type."
                                     
2006-11-02
SUGGESTED FIX

Index: j2se/src/share/classes/com/sun/tools/javac/comp/Resolve.java
--- /tmp/geta18390	2006-11-01 18:30:53.000000000 -0800
+++ /tmp/getb18390	2006-11-01 18:30:53.000000000 -0800
@@ -275,7 +275,7 @@
         if (typeargtypes == null) typeargtypes = List.nil();
         if (mt.tag != FORALL && typeargtypes.nonEmpty()) {
             // This is not a polymorphic method, but typeargs are supplied
-            return null;
+            // which is fine, see JLS3 15.12.2.1
         } else if (mt.tag == FORALL && typeargtypes.nonEmpty()) {
             ForAll pmt = (ForAll) mt;
             if (typeargtypes.length() != pmt.tvars.length())
                                     
2006-11-02
EVALUATION

To treat "Can", "I", etc as type names requires a change to JLS3 6.5.1. See spec bug 6776599.
                                     
2006-11-02
SUGGESTED FIX

Webrev of changes: http://sa.sfbay/projects/langtools/bugid_summary.pl?bugid=5081782
See also attachment 5081782.tar.gz.
                                     
2006-11-02
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
dragon
mustang


                                     
2004-09-07
EVALUATION

This test appears to be wrong.  No type arguments are given to methods without type parameters (both callA and callB are generic and takes one type argument).

Also the current specification allows one to pass any number of type arguments to a method which does not take one.  Currently, the compiler cannot handle this, so I'll leave this bug open.

###@###.### 2004-08-03
                                     
2004-08-03



Hardware and Software, Engineered to Work Together