I am using Groovy in Android and I suddenly saw that the super is deprecated? If it is, then how do you call the parent's method in contrast to the child's method?
The super keyword is not deprecated. It sounds like the method/constructor you are calling on the super class is deprecated.
Check the documentation/source code for the super class you are extending.
Related
I'm learning android studio 4.1 using Kotlin from a 2020 book. In one of the examples they are using a MutableLiveData object. When I try to use code completion with this line:
result.setValue(value.toFloat()*usd_to_eu_rate)
the only option is the setter result.value tough result.setValue does work just fine. So I was wondering what the difference is between the two and why value does not show up in code compleation.
Thanks to this link - kotlinlang.org/docs/reference/java-interop.html#getters-and-setters - provided by #IR42 and other information by other contributors whos comments were unfortunately deleted I found my answer:
MutableLiveData is a Java class and Kotlin will infer a property when the Java class has methods that follow the Java conventions for getters and setters (no-argument methods with names starting with get and single-argument methods with names starting with set)
Code completion will not suggest the Java getter methods (i.e. getValue and setValue) but it will suggest the Kotlin inferred property (i.e. value)
You can still use the Java getter/setter methods but this is discouraged.
What is the purpose of runtime mixins in groovy?
Mixins overall one of the ways to add functionallity to class without multiple inheritance issues. But what is its purpose in Groovy? Traits can do the same.
Annotation #Mixin is considered deprecated at all. Will runtime mixins have the same fate one day?
Runtime mixins give us the ability to add methods to existed classes at runtime. The important part of the answer is "existed" and "runtime".
So, you can easily add new methods to any 3rd party library at runtime.
Actually, recently I found a case where using mixins helped me a lot.
String.metaClass {
invokeMethod {
String name, args ->
System.out.println "[$name] invokation with $args"
}
}
class GroovyInterceptableWrapper
implements GroovyInterceptable {
}
String.mixin(GroovyInterceptableWrapper)
Though again if I used there trait instead of class and then invoked withTraits I would have achieved the same result.
Given a class annotated with #Log; is it possible to mock the injected logger with Mockito for unit testing? Is this correct "Groovy-way" of doing things?
We've been having some issues while using Mockito with Groovy, as it is described here. Perhaps, you might want to use another mocking framework with Groovy support, such as GMock.
For more info you can check also this link.
The way I've achieved this is to define another Logger variable within the class to be tested, non-final and with a more permissive scope. The constructor then defaults to taking the injected logger and assigning it the more permissive object. In the tests, the logger instance can be set to a mockito mocked object which is then assigned to the more permissive object.
Not as perfect as allowing for the mocking of injected objects; but functional.
If it doesn't, does it exist on EasyMock?
Thanks.
PowerMock is intended as an extension of both EasyMock and Mockito. From the horse's mouth: "PowerMock is a framework that extend other mock libraries such as EasyMock with more powerful capabilities."
In any case, there is no EasyMock equivalent to expectNew and neither is there one in Mockito, either - that's exactly the hole that PowerMock is trying to fill. That being said, PowerMock is perfectly capable of doing this with Mockito. Here is the sample from the documentation:
How to mock construction of new objects
Use PowerMockito.whenNew, e.g.
whenNew(MyClass.class).withNoArguments().thenThrow(new
IOException("error message"));
Note that you must prepare the class
creating the new instance of MyClass for test, not the MyClass itself.
E.g. if the class doing new MyClass() is called X then you'd have to
do #PrepareForTest(X.class) in order for whenNew to work.
How to verify construction of new objects
Use PowerMockito.verifyNew,
e.g.
verifyNew(MyClass.class).withNoArguments();
Is it possible in J2ME to call/invoke a method by its name.just like we have getDeclaredMethod in java .
The java.lang.reflect package is available only in CDC 1.1.2. If you're not on such configuration, you're out of luck. There is no way to invoke a method by name without reflection.
A workaround would be to create a map from strings (method names) to appropriate classes on which you can invoke the methods.
Closest you can get is to instantiate a class by name using Class.forName("com.class.ClassName").newInstance() -- that will execute a parameterless constructor.