Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken link in FAQ #1045

Closed
vgarmash opened this issue Sep 15, 2016 · 4 comments
Closed

Broken link in FAQ #1045

vgarmash opened this issue Sep 15, 2016 · 4 comments

Comments

@vgarmash
Copy link

In the end of this section: https://github.com/google/guice/wiki/FrequentlyAskedQuestions#how-do-i-build-two-similar-but-slightly-different-trees-of-objects

There is a link:

See also Alen Vrecko's more complete example.

Currently this page opens an error page. Please update the link to point to some working page.

@rcchan
Copy link

rcchan commented Oct 23, 2016

It looks like the link is working again (content for reference):

import com.google.inject.*;
import com.google.inject.privatemodules.PrivateModule;

import static java.lang.annotation.ElementType.*;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Date;

public class RobotLegsProblem2 {


    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new AbstractModule() {
            @Override
            protected void configure() {
                // global stuff
                // same to all
                bind(Driveline.class).to(FrontWheelDrive.class);
                bind(Engine.class).to(DieselEngine.class);
            }
        }, new PrivateModule() {
            @Override
            protected void configurePrivateBindings() {
                // private Module is different story
                // Bind car annotated with blue and expose it
                bind(Car.class).annotatedWith(Blue.class).to(Car.class);
                expose(Car.class).annotatedWith(Blue.class);

                // What we bind in here only applies to the exposed stuff
                // i.e. the exposed car from this module will get this injected
                // where stuff in regular module (Engine,Driveline) is "inherited" - it is global
                bind(Transmission.class).to(AutomaticTransmission.class);
            }
        }, new PrivateModule() {
            @Override
            protected void configurePrivateBindings() {
                bind(Car.class).annotatedWith(Red.class).to(Car.class);
                expose(Car.class).annotatedWith(Red.class);

                bind(Transmission.class).to(ManualTransmission.class);
               /*
                 The point is that you cannot do this with regular module i.e.
                 bind(Car.class).annotatedWith(Blue.class).to(Car.class);
                 bind(Car.class).annotatedWith(Red.class).to(Car.class);
                 now notice the dilemma
                 class Car{
                 @Inject Transmission transmission;
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                 You cannot solve this by
                 @Inject @Blue @Red Transmission transmission;
                 or
                 bind(Transmission.class).to(AutomaticTransmission.class).ifParentAnnotatedWith(Blue.class)
                 bind(Transmission.class).to(ManualTransmission.class).ifParentAnnotatedWith(Red.class)
                 but with private modules you can get this functionality
                 */
            }
        });

        Car blueCar = injector.getInstance(Key.get(Car.class, Blue.class));
        System.out.println("Blue car transmission: " + blueCar.getTransmission());

        Car redCar = injector.getInstance(Key.get(Car.class, Red.class));
        System.out.println("Red car transmission: " + redCar.getTransmission());

    }


}

@Retention(RetentionPolicy.RUNTIME)
@Target({FIELD, PARAMETER, METHOD})
@BindingAnnotation
        @interface Blue {
}

@Retention(RetentionPolicy.RUNTIME)
@Target({FIELD, PARAMETER, METHOD})
@BindingAnnotation
        @interface Red {
}

class Car {

    private final Engine engine;
    private final Transmission transmission;
    private final Driveline driveline;

    @Inject
    public Car(Engine engine, Transmission transmission, Driveline driveline) {
        this.engine = engine;
        this.transmission = transmission;
        this.driveline = driveline;;
    }

    public Driveline getDriveline() {
        return driveline;
    }

    public Engine getEngine() {
        return engine;
    }

    public Transmission getTransmission() {
        return transmission;
    }
}


interface Transmission {
}

class AutomaticTransmission implements Transmission {
}

class ManualTransmission implements Transmission {
}

interface Engine {
}

class DieselEngine implements Engine {
}

class PetrolEngine implements Engine {
}

interface Driveline {
}

class FourWheelDrive implements Driveline {
}

class FrontWheelDrive implements Driveline {
}

class RearWheelDrive implements Driveline {
}

@EdgeCaseBerg
Copy link

The link is broken again.

@dimo414
Copy link
Contributor

dimo414 commented Oct 3, 2018

Looks like the pastie server is down. It might come back, but it seems better to update the link to point to this bug; thanks @rcchan for pulling in the content.

@dimo414 dimo414 closed this as completed Oct 3, 2018
@abliss
Copy link

abliss commented Jul 25, 2019

Hi, does this code sample still work in the latest version of Guice? it seems that perhaps PrivateModule changed packages and configurePrivateBindings became configure?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants