facebook

classes with the same fully qualified name in two projects

  1. MyEclipse IDE
  2.  > 
  3. General Development
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #302526 Reply

    Humppa
    Member

    Hi,

    it’s a more general Eclipse question.

    I’ve got two projects in my workbench

      MainProj
      LibProj

    There is a dependency between MainProj and LibProj, so MainProj uses LibProj.

    Now there is a class with the fully qualified name saatanat.Humppa in both Projects.
    In the MainProj:

    
    package saatanat;
    public class Humppa
    {
            @Override
            public String toString(){return "I'm in the MainProj";}
    }
    

    In the LibProj:

    
    package saatanat;
    public class Humppa
    {
            @Override
            public String toString(){return "I'm in the LibProj";}
    }
    

    There is also a class in the LibProj which has a method giving back a saatanat.Humppa object.

    
    package wrc;
    import saatanat;
    
    public class GiveBack
    {
            public Humppa gimmeHumppa(){return new Humppa();}
    }
    

    In the main method, located in the MainProj the class GiveBack is used.

    
    package main;
    import wrc.GiveBack;
    import saatanat.Humppa;
    
    public class Main
    {
            public static void main(String []args)
            {
                      GiveBack gb = new GiveBack();
                      System.out.println(gb.gimmeHumppa());
            }
    }
    

    The output then is “I’m in the MainProj”.

    After that, in (My)Eclipse, I go to the project properties menu of the MainProj and push the LibProj to the top of the list in the Java Build Path/”Order and Export” tab and rebuild and compile the project, the output will be “I’m in the LibProj”.

    My question now is, is that a reliable behavior? Or is there no specification of what the compiler does with two classes having the same fully qualified name located in two dependent projects, so maybe the build behavior will change in future?

    Thx for reading
    Humppa!

    #302527 Reply

    Ton Huisman
    Member

    It’s really simple: You shouldn’t do that.

    If you are designing some plug-in or overriding construction, then do that using proven technology, this way you are relying on a platform specific behavior. This is despite the fact it is documented in what order classes are found, but that doesn’t mean it won’t ever change, and you really have no way of explicitly invoking the class/method you think you are invoking.

    #302528 Reply

    Humppa
    Member

    Hi huisma13,

    thank you for your answer.

    …then do that using proven technology

    Yes, that is why I’m asking.
    It does exactly what I need, but I did not find anything about that behavior in the eclipse or java documentation and I want to avoid some fundamental design faults.
    So if anyone knows a proven way to override a complete class, please tell me.

    Thx in advance
    Humppa!

    #302578 Reply

    rmcvay
    Member

    The first class with a given name found in the run time classpath will be loaded.

    #302588 Reply

    Humppa
    Member

    Hi rmcavy,

    The first class with a given name found in the run time classpath will be loaded.

    Do you have a link to the documentation with some details for me?
    I just want to know where I have to look first the next time.

    Thx!
    Humppa

    #302607 Reply

    is that a reliable behavior?

    Yes.

    In the Build class path order list, you can change the order of the projects using the Up and Down buttons to move the selected path entry in the build path

    When executing

    System.out.println(gb.gimmeHumppa());

    JRE will look into classpath to locate GiveBack and Humppa classes and execute the class based on classpath entries.

    If you place MainProj on top of LibProj in “Order and Export” tab and the related .classpath(locat under MainProj in Navigator view) will be updated with the following entries

    <classpath>
    <classpathentry kind=”src” path=”src”/>
    <classpathentry kind=”src” path=”/LibProj”/>
    <classpathentry kind=”con” path=”org.eclipse.jdt.launching.JRE_CONTAINER”/>
    <classpathentry kind=”output” path=”bin”/>
    </classpath>

    Here both the projects have classes with same package(saatanat) and same name(Humppa). Because of the above classpath entries,JRE will locate the class Humppa in MainProj and display the output as “I’m in the MainProj”.

    If you place LibProj on top of MainProj in “Order and Export” tab and the related .classpath will be updated with the following entries

    <classpath>
    <classpathentry kind=”src” path=”/LibProj”/>
    <classpathentry kind=”src” path=”src”/>
    <classpathentry kind=”con” path=”org.eclipse.jdt.launching.JRE_CONTAINER”/>
    <classpathentry kind=”output” path=”bin”/>
    </classpath>

    In this case, JRE will locate the class Humppa in LibProj and display the output as “I’m in the LibProj”

Viewing 6 posts - 1 through 6 (of 6 total)
Reply To: classes with the same fully qualified name in two projects

You must be logged in to post in the forum log in