Guice Explicit Bindings Not Matching - classloader

I have a strange problem with Guice not using the explicit binding defined in a Guice Module. It's a simple mapping of an Interface to a Class.
I have followed it via the debugger and can see in the InjectorImpl.findBindingsByType() the bindings MultiMap does contain the binding in question. However it returns null and then throws an exception that no implementation is bound (see below). The bound type is an interface so clearly it can't new it without using the binding.
1) No implementation for org.example.module.approval.ApprovalController was bound.
Did you mean?
org.example.module.approval.ApprovalController bound at org.example.module.ApBaseModule.configure(ApBaseModule.kt:88)
org.example.module.approval.ApprovalControllerImpl bound at org.example.module.approval.ApprovalControllerImpl.class(Unknown Source)
while locating org.example.module.approval.ApprovalController
for the 1st parameter of org.example.module.invoices.delegates.BeginApprovalDelegate.<init>(Unknown Source)
while locating org.example.module.invoices.delegates.BeginApprovalDelegate
I have tried using the Execute Expression tool in the debugger to call the equals methods on the TypeLiteral and the MultiMap entry it should match. This does return false. However I can't figure out why. Interestingly if I call toString on each and test the equality it returns true.
The JavaDocs I found don't specify the equality criteria for a Type Literal and I am struggling to follow the logic in the Guice source. What is it testing besides the conical name of the class? Does the Class Loader come into it?
The reason I ask is that the effected class is stored inside a separate "plugin" jar which my app loads via a Child Class Loader at startup. So it's a different class loader to most of the app.
Any suggestions regarding where to look next would be greatly appreciated.

Related

MediatR gets multiple handlers instances when expected single

I'm using Generic Type Requests and Handlers.If I have more than one IAsyncRequestHandler DryIoc is resolving multiple instances, instead of a single instance, causing MediatR to throw an exception.How can I solve this problem?Please check this behavior at Net Fiddle here.(Note that in this example Generic Type is only used to prove the point - MediatR throwing the exception - In my production code, Generic Types are actually needed)
The fix for your problem is out with DryIoc 2.10.1.
The reason details are in issue #446.
Update:
Regarding sample with class GoodMorningRequestHandler<T> : IAsyncRequestHandler<GoodMorningRequest, string> the exception is bit misleading but it is here as expected.
Reason is that above mapping is not registered at all by DryIoc RegisterMany. It happens because RegisterMany checks that service type (IAsyncRequestHandler<GoodMorningRequest, string>) should supply open-generic parameter T for implementation GoodMorningRequestHandler<T>, and obviously it cannot do that. Therefore this service type is filtered out.
After that, the remaining implementation would be HelloRequestHandler<T> which does not match to resolved IAsyncRequestHandler<GoodMorningRequest, string>, so the exception.
To confirm for yourself try to just register explicitly:
container.Register(typeof(IAsyncRequestHandler<GoodMorningRequest, string>), typeof(GoodMorningRequestHandler<>)). It will throw the exception. In comparison RegisterMany designed to be more tolerant, therefore it will just skip this pair.

How do intellisense and autocompletions work?

I've been wondering for a while: how do autocompletions work?
Example:
In PhpStorm whenever I use a class and type -> it shows me all properties and methods of that class. It also auto completes namespaces and even functions inside libraries such as jQuery.
Does it run some sort of regex on the files, or does it parse them somehow?
PhpStorm developer here. I'd like to go through some basics in case it may be helpful for those who want to implement their own plugin.
First of all, a code has to be broken into tokens using a lexer. Then AST (an abstract syntax tree) and PSI (a program structure interface) are built using a parser. PhpStorm has its own implementations of the lexer and the parser. This is how a PSI tree looks like for a simple class.
When you type in an editor or explicitly invoke a completion action (Ctrl+Space) a number of completion contributors are invoked. They're intended to return a list of suggestions based on a cursor's position.
Let's consider a case when completion is invoked inside a field reference.
PhpStorm knows that at the current position all class members can be suggested. It starts obtaining a class reference (the $class variable in our case) and determining its type. If a variable resolves to a class its type is a class' FQN (fully qualified name).
To obtain methods and fields of a class its PSI element is needed. A special index is used to map an FQN to an appropriate PhpClass tree element. Indices are initially built when a project is opened for the first time and updated for each modified file.
PhpStorm collects all members from the PSI element (including parent's ones), then from its traits. They're filtered depending on a current context (e.g. access scope) and an already typed name's part (f).
Suggestions are shown in a list which is sorted by how good element's name matches, its type, position and so on. The list rearranges when you type.
When you press Enter to insert an element PhpStorm invokes one more handler. It knows how to properly insert the element into a code. For instance, it can add parentheses for a method or import a class reference. In our case, it's enough to put brackets and place a cursor just after them because the method has no parameters.
That's basically it. It worth mention that the IntelliJ IDEA platform allows a plugin to provide an implementation for each step described above. Thus completion can be improved or extended for some particular framework or language.

Is there a way to create a constrained data type in Clojure?

As an example, a string that contains only a valid email address, as defined by some regex.
If a field of this type would be a part of a more complex data structure, or would be used as a function parameter, or used in any other context, the client code would be able to assume the field is a string containing a valid email address. Thus, no checks like "valid?" should be ever necessary, so approach of domaintypes would not work.
In Haskell this could be accomplished by a smart constructor (section 1.2) and in Java by ensuring the type is immutable (all setters private) and by adding a check in the constructor that throws a RuntimeException if the string used to create the type doesn't contain a valid email address.
If this is impossible in plain Clojure, I would like to see an example implementation in some well known extensions of the language, like Typed Clojure.
Ok, maybe, I understand now a question and I formulate in the comment my thoughts not really well. So I try to suggest an admissible solution to your question and then I try to explain some ideas I tried to tell in the comment.
1) There is a gen-class that generates compiled bytecode for a class and you can set constructor for the class there.
2) You can create a record with defrecord in some namespace that is private by convention in your project, then you
create another namespace with public api and define your factory function here. So the user of your public namespace will be able to call only public functions of your public namespace. (Of course, he can call also private ones, but with some another code)
3) You can just define a function like make-email that will return a map.
So you didn't specify your data structure anywhere.
4) You can just document your code where you will warn people to use the factory function for construction.
But! In Java if your code requires some interface, then it's user problem to give to your code the valid interface implementation. So if you write even a little bit general code in Java you already has lost the property of the valid email string. This stuff with interfaces is because Java is statically typed language.
Clojure is, in general, dynamically typed, so the user, in general, should be able to pass arbitrary data structure to arbitrary function without any type problems in compile time and it's his fault if he pass the wrong data. That makes, for example, this thing possible: You create a record and create a factory (constructor) function. And you expect a record to be passed in your code. But the user can pass a map with the same keys as your record fields names and the code will work.
So, in general, if you want the user of your code to be responsible for passing a required typed in dynamically typed language, then it cost nothing for user to be responsible for constructing it in a correct way that you provide to him.
Another solutions are: User just write tests. You can specify in your api functions :pre and :post conditions to check the structure. You can use typed clojure with the ideas I wrote above. And you can use some additional declarative libraries, like that was mentioned in the first comment of #Thumbnail.
P.S. I'm not a clojure professional, so I could easily miss some better solutions.

Getting a reference to a Node module and work with it in a separate thread

Assuming I have 2 different sources:
node_module.cc
threaded_class.cc
node_module.cc is where I am calling NODE_MODULE to initialize my module. This module has a function that makes an instance of threaded_class.cc (in a separate thread). I understand that I need to use Lockers and Isolates to access v8 in a separate thread but my issue is bigger than that.
NODE_MODULE function is my only chance to catch the module's instance from my understanding. I found this article that uses a piece of code that is what I am exactly looking for. The author stores the module handle in a persistent object like this:
auto module_handle = Persistent<Object>::New(target);
But this either seems deprecated or not possible anymore. However I figured that it can be achieved like this:
auto module_handle = Persistent<Object>(context->GetIsolate() ,target);
However the latter, when I am trying to access its properties, are mostly private methods and properties, nothing worth to be used or I am not knowing how to use this.
My question is, is there any updated guide on how to properly handle these kind of stuff in writing a Node module? Or can you show me an example how I can pass my latter module_handle to my thread and use it for example for executing a js function called test?
I also want to know, what is the difference between NODE_MODULE and NODE_MODULE_CONTEXT_AWARE when initializing a node module?

How do I pass reference types between webservices?

I'm having a bit of difficulty passing a reference type between webservices.
My set up is as follows.
I have a console application that references two web-services:
WebServiceOne
WebServiceTwo
WebServiceOne declares the details of a class I am using in my console application...let's call it MyClass.
My console application calls WebServiceOne to retrieve a list of MyClass.
It then sends each MyClass off to WebServiceTwo for processing.
Within in the project that holds WebServiceTwo, there is a reference to WebServiceOne so that I can have the declaration of MyClass.
The trouble I'm having is that, when I compile, it can't seem to determine that the MyClass passed from the console application is the same as the MyClass declared in WebServiceOne referenced in WebServiceTwo.
I basically get an error saying Console.WebServiceOne.MyClass is not the same as MyProject.WebServiceOne.MyClass.
Does anyone know if doing this is possible? Perhaps I'm referencing WebServiceOne incorrectly? Any idea what I might be doing wrong?
My only other option is to pass each of the properties of the reference type directly to WebServiceTwo as value types...but I'd like to avoid that since I'd end up passing 10-15 parameters.
Any help would be appreciated!
I had a chat with one of the more senior guys at my work and they proposed the following solution that has worked out well for me.
The solution was to use a Data Transfer Object and remove the reference to WebServiceOne in WebServiceTwo.
Basically, in WebServiceTwo I defined a representation of all the value type fields needed as BenefitDTO. This effectively allows me to package up all the fields into one object so I don't have to pass each of them as parameters in a method.
So for the moment, that seems to be the best solution...since it works and achieves my goal.
It's likely that I didn't explain my question very well...which explains why no one was able to help...
But thanks anyway! :-)
Move the types to a separate assembly and ensure that both services use this. In the web service reference there is probably some autogenerated code called Reference.cs. Alter this to use your types.
Edit: To reflect comments
In that case take the reference.cs from that web service you cannot control use it as the shared type.
Your error message explains the problem. The proxy class on the client side is not the same type as the original class on the server side, and never will be. Whether it's a reference type or a value type is irrelevant to how it works.
I don't quite understand what your exact problem is, but here are a few guesses:
If you are trying to compare two objects for equality, then you will have to write your own compare function that compares the values of each significant property/field in turn.
If you are trying to copy an object from one service to the other, then you will have to write your own copy function that copies the values of each significant property/field in turn.
If you were using WCF, you would have the option of bypassing all this and just sharing one class definition between the client and both services.