will
will

Reputation: 5071

Using the Toothpick DI framework be used with a Java (only) project

I started a small proof of concept exercise to make us of the Toothpick DI framework with an existing Gradle based Java project. I have read quite a few claims that you can use Toothpick with Java (meaning the JRE, OpenJDK, JDK or JSE - No Android) ... However, every example I've been able to check/work through has at some point a dependency on Android in some way, shape or configuration.

With the most (partially) successful effort so far has been to use an experimental, throw-away Android mocking package to have my Java project at least compile without error. That said it comes-up blank on resolving any should-be generated dependencies, such as the:

The Toothpick sample project itself and the simpler of the available examples all use an Android configuration and often as not dependencies on other Android specifics.

update Two

I took direction to the Toothpick sample project, a Java project

The Tootpick wiki and the sample project use the Java compile option:

     compileJava {
           options.annotationProcessorPath = configurations.annotationProcessor
           options.compilerArgs = ['-Atoothpick_registry_package_name=experiments.toothpick',]
     }

That gave me a warning that turned-out to be a mismatch in Gradle and plugins. And I needed a new build with --refresh-dependencies (hint: make sure you compile the TestsPackages).

After fixing that the sample compiles and passes the Unit Tests. The generated files are under build/generated and I managed to encourage Netbeans to find them with this:

 sourceSets {
     generated {
         java {
             srcDirs = [ 'build/generated/source/apt/main' ];
         }
     }
 }

Sadly Netbeans continues to put little red-lines under the generated symbols. At least it runs. Netbeans support could be better.

My earlier experiment looked on the Toothpick Smoothie which is an Android example. Kind of interesting as an intellectual exercise ...

That build.gradle file relies on Android. So I tried mock substitutes for missing components. The project compiles but can't find (any) generated code.

I would have considered by now that there might be at least ONE successful Java JRE/JDK Toothpick project example 'out there'.

update One

I decided to tackle this from the other end and look to the common Java annotation processing examples. This works as far as I got, with Gradle v4.7 (and also I think v4.6).

In your (sub-)project build.gradle ...

    plugins {
        id "net.ltgt.apt" version "0.15"
    }
       :

    dependencies {
         annotationProcessor (
             dep_toothpickCompiler
         )
       :
    }

The dep_toothpickCompiler is defined earlier as:

 //      Annotation Processor    
     dep_toothpickCompiler   = "com.github.stephanenicolas.toothpick:toothpick-compiler:${ver_toothpick}"

results ...

This step at least managed to create a

Unfortunately no generated output so far. There is light at the end of the tunnel, I'm sure. I'll post updates here if/as I get closer to a solution.


learning examples (GitHub)

I've identified some 'reliable' Toothpick examples. So far they want Android in there some place. Either as Android targeted modules or using related dependencies.

Given (or assuming) that a pure Java / Toothpick project can build, debug and run on the desktop or from the command line; it doesn't seem to be a popular choice as far as my googling went ...

--

Upvotes: 2

Views: 589

Answers (1)

Snicolas
Snicolas

Reputation: 38168

You should look at the TP sample, it is pure Java.

https://github.com/stephanenicolas/toothpick/tree/master/toothpick-sample

Smoothie is actually the android specific part of TP.

Upvotes: 1

Related Questions