c# / Xamarin: Getting a class name instead of instance of class - xamarin

I'm trying to save a variable to persistent storage (as described here) but the complier doesn't recognise the 'Current' member of 'Application' class. I'm trying this:
Application.Current.Properties["id"] = id;
but VS2015 assumes that 'application' is an instance of the 'Application' class, not the class itself. (the error given is: 'application' does not contain a definition for 'Current').
The instance is defined in a library module:
public Application Application { get; }
I've tried GetType() and MethodBase.GetCurrentMethod() without success. Sorry if this is basic c# knowledge that I've forgotten.

Use the fully qualified type name including the namespace where the class is declared in:
Xamarin.Forms.Application.Current.Properties["id"] = id;

Related

Quarkus + Panache + Active Record Pattern + Inheritance problem

I can create a Base class that extends PanacheMongoEntity (example below) and the Child class extending Base works as expected. However, if I move the Base class into a separate JAR file (e.g., core.jar), I get an error “java.lang.IllegalStateException: This method is normally automatically overridden in subclasses” when calling Child.listAll().
public class Base extends PanacheMongoEntity {
public String modifiedDate;
}
public class Child extends Base {
public String name;
}
// works
Child.listAll();
As mentioned, if the Base class and Child class are compiled at the same time, it works. But moving Base to a JAR and including as a dependency does not work.
For all external jars to get scanned by Quarkus, you need to add an empty beans.xml in /src/main/resources/META-INF in your external project.
This is described somewhere on Quarkus guides.

What 'final' keyword next to the field stands for?

In a legacy code, I'm working with, I found the following thing:
#Autowired
final lateinit var controller: CustomController
what does this final keyword mean here?
In a Kotlin documentation I found a short description about final keyword that is blocking overriding of the methods in open classes but no information about fields. Also - the class within which I found the line is not open
A final property or a method in Kotlin prevents overriding of the field / method. That being said, Kotlin by default considers a property or a method/function to be final unless specified by the keyword open. In your case, the final keyword is redundant.
Here's a small demo test case to illustrate the same.
open class Parent {
open val someValue = 0
final val otherValue = 13 // redundant modifier 'final' warning in Android Studio
}
class Child : Parent() {
override val someValue = 5
// override val otherValue = 19 // compile error
}
There is an interesting problem called Fragile Base Class in OOP and why some languages like Kotlin prefer final by default.
What you have there is a property, not a field.
It looks just like a field, as it would in Java; but in Kotlin, it actually defines a public getter method, a public setter method, and a private backing field*.
So the final modifier applies to the accessor methods, preventing those from being overridden in a subclass.  (As you say, the backing field itself can't be overridden anyway.)
As Siddharth says, final is the default in Kotlin, so you usually wouldn't need to specify it, though there are a few situations in which it would be needed — e.g. if it were already overriding something, or you were using the all-open or kotlin-spring compiler plug-ins.  (The use of #Autowired suggests that this is a Spring module, which probably explains why final is needed here.)  In any case, your IDE would probably indicate where it's not needed, e.g. by showing it greyed-out.
(* Only the getter is necessary; the setter isn't generated for a val, and the backing field isn't generated if you override the accessor(s) and they don't refer to it.)

Conflicting 'name' declaration in enum

I have a simple enum class in which I would like to have a field called name.
enum class DeviceFieldQuery(val clazz: Class<*>) {
id(Int::class.java),
name(String::class.java),
}
Unfortunately, this does not seem to work in Kotlin. Compilation fails with the message:
Error:(9, 5) Kotlin: Conflicting declarations: enum entry name, public final val name: String
The same Enum class as Java code works fine. How may I solve this in Kotlin?
Enums in Kotlin already have a name property already defined (like Java). This is conflicting with your enum called name. To fix it, you could capitalize it, which is more idiomatic:
enum class DeviceFieldQuery(val clazz: Class<*>) {
Id(Int::class.java),
Name(String::class.java),
}

Groovy override default call method

I have the following groovy class as part of my gradle plugin:
class MyClass {
final Expando someOptions
MyClass() {
someOptions = new Expando()
}
def call(Closure configure) {
configure.delegate = someOptions
configure.resolveStrategy = Closure.DELEGATE_ONLY
configure()
}
}
Now I want to user to have the ability to configure this class by adding extra properties to it, but those properties should be stored in someOptions.
I tried doing this in the class:
def call(final Closure configure) {
configure.delegate = someOptions
configure.resolveStrategy = Closure.DELEGATE_ONLY
configure()
}
The user of the plugin can do:
myClass {
hello='world'
}
However, gradle does not seem to understand that the hello property does not exist on the myClass instance but rather on someOptions within the class. Whenever I use the above, I get errors about hello not existing in the MyClass instance.
How do I do this? Is it possible?
FWIW, it works in the groovy console, but not in gradle.
Any classes you define in your plugin are not directly used in Gradle, but wrapped in proxy classes by Gradle. As an example,
Gradle will create a proxy class for the actual class implementation and adds (among other things) also a property setter method. The method has the name of the property and has a single argument of the same type as the property. It is different from the setProperty and getProperty methods already added by Groovy. For example if we have a task with a property with the name message of type String then Gradle will add the method message(String) to the proxy class. (Source)
This is the reason, why you can omit the assignment sign in Gradle scrips:
task myTask {
myProperty true // uses Gradle generated method
myProperty = true // uses Groovy generated setter
}
Gradle also adds a method similar to yours to allow the configuration of any object in the DSL:
myExtension {
// this works thanks to Gradle
}
Without this proxy method, it would be necessary to use the method with(Closure) from the Groovy language for any block:
myExtension.with {
// this works thanks to Groovy
}
It seems like this proxy method overrides the call(Closure) method of your example.
To solve this, you could use the Delegate annotation in Groovy on someOptions. This would make all its properties available to the MyClass instance. You could also register someOptions as convention on MyClass.
EDIT
You can see that your method is never called by comparing the stacktrace of your current example and a second stacktrace, after you changed the name of the call method and called it explicitly (you need to use another property to get the same exception).

Spring property loading depending on class that invokes

I have a class A which contains a property
class A{
String valA;
}
I have two classes B and C which have a reference to class A
class B{
#Autowired
private A aaa;
}
class C{
#Autowired
private A aaa;
}
The valA in class A would be property driven and should depend on which class in actually invoking it.
If class B is invoking it , it should be some thing like b.property defined in a property file
and for class C the value would be c.property
Is this possible to do this without using an XML configuration and only annotation , SPEL etc
Thanks in Advance
You need two different instances of A (one for B and one for C), because you can not change the value of an property in A depended on the way A is invoked*.
Have a look at the concept of qualifies to see how they can be used to distinguishes between two instances of the same class.
footenote * of course you can change the parameter in A depended on how A is invoked, but this requires a lot of technical code and some hacks. And should be not the code you want to have in your spring app.

Resources