Skip to content

sebthom.de

Menu
  • Home
  • About me
  • Imprint
    • Datenschutzerklärung
  • Guestbook
Menu

Sun JDK5/6 compilers broken when linking overloaded methods with variable arguments

Posted on Friday May 16th, 2008Friday May 16th, 2008 by sebthom

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 an ordinary Java class which only referenced other non-AspectJ related classes. This is the original error message:

[INFO] [compiler:compile]
[INFO] Compiling 180 source files to C:\projects\oval\src\trunk\target\classes
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure
C:\projects\oval\src\trunk\src\main\java\net\sf\oval\Validator.java:[373,58] 
addMethodParameterChecks(java.lang.reflect.Method,int,java.lang.Object)
has private access in net.sf.oval.internal.ClassChecks 

There exist multiple methods named addMethodParameterChecks in the class ClassChecks with different signatures. The problem is that javac tries to link against the wrong method when compiling the class calling one of the methods.

Here is an example of two simple classes that reflect the constellation can be used to reproduced the problem:

public class ClassWithVarArgs
{
	public void foo(String... strings)
	{
		for (String string : strings)
		{
			System.out.println("String:" + string);
		}
	}

	private void foo(Object obj)
	{
		System.out.println("Object:" + obj);
	}
}
public class OtherClass
{
	public OtherClass()
	{
		ClassWithVarArgs instance = new ClassWithVarArgs();
		instance.foo("string");
	}
}

Class ClassWithVarArgs declares two foo methods one having variable arguments of type String, the other one having a single parameter of type Object. The constructor of class OtherClass instantiates an object of type ClassWithVarArgs and then calls the foo method with a String object. The suitable foo method to link against would be public void foo(String… strings) 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.

However if you try to compile these classes using javac it will fail with:

src\OtherClass.java:6: foo(java.lang.Object) has private access in ClassWithVarArgs
                instance.foo("string");
                        ^
1 error

javac tries to link against private void foo(Object obj) instead of public void foo(String… strings) which is obviously wrong. The dangerous thing is, if private void foo(Object obj) is declared public instead, then javac will successfully compile the classes but will link against the wrong method.

I tried javac of JDK 1.5.0_15 and 1.6.0_05 both show the same behavior.

To see how Eclipse interprets this constellation I opened the Java files in Eclipse, navigated the cursor to the instance.foo(“string”); method and pressed F3 to open the declaration. Eclipse correctly jumped to the method declaration with the variable arguments.

I did not test other JDK implementations. Currently I only know that AspectJ’s compiler does it right.

1 thought on “Sun JDK5/6 compilers broken when linking overloaded methods with variable arguments”

  1. shashi says:
    Wednesday July 9th, 2008 at 11:33 AM

    hi i am getting an error like this

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

  • Blog (1)
  • IT (21)
    • Development (16)
      • Java (7)
      • Jython (4)
      • Visual Basic (5)
    • Linux (3)
    • WebSphere Application Server (1)
    • WebSphere Portal (2)
    • Windows (1)
  • My Freeware (2)
  • My Music (3)

Recent Posts

  • Logging WebSphere API calls in wsadmin scripts
  • [Solved] Windows 7 “Safely Remove Hardware” pop-up menu horrendously slow
  • Bash: Capturing stderr in a variable while still printing to the console.
  • Configuring EMF Teneo with Hibernate, Commons DBCP, Spring Hibernate Transaction Manager, and the OpenSessionInViewFilter
  • Using EMF ECore model objects with Wicket components
  • Installing Tomcat 6 on Debian Squeeze
  • Leveraging PyDev’s auto-completion for indirectly created objects
  • OVal 1.40 released
  • Installing WebSphere Portal in a local network
  • Comparing version numbers in Jython / Python

Blogroll

  • E L S U A
  • elektrofever.de
  • OVal
  • Sweettt.com
  • Twins’ Running Blog

Recent Comments

  • Annibale on Visual Basic – Multiple Undos Class v2.04
  • Annibale on Visual Basic – Multiple Undos Class v2.04
  • koliko2k3 on Guestbook
  • hdkid on MyPad v1.1.6 – a PHP Editor
  • Luis Diego Villarreal on Excel – VBA Timer Example v1.0.1

Archives

  • June 2014
  • May 2012
  • January 2011
  • October 2010
  • September 2010
  • March 2010
  • February 2010
  • September 2009
  • July 2009
  • March 2009
  • February 2009
  • November 2008
  • September 2008
  • May 2008
  • September 2007
  • July 2007
  • July 2004
  • March 2003
  • August 2002
  • April 2002
  • January 2002
  • Deutsch (de)Deutsch
  • English (en)English
© 2025 sebthom.de | Powered by Minimalist Blog WordPress Theme