I would like to use Koin library from Java. According to this tutorial, Koin modules can be written in Kotlin even using Java in the rest of a project.
https://insert-koin.io/docs/quickstart/android-java/
I use also #JvmField annotation for the module field because in the source code of this tutorial it is used
https://github.com/InsertKoinIO/koin/blob/master/quickstart/getting-started-koin-android/app/src/main/kotlin/org/koin/sample/AppModule.kt
In my project I use koinInjector which is a list of modules
import org.koin.android.java.KoinAndroidApplication;
import org.koin.core.KoinApplication;
import static org.koin.core.context.DefaultContextExtKt.startKoin;
import static com.x.y.KoinInjectorKt.koinInjector;
public class App extends Singleton {
#Override
public void onCreate() {
super.onCreate();
KoinApplication koin = KoinAndroidApplication
.create(this)
.modules(koinInjector);
startKoin(koin);
}
}
KoinInjector.kt:
#JvmField
val koinInjector: List<Module> = listOf(
localDbModule,
)
But during build I get the error:
error: cannot find symbol
import static com.x.y.KoinInjectorKt.koinInjector;
^
symbol: class KoinInjectorKt
location: package com.x.y
I had to add
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"
to build.gradle and
apply plugin: 'kotlin-android' to build.gradle (app)`
Now it works.
Thanks #Jeroen Steenbeeke for your comment
Try to replace the code with -
import org.koin.android.java.KoinAndroidApplication;
import org.koin.core.KoinApplication;
import static org.koin.core.context.DefaultContextExtKt.startKoin;
import static com.x.y.KoinInjectorKt.koinInjector;
public class App extends Singleton {
#Override
public void onCreate() {
super.onCreate();
KoinApplication koin = KoinAndroidApplication
.create(this)
.modules(listOf(localDbModule));
startKoin(koin);
}
}
remove this import
'import static com.x.y.KoinInjectorKt.koinInjector;'
and write your code like this
public class App extends Singleton {
#Override
public void onCreate() {
super.onCreate();
List<Module> list = new ArrayList<Module>();
list.add(localDbModule);
KoinApplication koin = KoinAndroidApplication
.create(this)
.modules(list);
startKoin(koin);
}
}
Related
Can we have more than 1 implementation of IAnnotationTransformer in a project that is using TestNG?
I'm using TestNg version 7.0.0.
TestNG currently lets you wire in ONLY ONE implementation of IAnnotationTransformer. If you try to plug in multiple ones of them, the last one that got added is what will get invoked.
There's an open issue that is tracking this ask. See GITHUB-1894.
As an alternative you can build your own composite IAnnotationTransformer which can be used to iterate through all the other annotation transformer instances. Here's a sample (Its available in the above mentioned github link)
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
import org.testng.collections.Lists;
import org.testng.internal.ClassHelper;
public class CompositeTransformer implements IAnnotationTransformer {
private static final String JVM_ARGS =
"com.rationaleemotions.github.issue1894.Listener1, com.rationaleemotions.github.issue1894.Listener2";
private List<IAnnotationTransformer> transformers = Lists.newArrayList();
public CompositeTransformer() {
// Ideally this would get a value from the command line. But just for demo purposes
// I am hard-coding the values.
String listeners = System.getProperty("transformers", JVM_ARGS);
Arrays.stream(listeners.split(","))
.forEach(
each -> {
Class<?> clazz = ClassHelper.forName(each.trim());
IAnnotationTransformer transformer =
(IAnnotationTransformer) ClassHelper.newInstance(clazz);
transformers.add(transformer);
});
}
#Override
public void transform(
ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
for (IAnnotationTransformer each : transformers) {
each.transform(annotation, testClass, testConstructor, testMethod);
}
}
}
I'm looking to test the Dropwizard 'Application' class without bootstrapping an entire Dropwizard server.
I'd essentially just like to ensure that the one bundle I'm registering is registered successfully.
All the routes I've been down so far result in NullPointer exceptions due to various other components not being setup correctly. Is there an easy path here?
public class SentimentApplication extends Application<SentimentConfiguration> {
public static void main(final String[] args) throws Exception {
new SentimentApplication().run(args);
}
#Override
public String getName() {
return "Sentiment";
}
#Override
public void initialize(final Bootstrap<SentimentConfiguration> bootstrap) {
bootstrap.setConfigurationSourceProvider(
new SubstitutingSourceProvider(bootstrap.getConfigurationSourceProvider(),
new EnvironmentVariableSubstitutor(false)
)
);
}
#Override
public void run(final SentimentConfiguration configuration,
final Environment environment) {
// TODO: implement application
}
}
You can register a simple command and call run method of your application with that command instead of server command. That way your application will be executed without running a server.
I wanted to do sth similar to what you want. (Considering ExampleApp as main Application class of my code) I wanted to write a test to make sure that there is no exception parsing the configuration. (Because KotlinModule() should have beed registered to environment.objectMaooer in initialize method of app, otherwise we would have had a runtime error.) I achieved it with sth similar to:
import io.dropwizard.cli.EnvironmentCommand
import io.dropwizard.setup.Bootstrap
import io.dropwizard.setup.Environment
import com.example.config.ExampleConfiguration
import net.sourceforge.argparse4j.inf.Namespace
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
class DummyCommand(app: DummyApp) : EnvironmentCommand<ExampleConfiguration>(app, "dummy", "sample test cmd") {
var parsedConfig: ExampleConfiguration? = null
override fun run(environment: Environment, namespace: Namespace, configuration: ExampleConfiguration) {
parsedConfig = configuration
}
}
class DummyApp : ExampleApp() {
val cmd: DummyCommand by lazy { DummyCommand(this) }
override fun initialize(bootstrap: Bootstrap<ExampleConfiguration>) {
super.initialize(bootstrap)
bootstrap.addCommand(cmd)
}
}
class ExampleAppTest {
#Test
fun `Test ExampleConfiguration is parsed successfully`() {
val app = DummyApp()
app.run("dummy", javaClass.getResource("/example-app-test/test-config.yml").file)
val config = app.cmd.parsedConfig
assertNotNull(config)
assertEquals("foo", config.nginxUsername)
}
}
i am creating a little game with libgdx framework and netbeans 8. I have all java classes in a single package that match with the directory structure.
The problem is that i cant import or isntantiate classes, for example:
package com.myfolder.folder2;
import ...
public class myclass1{
private myclass2 mc2;
etc...
}
In this case myclass2 is public and is inside the package but netbeans complains "cannot find symbol".
If i try with alt+enter, netbeans says "Create class myclass2 in package com.myfolder.folder2" or the same like an inner class. If i press the first option, netbeans create a class in the package with the file name myclass2_1 (becouse myclass2 exists!), and myclass1 doesnt recognize the new class.
If i try to import the class:
import com.myfolder.folder2.myclass2;
It gives me the same error, and in fact the code completion tool only gives me one crazy option in the import sentence:
import com.myfolder.folder2.myclass1;
Import the same class.
What can i do? I never have these problems using netbeans.
PD: Sorry for my english :)
You can use a class inside the same package like this:
ClassName classVariableName = new ClassName();
Then when you want to run something from the class you would put
classVariableName.MethodThatIWantToRun();
Or if you want to access a property from that method you would access it in a very similar way:
classVarabileName.PropertyIWantToAccess
Example:
You have one class with a property you want to access:
class MyClass {
public int MyProperty = 5;
}
You access it in this class:
public class MainClass {
public static void main(String[] args) {
MyClass myClass = new MyClass();
System.out.println(myClass.MyProperty);
}
}
If that doesn't work than you might have some other problem.
It was an error with one of my class package definition:
public class DesktopLauncher{
public static void main(String... args){
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
.
.
.
new LwjglApplication(new MyClass, config);
}
}
It was in MyClass, becouse i copied a snippet from an older project, and accidentally copied the older package.
NetBeans is not smart enough,
Solution: just declare the package name in all classes, example:
Class A:
package test;
public class ClassA {
public static void main(String[ ] args) {
ClassB.myFunctionB();
}
}
Class B:
package test;
public class ClassB {
public static void myFunctionB () {
System.out.print("I am ClassB!");
}
}
I am trying to create a custom Emitter for BIRT. I created a Plugin, here works everything fine the problem is that I need it as a Java class.
The problem is when I try to Render in my own Emitter it says that the custom RenderOption is not supported.
Here is some of my Code:
IJsonRenderOption:
package org.eclipse.birt.report.engine.emitter.json;
import org.eclipse.birt.report.engine.api.IRenderOption;
public interface IJsonRenderOption extends IRenderOption {
public static final String OUTPUT_FORMAT_JSON = "json";
public static final String OUTPUT_EMITTERID_JSON = "org.eclipse.birt.report.engine.emitter.json";
}
JsonRenderOption:
package org.eclipse.birt.report.engine.emitter.json;
import org.eclipse.birt.report.engine.api.IRenderOption;
import org.eclipse.birt.report.engine.api.RenderOption;
public class JsonRenderOption extends RenderOption implements IJsonRenderOption {
public JsonRenderOption() {
super();
}
public JsonRenderOption(IRenderOption options) {
super(options);
}
}
I hope somebody can help me
Greetz
Try importing the following packages
importPackage(org.eclipse.birt.report.engine.emitter.json);
importPackage(org.eclipse.birt.report.engine.api.IRenderOption);
importPackage(org.eclipse.birt.report.engine.api.RenderOption);
I just downloaded the play framework from their site and am working through this tutorial.
I've noticed the framework creates the folders app/controllers and app/views, but not a models folder. I created it manually and added Task.java to it. When I get to the section entitled "Rendering the first page" and open localhost:9000/tasks I get a compilation error that says package play.models does not exist. Here is what my Task.java looks like:
package models;
import java.util.*;
public class Task {
public Long id;
#Required
public String label;
public static List<Task> all() {
return new ArrayList<Task>();
}
public static void create(Task task) {
}
public static void delete(Long id) {
}
}
Here is application.java, the file generating the compilation error:
package controllers;
import play.*;
import play.mvc.*;
import views.html.*;
import play.data.*;
import play.models.*; // COMPILATION ERROR: "package play.models does not exist"!
public class Application extends Controller {
static Form<Task> taskForm = Form.form(Task.class);
public static Result index() {
//return ok(index.render("Your new application is ready."));
return redirect(routes.Application.tasks());
}
public static Result tasks() {
return ok(views.html.index.render(Task.all(), taskForm));
}
public static Result newTask() {
return TODO;
}
public static Result deleteTask(Long id) {
return TODO;
}
}
I believe it's supposed to be import models.Task; as opposed to import play.models.*;
That's quite confusing (IMHO) step in this tutorial, instead scroll down to Persist the tasks in a database section which describes preparing a model to cooperate with DB :) (it extends Model class, uses proper annotations, etc)
As you recognized it yet, you need to create a models package yourself.
Also as cYn wrote: you should import models like models.SomeModel into your controller
You are correct HukeLau_DABA , the Play will not create the models package for you. you have to create it.
I got these imports in my Application controller class. I got this sample play application running.
import play.api._
import play.api.mvc._
import play.api.data.Form
import play.api.data.Forms._
import models.Task
and another thing in Eclipse is it will not import the necessary imports automatically.
it is bit pain now, once the IDE support get better I hope this will change.