Re: [j2objc-discuss] anyone have an example of how to use @Inject?

329 views
Skip to first unread message

Tom Ball

unread,
Jan 23, 2015, 4:57:53 PM1/23/15
to j2objc-...@googlegroups.com
You'll need a dependency injection framework to create modules and handle the injection. Guice won't work because it defines injector classes at runtime by creating new classes using JVM bytecode, which isn't allowed on iOS. However, since that's also an issue on Android and with GWT code, several other frameworks have sprung up that define these module classes at compile time. Any of these that don't have Android or GWT dependencies, or generate bytecode, are likely to "just work" with j2objc.

We really like Daggera lightweight, fast DI framework from Square.com. Dagger uses an annotation processor to generate injector classes, so they auto-magically get created during compilation. Square's Dagger won't work with GWT, so with Square's blessing, Google forked it and will soon have a 2.0 release that will work with GWT/Android/Java/iOS. The latest source is stable if you want to start using it now.

Today, j2objc doesn't support annotation processing (coming Real Soon Now), so the workaround when using Dagger is to compile your Java sources with javac to a temporary directory (be sure the Dagger jar is on the classpath), then locate all the .java files in that tempdir and include them in your translation. If that's not clear, build the dagger/examples/simple project with javac, and list the output directory -- in addition to the normal .class files there will be some .java source files with weird names, which are the Dagger injector classes.

On Thu Jan 15 2015 at 9:21:17 PM <jno...@developertown.com> wrote:
I'm coming from objective-c to java and trying to wrap my head around j2objc.  I can see that javax.inject.jar is a part of j2objc and I can get the @Inject annotation to compile in my code.  but I can't figure out how to get something to actually inject.


Here is a test driving my case:

import org.junit.Test;

import javax.inject.Inject;

import static org.mockito.Mockito.*;

public class PrintsHelloWorld {
    @Test
    public void happyPath() throws Exception {
        HelloWorldPrinter printer = mock(HelloWorldPrinter.class);

        new PresenterThingaMajig(printer).doMessageThing();

        verify(printer).printHelloWorld();

        //What do I do here if I want to see DI using @Inject in action?
    }


    public class PresenterThingaMajig {
        private final HelloWorldPrinter printer;


        @Inject
        public PresenterThingaMajig(HelloWorldPrinter printer) {
            this.printer = printer;
        }

        public void doMessageThing() {
            printer.printHelloWorld();
        }
    }

    public interface HelloWorldPrinter {
        void printHelloWorld();
    }

    public class ConsoleHelloWorldPrinter implements HelloWorldPrinter{
        @Override
        public void printHelloWorld() {
            System.out.println("HELLO WORLD");
        }
    }
}

I can run and pass this test from intellij, and following the test instructions I can build using j2objc and j2objcc and run it from the command line and it passes... (linking mockito and javax_inject).

What is my next play?

thanks,
jon

--
You received this message because you are subscribed to the Google Groups "j2objc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to j2objc-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

confile

unread,
Feb 1, 2015, 12:12:27 PM2/1/15
to j2objc-...@googlegroups.com
I am using j2obc 0.9.6. The release said that "Annotation processing now supported." I also included Google Dagger 2 in my project. When I compile my Java project in Eclipse (using the J2Objc Eclipse Plugin) to Objective-C I get the following errors for this class:

import javax.inject.Inject;

public class NumberIncrementer {

@Inject
public NumberIncrementer(Heater heater) {
}
}

The error is: 

error: /src/com/example/incrementer/shared/NumberIncrementer.java:3: The import javax.inject cannot be resolved
error: /src/com/example/incrementer/shared/NumberIncrementer.java:6: The import com.test cannot be resolved
error: /src/com/example/incrementer/shared/NumberIncrementer.java:27: Inject cannot be resolved to a type
error: /src/com/example/incrementer/shared/NumberIncrementer.java:28: Heater cannot be resolved to a type
 

You wrote that one has to compile the Dagger annotated code until J2Objc provides annotation processing. Now I should work without precompile I guess. What is the problem here? IF precompile is still needed. Could you please be more specific what to do?

Thank you.
Michael
To unsubscribe from this group and stop receiving emails from it, send an email to j2objc-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages