Q: Java create generalized generic class for composite behavior I m beginner in java, so need your professional suggestions.
Short description:


*

*I have some classes A1, A2, A3 ..  B1 ..   

*I need one magic generic class or method that creates CompositeA CompositeB ...  

*These composite classes, have defined generalized behavior, what to do with what kind of methods

*The kind of methods, could be set by annotations    

*The methods itself are different for A, B ..

*I want to avoid writing CompositeX class every time I have new class X.  

*The question: Is it possible? If yes, how to?


Long description:
Our problem now:
There are 2+ data sources, like DB, files, libs.
From this data sources we get similar information. For example IDs.
The information is in objects (like hibernate entity), that extends one interface/abstract class.
For example if the content we need is in Foo class, so we have FooDB, FooFile, FooLib. All implementing/extending Foo.
And we create some CombinedFoo, to work with combined data of all this sources.  
to update CombinedFoo data source (insert or change some information),  only one source will be really used,
for retreiving the data from CombinedFoo all the sources will be used 
(see CompositeFoo constructor later)  
For example I have a Foo object, that has data (in this case IDs) from DB, another Foo from files and another Foo from some lib.
All this Foo's, implements same Foo interface.
Now I need all the sources in one combined object (that implements Foo interface too).
By initialization of combined foo I do
CompositeFoo compFoo = new CompositeFoo(fooDB, fooFILE, fooLIB)  

compFoo.addId("value") calls only fooDB.addId("value").
compFoo.getIds() gets all the ids from all the data sources
(fooDB.getIds() + fooFILE.getIds() + fooLIB.getIds()).  
If I want not only Foo, but for example Bar, I need to implement again new CompositeBar, with its own methods (this I want to avoid). But in the end all I need is: to make sure what methods are only for retrieving (get_methods) and what for changing information (set_methods).  
now the question:
Is it possible to create such magic generic class, that is written once, and can be applied to every object class, I want to combine?
Here some example for more understanding:
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

//interface used for composite class and data classes
public abstract class Foo {
    abstract Set<String> getIds();
    abstract void addId(String id);
}

//data source from file
class FooFromFile extends Foo {

    @Override
    Set<String> getIds() {
        //loads data from file
    }

    @Override
    void addId(String id) {
        //changes file, add there one id
    }
}

//data source from DB
class FooFromDB extends Foo {

    @Override
    Set<String> getIds() {
        //loads ids from DataBase
    }

    @Override
    void addId(String id) {
        //adds id to database
    }
}

//class i use now, combines all Foo's
//gets info from all, updates only main data source
//this one i want to make generic
class CompositeFoo extends Foo {
    private Foo mainFoo;
    private Set<Foo> others;

    public CompositeFoo(Foo mainSource, Foo ...otherSources ) {
        mainFoo = mainSource;
        others = new HashSet<>();
        Collections.addAll(others, otherSources);
    }

    @Override
    Set<String> getIds() {
        HashSet<String> result = new HashSet<>();
        //load from main source
        result.addAll(mainFoo.getIds());
        //and from other sources
        for (Foo otherFoo : others ) {
            result.addAll(otherFoo.getIds());
        }
        //ids from all the sources
        return result;
    }

    @Override
    void addId(String id) {
        //uses only main source, we don't want to add ID to all the sources
        mainFoo.addId(id);
    }
}

Now it works so:
public static void main(String[] args) {
        FooFromDB dbFooMain = new FooFromDB();
        FooFromFile fileFoo = new FooFromFile();
        CompositeFoo compFoo= new CompositeFoo(dbFooMain, fileFoo);

        compFoo.add("new id"); //adds to dbFooMain only
        compFoo.getIds(); //gets from all sources (dbFooMain  + fileFoo)
    }

This magic I want to realize but don't know how:
It should work somehow in this example
    public static void main(String[] args) {
        FooFromDB dbFooMain = new FooFromDB();
        FooFromFile fileFoo = new FooFromFile();

        //something like this:
        MagicComposite<Foo> compFoo = new MagicComposite<>(dbFooMain, fileFoo);
        //or something like this:
        Foo compFoo = AnyCompositeFactory.createComposite(Foo.class, dbFoo, fileFoo);

        //than this should work
        //adds to dbFooMain only. dbFoo.add("new id") called
        compFoo.add("new id"); 

        //gets ids from all sources so dbFoo.getIds() + fileFoo.getIds() called and combined
        compFoo.getIds();


        //-----------------------------------------
        //for Bar the same magic class should do
        MagicComposite<Bar> compBar = new MagicComposite<>(dbBarMain, fileBar);
        //or
        Bar compBar = AnyCompositeFactory.createComposite(Bar.class, dbBar, fileBar);
    }

So I want to avoid the creating of Composite%ClassName% for every %ClassName% data class.
I need some dynamic, generic CompositeMagic<Class> or whatever.  
What I imagine..
If I have Bar class, than I somehow annotate it, what methods are to be combined to get all information, and what methods are just for changing information.
abstract class Bar{

    @Magic(Type = retrieveInformationOnly)
    Set<String> getSomething();

    @Magic(Type = addInformation)
    void addSomething(String id);
}

In this case the if I initialize the CombinedBar with some other Bars, and call addSomething() of this composite object, than only main (first arg in constructor) data source should add information, if I call getSomething(), all the initialized classed are called, and combined result is returned. (see CompositeFoo implementation)
The implementation could be something else. Code examples here are just to clarify what I need.
