     <link rel="alternate" type="application/atom+xml" title="sebthom.de Category: Java" href="https://sebthom.de/de/category/it/development/java-coding/feed/" />
{"id":56,"date":"2008-05-16T01:23:44","date_gmt":"2008-05-15T23:23:44","guid":{"rendered":"http:\/\/sebthom.de\/56-sun_jdk5_6_compilers_broken_when_linking_overloaded_methods_with_variable_arguments\/"},"modified":"2008-05-16T23:37:45","modified_gmt":"2008-05-16T21:37:45","slug":"sun_jdk5_6_compilers_broken_when_linking_overloaded_methods_with_variable_arguments","status":"publish","type":"post","link":"https:\/\/sebthom.de\/de\/56-sun_jdk5_6_compilers_broken_when_linking_overloaded_methods_with_variable_arguments\/","title":{"rendered":"Sun JDK5\/6 compilers broken when linking overloaded methods with variable arguments"},"content":{"rendered":"<p>We are currently switching the build system of <a title=\"OVal - the object validation framework for Java 5 or later\" href=\"http:\/\/oval.sourceforge.net\/\" target=\"_blank\">OVal<\/a> from custom Ant scripts to Maven 2. During that process we accidentally compiled the project using the Java compiler of the Sun JDK 5 instead of the AspectJ compiler. Surprisingly javac did not complain about the missing aspect class files. Instead it already aborted while compiling an ordinary Java class which only referenced other non-AspectJ related classes. This is the original error message:<\/p>\n<pre>[INFO] [compiler:compile]\r\n[INFO] Compiling 180 source files to C:\\projects\\oval\\src\\trunk\\target\\classes\r\n[INFO] ------------------------------------------------------------------------\r\n[ERROR] BUILD FAILURE\r\n[INFO] ------------------------------------------------------------------------\r\n[INFO] Compilation failure\r\nC:\\projects\\oval\\src\\trunk\\src\\main\\java\\net\\sf\\oval\\Validator.java:[373,58] \r\naddMethodParameterChecks(java.lang.reflect.Method,int,java.lang.Object)\r\nhas private access in net.sf.oval.internal.ClassChecks <\/pre>\n<p>There exist multiple methods named <strong>addMethodParameterChecks<\/strong> in the class <strong>ClassChecks<\/strong> with different signatures. The problem is that javac tries to link against the wrong method when compiling the class calling one of the methods. <\/p>\n<p><!--more--><\/p>\n<p>Here is an example of two simple classes that reflect the constellation can be used to reproduced the problem:<\/p>\n<pre class=\"java\" name=\"code\">public class ClassWithVarArgs\r\n{\r\n\tpublic void foo(String... strings)\r\n\t{\r\n\t\tfor (String string : strings)\r\n\t\t{\r\n\t\t\tSystem.out.println(\"String:\" + string);\r\n\t\t}\r\n\t}\r\n\r\n\tprivate void foo(Object obj)\r\n\t{\r\n\t\tSystem.out.println(\"Object:\" + obj);\r\n\t}\r\n}<\/pre>\n<pre class=\"java\" name=\"code\">public class OtherClass\r\n{\r\n\tpublic OtherClass()\r\n\t{\r\n\t\tClassWithVarArgs instance = new ClassWithVarArgs();\r\n\t\tinstance.foo(\"string\");\r\n\t}\r\n}<\/pre>\n<p>Class <strong>ClassWithVarArgs<\/strong> declares two <strong>foo<\/strong> methods one having variable arguments of type <strong>String<\/strong>, the other one having a single parameter of type <strong>Object<\/strong>. The constructor of class OtherClass instantiates an object of type ClassWithVarArgs and then calls the <strong>foo<\/strong> method with a <strong>String<\/strong> object. The suitable <strong>foo<\/strong> method to link against would be <strong>public void foo(String&#8230; strings)<\/strong> since it has the right visibility and a matching signature allowing the caller to either pass in a single String object or an array of Strings.<\/p>\n<p>However if you try to compile these classes using javac it will fail with:<\/p>\n<pre>src\\OtherClass.java:6: foo(java.lang.Object) has private access in ClassWithVarArgs\r\n                instance.foo(\"string\");\r\n                        ^\r\n1 error<\/pre>\n<p>javac tries to link against <strong>private void foo(Object obj)<\/strong> instead of <strong>public void foo(String&#8230; strings)<\/strong> which is obviously wrong.<strong> <\/strong>The dangerous thing is, if <strong>private void foo(Object obj)<\/strong> is declared <strong>public<\/strong> instead, then javac will successfully compile the classes but will link against the wrong method.<\/p>\n<p>I tried javac of JDK 1.5.0_15 and 1.6.0_05 both show the same behavior. <\/p>\n<p>To see how Eclipse interprets this constellation I opened the Java files in Eclipse, navigated the cursor to the instance.foo(&#8220;string&#8221;); method and pressed <strong>F3<\/strong> to open the declaration. Eclipse correctly jumped to the method declaration with the variable arguments.<\/p>\n<p>I did not test other JDK implementations. Currently I only know that AspectJ&#8217;s compiler does it right.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We are currently switching the build system of OVal from custom Ant scripts to Maven 2. During that process we accidentally compiled the project using the Java compiler of the Sun JDK 5 instead of the AspectJ compiler. Surprisingly javac did not complain about the missing aspect class files. Instead it already aborted while compiling&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[53,51,30,52],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-java-coding","tag-bug","tag-compiler","tag-java","tag-jdk"],"_links":{"self":[{"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":0,"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"wp:attachment":[{"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sebthom.de\/de\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}