How to create NObject with constructor parameters (JavoNet) - javonet

What is the syntax for creating a .Net object from java code (NObject) when the constructor of the .Net object has one or more parameters?
Thanks

The answer by erotavlas is correct although the syntax for classes is much simpler and can be done with a one-liner (https://www.javonet.com/java-devs/guides/creating-instance-calling-instance-methods/).
To create .NET object from Java you simply use:
NObject object = Javonet.New("Namespace.ClassName", params...);
Additional note if your class constructor has array parameter (of any type) you need to cast it to Object array.
int[] arg1;
Javonet.New("Namespace.ClassName", new Object[] {arg1})
Also, you can try new service that will create a strongly typed java wrapper for you (have a read here https://www.javonet.com/blog/more-about-javonet-io/)

I figured it out in case it wasn't obvious from the documentation
Add your reference to the dll using
Javonet.addReference()
Get the type (class name)
NType test = Javonet.getType("Namespace.Classname");
Call constructor with zero or more parameters
NObject obj = test.create(parameter1,parameter2, parameter3,.....etc);

Related

Can I get an InstanceMirror of a class without using any constructor in Dart?

Can I get an InstanceMirror without using any constructor in Dart?
More precisely: I have a class with or without any special constructor, yet I'd like to get an InstanceMirror without having to provide a constructor or any arguments at all.
For example PHP offers ReflectionClass::newInstanceWithoutConstructor
Cheers
Why do you need an InstanceMirror and why can't you use a ClassMirror?
Just list all the available constructors from the ClassMirror and then use a constructor with 0 arguments to create a new instance.
The PHP Version says: "Creates a new class instance without invoking the constructor.". For me this totally makes no sense. That's why there are constructors: To be called at creation time.
New
If the class doesn't have a constructor, a default constructor with no arguments is implicitly created for this class and you can use it.
If a class has one or more explicit constructors, you can create a new instance only by using one of them.
Old
I'm not sure if I fully understand your questions, but basically - no. If your class doesn't have a constructor an implicit default constructor is used.
An instance of a class is created with new SomeClass which creates a new instance and calls the constructor. There are other ways like literals {'x': 1, 'y': 2} but I'm pretty sure this way a constructor is called as well.

F# Ninject Constructor Injection

Is it possible to do implicit constructor injection in F# using Ninject? If so, how?
I tried to put the [<Inject>] attribute on the type definition, but I got back an error that it's invalid.
Here is what I tried:
[<Inject>]
type Blah(x : ISword) =
The Inject attribute is for Property setter injection only. Constructor injection is implicit. Just create your bindings and then make a kernel.Get<Blah>() and Blah is created using constructor injection.
The spec allows this as follows:
class-type-defn :=
type-name primary-constr-args_opt object-val_opt '=' class class-type-body end
where
primary-constr-args :=
attributes_opt accessopt (simple-pat, ... , simple-pat)
As a result, you just need to change your code to
type Blah [<Inject>](x : ISword) =
Here's Constructor Injection in F#:
type Foo(bar : IBar) =
// class members etc. here
Any library that requires you to slap an attribute on the type in order to understand that, is doing something wrong; pick another library.

Telling StructureMap to use another Constructor

I have a class with 2 constructors.
MyClass()
and
MyClass(IMyService service)
How do I tell StructureMap then whenever I do a 'new MyClass()' it should actually call the second constructor and not the first constructor.
Please help.
If you call new MyClass(), then StructureMap is not involved at all. No amount of StructureMap configuration will change the behavior.
If you call ObjectFactory.GetInstance<MyClass>(), StructureMap will by default call the constructor with more parameters.
If you want StructureMap to use a different constructor, you can specify the constructor (via PHeiberg's answer):
x.SelectConstructor<IMyClass>(() => new MyClass(null));
Or you can just tell StructureMap explicitly how to create the instance using the overload of Use() that accepts a Func<>:
x.For<IMyClass>().Use(ctx => new MyClass(ctx.GetInstance<IMyService>()))
Joshua's answer is covering all aspects. As a side note in order to configure Structuremap to choose a specific constructor without hardcoding the arguments to the constructor as done in Joshua's example you can use the SelectContructor method:
x.SelectConstructor<MyService>(() => new MyService());
The lambda in the SelectConstructor method call should invoke the needed constructor (put nulls or any value of the correct type for all parameters present). See the documentation for further info.
When using a DI container like structuremap it's best to have just a single constructor on every class. This constructor has to resolve all the dependencies of the class, if IMyService is a dependency (which looks a bit strange though) this should always be resolved when instantiating so the parameterless constructor is not needed.

ActionScript: Constructor for Value Object classes

Is it OK to use the constructor to set properties for a value object class or must I use dot notation and set each one, one-by-one?
I recently read an article that was saying I should do it one-by-one as value objects should only contain properties and went on to say using the constructor is not OK (best-practice wise).
Code:
("not OK")
var employee=new
Employee(firstName,lastName,age);
("OK")
var employee=new Employee();
employee.firstName=firstName;
employee.lastName=lastName;
employee.age=age;
What's your take on this?
Thank you.
I've never heard anyone say that using a constructor to construct an object is a bad idea. The only case that I can think of is if the list of elements to be initialized can be changed (add/removed) and therefore changing the API of the object (which is bad, especially when developing libraries). In this case, I'd still use the constructor, but I'd pass in an initialization object (which contains n parameters) rather than modifying the function signature.
The statement "it's bad practice to use the constructor to construct the object" (paraphrasing) just doesn't make sense to me :P

Using bindData method outside of controller

I was wondering if anyone had an idea for the best way to provide the
functionality of bindData() outside of my grails controllers. In my current
project I have created several groovy classes to model objects returned by
an api. In these classes I have a static method that parses xml and returns
a List of objects of the class. I would like to skip all the type casting
nonsense by using the bindData method in these classes. Any suggestions on
how to do this would be appreciated.
I was looking for a similar solution, to use bindData in a service class. I found a solution in JT's blog. The solution is basically to import:
import org.codehaus.groovy.grails.web.metaclass.BindDynamicMethod
then add this to your code:
def foo = new Foo()
BindDynamicMethod bind = new BindDynamicMethod()
def args = [ foo, params, [exclude:['name', 'mail']] ] // for example
bind.invoke( foo, 'bind', (Object[])args)
The (Object[]) cast is necessary du to Groovy/Java compatability. (Groovy is treating the ‘args’ object as an ArrayList, not an array of Objects.)

Resources