I have imported the firestore snapshot and trying to create it's object
(:import [com.google.cloud.firestore
QueryDocumentSnapshot])
(def snapshot1 (QueryDocumentSnapshot.toObject. [:reference "user1" :type "Promotion" :included-scans 100]))
But it compile failed and error is :
Exception in thread "main" java.lang.ClassNotFoundException: QueryDocumentSnapshot.toObject,
Can you please help me to create a new object for that class QueryDocumentSnapshot ?
You can call a constructor like
(QueryDocumentSnapshot. whatever-arguments)
But QueryDocumentSnapshot has no public constructor and looking at the source it can only be instantiated using a static factory method like such:
(QueryDocumentSnapshot/fromDocument firestore timestamp document)
I'm not sure what you're actually trying to achieve here but it doesn't look like you can do what you think you can do to that class.
Okay, after taking your and Joost's answers into consideration, I discovered something rather funny:
Your error message says, that the class QueryDocumentSnapshot.toObject cannot be found. There you have it.
If you want to call a static method, you have to write (class/method args).
For more information on java interop I highly recommend the official documentation: https://clojure.org/reference/java_interop
Also, take Joost's comments on the class itself into consideration.
Related
I'm trying to override the +initialize method of a class using ASOC, but I cannot find a way to override a class method. Is it even possible?
Not to let any confusion possible about the language I'm talking about, here's some code:
script FLAppDelegate
property parent: class "NSObject"
-- -- Class Methods -- --
-- Insert code here
end script
I've done some tests, and as far as I can tell, weird as it is, methods defined using AppleScriptObjC are both class and instance methods.
Let's say I have an AppleScriptObjC file:
script iTunesController
property parent: class "NSObject"
on playpause()
tell application id "com.apple.iTunes" to playpause
end playpause
end script
In an Objective-C method, both:
- (void)callASOC
{
iTunesControllerInstance = [NSClassFromString(#"iTunesController") new];
[iTunesControllerInstance playpause];
[iTunesControllerInstance release];
}
and
- (void)callASOC
{
[NSClassFromString(#"iTunesController") playpause];
}
will call the playpause handler in the AppleScriptObjC file. The latter formulation will generate a warning a compile time, but works.
I was not able to find any documentation confirming or refuting this.
Thanks to #Friziab who reminded me of the NSClassFromString
So I could call a AppleScriptObjC method in my AppDelegate.applescript from another class (script) (NSView subclass)
I don't use AppleScriptObjC so there may be a proper way of doing it but this worked
current application's NSClassFromString("AppDelegate")'s popWindow()
I'm trying to write some unit tests for a service in my Grails app. The service, 'MyService', uses a class located in $APP-ROOT/src/groovy/ called 'MyHelperClass'.
In the unit test for MyService, I try to create a mock for MyHelperClass like so:
def myHelperClassMock = mockFor(MyHelperClass)
def myService = new MyService()
myService.myHelperClass = myHelperClassMock.createMock()
This gives the error:
Error casting map to com.mycompany.myproject.MyHelperClass, Reason: Could not find matching constructor for: com.mycompany.myproject.MyHelperClass()
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Error casting map to com.mycompany.myproject.MyHelperClass, Reason: Could not find matching constructor for: com.mycompany.myproject.MyHelperClass()
at grails.test.GrailsMock.createMock(GrailsMock.groovy:91)
at grails.test.GrailsMock$createMock.call(Unknown Source)
at com.mycompany.myproject.MyServiceTests.testSomething(MyServiceTests.groovy:17)
This seems to happen only for classes in src/, and appears to work fine for classes in grails-app/services for example. Any idea how I can get it to see the classes in src/?
Thanks!
The code doesn't specify it, but createMock appears to require that the class being mocked have a default (no arguments) constructor.
The normal Grails artifacts under /grails-app all have these, whereas some other class under /src may not.
Adding a default constructor fixed the error when I ran into this.
Also see https://groups.google.com/forum/?fromgroups=#!topic/groovymn/u2Ng_RM224A for a related discussion of this.
This answer should give you the info you need.
Mocking out constructors in Grails
I'm calling the twitter4j library using Clojure like so:
(def twitter (. (TwitterFactory.) getInstance))
This works fine when I call it as a script. But when I use gen-class, I get:
java.lang.IllegalArgumentException: Can't call public method of non-public class: public java.lang.Object twitter4j.TwitterFactoryBase.getInstance()
Is there a workaround for this?
I have no experience with it myself, but Meikel Brandmeyer did a nice writeup on gen-class once, maybe that will help you:
http://kotka.de/blog/2010/02/gen-class_how_it_works_and_how_to_use_it.html
Try:
(def twitter (.getInstance (new TwitterFactory)))
I've been reading through Programming Clojure, and I've been having some trouble understanding Stuarts primary Java Interop example. He extends DefaultHandler, and creates a startElement method, and then passes that handler to an XML parser. What I don't understand, is what exactly is happening. Does his implementation of startElement override the one defined in DefaultHandler? I'm confused. I have no experience with Java, and little with object orientation.
Thanks!
I don't own the book, but I found the code and it looks like you're right. Here is the function (for others to see):
(def print-element-handler
(proxy [DefaultHandler] []
(startElement
[uri local qname atts]
(println (format "Saw element: %s" qname)))))
You're right about what it does. The proxy statement makes a new class, the equivilent of this Java code:
public class SomeNewClass extends DefaultHandler {
public void startElement(String uri,
String localName,
String qName,
Attributes attributes) {
System.out.println(*stuff*);
}
}
So the proxy statement defines that class, and gives you an instance, which is now held in print-element-handler.
Glancing over the Java documentation for DefaultHandler answered my own question. http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/helpers/DefaultHandler.html#startElement%28java.lang.String,%20java.lang.String,%20java.lang.String,%20org.xml.sax.Attributes%29
By default, do nothing. Application writers may override this method in a subclass to take specific actions at the start of each element (such as allocating a new tree node or writing output to a file).
I'm trying to use the MoqAutoMocker class that comes with StructureMap and I can't find any examples of how it should be used. All I have to go on is the example at the StructureMap site that uses RhinoMocks.
What I'm trying to do is get reference to one of my auto-mocked/injected dependencies using the Get method. According to that link above, I should be able to do something like this
// This retrieves the mock object for IMockedService
autoMocker.Get<IMockedService>().AssertWasCalled(s => s.Go());
Note how you can use AssertWasCalled, which inidcates that the Get function returns a reference to the RhinoMocks Mock object? This same bit of code doesn't work for me when I use the MoqAutoMocker.
I have a class SignInController that depends upon an ISecurityService in the constructor. Using the MoqAutoMocker like the RhinoAutoMocker is used in the example, I think I should be able to do this...
var autoMocker = new MoqAutoMocker<SignInController>();
autoMocker.Get<ISecurityService>().Setup(ss => ss.ValidateLogin
(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
But the problem is that I never get access to the Setup method. In this case, the call to autoMocker.Get seems to be returning an instance of ISecurityService and not Mock<ISecurityService>
Has anyone successfully used MoqAutoMocker this way? Am I just doing it wrong?
I recently ran into a simillar problem. It seems that the solution is to do something like this:
var autoMocker = new MoqAutoMocker<SignInController>();
var mock = autoMocker.Get<ISecurityService>();
Mock.Get(mock).Setup(ss => ss.ValidateLogin
(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
I have also posted a lengthier example on my blog: Setting Expectations With StructureMap’s MoqAutoMocker.
autoMocker.Get<ISecurityService>()
will return ISecurityService and you can't Setup on it. In contrary
Mock.Get(mock)
will return Moq.Mock, then you could Setup on it.