Results 1 to 11 of 11
  1. #1
    oontvoo is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Post Merging two classes into one

    Ok, I know this might a little bit crazy, but does anyone know if there is any library out there that could help me with merging two classes into one?
    In other words, say I have a class named Student which contains certain fields and methods, and I also have another class with the same name which contains some other fields and methods. Now I want to merge the two files into one. Is there any (easy ) way to do so?

    Thanks!
    Last edited by oontvoo; 07-25-2011 at 04:57 PM. Reason: adding details

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    I have a stupid question- why don't you just use the old fashioned copy-paste method?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    oontvoo is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default

    No, I can't. Because I want the merging to be done automatically. Also, if the class file is long, it's gonna take quite a lot of time for a HUMAN BEING to read through, and then decide what to keep, and what not to keep.

    Well, maybe I should include some example...

    So suppose I have a class
    Java Code:
    public class Student 
    {
    String description;
    
    public Student (String de )
    {
    description = de;
    }
    
    public String getDescription ()
    {
    return description;
    }
    
    public void setDescription (String de )
    {
    description = de;
    }
    
    }
    And another class with the same name

    Java Code:
    public class Student
    {
    Grade theGrade;
    
    public Student (Grade grade )
    {
    theGrade = grade;
    }
    
    
    public getGrade ()
    {
    return Grade;
    }
    
    }
    So now the objective is to merge these two files so I can have ONE SINGLE source ffile of the class Student, which, of course, has to contain both the description and Grade.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    Because I want the merging to be done automatically.
    Are you trying to put programmers out of work? Have the computer do a programming tasks will reduce the number of programmers needed.

    If the classes consist only of variables and getter and setter methods, then it might be possible.

  5. #5
    oontvoo is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default

    Are you saying this is impossible?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    Quote Originally Posted by oontvoo View Post
    No, I can't. Because I want the merging to be done automatically. Also, if the class file is long, it's gonna take quite a lot of time for a HUMAN BEING to read through, and then decide what to keep, and what not to keep.
    What you're talking about might be possible (have you googled "merge java files"?), but I'm not sure it's a good approach. Anything more complicated than simple getters and setters is going to be hit-or-miss with the machine (for an extremely simple example, what should it do with your constructor? Split it into two? Add a parameter? Which parameter comes first? Is that true in every case?), and anything less complicated really doesn't need to be automated (seriously, how much faster are you going to get than control-C, click, control-V?).

    The simple truth is, you probably could have finished the job by now.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    oontvoo is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default

    It's not like I'm lazy to do the copy- and-paste thingy.
    In fact, I'm doing a code-generating project, which would be more robust if it has this kind of feature! ...

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    Quote Originally Posted by oontvoo View Post
    It's not like I'm lazy to do the copy- and-paste thingy.
    In fact, I'm doing a code-generating project, which would be more robust if it has this kind of feature! ...
    Okay, but like I said, any merge that isn't simple enough to be pointless isn't going to be exactly straightforward. What if you have two methods that have the same signature but a different body? And that's just another really simple example, not even trying to get into the inevitable gotchas.

    What you're talking about probably exists (did you google it yet?), as I know things like SVN do merges- whether or not that's the kind of thing you're looking for is up to you.

    Code generators are the devil's work.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    oontvoo is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default

    I've done quite a lot of googlings. And quite honestly, I didn't find anything of much use.
    Anyways, I'll keep looking.
    Thanks all for the replies.

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    The reason you haven't found anything of much use is probably due to the problems I listed with merging files in the first place- a lot of it depends on semantics as opposed to simple syntax, and that's not exactly trivial. There might be an eclipse plugin or something.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default

    For ordinary methods and member variables the merging of the two classes is easy: simply rename those methods/variables whenever necessary; for methods that implement identical interfaces (implemented by both classes) and constructors the problem is a lot nastier ... This entire exercise sounds like anti-refactoring to me.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Merging
    By GoodOldDany in forum Java Applets
    Replies: 3
    Last Post: 06-01-2011, 12:28 AM
  2. merging arrays
    By ehca87 in forum New To Java
    Replies: 5
    Last Post: 11-12-2009, 04:47 PM
  3. Merging XML
    By KEVINR in forum XML
    Replies: 5
    Last Post: 01-21-2009, 09:09 PM
  4. Merging Ideas
    By CompleteBeginner in forum New To Java
    Replies: 1
    Last Post: 05-19-2008, 03:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •