What returntype should I use when returning a Java object? - coldfusion

What returntype should I use when returning a Java object created with createObject("java", "<someclass>") from a function?
Is "Any" the only solution?

Yes, for java objects use type="any". Aside from "any", cffunction only supports basic types (string, numeric, struct, query, etcetera...). Everything else is assumed to be a component name. So using a java class name, such as java.lang.String, would cause an error because CF looks for a component with that path and obviously does not find it.

Related

How to create custom matchers in Mockito?

I am using Mockito for unit testing. And there are many matchers like anyString(), anyBoolean() in Mockito. But suppose if I have a custom enum like
Enum LoginType.java
//LoginType.java
public enum LoginType {
FACEBOOK,
EMAIL,
GOOGLE
}
In one of the method arguments I need to pass an instance of LoginType. How do I pass the argument without explicitly passing LoginType.FACEBOOK or LoginType.GOOGLE. Something like anyString(). Any hint in that direction will be useful.
For any behavior, just calling Matchers.any() may be good enough on Java 8. That's when parameter type inference came out.
You might also choose Matchers.any(LoginType.class), which has pure any() behavior in Mockito 1.x but will provide type checking in Mockito 2.0 and beyond. In either case, passing in the class literal will help Java get the type information you need for inference.
For related problems:
If you have a generic type, the class literal isn`t enough either; you need to specify it as an explicit method parameter:
Matchers.<YourContainer<YourType>>any();
...or extract to a static helper method, which you need to do instead of a constant or local variable because Mockito matchers work via side effects:
public static LoginType anyLoginType() {
return Matchers.any();
}
Finally, for future readers who might be here to implement custom matching logic, look for Matchers.argThat or MockitoHamcrest.argThat to adapt a Hamcrest-style Matcher object into a Mockito method call.

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.

Is there a unified way to store and pass any callable objects?

Background:
I have a Framework where I work on Objects. Up till now I created Objects in the framework with a default constructor. Now I want to introduce some customization on creation of the Objects. I decided it would be nice to allow to pass a factory into the Framework. I call it Provider, I will explain why below.
The only thing I expect in the Framework is to have a thing that will behave something like this
template< typename Provider >
void Framework::make_objects( Provider obj_provider)
{
Object obj = obj_provider();
}
I would like Provider to be anything that is callable, and returns an Object to be passed. E.g:
Factory factory;
framework.make_objects( factory.make_object ); // [1] a Factory method
framework.make_objects( []() { return Object(); } ); // [2] lambda
framework.make_objects( function_that_spits_Object ); // [3] a simple function
I call Provider a provider, and not a factory, because it is more of just a method of a factory.
Problem:
I cannot figure out a way with a simple front-end interface to pass and possibly store any kind of callable object (with a given signature). Is it possible?
What I tried:
I tried std::function, and got it to work, but gets really ugly when I want to provide Objects using a Factory method because it is overloaded, and a member method. So I need to bind factory instance to an overloaded member method. Possible but really ugly from the user side.
I think a template similar to the one Background example, would work, but it would be extremely nice to be able to store and pass the Provider. And I couldn't figure out how the template should be written to allow that.
I know that I can resolve my background/original problem, I could accept a whole Factory in the Framework, and write a constructor that would accept std::function and wrap it, so the function-type providers would implicitly get converted to Factory.
However my question for here is, is this possible to implement, to accept and store any kind of callable object, so I can just use provider() any where in the framework whenever I need a new object. This is the technical issue I am interested in here.
You can just put the member call inside the lambda, e.g. [factory] { return factory.make_object(); }. std::function is the solution here.
The problem that binding member functions sucks has nothing to do with what you're going to do with the result- there's no class or type you can use that can solve the problem of producing a wrappable function object in the first place. The syntax of f(factory.make_object) is impossible to support for any type.
Just use a lambda to wrap the member function and use std::function.

Passing List object in the glassfish webservice tester

There are inputs for all the web service methods each for one argument in the glassfish tester. It's easy to pass integers, strings, etc. but how to pass a List? I'm trying to use java syntax like
new List().add(3)
but it doesn't work. How is it possible to pass a list object as an argument?
It's better to use serialization in this case so passing serialized list is not a problem - it's just a string.

Using DLL import/Declare in VB.NET with parameter types not used by .NET

I am working on a project where I have to import a DLL file into a VB project that was created a few years back. The DLL was created in C++, and looks like:
void CoordinateConversionService::convert( SourceOrTarget::Enum sourceDirection, SourceOrTarget::Enum targetDirection, CoordinateTuple* sourceCoordinates, Accuracy* sourceAccuracy, CoordinateTuple& targetCoordinates, Accuracy& targetAccuracy )
I am an intern at my job, and I haven't had to use this yet, so my understanding is extremely limited, along with my usage of VB (I'm a C++/C# guy). Here are a few questions:
1) Looking at Dllimport, it seems like the last part outside of the parameters is a return type. Example code from another site:
<DllImport("advapi32.dll")> _
Public Function GetUserName( _
ByVal lpBuffer As StringBuilder, _
ByRef nSize As Integer) As Boolean
Is "As Boolean" the return type? If so, I tried using "Sub" and it says "Keyword does not name a type." Hence why I looked into declare, because it seems I CAN return void/sub as a return type.
2) Trying to use the types "CoordinateTuple" and "Accuracy" give me problems, saying that they aren't defined. How do I get around this since I don't think I can really define them, and what about the fact that they're pointers? Also - I cannot modify the C++ code in any way, so what I have is what I have.
In VB you say either Public Function Whatever (params) As ReturnType (which is the same as public ReturnType Whatever(params) in C#) or Public Sub Whatever (params) which is for things that don't return anything (return void in C++/C#).
If your function/sub takes custom types you will need to declare .NET equivalents to those as well. This can make PInvoke hard work. Tools like the interop assistant can help.
For the custom types, you may have to create a wrapper in managed C++, which has the ability both to consume native C++ APIs and expose managed APIs. Although dated, a walkthrough can be found here:
Writing Managed Wrappers with Managed C++